ref.tex 258 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22. % Preamble.
  23. \input{preamble.inc}
  24. \begin{latexonly}
  25. \ifpdf
  26. \pdfinfo{/Author(Michael Van Canneyt)
  27. /Title(Standard units Reference Guide)
  28. /Subject(Free Pascal Reference guide)
  29. /Keywords(Free Pascal, Language, System Unit)
  30. }
  31. \fi
  32. \end{latexonly}
  33. %
  34. % Settings
  35. %
  36. \makeindex
  37. %
  38. % Syntax style
  39. %
  40. \usepackage{syntax}
  41. \input{syntax/diagram.tex}
  42. %
  43. % Start of document.
  44. %
  45. \begin{document}
  46. \renewcommand{\hline}{\xspace}
  47. \title{Free Pascal :\\ Reference guide.}
  48. \docdescription{Reference guide for Free Pascal, version \fpcversion}
  49. \docversion{1.9}
  50. \input{date.inc}
  51. \author{Micha\"el Van Canneyt}
  52. \maketitle
  53. \tableofcontents
  54. \newpage
  55. \listoftables
  56. \newpage
  57. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  58. % Introduction
  59. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  60. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  61. % About this guide
  62. \section*{About this guide}
  63. This document describes all constants, types, variables, functions and
  64. procedures as they are declared in the system unit.
  65. Furthermore, it describes all pascal constructs supported by \fpc, and lists
  66. all supported data types. It does not, however, give a detailed explanation
  67. of the pascal language. The aim is to list which Pascal constructs are
  68. supported, and to show where the \fpc implementation differs from the
  69. Turbo Pascal implementation.
  70. \subsection*{Notations}
  71. Throughout this document, we will refer to functions, types and variables
  72. with \var{typewriter} font. Functions and procedures have their own
  73. subsections, and for each function or procedure we have the following
  74. topics:
  75. \begin{description}
  76. \item [Declaration] The exact declaration of the function.
  77. \item [Description] What does the procedure exactly do ?
  78. \item [Errors] What errors can occur.
  79. \item [See Also] Cross references to other related functions/commands.
  80. \end{description}
  81. The cross-references come in two flavours:
  82. \begin{itemize}
  83. \item References to other functions in this manual. In the printed copy, a
  84. number will appear after this reference. It refers to the page where this
  85. function is explained. In the on-line help pages, this is a hyperlink,
  86. which can be clicked to jump to the declaration.
  87. \item References to Unix manual pages. (For linux and unix related things only) they
  88. are printed in \var{typewriter} font, and the number after it is the Unix
  89. manual section.
  90. \end{itemize}
  91. \subsection*{Syntax diagrams}
  92. All elements of the pascal language are explained in syntax diagrams.
  93. Syntax diagrams are like flow charts. Reading a syntax diagram means getting
  94. from the left side to the right side, following the arrows.
  95. When the right side of a syntax diagram is reached, and it ends with a single
  96. arrow, this means the syntax diagram is continued on the next line. If
  97. the line ends on 2 arrows pointing to each other, then the diagram is
  98. ended.
  99. Syntactical elements are written like this
  100. \begin{mysyntdiag}
  101. \synt{syntactical\ elements\ are\ like\ this}
  102. \end{mysyntdiag}
  103. Keywords which must be typed exactly as in the diagram:
  104. \begin{mysyntdiag}
  105. \lit*{keywords\ are\ like\ this}
  106. \end{mysyntdiag}
  107. When something can be repeated, there is an arrow around it:
  108. \begin{mysyntdiag}
  109. \begin{rep}[b] \synt{this\ can\ be\ repeated} \\ \end{rep}
  110. \end{mysyntdiag}
  111. When there are different possibilities, they are listed in columns:
  112. \begin{mysyntdiag}
  113. \begin{stack}
  114. \synt{First\ possibility} \\
  115. \synt{Second\ possibility}
  116. \end{stack}
  117. \end{mysyntdiag}
  118. Note, that one of the possibilities can be empty:
  119. \begin{mysyntdiag}
  120. \begin{stack}\\
  121. \synt{First\ possibility} \\
  122. \synt{Second\ possibility}
  123. \end{stack}
  124. \end{mysyntdiag}
  125. This means that both the first or second possibility are optional.
  126. Of course, all these elements can be combined and nested.
  127. \part{The Pascal language}
  128. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  129. % The Pascal language
  130. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  131. \chapter{Pascal Tokens}
  132. In this chapter we describe all the pascal reserved words, as well as the
  133. various ways to denote strings, numbers, identifiers etc.
  134. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  135. % Symbols
  136. \section{Symbols}
  137. Free Pascal allows all characters, digits and some special ASCII symbols
  138. in a Pascal source file.
  139. \input{syntax/symbol.syn}
  140. The following characters have a special meaning:
  141. \begin{verbatim}
  142. + - * / = < > [ ] . , ( ) : ^ @ { } $ #
  143. \end{verbatim}
  144. and the following character pairs too:
  145. \begin{verbatim}
  146. <= >= := += -= *= /= (* *) (. .) //
  147. \end{verbatim}
  148. When used in a range specifier, the character pair \var{(.} is equivalent to
  149. the left square bracket \var{[}. Likewise, the character pair \var{.)} is
  150. equivalent to the right square bracket \var{]}.
  151. When used for comment delimiters, the character pair \var{(*} is equivalent
  152. to the left brace \var{\{} and the character pair \var{*)} is equivalent
  153. to the right brace \var{\}}.
  154. These character pairs retain their normal meaning in string expressions.
  155. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  156. % Comments
  157. \section{Comments}
  158. \fpc supports the use of nested comments. The following constructs are valid
  159. comments:
  160. \begin{verbatim}
  161. (* This is an old style comment *)
  162. { This is a Turbo Pascal comment }
  163. // This is a Delphi comment. All is ignored till the end of the line.
  164. \end{verbatim}
  165. The following are valid ways of nesting comments:
  166. \begin{verbatim}
  167. { Comment 1 (* comment 2 *) }
  168. (* Comment 1 { comment 2 } *)
  169. { comment 1 // Comment 2 }
  170. (* comment 1 // Comment 2 *)
  171. // comment 1 (* comment 2 *)
  172. // comment 1 { comment 2 }
  173. \end{verbatim}
  174. The last two comments {\em must} be on one line. The following two will give
  175. errors:
  176. \begin{verbatim}
  177. // Valid comment { No longer valid comment !!
  178. }
  179. \end{verbatim}
  180. and
  181. \begin{verbatim}
  182. // Valid comment (* No longer valid comment !!
  183. *)
  184. \end{verbatim}
  185. The compiler will react with a 'invalid character' error when it encounters
  186. such constructs, regardless of the \var{-So} switch.
  187. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  188. % Reserved words
  189. \section{Reserved words}
  190. Reserved words are part of the Pascal language, and cannot be redefined.
  191. They will be denoted as {\sffamily\bfseries this} throughout the syntax
  192. diagrams. Reserved words can be typed regardless of case, i.e. Pascal is
  193. case insensitive.
  194. We make a distinction between Turbo Pascal and Delphi reserved words, since
  195. with the \var{-So} switch, only the Turbo Pascal reserved words are
  196. recognised, and the Delphi ones can be redefined. By default, \fpc
  197. recognises the Delphi reserved words.
  198. \subsection{Turbo Pascal reserved words}
  199. The following keywords exist in Turbo Pascal mode
  200. \begin{multicols}{4}
  201. \begin{verbatim}
  202. absolute
  203. and
  204. array
  205. asm
  206. begin
  207. break
  208. case
  209. const
  210. constructor
  211. continue
  212. destructor
  213. div
  214. do
  215. downto
  216. else
  217. end
  218. file
  219. for
  220. function
  221. goto
  222. if
  223. implementation
  224. in
  225. inherited
  226. inline
  227. interface
  228. label
  229. mod
  230. nil
  231. not
  232. object
  233. of
  234. on
  235. operator
  236. or
  237. packed
  238. procedure
  239. program
  240. record
  241. repeat
  242. self
  243. set
  244. shl
  245. shr
  246. string
  247. then
  248. to
  249. type
  250. unit
  251. until
  252. uses
  253. var
  254. while
  255. with
  256. xor
  257. \end{verbatim}
  258. \end{multicols}
  259. \subsection{Delphi reserved words}
  260. The Delphi (II) reserved words are the same as the pascal ones, plus the
  261. following ones:
  262. \begin{multicols}{4}
  263. \begin{verbatim}
  264. as
  265. class
  266. except
  267. exports
  268. finalization
  269. finally
  270. initialization
  271. is
  272. library
  273. on
  274. property
  275. raise
  276. try
  277. \end{verbatim}
  278. \end{multicols}
  279. \subsection{\fpc reserved words}
  280. On top of the Turbo Pascal and Delphi reserved words, \fpc also considers
  281. the following as reserved words:
  282. \begin{multicols}{4}
  283. \begin{verbatim}
  284. dispose
  285. exit
  286. false
  287. new
  288. true
  289. \end{verbatim}
  290. \end{multicols}
  291. \subsection{Modifiers}
  292. The following is a list of all modifiers. Contrary to Delphi, \fpc doesn't
  293. allow the programmer to redefine these modifiers.
  294. \begin{multicols}{4}
  295. \begin{verbatim}
  296. absolute
  297. abstract
  298. alias
  299. assembler
  300. cdecl
  301. default
  302. export
  303. external
  304. far
  305. forward
  306. index
  307. name
  308. near
  309. override
  310. pascal
  311. popstack
  312. private
  313. protected
  314. public
  315. published
  316. read
  317. register
  318. saveregisters
  319. stdcall
  320. virtual
  321. write
  322. \end{verbatim}
  323. \end{multicols}
  324. \begin{remark}
  325. Predefined types such as \var{Byte}, \var{Boolean} and constants
  326. such as \var{maxint} are {\em not} reserved words. They are
  327. identifiers, declared in the system unit. This means that these types
  328. can be redefined in other units. The programmer is, however, not
  329. encouraged to do this, as it will cause a lot of confusion.
  330. \end{remark}
  331. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  332. % Identifiers
  333. \section{Identifiers}
  334. Identifiers denote constants, types, variables, procedures and functions,
  335. units, and programs. All names of things that are defined are identifiers.
  336. An identifier consists of 255 significant characters (letters, digits and
  337. the underscore character), from which the first must be an alphanumeric
  338. character, or an underscore (\var{\_})
  339. The following diagram gives the basic syntax for identifiers.
  340. \input{syntax/identifier.syn}
  341. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  342. % Numbers
  343. \section{Numbers}
  344. Numbers are by default denoted in decimal notation.
  345. Real (or decimal) numbers are written using engineering or scientific
  346. notation (e.g. \var{0.314E1}).
  347. For integer type constants, \fpc supports 4 formats:
  348. \begin{enumerate}
  349. \item Normal, decimal format (base 10). This is the standard format.
  350. \item Hexadecimal format (base 16), in the same way as Turbo Pascal does.
  351. To specify a constant value in hexadecimal format, prepend it with a dollar
  352. sign (\var{\$}). Thus, the hexadecimal \var{\$FF} equals 255 decimal.
  353. Note that case is insignificant when using hexadecimal constants.
  354. \item As of version 1.0.7, Octal format (base 8) is also supported.
  355. To specify a constant in octal format, prepend it with a ampersand (\&).
  356. For instance 15 is specified in octal notation as \var{\&17}.
  357. \item Binary notation (base 2). A binary number can be specified
  358. by preceding it with a percent sign (\var{\%}). Thus, \var{255} can be
  359. specified in binary notation as \var{\%11111111}.
  360. \end{enumerate}
  361. The following diagrams show the syntax for numbers.
  362. \input{syntax/numbers.syn}
  363. %%% !!!!!!!!!!!!
  364. %%% For 2.0, the range has changed, and constants within
  365. %%% a 64bit range are parsed as int64. and no longer
  366. %%% as reals (except if they don't fit in int64!) !!!! - carl
  367. \begin{remark}
  368. It is to note that all decimal constants which do no fit within
  369. the -2147483648..2147483647 range, are silently and automatically
  370. parsed as real-type constants.
  371. \end{remark}
  372. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  373. % Labels
  374. \section{Labels}
  375. Labels can be digit sequences or identifiers.
  376. \input{syntax/label.syn}
  377. \begin{remark}
  378. Note that the \var{-Sg} switch must be specified before labels can be used.
  379. By default, \fpc doesn't support \var{label} and \var{goto} statements.
  380. \end{remark}
  381. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  382. % Character strings
  383. \section{Character strings}
  384. A character string (or string for short) is a sequence of zero or more
  385. characters from the ASCII character set, enclosed by single quotes, and on 1
  386. line of the program source.
  387. A character set with nothing between the quotes (\var{'{}'}) is an empty
  388. string.
  389. \input{syntax/string.syn}
  390. \chapter{Constants}
  391. Just as in Turbo Pascal, \fpc supports both normal and typed constants.
  392. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  393. % Ordinary constants
  394. \section{Ordinary constants}
  395. Ordinary constants declarations are not different from the Turbo Pascal or
  396. Delphi implementation.
  397. \input{syntax/const.syn}
  398. The compiler must be able to evaluate the expression in a constant
  399. declaration at compile time. This means that most of the functions
  400. in the Run-Time library cannot be used in a constant declaration.
  401. Operators such as \var{+, -, *, /, not, and, or, div, mod, ord, chr,
  402. sizeof, pi, int, trunc, round, frac, odd} can be used, however. For more
  403. information on expressions, see \seec{Expressions}.
  404. Only constants of the following types can be declared: \var{Ordinal types},
  405. \var{Real types}, \var{Char}, and \var{String}.
  406. The following are all valid constant declarations:
  407. \begin{verbatim}
  408. Const
  409. e = 2.7182818; { Real type constant. }
  410. a = 2; { Ordinal (Integer) type constant. }
  411. c = '4'; { Character type constant. }
  412. s = 'This is a constant string'; {String type constant.}
  413. s = chr(32)
  414. ls = SizeOf(Longint);
  415. \end{verbatim}
  416. Assigning a value to an ordinary constant is not permitted.
  417. Thus, given the previous declaration, the following will result
  418. in a compiler error:
  419. \begin{verbatim}
  420. s := 'some other string';
  421. \end{verbatim}
  422. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  423. % Typed constants
  424. \section{Typed constants}
  425. Typed constants serve to provide a program with initialised variables.
  426. Contrary to ordinary constants, they may be assigned to at run-time.
  427. The difference with normal variables is that their value is initialised
  428. when the program starts, whereas normal variables must be initialised
  429. explicitly.
  430. \input{syntax/tconst.syn}
  431. Given the declaration:
  432. \begin{verbatim}
  433. Const
  434. S : String = 'This is a typed constant string';
  435. \end{verbatim}
  436. The following is a valid assignment:
  437. \begin{verbatim}
  438. S := 'Result : '+Func;
  439. \end{verbatim}
  440. Where \var{Func} is a function that returns a \var{String}.
  441. Typed constants are often used to initialize arrays and records. For arrays,
  442. the initial elements must be specified, surrounded by round brackets, and
  443. separated by commas. The number of elements must be exactly the same as
  444. the number of elements in the declaration of the type.
  445. As an example:
  446. \begin{verbatim}
  447. Const
  448. tt : array [1..3] of string[20] = ('ikke', 'gij', 'hij');
  449. ti : array [1..3] of Longint = (1,2,3);
  450. \end{verbatim}
  451. For constant records, each element of the record should be specified, in
  452. the form \var{Field : Value}, separated by commas, and surrounded by round
  453. brackets.
  454. As an example:
  455. \begin{verbatim}
  456. Type
  457. Point = record
  458. X,Y : Real
  459. end;
  460. Const
  461. Origin : Point = (X:0.0; Y:0.0);
  462. \end{verbatim}
  463. The order of the fields in a constant record needs to be the same as in the type declaration,
  464. otherwise a compile-time error will occur.
  465. \begin{remark}
  466. It should be stressed that typed constants are initialized at program start.
  467. This is also true for {\em local} typed constants. Local typed constants are
  468. also initialized at program start. If their value was changed during previous
  469. invocations of the function, they will retain their changed value, i.e. they
  470. are not initialized each time the function is invoked.
  471. \end{remark}
  472. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  473. % resource strings
  474. \section{Resource strings}
  475. \label{se:resourcestring}
  476. A special kind of constant declaration part is the \var{Resourestring}
  477. part. This part is like a \var{Const} section, but it only allows
  478. to declare constant of type string. This part is only available in the
  479. \var{Delphi} or \var{objfpc} mode.
  480. The following is an example of a resourcestring definition:
  481. \begin{verbatim}
  482. Resourcestring
  483. FileMenu = '&File...';
  484. EditMenu = '&Edit...';
  485. \end{verbatim}
  486. All string constants defined in the resourcestring section are stored
  487. in special tables, allowing to manipulate the values of the strings
  488. at runtime with some special mechanisms.
  489. Semantically, the strings are like constants; Values can not be assigned to
  490. them, except through the special mechanisms in the objpas unit. However,
  491. they can be used in assignments or expressions as normal constants.
  492. The main use of the resourcestring section is to provide an easy means
  493. of internationalization.
  494. More on the subject of resourcestrings can be found in the \progref, and
  495. in the chapter on the \file{objpas} later in this manual.
  496. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  497. % Types
  498. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  499. \chapter{Types}
  500. All variables have a type. \fpc supports the same basic types as Turbo
  501. Pascal, with some extra types from Delphi.
  502. The programmer can declare his own types, which is in essence defining an identifier
  503. that can be used to denote this custom type when declaring variables further
  504. in the source code.
  505. \input{syntax/typedecl.syn}
  506. There are 7 major type classes :
  507. \input{syntax/type.syn}
  508. The last class, {\sffamily type identifier}, is just a means to give another
  509. name to a type. This presents a way to make types platform independent, by
  510. only using these types, and then defining these types for each platform
  511. individually. The programmer that uses these units doesn't have to worry
  512. about type size and so on. It also allows to use shortcut names for
  513. fully qualified type names. e.g. define \var{system.longint} as
  514. \var{Olongint} and then redefine \var{longint}.
  515. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  516. % Base types
  517. \section{Base types}
  518. The base or simple types of \fpc are the Delphi types.
  519. We will discuss each separate.
  520. \input{syntax/typesim.syn}
  521. \subsection{Ordinal types}
  522. With the exception of \var{int64}, \var{qword} and Real types,
  523. all base types are ordinal types. Ordinal types have the following
  524. characteristics:
  525. \begin{enumerate}
  526. \item Ordinal types are countable and ordered, i.e. it is, in principle,
  527. possible to start counting them one bye one, in a specified order.
  528. This property allows the operation of functions as \seep{Inc}, \seef{Ord},
  529. \seep{Dec}
  530. on ordinal types to be defined.
  531. \item Ordinal values have a smallest possible value. Trying to apply the
  532. \seef{Pred} function on the smallest possible value will generate a range
  533. check error if range checking is enabled.
  534. \item Ordinal values have a largest possible value. Trying to apply the
  535. \seef{Succ} function on the largest possible value will generate a range
  536. check error if range checking is enabled.
  537. \end{enumerate}
  538. \subsubsection{Integers}
  539. A list of pre-defined integer types is presented in \seet{integerstyp}
  540. %
  541. \begin{table}[ht]
  542. \caption{Predefined integer types}
  543. \label{tab:integerstyp}
  544. \begin{center}
  545. \begin{tabular}{l}
  546. %\begin{FPCltable}{l}{Predefined integer types}{integerstyp}
  547. Name\\ \hline
  548. Integer \\
  549. Shortint \\
  550. SmallInt \\
  551. Longint \\
  552. Longword \\
  553. Int64 \\
  554. Byte \\
  555. Word \\
  556. Cardinal \\
  557. QWord \\
  558. Boolean \\
  559. ByteBool \\
  560. LongBool \\
  561. Char \\ \hline
  562. \end{tabular}
  563. \end{center}
  564. \end{table}
  565. %\end{FPCltable}
  566. The integer types, and their ranges and sizes, that are predefined in
  567. \fpc are listed in \seet{integersranges}. It is to note that
  568. the \var{qword} and \var{int64} types are not true ordinals, so
  569. some pascal constructs will not work with these two integer types.
  570. \begin{FPCltable}{lcr}{Predefined integer types}{integersranges}
  571. Type & Range & Size in bytes \\ \hline
  572. Byte & 0 .. 255 & 1 \\
  573. Shortint & -128 .. 127 & 1\\
  574. Smallint & -32768 .. 32767 & 2\\
  575. Word & 0 .. 65535 & 2 \\
  576. Integer & either smallint, longint or int64 & size 2,4 or 8 \\
  577. Cardinal & either word, longword or qword & size 2,4 or 8 \\
  578. Longint & -2147483648 .. 2147483647 & 4\\
  579. Longword & 0..4294967295 & 4 \\
  580. Int64 & -9223372036854775808 .. 9223372036854775807 & 8 \\
  581. QWord & 0 .. 18446744073709551615 & 8 \\ \hline
  582. \end{FPCltable}
  583. The \var{integer} type maps to the smallint type in the default
  584. \fpc mode. It maps to either a longint or int64 in either Delphi or ObjFPC
  585. mode. The \var{cardinal} type is currently always mapped to the
  586. longword type. The definition of the \var{cardinal} and \var{integer}
  587. types may change from one architecture to another and from one
  588. compiler mode to another. They usually have the same size as the
  589. underlying target architecture.
  590. % This IS NOT TRUE, this is a 32-bit compiler, so the integer type
  591. % will always be the same independently the CPU type.
  592. %This is summarized in \seet{integer32type} for 32-bit processors
  593. %(such as Intel 80x86, Motorola 680x0, PowerPC 32-bit, SPARC v7, MIPS32), and
  594. %in \seet{integer64type} for 64-bit processors (such as Alpha AXP,
  595. %SPARC v9 or later, Intel Itanium, MIPS64).
  596. %\begin{FPCltable}{lcr}{\var{Integer} type mapping for 32-bit processors}{integer32type}
  597. %Compiler mode & Range & Size in bytes \\ \hline
  598. %<default> & -32768 .. 32767 & 2\\
  599. %tp & -32768 .. 32767 & 2\\
  600. %Delphi & -2147483648 .. 2147483647 & 4\\
  601. %ObjFPC & -2147483648 .. 2147483647 & 4\\
  602. %\end{FPCltable}
  603. %\begin{FPCltable}{lcr}{\var{Integer} type mapping for 64-bit processors}{integer64type}
  604. %Compiler mode & Range & Size in bytes \\ \hline
  605. %<default> & -32768 .. 32767 & 2\\
  606. %tp & -32768 .. 32767 & 2\\
  607. %Delphi & -9223372036854775808 .. 9223372036854775807 & 8 \\
  608. %ObjFPC & -9223372036854775808 .. 9223372036854775807 & 8 \\
  609. %\end{FPCltable}
  610. \fpc does automatic type conversion in expressions where different kinds of
  611. integer types are used.
  612. \subsubsection{Boolean types}
  613. \fpc supports the \var{Boolean} type, with its two pre-defined possible
  614. values \var{True} and \var{False}. It also supports the \var{ByteBool},
  615. \var{WordBool} and \var{LongBool} types. These are the only two values that can be
  616. assigned to a \var{Boolean} type. Of course, any expression that resolves
  617. to a \var{boolean} value, can also be assigned to a boolean type.
  618. \begin{FPCltable}{lll}{Boolean types}{booleantypes}
  619. Name & Size & Ord(True) \\ \hline
  620. Boolean & 1 & 1 \\
  621. ByteBool & 1 & Any nonzero value \\
  622. WordBool & 2 & Any nonzero value \\
  623. LongBool & 4 & Any nonzero value \\ \hline
  624. \end{FPCltable}
  625. Assuming \var{B} to be of type \var{Boolean}, the following are valid
  626. assignments:
  627. \begin{verbatim}
  628. B := True;
  629. B := False;
  630. B := 1<>2; { Results in B := True }
  631. \end{verbatim}
  632. Boolean expressions are also used in conditions.
  633. \begin{remark}
  634. In \fpc, boolean expressions are always evaluated in such a
  635. way that when the result is known, the rest of the expression will no longer
  636. be evaluated (Called short-cut evaluation). In the following example, the function \var{Func} will never
  637. be called, which may have strange side-effects.
  638. \begin{verbatim}
  639. ...
  640. B := False;
  641. A := B and Func;
  642. \end{verbatim}
  643. Here \var{Func} is a function which returns a \var{Boolean} type.
  644. \end{remark}
  645. \subsubsection{Enumeration types}
  646. Enumeration types are supported in \fpc. On top of the Turbo Pascal
  647. implementation, \fpc allows also a C-style extension of the
  648. enumeration type, where a value is assigned to a particular element of
  649. the enumeration list.
  650. \input{syntax/typeenum.syn}
  651. (see \seec{Expressions} for how to use expressions)
  652. When using assigned enumerated types, the assigned elements must be in
  653. ascending numerical order in the list, or the compiler will complain.
  654. The expressions used in assigned enumerated elements must be known at
  655. compile time.
  656. So the following is a correct enumerated type declaration:
  657. \begin{verbatim}
  658. Type
  659. Direction = ( North, East, South, West );
  660. \end{verbatim}
  661. The C style enumeration type looks as follows:
  662. \begin{verbatim}
  663. Type
  664. EnumType = (one, two, three, forty := 40,fortyone);
  665. \end{verbatim}
  666. As a result, the ordinal number of \var{forty} is \var{40}, and not \var{3},
  667. as it would be when the \var{':= 40'} wasn't present.
  668. The ordinal value of \var{fortyone} is then {41}, and not \var{4}, as it
  669. would be when the assignment wasn't present. After an assignment in an
  670. enumerated definition the compiler adds 1 to the assigned value to assign to
  671. the next enumerated value.
  672. When specifying such an enumeration type, it is important to keep in mind
  673. that the enumerated elements should be kept in ascending order. The
  674. following will produce a compiler error:
  675. \begin{verbatim}
  676. Type
  677. EnumType = (one, two, three, forty := 40, thirty := 30);
  678. \end{verbatim}
  679. It is necessary to keep \var{forty} and \var{thirty} in the correct order.
  680. When using enumeration types it is important to keep the following points
  681. in mind:
  682. \begin{enumerate}
  683. \item The \var{Pred} and \var{Succ} functions cannot be used on
  684. this kind of enumeration types. Trying to do this anyhow will result in a
  685. compiler error.
  686. \item Enumeration types stored using a default size. This behaviour can be changed
  687. with the \var{\{\$PACKENUM n\}} compiler directive, which
  688. tells the compiler the minimal number of bytes to be used for enumeration
  689. types.
  690. For instance
  691. \begin{verbatim}
  692. Type
  693. {$PACKENUM 4}
  694. LargeEnum = ( BigOne, BigTwo, BigThree );
  695. {$PACKENUM 1}
  696. SmallEnum = ( one, two, three );
  697. Var S : SmallEnum;
  698. L : LargeEnum;
  699. begin
  700. WriteLn ('Small enum : ',SizeOf(S));
  701. WriteLn ('Large enum : ',SizeOf(L));
  702. end.
  703. \end{verbatim}
  704. will, when run, print the following:
  705. \begin{verbatim}
  706. Small enum : 1
  707. Large enum : 4
  708. \end{verbatim}
  709. \end{enumerate}
  710. More information can be found in the \progref, in the compiler directives
  711. section.
  712. \subsubsection{Subrange types}
  713. A subrange type is a range of values from an ordinal type (the {\em host}
  714. type). To define a subrange type, one must specify it's limiting values: the
  715. highest and lowest value of the type.
  716. \input{syntax/typesubr.syn}
  717. Some of the predefined \var{integer} types are defined as subrange types:
  718. \begin{verbatim}
  719. Type
  720. Longint = $80000000..$7fffffff;
  721. Integer = -32768..32767;
  722. shortint = -128..127;
  723. byte = 0..255;
  724. Word = 0..65535;
  725. \end{verbatim}
  726. Subrange types of enumeration types can also be defined:
  727. \begin{verbatim}
  728. Type
  729. Days = (monday,tuesday,wednesday,thursday,friday,
  730. saturday,sunday);
  731. WorkDays = monday .. friday;
  732. WeekEnd = Saturday .. Sunday;
  733. \end{verbatim}
  734. \subsection{Real types}
  735. \fpc uses the math coprocessor (or emulation) for all its floating-point
  736. calculations. The Real native type is processor dependant,
  737. but it is either Single or Double. Only the IEEE floating point types are
  738. supported, and these depend on the target processor and emulation options.
  739. The true Turbo Pascal compatible types are listed in
  740. \seet{Reals}.
  741. \begin{FPCltable}{lccr}{Supported Real types}{Reals}
  742. Type & Range & Significant digits & Size \\ \hline
  743. Real & platform dependant & ??? & 4 or 8 \\
  744. Single & 1.5E-45 .. 3.4E38 & 7-8 & 4 \\
  745. Double & 5.0E-324 .. 1.7E308 & 15-16 & 8 \\
  746. Extended & 1.9E-4951 .. 1.1E4932 & 19-20 & 10\\
  747. Comp & -2E64+1 .. 2E63-1 & 19-20 & 8 \\
  748. \end{FPCltable}
  749. The \var{Comp} type is, in effect, a 64-bit integer and is not available
  750. on all target platforms. To get more information on the supported types
  751. for each platform, refer to the \progref.
  752. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  753. % Character types
  754. \section{Character types}
  755. \subsection{Char}
  756. \fpc supports the type \var{Char}. A \var{Char} is exactly 1 byte in
  757. size, and contains one character.
  758. A character constant can be specified by enclosing the character in single
  759. quotes, as follows : 'a' or 'A' are both character constants.
  760. A character can also be specified by its ASCII
  761. value, by preceding the ASCII value with the number symbol (\#). For example
  762. specifying \var{\#65} would be the same as \var{'A'}.
  763. Also, the caret character (\verb+^+) can be used in combination with a letter to
  764. specify a character with ASCII value less than 27. Thus \verb+^G+ equals
  765. \var{\#7} (G is the seventh letter in the alphabet.)
  766. When the single quote character must be represented, it should be typed
  767. two times successively, thus \var{''''} represents the single quote character.
  768. \subsection{Strings}
  769. \fpc supports the \var{String} type as it is defined in Turbo Pascal
  770. (A sequence of characters with a specified length) and it
  771. supports ansistrings as in Delphi.
  772. To declare a variable as a string, use the following type specification:
  773. \input{syntax/sstring.syn}
  774. The meaning of a string declaration statement is interpreted differently
  775. depending on the \var{\{\$H\}} switch. The above declaration can declare an
  776. ansistrng or a short string.
  777. Whatever the actual type, ansistrings and short strings can be used
  778. interchangeably. The compiler always takes care of the necessary type
  779. conversions. Note, however, that the result of an expression that contains
  780. ansistrings and short strings will always be an ansistring.
  781. \subsection{Short strings}
  782. A string declaration declares a short string in the following cases:
  783. \begin{enumerate}
  784. \item If the switch is off: \var{\{\$H-\}}, the string declaration
  785. will always be a short string declaration.
  786. \item If the switch is on \var{\{\$H+\}}, and there is a length
  787. specifier, the declaration is a short string declaration.
  788. \end{enumerate}
  789. The predefined type \var{ShortString} is defined as a string of length 255:
  790. \begin{verbatim}
  791. ShortString = String[255];
  792. \end{verbatim}
  793. If the size of the string is not specified, \var{255} is taken as a
  794. default. The length of the string can be obtained with the \seef{Length}
  795. standard runtime routine.
  796. For example in
  797. \begin{verbatim}
  798. {$H-}
  799. Type
  800. NameString = String[10];
  801. StreetString = String;
  802. \end{verbatim}
  803. \var{NameString} can contain a maximum of 10 characters. While
  804. \var{StreetString} can contain up to 255 characters.
  805. \subsection{Ansistrings}
  806. Ansistrings are strings that have no length limit. They are reference
  807. counted and null terminated. Internally, an ansistring is treated as
  808. a pointer. This is all handled transparantly, i.e. they can be manipulated
  809. as a normal short string. Ansistrings can be defined using the predefined
  810. \var{AnsiString} type.
  811. If the \var{\{\$H\}} switch is on, then a string definition using the
  812. regular \var{String} keyword and that doesn't contain a length specifier,
  813. will be regarded as an ansistring as well. If a length specifier is present,
  814. a short string will be used, regardless of the \var{\{\$H\}} setting.
  815. If the string is empty (\var{''}), then the internal pointer representation
  816. of the string pointer is \var{Nil}. If the string is not empty, then the
  817. pointer points to a structure in heap memory.
  818. The internal representation as a pointer, and the automatic null-termination
  819. make it possible to typecast an ansistring to a pchar. If the string is empty
  820. (so the pointer is nil) then the compiler makes sure that the typecasted
  821. pchar will point to a null byte.
  822. Assigning one ansistring to another doesn't involve moving the actual
  823. string. A statement
  824. \begin{verbatim}
  825. S2:=S1;
  826. \end{verbatim}
  827. results in the reference count of \var{S2} being decreased by one,
  828. The referece count of \var{S1} is increased by one, and finally \var{S1}
  829. (as a pointer) is copied to \var{S2}. This is a significant speed-up in
  830. the code.
  831. If the reference count reaches zero, then the memory occupied by the
  832. string is deallocated automatically, so no memory leaks arise.
  833. When an ansistring is declared, the \fpc compiler initially
  834. allocates just memory for a pointer, not more. This pointer is guaranteed
  835. to be nil, meaning that the string is initially empty. This is
  836. true for local and global ansistrings or anstrings that are part of a
  837. structure (arrays, records or objects).
  838. This does introduce an overhead. For instance, declaring
  839. \begin{verbatim}
  840. Var
  841. A : Array[1..100000] of string;
  842. \end{verbatim}
  843. Will copy 100,000 times \var{nil} into \var{A}. When \var{A} goes out of scope, then
  844. the reference count of the 100,000 strings will be decreased by 1 for each
  845. of these strings. All this happens
  846. invisibly for the programmer, but when considering performance issues,
  847. this is important.
  848. Memory will be allocated only when the string is assigned a value.
  849. If the string goes out of scope, then its reference count is automatically
  850. decreased by 1. If the reference count reaches zero, the memory reserved for
  851. the string is released.
  852. If a value is assigned to a character of a string that has a reference count
  853. greater than 1, such as in the following
  854. statements:
  855. \begin{verbatim}
  856. S:=T; { reference count for S and T is now 2 }
  857. S[I]:='@';
  858. \end{verbatim}
  859. then a copy of the string is created before the assignment. This is known
  860. as {\em copy-on-write} semantics.
  861. The \seef{Length} function must be used to get the length of an
  862. ansistring.
  863. To set the length of an ansistring, the \seep{SetLength} function must be used.
  864. Constant ansistrings have a reference count of -1 and are treated specially.
  865. Ansistrings are converted to short strings by the compiler if needed,
  866. this means that the use of ansistrings and short strings can be mixed
  867. without problems.
  868. Ansistrings can be typecasted to \var{PChar} or \var{Pointer} types:
  869. \begin{verbatim}
  870. Var P : Pointer;
  871. PC : PChar;
  872. S : AnsiString;
  873. begin
  874. S :='This is an ansistring';
  875. PC:=Pchar(S);
  876. P :=Pointer(S);
  877. \end{verbatim}
  878. There is a difference between the two typecasts. When an empty
  879. ansistring is typecasted to a pointer, the pointer wil be \var{Nil}. If an
  880. empty ansistring is typecasted to a \var{PChar}, then the result will be a pointer to a
  881. zero byte (an empty string).
  882. The result of such a typecast must be used with care. In general, it is best
  883. to consider the result of such a typecast as read-only, i.e. suitable for
  884. passing to a procedure that needs a constant pchar argument.
  885. It is therefore NOT advisable to typecast one of the following:
  886. \begin{enumerate}
  887. \item expressions.
  888. \item strings that have reference count larger than 0.
  889. (call uniquestring to ensure a string has reference count 1)
  890. \end{enumerate}
  891. % Constant strings
  892. \subsection{Constant strings}
  893. To specify a constant string, it must be enclosed in single-quotes, just
  894. as a \var{Char} type, only now more than one character is allowed.
  895. Given that \var{S} is of type \var{String}, the following are valid assignments:
  896. \begin{verbatim}
  897. S := 'This is a string.';
  898. S := 'One'+', Two'+', Three';
  899. S := 'This isn''t difficult !';
  900. S := 'This is a weird character : '#145' !';
  901. \end{verbatim}
  902. As can be seen, the single quote character is represented by 2 single-quote
  903. characters next to each other. Strange characters can be specified by their
  904. ASCII value.
  905. The example shows also that two strings can be added. The resulting string is
  906. just the concatenation of the first with the second string, without spaces in
  907. between them. Strings can not be substracted, however.
  908. Whether the constant string is stored as an ansistring or a short string
  909. depends on the settings of the \var{\{\$H\}} switch.
  910. % PChar
  911. \subsection{PChar - Null terminated strings}
  912. \fpc supports the Delphi implementation of the \var{PChar} type. \var{PChar}
  913. is defined as a pointer to a \var{Char} type, but allows additional
  914. operations.
  915. The \var{PChar} type can be understood best as the Pascal equivalent of a
  916. C-style null-terminated string, i.e. a variable of type \var{PChar} is a
  917. pointer that points to an array of type \var{Char}, which is ended by a
  918. null-character (\var{\#0}).
  919. \fpc supports initializing of \var{PChar} typed constants, or a direct
  920. assignment. For example, the following pieces of code are equivalent:
  921. \begin{verbatim}
  922. program one;
  923. var p : PChar;
  924. begin
  925. P := 'This is a null-terminated string.';
  926. WriteLn (P);
  927. end.
  928. \end{verbatim}
  929. Results in the same as
  930. \begin{verbatim}
  931. program two;
  932. const P : PChar = 'This is a null-terminated string.'
  933. begin
  934. WriteLn (P);
  935. end.
  936. \end{verbatim}
  937. These examples also show that it is possible to write {\em the contents} of
  938. the string to a file of type \var{Text}.
  939. The \seestrings unit contains procedures and functions that manipulate the
  940. \var{PChar} type as in the standard C library.
  941. Since it is equivalent to a pointer to a type \var{Char} variable, it is
  942. also possible to do the following:
  943. \begin{verbatim}
  944. Program three;
  945. Var S : String[30];
  946. P : PChar;
  947. begin
  948. S := 'This is a null-terminated string.'#0;
  949. P := @S[1];
  950. WriteLn (P);
  951. end.
  952. \end{verbatim}
  953. This will have the same result as the previous two examples.
  954. Null-terminated strings cannot be added as normal Pascal
  955. strings. If two \var{PChar} strings mustt be concatenated; the functions from
  956. the unit \seestrings must be used.
  957. However, it is possible to do some pointer arithmetic. The
  958. operators \var{+} and \var{-} can be used to do operations on \var{PChar} pointers.
  959. In \seet{PCharMath}, \var{P} and \var{Q} are of type \var{PChar}, and
  960. \var{I} is of type \var{Longint}.
  961. \begin{FPCltable}{lr}{\var{PChar} pointer arithmetic}{PCharMath}
  962. Operation & Result \\ \hline
  963. \var{P + I} & Adds \var{I} to the address pointed to by \var{P}. \\
  964. \var{I + P} & Adds \var{I} to the address pointed to by \var{P}. \\
  965. \var{P - I} & Substracts \var{I} from the address pointed to by \var{P}. \\
  966. \var{P - Q} & Returns, as an integer, the distance between 2 addresses \\
  967. & (or the number of characters between \var{P} and \var{Q}) \\
  968. \hline
  969. \end{FPCltable}
  970. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  971. % Structured Types
  972. \section{Structured Types}
  973. A structured type is a type that can hold multiple values in one variable.
  974. Stuctured types can be nested to unlimited levels.
  975. \input{syntax/typestru.syn}
  976. Unlike Delphi, \fpc does not support the keyword \var{Packed} for all
  977. structured types, as can be seen in the syntax diagram. It will be mentioned
  978. when a type supports the \var{packed} keyword.
  979. In the following, each of the possible structured types is discussed.
  980. \subsection{Arrays}
  981. \fpc supports arrays as in Turbo Pascal, multi-dimensional arrays
  982. and packed arrays are also supported:
  983. \input{syntax/typearr.syn}
  984. The following is a valid array declaration:
  985. \begin{verbatim}
  986. Type
  987. RealArray = Array [1..100] of Real;
  988. \end{verbatim}
  989. As in Turbo Pascal, if the array component type is in itself an array, it is
  990. possible to combine the two arrays into one multi-dimensional array. The
  991. following declaration:
  992. \begin{verbatim}
  993. Type
  994. APoints = array[1..100] of Array[1..3] of Real;
  995. \end{verbatim}
  996. is equivalent to the following declaration:
  997. \begin{verbatim}
  998. Type
  999. APoints = array[1..100,1..3] of Real;
  1000. \end{verbatim}
  1001. The functions \seef{High} and \seef{Low} return the high and low bounds of
  1002. the leftmost index type of the array. In the above case, this would be 100
  1003. and 1.
  1004. \subsection{Record types}
  1005. \fpc supports fixed records and records with variant parts.
  1006. The syntax diagram for a record type is
  1007. \input{syntax/typerec.syn}
  1008. So the following are valid record types declarations:
  1009. \begin{verbatim}
  1010. Type
  1011. Point = Record
  1012. X,Y,Z : Real;
  1013. end;
  1014. RPoint = Record
  1015. Case Boolean of
  1016. False : (X,Y,Z : Real);
  1017. True : (R,theta,phi : Real);
  1018. end;
  1019. BetterRPoint = Record
  1020. Case UsePolar : Boolean of
  1021. False : (X,Y,Z : Real);
  1022. True : (R,theta,phi : Real);
  1023. end;
  1024. \end{verbatim}
  1025. The variant part must be last in the record. The optional identifier in the
  1026. case statement serves to access the tag field value, which otherwise would
  1027. be invisible to the programmer. It can be used to see which variant is
  1028. active at a certain time. In effect, it introduces a new field in the
  1029. record.
  1030. \begin{remark}
  1031. It is possible to nest variant parts, as in:
  1032. \begin{verbatim}
  1033. Type
  1034. MyRec = Record
  1035. X : Longint;
  1036. Case byte of
  1037. 2 : (Y : Longint;
  1038. case byte of
  1039. 3 : (Z : Longint);
  1040. );
  1041. end;
  1042. \end{verbatim}
  1043. \end{remark}
  1044. The size of a record is the sum of the sizes of its fields, each size of a
  1045. field is rounded up to a power of two. If the record contains a variant part, the size
  1046. of the variant part is the size of the biggest variant, plus the size of the
  1047. tag field type {\em if an identifier was declared for it}. Here also, the size of
  1048. each part is first rounded up to two. So in the above example,
  1049. \seef{SizeOf} would return 24 for \var{Point}, 24 for \var{RPoint} and
  1050. 26 for \var{BetterRPoint}. For \var{MyRec}, the value would be 12.
  1051. If a typed file with records, produced by a Turbo Pascal program, must be read,
  1052. then chances are that attempting to read that file correctly will fail.
  1053. The reason for this is that by default, elements of a record are aligned at
  1054. 2-byte boundaries, for performance reasons. This default behaviour can be
  1055. changed with the \var{\{\$PackRecords n\}} switch. Possible values for
  1056. \var{n} are 1, 2, 4, 16 or \var{Default}.
  1057. This switch tells the compiler to align elements of a record or object or
  1058. class that have size larger than \var{n} on \var{n} byte boundaries.
  1059. Elements that have size smaller or equal than \var{n} are aligned on
  1060. natural boundaries, i.e. to the first power of two that is larger than or
  1061. equal to the size of the record element.
  1062. The keyword \var{Default} selects the default value for the platform
  1063. that the code is compiled for (currently, this is 2 on all platforms)
  1064. Take a look at the following program:
  1065. \begin{verbatim}
  1066. Program PackRecordsDemo;
  1067. type
  1068. {$PackRecords 2}
  1069. Trec1 = Record
  1070. A : byte;
  1071. B : Word;
  1072. end;
  1073. {$PackRecords 1}
  1074. Trec2 = Record
  1075. A : Byte;
  1076. B : Word;
  1077. end;
  1078. {$PackRecords 2}
  1079. Trec3 = Record
  1080. A,B : byte;
  1081. end;
  1082. {$PackRecords 1}
  1083. Trec4 = Record
  1084. A,B : Byte;
  1085. end;
  1086. {$PackRecords 4}
  1087. Trec5 = Record
  1088. A : Byte;
  1089. B : Array[1..3] of byte;
  1090. C : byte;
  1091. end;
  1092. {$PackRecords 8}
  1093. Trec6 = Record
  1094. A : Byte;
  1095. B : Array[1..3] of byte;
  1096. C : byte;
  1097. end;
  1098. {$PackRecords 4}
  1099. Trec7 = Record
  1100. A : Byte;
  1101. B : Array[1..7] of byte;
  1102. C : byte;
  1103. end;
  1104. {$PackRecords 8}
  1105. Trec8 = Record
  1106. A : Byte;
  1107. B : Array[1..7] of byte;
  1108. C : byte;
  1109. end;
  1110. Var rec1 : Trec1;
  1111. rec2 : Trec2;
  1112. rec3 : TRec3;
  1113. rec4 : TRec4;
  1114. rec5 : Trec5;
  1115. rec6 : TRec6;
  1116. rec7 : TRec7;
  1117. rec8 : TRec8;
  1118. begin
  1119. Write ('Size Trec1 : ',SizeOf(Trec1));
  1120. Writeln (' Offset B : ',Longint(@rec1.B)-Longint(@rec1));
  1121. Write ('Size Trec2 : ',SizeOf(Trec2));
  1122. Writeln (' Offset B : ',Longint(@rec2.B)-Longint(@rec2));
  1123. Write ('Size Trec3 : ',SizeOf(Trec3));
  1124. Writeln (' Offset B : ',Longint(@rec3.B)-Longint(@rec3));
  1125. Write ('Size Trec4 : ',SizeOf(Trec4));
  1126. Writeln (' Offset B : ',Longint(@rec4.B)-Longint(@rec4));
  1127. Write ('Size Trec5 : ',SizeOf(Trec5));
  1128. Writeln (' Offset B : ',Longint(@rec5.B)-Longint(@rec5),
  1129. ' Offset C : ',Longint(@rec5.C)-Longint(@rec5));
  1130. Write ('Size Trec6 : ',SizeOf(Trec6));
  1131. Writeln (' Offset B : ',Longint(@rec6.B)-Longint(@rec6),
  1132. ' Offset C : ',Longint(@rec6.C)-Longint(@rec6));
  1133. Write ('Size Trec7 : ',SizeOf(Trec7));
  1134. Writeln (' Offset B : ',Longint(@rec7.B)-Longint(@rec7),
  1135. ' Offset C : ',Longint(@rec7.C)-Longint(@rec7));
  1136. Write ('Size Trec8 : ',SizeOf(Trec8));
  1137. Writeln (' Offset B : ',Longint(@rec8.B)-Longint(@rec8),
  1138. ' Offset C : ',Longint(@rec8.C)-Longint(@rec8));
  1139. end.
  1140. \end{verbatim}
  1141. The output of this program will be :
  1142. \begin{verbatim}
  1143. Size Trec1 : 4 Offset B : 2
  1144. Size Trec2 : 3 Offset B : 1
  1145. Size Trec3 : 2 Offset B : 1
  1146. Size Trec4 : 2 Offset B : 1
  1147. Size Trec5 : 8 Offset B : 4 Offset C : 7
  1148. Size Trec6 : 8 Offset B : 4 Offset C : 7
  1149. Size Trec7 : 12 Offset B : 4 Offset C : 11
  1150. Size Trec8 : 16 Offset B : 8 Offset C : 15
  1151. \end{verbatim}
  1152. And this is as expected. In \var{Trec1}, since \var{B} has size 2, it is
  1153. aligned on a 2 byte boundary, thus leaving an empty byte between \var{A}
  1154. and \var{B}, and making the total size 4. In \var{Trec2}, \var{B} is aligned
  1155. on a 1-byte boundary, right after \var{A}, hence, the total size of the
  1156. record is 3.
  1157. For \var{Trec3}, the sizes of \var{A,B} are 1, and hence they are aligned on 1
  1158. byte boundaries. The same is true for \var{Trec4}.
  1159. For \var{Trec5}, since the size of B -- 3 -- is smaller than 4, \var{B} will
  1160. be on a 4-byte boundary, as this is the first power of two that is
  1161. larger than it's size. The same holds for \var{Trec6}.
  1162. For \var{Trec7}, \var{B} is aligned on a 4 byte boundary, since it's size --
  1163. 7 -- is larger than 4. However, in \var{Trec8}, it is aligned on a 8-byte
  1164. boundary, since 8 is the first power of two that is greater than 7, thus
  1165. making the total size of the record 16.
  1166. \fpc supports also the 'packed record', this is a record where all the
  1167. elements are byte-aligned.
  1168. Thus the two following declarations are equivalent:
  1169. \begin{verbatim}
  1170. {$PackRecords 1}
  1171. Trec2 = Record
  1172. A : Byte;
  1173. B : Word;
  1174. end;
  1175. {$PackRecords 2}
  1176. \end{verbatim}
  1177. and
  1178. \begin{verbatim}
  1179. Trec2 = Packed Record
  1180. A : Byte;
  1181. B : Word;
  1182. end;
  1183. \end{verbatim}
  1184. Note the \var{\{\$PackRecords 2\}} after the first declaration !
  1185. \subsection{Set types}
  1186. \fpc supports the set types as in Turbo Pascal. The prototype of a set
  1187. declaration is:
  1188. \input{syntax/typeset.syn}
  1189. Each of the elements of \var{SetType} must be of type \var{TargetType}.
  1190. \var{TargetType} can be any ordinal type with a range between \var{0} and
  1191. \var{255}. A set can contain maximally \var{255} elements.
  1192. The following are valid set declaration:
  1193. \begin{verbatim}
  1194. Type
  1195. Junk = Set of Char;
  1196. Days = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
  1197. WorkDays : Set of days;
  1198. \end{verbatim}
  1199. Given this set declarations, the following assignment is legal:
  1200. \begin{verbatim}
  1201. WorkDays := [ Mon, Tue, Wed, Thu, Fri];
  1202. \end{verbatim}
  1203. The operators and functions for manipulations of sets are listed in
  1204. \seet{SetOps}.
  1205. \begin{FPCltable}{lr}{Set Manipulation operators}{SetOps}
  1206. Operation & Operator \\ \hline
  1207. Union & + \\
  1208. Difference & - \\
  1209. Intersection & * \\
  1210. Add element & \var{include} \\
  1211. Delete element & \var{exclude} \\ \hline
  1212. \end{FPCltable}
  1213. Two sets can be compared with the \var{<>} and \var{=} operators, but not
  1214. (yet) with the \var{<} and \var{>} operators.
  1215. The compiler stores small sets (less than 32 elements) in a Longint, if the
  1216. type range allows it. This allows for faster processing and decreases
  1217. program size. Otherwise, sets are stored in 32 bytes.
  1218. \subsection{File types}
  1219. File types are types that store a sequence of some base type, which can be
  1220. any type except another file type. It can contain (in principle) an infinite
  1221. number of elements.
  1222. File types are used commonly to store data on disk. Nothing prevents the programmer,
  1223. however, from writing a file driver that stores it's data in memory.
  1224. Here is the type declaration for a file type:
  1225. \input{syntax/typefil.syn}
  1226. If no type identifier is given, then the file is an untyped file; it can be
  1227. considered as equivalent to a file of bytes. Untyped files require special
  1228. commands to act on them (see \seep{Blockread}, \seep{Blockwrite}).
  1229. The following declaration declares a file of records:
  1230. \begin{verbatim}
  1231. Type
  1232. Point = Record
  1233. X,Y,Z : real;
  1234. end;
  1235. PointFile = File of Point;
  1236. \end{verbatim}
  1237. Internally, files are represented by the \var{FileRec} record, which is
  1238. declared in the DOS unit.
  1239. A special file type is the \var{Text} file type, represented by the
  1240. \var{TextRec} record. A file of type \var{Text} uses special input-output
  1241. routines.
  1242. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1243. % Pointers
  1244. \section{Pointers}
  1245. \fpc supports the use of pointers. A variable of the pointer type
  1246. contains an address in memory, where the data of another variable may be
  1247. stored.
  1248. \input{syntax/typepoin.syn}
  1249. As can be seen from this diagram, pointers are typed, which means that
  1250. they point to a particular kind of data. The type of this data must be
  1251. known at compile time.
  1252. Dereferencing the pointer (denoted by adding \var{\^{}} after the variable
  1253. name) behaves then like a variable. This variable has the type declared in
  1254. the pointer declaration, and the variable is stored in the address that is
  1255. pointed to by the pointer variable.
  1256. Consider the following example:
  1257. \begin{verbatim}
  1258. Program pointers;
  1259. type
  1260. Buffer = String[255];
  1261. BufPtr = ^Buffer;
  1262. Var B : Buffer;
  1263. BP : BufPtr;
  1264. PP : Pointer;
  1265. etc..
  1266. \end{verbatim}
  1267. In this example, \var{BP} {\em is a pointer to} a \var{Buffer} type; while \var{B}
  1268. {\em is} a variable of type \var{Buffer}. \var{B} takes 256 bytes memory,
  1269. and \var{BP} only takes 4 bytes of memory (enough to keep an adress in
  1270. memory).
  1271. \begin{remark} \fpc treats pointers much the same way as C does. This means
  1272. that a pointer to some type can be treated as being an array of this type.
  1273. The pointer then points to the zeroeth element of this array. Thus the
  1274. following pointer declaration
  1275. \begin{verbatim}
  1276. Var p : ^Longint;
  1277. \end{verbatim}
  1278. Can be considered equivalent to the following array declaration:
  1279. \begin{verbatim}
  1280. Var p : array[0..Infinity] of Longint;
  1281. \end{verbatim}
  1282. The difference is that the former declaration allocates memory for the
  1283. pointer only (not for the array), and the second declaration allocates
  1284. memory for the entire array. If the former is used, the memory must be
  1285. allocated manually, using the \seep{Getmem} function.
  1286. The reference \var{P\^{}} is then the same as \var{p[0]}. The following program
  1287. illustrates this maybe more clear:
  1288. \begin{verbatim}
  1289. program PointerArray;
  1290. var i : Longint;
  1291. p : ^Longint;
  1292. pp : array[0..100] of Longint;
  1293. begin
  1294. for i := 0 to 100 do pp[i] := i; { Fill array }
  1295. p := @pp[0]; { Let p point to pp }
  1296. for i := 0 to 100 do
  1297. if p[i]<>pp[i] then
  1298. WriteLn ('Ohoh, problem !')
  1299. end.
  1300. \end{verbatim}
  1301. \end{remark}
  1302. \fpc supports pointer arithmetic as C does. This means that, if \var{P} is a
  1303. typed pointer, the instructions
  1304. \begin{verbatim}
  1305. Inc(P);
  1306. Dec(P);
  1307. \end{verbatim}
  1308. Will increase, respectively decrease the address the pointer points to
  1309. with the size of the type \var{P} is a pointer to. For example
  1310. \begin{verbatim}
  1311. Var P : ^Longint;
  1312. ...
  1313. Inc (p);
  1314. \end{verbatim}
  1315. will increase \var{P} with 4.
  1316. Normal arithmetic operators on pointers can also be used, that is, the
  1317. following are valid pointer arithmetic operations:
  1318. \begin{verbatim}
  1319. var p1,p2 : ^Longint;
  1320. L : Longint;
  1321. begin
  1322. P1 := @P2;
  1323. P2 := @L;
  1324. L := P1-P2;
  1325. P1 := P1-4;
  1326. P2 := P2+4;
  1327. end.
  1328. \end{verbatim}
  1329. Here, the value that is added or substracted {\em is } multiplied by the
  1330. size of the type the pointer points to. In the previous
  1331. example \var{P1} will be decremented by 16 bytes, and
  1332. \var{P2} will be incremented by 16.
  1333. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1334. % Procedural types
  1335. \section{Procedural types}
  1336. \fpc has support for procedural types, although it differs a little from
  1337. the Turbo Pascal implementation of them. The type declaration remains the
  1338. same, as can be seen in the following syntax diagram:
  1339. \input{syntax/typeproc.syn}
  1340. For a description of formal parameter lists, see \seec{Procedures}.
  1341. The two following examples are valid type declarations:
  1342. \begin{verbatim}
  1343. Type TOneArg = Procedure (Var X : integer);
  1344. TNoArg = Function : Real;
  1345. var proc : TOneArg;
  1346. func : TNoArg;
  1347. \end{verbatim}
  1348. One can assign the following values to a procedural type variable:
  1349. \begin{enumerate}
  1350. \item \var{Nil}, for both normal procedure pointers and method pointers.
  1351. \item A variable reference of a procedural type, i.e. another variable of
  1352. the same type.
  1353. \item A global procedure or function address, with matching function or
  1354. procedure header and calling convention.
  1355. \item A method address.
  1356. \end{enumerate}
  1357. Given these declarations, the following assignments are valid:
  1358. \begin{verbatim}
  1359. Procedure printit (Var X : Integer);
  1360. begin
  1361. WriteLn (x);
  1362. end;
  1363. ...
  1364. Proc := @printit;
  1365. Func := @Pi;
  1366. \end{verbatim}
  1367. From this example, the difference with Turbo Pascal is clear: In Turbo
  1368. Pascal it isn't necessary to use the address operator (\var{@})
  1369. when assigning a procedural type variable, whereas in \fpc it is required
  1370. (unless the \var{-So} switch is used, in which case the address
  1371. operator can be dropped.)
  1372. \begin{remark} The modifiers concerning the calling conventions
  1373. must be the same as the declaration;
  1374. i.e. the following code would give an error:
  1375. \begin{verbatim}
  1376. Type TOneArgCcall = Procedure (Var X : integer);cdecl;
  1377. var proc : TOneArgCcall;
  1378. Procedure printit (Var X : Integer);
  1379. begin
  1380. WriteLn (x);
  1381. end;
  1382. begin
  1383. Proc := @printit;
  1384. end.
  1385. \end{verbatim}
  1386. Because the \var{TOneArgCcall} type is a procedure that uses the cdecl
  1387. calling convention.
  1388. \end{remark}
  1389. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1390. % Objects
  1391. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1392. \chapter{Objects}
  1393. \label{ch:Objects}
  1394. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1395. % Declaration
  1396. \section{Declaration}
  1397. \fpc supports object oriented programming. In fact, most of the compiler is
  1398. written using objects. Here we present some technical questions regarding
  1399. object oriented programming in \fpc.
  1400. Objects should be treated as a special kind of record. The record contains
  1401. all the fields that are declared in the objects definition, and pointers
  1402. to the methods that are associated to the objects' type.
  1403. An object is declared just as a record would be declared; except that
  1404. now,procedures and functions can be declared as if they were part of the record.
  1405. Objects can ''inherit'' fields and methods from ''parent'' objects. This means
  1406. that these fields and methods can be used as if they were included in the
  1407. objects declared as a ''child'' object.
  1408. Furthermore, a concept of visibility is introduced: fields, procedures and functions
  1409. can be delcared as \var{public} or \var{private}. By default, fields and
  1410. methods are \var{public}, and are
  1411. exported outside the current unit. Fields or methods that are declared
  1412. \var{private} are only accessible in the current unit.
  1413. The prototype declaration of an object is as follows:
  1414. \input{syntax/typeobj.syn}
  1415. As can be seen, as many \var{private} and \var{public} blocks as needed can be
  1416. declared.
  1417. \var{Method definitions} are normal function or procedure declarations.
  1418. Fields cannot be declared after methods in the same block, i.e. the following
  1419. will generate an error when compiling:
  1420. \begin{verbatim}
  1421. Type MyObj = Object
  1422. Procedure Doit;
  1423. Field : Longint;
  1424. end;
  1425. \end{verbatim}
  1426. But the following will be accepted:
  1427. \begin{verbatim}
  1428. Type MyObj = Object
  1429. Public
  1430. Procedure Doit;
  1431. Private
  1432. Field : Longint;
  1433. end;
  1434. \end{verbatim}
  1435. because the field is in a different section.
  1436. \begin{remark}
  1437. \fpc also supports the packed object. This is the same as an object, only
  1438. the elements (fields) of the object are byte-aligned, just as in the packed
  1439. record.
  1440. The declaration of a packed object is similar to the declaration
  1441. of a packed record :
  1442. \begin{verbatim}
  1443. Type
  1444. TObj = packed object;
  1445. Constructor init;
  1446. ...
  1447. end;
  1448. Pobj = ^TObj;
  1449. Var PP : Pobj;
  1450. \end{verbatim}
  1451. Similarly, the \var{\{\$PackRecords \}} directive acts on objects as well.
  1452. \end{remark}
  1453. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1454. % Fields
  1455. \section{Fields}
  1456. Object Fields are like record fields. They are accessed in the same way as
  1457. a record field would be accessed : by using a qualified identifier. Given the
  1458. following declaration:
  1459. \begin{verbatim}
  1460. Type TAnObject = Object
  1461. AField : Longint;
  1462. Procedure AMethod;
  1463. end;
  1464. Var AnObject : TAnObject;
  1465. \end{verbatim}
  1466. then the following would be a valid assignment:
  1467. \begin{verbatim}
  1468. AnObject.AField := 0;
  1469. \end{verbatim}
  1470. Inside methods, fields can be accessed using the short identifier:
  1471. \begin{verbatim}
  1472. Procedure TAnObject.AMethod;
  1473. begin
  1474. ...
  1475. AField := 0;
  1476. ...
  1477. end;
  1478. \end{verbatim}
  1479. Or, one can use the \var{self} identifier. The \var{self} identifier refers
  1480. to the current instance of the object:
  1481. \begin{verbatim}
  1482. Procedure TAnObject.AMethod;
  1483. begin
  1484. ...
  1485. Self.AField := 0;
  1486. ...
  1487. end;
  1488. \end{verbatim}
  1489. One cannot access fields that are in a private section of an object from
  1490. outside the objects' methods. If this is attempted anyway, the compiler will complain about
  1491. an unknown identifier.
  1492. It is also possible to use the \var{with} statement with an object instance:
  1493. \begin{verbatim}
  1494. With AnObject do
  1495. begin
  1496. Afield := 12;
  1497. AMethod;
  1498. end;
  1499. \end{verbatim}
  1500. In this example, between the \var{begin} and \var{end}, it is as if
  1501. \var{AnObject} was prepended to the \var{Afield} and \var{Amethod}
  1502. identifiers. More about this in \sees{With}
  1503. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1504. % Constructors and destructors
  1505. \section{Constructors and destructors }
  1506. \label{se:constructdestruct}
  1507. As can be seen in the syntax diagram for an object declaration, \fpc supports
  1508. constructors and destructors. The programmer is responsible for calling the
  1509. constructor and the destructor explicitly when using objects.
  1510. The declaration of a constructor or destructor is as follows:
  1511. \input{syntax/construct.syn}
  1512. A constructor/destructor pair is {\em required} if the object uses virtual methods.
  1513. In the declaration of the object type, a simple identifier should be used
  1514. for the name of the constuctor or destructor. When the constructor or destructor
  1515. is implemented, A qualified method identifier should be used,
  1516. i.e. an identifier of the form \var{objectidentifier.methodidentifier}.
  1517. \fpc supports also the extended syntax of the \var{New} and \var{Dispose}
  1518. procedures. In case a dynamic variable of an object type must be allocated
  1519. the constructor's name can be specified in the call to \var{New}.
  1520. The \var{New} is implemented as a function which returns a pointer to the
  1521. instantiated object. Consider the following declarations:
  1522. \begin{verbatim}
  1523. Type
  1524. TObj = object;
  1525. Constructor init;
  1526. ...
  1527. end;
  1528. Pobj = ^TObj;
  1529. Var PP : Pobj;
  1530. \end{verbatim}
  1531. Then the following 3 calls are equivalent:
  1532. \begin{verbatim}
  1533. pp := new (Pobj,Init);
  1534. \end{verbatim}
  1535. and
  1536. \begin{verbatim}
  1537. new(pp,init);
  1538. \end{verbatim}
  1539. and also
  1540. \begin{verbatim}
  1541. new (pp);
  1542. pp^.init;
  1543. \end{verbatim}
  1544. In the last case, the compiler will issue a warning that the
  1545. extended syntax of \var{new} and \var{dispose} must be used to generate instances of an
  1546. object. It is possible to ignore this warning, but it's better programming practice to
  1547. use the extended syntax to create instances of an object.
  1548. Similarly, the \var{Dispose} procedure accepts the name of a destructor. The
  1549. destructor will then be called, before removing the object from the heap.
  1550. In view of the compiler warning remark, the following chapter presents the
  1551. Delphi approach to object-oriented programming, and may be considered a
  1552. more natural way of object-oriented programming.
  1553. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1554. % Methods
  1555. \section{Methods}
  1556. Object methods are just like ordinary procedures or functions, only they
  1557. have an implicit extra parameter : \var{self}. Self points to the object
  1558. with which the method was invoked.
  1559. When implementing methods, the fully qualified identifier must be given
  1560. in the function header. When declaring methods, a normal identifier must be
  1561. given.
  1562. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1563. % Method invocation
  1564. \section{Method invocation}
  1565. Methods are called just as normal procedures are called, only they have an
  1566. object instance identifier prepended to them (see also \seec{Statements}).
  1567. To determine which method is called, it is necessary to know the type of
  1568. the method. We treat the different types in what follows.
  1569. \subsubsection{Static methods}
  1570. Static methods are methods that have been declared without a \var{abstract}
  1571. or \var{virtual} keyword. When calling a static method, the declared (i.e.
  1572. compile time) method of the object is used.
  1573. For example, consider the following declarations:
  1574. \begin{verbatim}
  1575. Type
  1576. TParent = Object
  1577. ...
  1578. procedure Doit;
  1579. ...
  1580. end;
  1581. PParent = ^TParent;
  1582. TChild = Object(TParent)
  1583. ...
  1584. procedure Doit;
  1585. ...
  1586. end;
  1587. PChild = ^TChild;
  1588. \end{verbatim}
  1589. As it is visible, both the parent and child objects have a method called
  1590. \var{Doit}. Consider now the following declarations and calls:
  1591. \begin{verbatim}
  1592. Var ParentA,ParentB : PParent;
  1593. Child : PChild;
  1594. ParentA := New(PParent,Init);
  1595. ParentB := New(PChild,Init);
  1596. Child := New(PChild,Init);
  1597. ParentA^.Doit;
  1598. ParentB^.Doit;
  1599. Child^.Doit;
  1600. \end{verbatim}
  1601. Of the three invocations of \var{Doit}, only the last one will call
  1602. \var{TChild.Doit}, the other two calls will call \var{TParent.Doit}.
  1603. This is because for static methods, the compiler determines at compile
  1604. time which method should be called. Since \var{ParentB} is of type
  1605. \var{TParent}, the compiler decides that it must be called with
  1606. \var{TParent.Doit}, even though it will be created as a \var{TChild}.
  1607. There may be times when the method that is actually called should
  1608. depend on the actual type of the object at run-time. If so, the method
  1609. cannot be a static method, but must be a virtual method.
  1610. \subsubsection{Virtual methods}
  1611. To remedy the situation in the previous section, \var{virtual} methods are
  1612. created. This is simply done by appending the method declaration with the
  1613. \var{virtual} modifier.
  1614. Going back to the previous example, consider the following alternative
  1615. declaration:
  1616. \begin{verbatim}
  1617. Type
  1618. TParent = Object
  1619. ...
  1620. procedure Doit;virtual;
  1621. ...
  1622. end;
  1623. PParent = ^TParent;
  1624. TChild = Object(TParent)
  1625. ...
  1626. procedure Doit;virtual;
  1627. ...
  1628. end;
  1629. PChild = ^TChild;
  1630. \end{verbatim}
  1631. As it is visible, both the parent and child objects have a method called
  1632. \var{Doit}. Consider now the following declarations and calls :
  1633. \begin{verbatim}
  1634. Var ParentA,ParentB : PParent;
  1635. Child : PChild;
  1636. ParentA := New(PParent,Init);
  1637. ParentB := New(PChild,Init);
  1638. Child := New(PChild,Init);
  1639. ParentA^.Doit;
  1640. ParentB^.Doit;
  1641. Child^.Doit;
  1642. \end{verbatim}
  1643. Now, different methods will be called, depending on the actual run-time type
  1644. of the object. For \var{ParentA}, nothing changes, since it is created as
  1645. a \var{TParent} instance. For \var{Child}, the situation also doesn't
  1646. change: it is again created as an instance of \var{TChild}.
  1647. For \var{ParentB} however, the situation does change: Even though it was
  1648. declared as a \var{TParent}, it is created as an instance of \var{TChild}.
  1649. Now, when the program runs, before calling \var{Doit}, the program
  1650. checks what the actual type of \var{ParentB} is, and only then decides which
  1651. method must be called. Seeing that \var{ParentB} is of type \var{TChild},
  1652. \var{TChild.Doit} will be called.
  1653. The code for this run-time checking of the actual type of an object is
  1654. inserted by the compiler at compile time.
  1655. The \var{TChild.Doit} is said to {\em override} the \var{TParent.Doit}.
  1656. It is possible to acces the \var{TParent.Doit} from within the
  1657. var{TChild.Doit}, with the \var{inherited} keyword:
  1658. \begin{verbatim}
  1659. Procedure TChild.Doit;
  1660. begin
  1661. inherited Doit;
  1662. ...
  1663. end;
  1664. \end{verbatim}
  1665. In the above example, when \var{TChild.Doit} is called, the first thing it
  1666. does is call \var{TParent.Doit}. The inherited keyword cannot be used in
  1667. static methods, only on virtual methods.
  1668. \subsubsection{Abstract methods}
  1669. An abstract method is a special kind of virtual method. A method can not be
  1670. abstract if it is not virtual (this is not obvious from the syntax diagram).
  1671. An instance of an object that has an abstract method cannot be created directly.
  1672. The reason is obvious: there is no method where the compiler could jump to !
  1673. A method that is declared \var{abstract} does not have an implementation for
  1674. this method. It is up to inherited objects to override and implement this
  1675. method. Continuing our example, take a look at this:
  1676. \begin{verbatim}
  1677. Type
  1678. TParent = Object
  1679. ...
  1680. procedure Doit;virtual;abstract;
  1681. ...
  1682. end;
  1683. PParent=^TParent;
  1684. TChild = Object(TParent)
  1685. ...
  1686. procedure Doit;virtual;
  1687. ...
  1688. end;
  1689. PChild = ^TChild;
  1690. \end{verbatim}
  1691. As it is visible, both the parent and child objects have a method called
  1692. \var{Doit}. Consider now the following declarations and calls :
  1693. \begin{verbatim}
  1694. Var ParentA,ParentB : PParent;
  1695. Child : PChild;
  1696. ParentA := New(PParent,Init);
  1697. ParentB := New(PChild,Init);
  1698. Child := New(PChild,Init);
  1699. ParentA^.Doit;
  1700. ParentB^.Doit;
  1701. Child^.Doit;
  1702. \end{verbatim}
  1703. First of all, Line 3 will generate a compiler error, stating that one cannot
  1704. generate instances of objects with abstract methods: The compiler has
  1705. detected that \var{PParent} points to an object which has an abstract
  1706. method. Commenting line 3 would allow compilation of the program.
  1707. \begin{remark}
  1708. If an abstract method is overridden, The parent method cannot be called
  1709. with \var{inherited}, since there is no parent method; The compiler
  1710. will detect this, and complain about it, like this:
  1711. \begin{verbatim}
  1712. testo.pp(32,3) Error: Abstract methods can't be called directly
  1713. \end{verbatim}
  1714. If, through some mechanism, an abstract method is called at run-time,
  1715. then a run-time error will occur. (run-time error 211, to be precise)
  1716. \end{remark}
  1717. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1718. % Visibility
  1719. \section{Visibility}
  1720. For objects, 3 visibility specifiers exist : \var{private}, \var{protected} and
  1721. \var{public}. If a visibility specifier is not specified, \var{public}
  1722. is assumed.
  1723. Both methods and fields can be hidden from a programmer by putting them
  1724. in a \var{private} section. The exact visibility rule is as follows:
  1725. \begin{description}
  1726. \item [Private\ ] All fields and methods that are in a \var{private} block,
  1727. can only be accessed in the module (i.e. unit or program) that contains
  1728. the object definition.
  1729. They can be accessed from inside the object's methods or from outside them
  1730. e.g. from other objects' methods, or global functions.
  1731. \item [Protected\ ] Is the same as \var{Private}, except that the members of
  1732. a \var{Protected} section are also accessible to descendent types, even if
  1733. they are implemented in other modules.
  1734. \item [Public\ ] sections are always accessible, from everywhere.
  1735. Fields and metods in a \var{public} section behave as though they were part
  1736. of an ordinary \var{record} type.
  1737. \end{description}
  1738. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1739. % Classes
  1740. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1741. \chapter{Classes}
  1742. \label{ch:Classes}
  1743. In the Delphi approach to Object Oriented Programming, everything revolves
  1744. around the concept of 'Classes'. A class can be seen as a pointer to an
  1745. object, or a pointer to a record.
  1746. \begin{remark}
  1747. In earlier versions of \fpc it was necessary, in order to use classes,
  1748. to put the \file{objpas} unit in the uses clause of a unit or program.
  1749. {\em This is no longer needed} as of version 0.99.12. As of version 0.99.12
  1750. the \file{system} unit contains the basic definitions of \var{TObject}
  1751. and \var{TClass}, as well as some auxiliary methods for using classes.
  1752. The \file{objpas} unit still exists, and contains some redefinitions of
  1753. basic types, so they coincide with Delphi types. The unit will be loaded
  1754. automatically when the \var{-S2} or \var{-Sd} options are specified.
  1755. \end{remark}
  1756. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1757. % Class definitions
  1758. \section{Class definitions}
  1759. The prototype declaration of a class is as follows :
  1760. \input{syntax/typeclas.syn}
  1761. As many \var{private}, \var{protected}, \var{published}
  1762. and \var{public} blocks as needed can be repeated.
  1763. Methods are normal function or procedure declarations.
  1764. As can be seen, the declaration of a class is almost identical to the
  1765. declaration of an object. The real difference between objects and classes
  1766. is in the way they are created (see further in this chapter).
  1767. The visibility of the different sections is as follows:
  1768. \begin{description}
  1769. \item [Private\ ] All fields and methods that are in a \var{private} block, can
  1770. only be accessed in the module (i.e. unit) that contains the class definition.
  1771. They can be accessed from inside the classes' methods or from outside them
  1772. (e.g. from other classes' methods)
  1773. \item [Protected\ ] Is the same as \var{Private}, except that the members of
  1774. a \var{Protected} section are also accessible to descendent types, even if
  1775. they are implemented in other modules.
  1776. \item [Public\ ] sections are always accessible.
  1777. \item [Published\ ] Is the same as a \var{Public} section, but the compiler
  1778. generates also type information that is needed for automatic streaming of
  1779. these classes. Fields defined in a \var{published} section must be of class type.
  1780. Array peroperties cannot be in a \var{published} section.
  1781. \end{description}
  1782. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1783. % Class instantiation
  1784. \section{Class instantiation}
  1785. Classes must be created using their constructor. Remember that a class is a
  1786. pointer to an object, so when a variable of some class is declared, the
  1787. compiler just allocates a pointer, not the entire object. The constructor of
  1788. a class returns a pointer to an initialized instance of the object.
  1789. So, to initialize an instance of some class, one would do the following :
  1790. \begin{verbatim}
  1791. ClassVar := ClassType.ConstructorName;
  1792. \end{verbatim}
  1793. The extended syntax of \var{new} and \var{dispose} can be used to
  1794. instantiate and destroy class instances.
  1795. That construct is reserved for use with objects only.
  1796. Calling the constructor will provoke a call to \var{getmem}, to allocate
  1797. enough space to hold the class instance data.
  1798. After that, the constuctor's code is executed.
  1799. The constructor has a pointer to it's data, in \var{self}.
  1800. \begin{remark}
  1801. \begin{itemize}
  1802. \item The \var{\{\$PackRecords \}} directive also affects classes.
  1803. i.e. the alignment in memory of the different fields depends on the
  1804. value of the \var{\{\$PackRecords \}} directive.
  1805. \item Just as for objects and records, a packed class can be declared.
  1806. This has the same effect as on an object, or record, namely that the
  1807. elements are aligned on 1-byte boundaries. i.e. as close as possible.
  1808. \item \var{SizeOf(class)} will return 4, since a class is but a pointer to
  1809. an object. To get the size of the class instance data, use the
  1810. \var{TObject.InstanceSize} method.
  1811. \end{itemize}
  1812. \end{remark}
  1813. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1814. % Methods
  1815. \section{Methods}
  1816. \subsection{invocation}
  1817. Method invocation for classes is no different than for objects. The
  1818. following is a valid method invocation:
  1819. \begin{verbatim}
  1820. Var AnObject : TAnObject;
  1821. begin
  1822. AnObject := TAnObject.Create;
  1823. ANobject.AMethod;
  1824. \end{verbatim}
  1825. \subsection{Virtual methods}
  1826. Classes have virtual methods, just as objects do. There is however a
  1827. difference between the two. For objects, it is sufficient to redeclare the
  1828. same method in a descendent object with the keyword \var{virtual} to
  1829. override it. For classes, the situation is different:
  1830. virtual methods {\em must} be overridden with the \var{override} keyword. Failing to do so,
  1831. will start a {\em new} batch of virtual methods, hiding the previous
  1832. one. The \var{Inherited} keyword will not jump to the inherited method, if
  1833. virtual was used.
  1834. The following code is {\em wrong}:
  1835. \begin{verbatim}
  1836. Type ObjParent = Class
  1837. Procedure MyProc; virtual;
  1838. end;
  1839. ObjChild = Class(ObjPArent)
  1840. Procedure MyProc; virtual;
  1841. end;
  1842. \end{verbatim}
  1843. The compiler will produce a warning:
  1844. \begin{verbatim}
  1845. Warning: An inherited method is hidden by OBJCHILD.MYPROC
  1846. \end{verbatim}
  1847. The compiler will compile it, but using \var{Inherited} can
  1848. produce strange effects.
  1849. The correct declaration is as follows:
  1850. \begin{verbatim}
  1851. Type ObjParent = Class
  1852. Procedure MyProc; virtual;
  1853. end;
  1854. ObjChild = Class(ObjPArent)
  1855. Procedure MyProc; override;
  1856. end;
  1857. \end{verbatim}
  1858. This will compile and run without warnings or errors.
  1859. \subsection{Message methods}
  1860. New in classes are \var{message} methods. Pointers to message methods are
  1861. stored in a special table, together with the integer or string cnstant that
  1862. they were declared with. They are primarily intended to ease programming of
  1863. callback functions in several \var{GUI} toolkits, such as \var{Win32} or
  1864. \var{GTK}. In difference with Delphi, \fpc also accepts strings as message
  1865. identifiers.
  1866. Message methods that are declared with an integer constant can take only one
  1867. var argument (typed or not):
  1868. \begin{verbatim}
  1869. Procedure TMyObject.MyHandler(Var Msg); Message 1;
  1870. \end{verbatim}
  1871. The method implementation of a message function is no different from an
  1872. ordinary method. It is also possible to call a message method directly,
  1873. but this should not be done. Instead, the \var{TObject.Dispatch} method
  1874. should be used.
  1875. The \var{TOBject.Dispatch} method can be used to call a \var{message}
  1876. handler. It is declared in the \file{system} unit and will accept a var
  1877. parameter which must have at the first position a cardinal with the
  1878. message ID that should be called. For example:
  1879. \begin{verbatim}
  1880. Type
  1881. TMsg = Record
  1882. MSGID : Cardinal
  1883. Data : Pointer;
  1884. Var
  1885. Msg : TMSg;
  1886. MyObject.Dispatch (Msg);
  1887. \end{verbatim}
  1888. In this example, the \var{Dispatch} method will look at the object and all
  1889. it's ancestors (starting at the object, and searching up the class tree),
  1890. to see if a message method with message \var{MSGID} has been
  1891. declared. If such a method is found, it is called, and passed the
  1892. \var{Msg} parameter.
  1893. If no such method is found, \var{DefaultHandler} is called.
  1894. \var{DefaultHandler} is a virtual method of \var{TObject} that doesn't do
  1895. anything, but which can be overridden to provide any processing that might be
  1896. needed. \var{DefaultHandler} is declared as follows:
  1897. \begin{verbatim}
  1898. procedure defaulthandler(var message);virtual;
  1899. \end{verbatim}
  1900. In addition to the message method with a \var{Integer} identifier,
  1901. \fpc also supports a message method with a string identifier:
  1902. \begin{verbatim}
  1903. Procedure TMyObject.MyStrHandler(Var Msg); Message 'OnClick';
  1904. \end{verbatim}
  1905. The working of the string message handler is the same as the ordinary
  1906. integer message handler:
  1907. The \var{TOBject.DispatchStr} method can be used to call a \var{message}
  1908. handler. It is declared in the system unit and will accept one parameter
  1909. which must have at the first position a string with the message ID that
  1910. should be called. For example:
  1911. \begin{verbatim}
  1912. Type
  1913. TMsg = Record
  1914. MsgStr : String[10]; // Arbitrary length up to 255 characters.
  1915. Data : Pointer;
  1916. Var
  1917. Msg : TMSg;
  1918. MyObject.DispatchStr (Msg);
  1919. \end{verbatim}
  1920. In this example, the \var{DispatchStr} method will look at the object and
  1921. all it's ancestors (starting at the object, and searching up the class tree),
  1922. to see if a message method with message \var{MsgStr} has been
  1923. declared. If such a method is found, it is called, and passed the
  1924. \var{Msg} parameter.
  1925. If no such method is found, \var{DefaultHandlerStr} is called.
  1926. \var{DefaultHandlerStr} is a virtual method of \var{TObject} that doesn't do
  1927. anything, but which can be overridden to provide any processing that might be
  1928. needed. \var{DefaultHandlerStr} is declared as follows:
  1929. \begin{verbatim}
  1930. procedure DefaultHandlerStr(var message);virtual;
  1931. \end{verbatim}
  1932. In addition to this mechanism, a string message method accepts a \var{self}
  1933. parameter:
  1934. \begin{verbatim}
  1935. TMyObject.StrMsgHandler(Data : Pointer; Self : TMyObject);Message 'OnClick';
  1936. \end{verbatim}
  1937. When encountering such a method, the compiler will generate code that loads
  1938. the \var{Self} parameter into the object instance pointer. The result of
  1939. this is that it is possible to pass \var{Self} as a parameter to such a
  1940. method.
  1941. \begin{remark}
  1942. The type of the \var{Self} parameter must be of the same class
  1943. as the class the method is defined in.
  1944. \end{remark}
  1945. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1946. % Properties
  1947. \section{Properties}
  1948. Classes can contain properties as part of their fields list. A property
  1949. acts like a normal field, i.e. its value can be retrieved or set, but it
  1950. allows to redirect the access of the field through functions and
  1951. procedures. They provide a means to associate an action with an assignment
  1952. of or a reading from a class 'field'. This allows for e.g. checking that a
  1953. value is valid when assigning, or, when reading, it allows to construct the
  1954. value on the fly. Moreover, properties can be read-only or write only.
  1955. The prototype declaration of a property is as follows:
  1956. \input{syntax/property.syn}
  1957. A \var{read specifier} is either the name of a field that contains the
  1958. property, or the name of a method function that has the same return type as
  1959. the property type. In the case of a simple type, this
  1960. function must not accept an argument. A \var{read specifier} is optional,
  1961. making the property write-only.
  1962. A \var{write specifier} is optional: If there is no \var{write specifier}, the
  1963. property is read-only. A write specifier is either the name of a field, or
  1964. the name of a method procedure that accepts as a sole argument a variable of
  1965. the same type as the property.
  1966. The section (\var{private}, \var{published}) in which the specified function or
  1967. procedure resides is irrelevant. Usually, however, this will be a protected
  1968. or private method.
  1969. Example:
  1970. Given the following declaration:
  1971. \begin{verbatim}
  1972. Type
  1973. MyClass = Class
  1974. Private
  1975. Field1 : Longint;
  1976. Field2 : Longint;
  1977. Field3 : Longint;
  1978. Procedure Sety (value : Longint);
  1979. Function Gety : Longint;
  1980. Function Getz : Longint;
  1981. Public
  1982. Property X : Longint Read Field1 write Field2;
  1983. Property Y : Longint Read GetY Write Sety;
  1984. Property Z : Longint Read GetZ;
  1985. end;
  1986. Var MyClass : TMyClass;
  1987. \end{verbatim}
  1988. The following are valid statements:
  1989. \begin{verbatim}
  1990. WriteLn ('X : ',MyClass.X);
  1991. WriteLn ('Y : ',MyClass.Y);
  1992. WriteLn ('Z : ',MyClass.Z);
  1993. MyClass.X := 0;
  1994. MyClass.Y := 0;
  1995. \end{verbatim}
  1996. But the following would generate an error:
  1997. \begin{verbatim}
  1998. MyClass.Z := 0;
  1999. \end{verbatim}
  2000. because Z is a read-only property.
  2001. What happens in the above statements is that when a value needs to be read,
  2002. the compiler inserts a call to the various \var{getNNN} methods of the
  2003. object, and the result of this call is used. When an assignment is made,
  2004. the compiler passes the value that must be assigned as a paramater to
  2005. the various \var{setNNN} methods.
  2006. Because of this mechanism, properties cannot be passed as var arguments to a
  2007. function or procedure, since there is no known address of the property (at
  2008. least, not always).
  2009. If the property definition contains an index, then the read and write
  2010. specifiers must be a function and a procedure. Moreover, these functions
  2011. require an additional parameter : An integer parameter. This allows to read
  2012. or write several properties with the same function. For this, the properties
  2013. must have the same type.
  2014. The following is an example of a property with an index:
  2015. \begin{verbatim}
  2016. {$mode objfpc}
  2017. Type TPoint = Class(TObject)
  2018. Private
  2019. FX,FY : Longint;
  2020. Function GetCoord (Index : Integer): Longint;
  2021. Procedure SetCoord (Index : Integer; Value : longint);
  2022. Public
  2023. Property X : Longint index 1 read GetCoord Write SetCoord;
  2024. Property Y : Longint index 2 read GetCoord Write SetCoord;
  2025. Property Coords[Index : Integer]:Longint Read GetCoord;
  2026. end;
  2027. Procedure TPoint.SetCoord (Index : Integer; Value : Longint);
  2028. begin
  2029. Case Index of
  2030. 1 : FX := Value;
  2031. 2 : FY := Value;
  2032. end;
  2033. end;
  2034. Function TPoint.GetCoord (INdex : Integer) : Longint;
  2035. begin
  2036. Case Index of
  2037. 1 : Result := FX;
  2038. 2 : Result := FY;
  2039. end;
  2040. end;
  2041. Var P : TPoint;
  2042. begin
  2043. P := TPoint.create;
  2044. P.X := 2;
  2045. P.Y := 3;
  2046. With P do
  2047. WriteLn ('X=',X,' Y=',Y);
  2048. end.
  2049. \end{verbatim}
  2050. When the compiler encounters an assignment to \var{X}, then \var{SetCoord}
  2051. is called with as first parameter the index (1 in the above case) and with
  2052. as a second parameter the value to be set.
  2053. Conversely, when reading the value of \var{X}, the compiler calls
  2054. \var{GetCoord} and passes it index 1.
  2055. Indexes can only be integer values.
  2056. Array propertie also exist. These are properties that accept an
  2057. index, just as an array does. Only now the index doesn't have to be an
  2058. ordinal type, but can be any type.
  2059. A \var{read specifier} for an array property is the name method function
  2060. that has the same return type as the property type.
  2061. The function must accept as a sole arguent a variable of the same type as
  2062. the index type. For an array property, one cannot specify fields as \var{read
  2063. specifiers}.
  2064. A \var{write specifier} for an array property is the name of a method
  2065. procedure that accepts two arguments: The first argument has the same
  2066. type as the index, and the second argument is a parameter of the same
  2067. type as the property type.
  2068. As an example, see the following declaration:
  2069. \begin{verbatim}
  2070. Type TIntList = Class
  2071. Private
  2072. Function GetInt (I : Longint) : longint;
  2073. Function GetAsString (A : String) : String;
  2074. Procedure SetInt (I : Longint; Value : Longint;);
  2075. Procedure SetAsString (A : String; Value : String);
  2076. Public
  2077. Property Items [i : Longint] : Longint Read GetInt
  2078. Write SetInt;
  2079. Property StrItems [S : String] : String Read GetAsString
  2080. Write SetAsstring;
  2081. end;
  2082. Var AIntList : TIntList;
  2083. \end{verbatim}
  2084. Then the following statements would be valid:
  2085. \begin{verbatim}
  2086. AIntList.Items[26] := 1;
  2087. AIntList.StrItems['twenty-five'] := 'zero';
  2088. WriteLn ('Item 26 : ',AIntList.Items[26]);
  2089. WriteLn ('Item 25 : ',AIntList.StrItems['twenty-five']);
  2090. \end{verbatim}
  2091. While the following statements would generate errors:
  2092. \begin{verbatim}
  2093. AIntList.Items['twenty-five'] := 1;
  2094. AIntList.StrItems[26] := 'zero';
  2095. \end{verbatim}
  2096. Because the index types are wrong.
  2097. Array properties can be declared as \var{default} properties. This means that
  2098. it is not necessary to specify the property name when assigning or reading
  2099. it. If, in the previous example, the definition of the items property would
  2100. have been
  2101. \begin{verbatim}
  2102. Property Items[i : Longint]: Longint Read GetInt
  2103. Write SetInt; Default;
  2104. \end{verbatim}
  2105. Then the assignment
  2106. \begin{verbatim}
  2107. AIntList.Items[26] := 1;
  2108. \end{verbatim}
  2109. Would be equivalent to the following abbreviation.
  2110. \begin{verbatim}
  2111. AIntList[26] := 1;
  2112. \end{verbatim}
  2113. Only one default property per class is allowed, and descendent classes
  2114. cannot redeclare the default property.
  2115. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2116. % Expressions
  2117. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2118. \chapter{Expressions}
  2119. \label{ch:Expressions}
  2120. Expressions occur in assignments or in tests. Expressions produce a value,
  2121. of a certain type.
  2122. Expressions are built with two components: Operators and their operands.
  2123. Usually an operator is binary, i.e. it requires 2 operands. Binary operators
  2124. occur always between the operands (as in \var{X/Y}). Sometimes an
  2125. operator is unary, i.e. it requires only one argument. A unary operator
  2126. occurs always before the operand, as in \var{-X}.
  2127. When using multiple operands in an expression, the precedence rules of
  2128. \seet{OperatorPrecedence} are used.
  2129. \begin{FPCltable}{lll}{Precedence of operators}{OperatorPrecedence}
  2130. Operator & Precedence & Category \\ \hline
  2131. \var{Not, @} & Highest (first) & Unary operators\\
  2132. \var{* / div mod and shl shr as} & Second & Multiplying operators\\
  2133. \var{+ - or xor} & Third & Adding operators \\
  2134. \var{< <> < > <= >= in is} & Lowest (Last) & relational operators \\
  2135. \hline
  2136. \end{FPCltable}
  2137. When determining the precedence, the compiler uses the following rules:
  2138. \begin{enumerate}
  2139. \item In operations with unequal precedences the operands belong to the
  2140. operater with the highest precedence. For example, in \var{5*3+7}, the
  2141. multiplication is higher in precedence than the addition, so it is
  2142. executed first. The result would be 22.
  2143. \item If parentheses are used in an expression, their contents is evaluated
  2144. first. Thus, \var {5*(3+7)} would result in 50.
  2145. \end{enumerate}
  2146. \begin{remark}
  2147. The order in which expressions of the same precedence are evaluated is not
  2148. guaranteed to be left-to-right. In general, no assumptions on which expression
  2149. is evaluated first should be made in such a case.
  2150. The compiler will decide which expression to evaluate first based on
  2151. optimization rules. Thus, in the following expression:
  2152. \begin{verbatim}
  2153. a := g(3) + f(2);
  2154. \end{verbatim}
  2155. \var{f(2)} may be executed before \var{g(3)}. This behaviour is distinctly
  2156. different from \delphi or \tp.
  2157. If one expression {\em must} be executed before the other, it is necessary
  2158. to split up the statement using temporary results:
  2159. \begin{verbatim}
  2160. e1 := g(3);
  2161. a := e1 + f(2);
  2162. \end{verbatim}
  2163. \end{remark}
  2164. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2165. % Expression syntax
  2166. \section{Expression syntax}
  2167. An expression applies relational operators to simple expressions. Simple
  2168. expressions are a series of terms (what a term is, is explained below), joined by
  2169. adding operators.
  2170. \input{syntax/expsimpl.syn}
  2171. The following are valid expressions:
  2172. \begin{verbatim}
  2173. GraphResult<>grError
  2174. (DoItToday=Yes) and (DoItTomorrow=No);
  2175. Day in Weekend
  2176. \end{verbatim}
  2177. And here are some simple expressions:
  2178. \begin{verbatim}
  2179. A + B
  2180. -Pi
  2181. ToBe or NotToBe
  2182. \end{verbatim}
  2183. Terms consist of factors, connected by multiplication operators.
  2184. \input{syntax/expterm.syn}
  2185. Here are some valid terms:
  2186. \begin{verbatim}
  2187. 2 * Pi
  2188. A Div B
  2189. (DoItToday=Yes) and (DoItTomorrow=No);
  2190. \end{verbatim}
  2191. Factors are all other constructions:
  2192. \input{syntax/expfact.syn}
  2193. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2194. % Function calls
  2195. \section{Function calls}
  2196. Function calls are part of expressions (although, using extended syntax,
  2197. they can be statements too). They are constructed as follows:
  2198. \input{syntax/fcall.syn}
  2199. The \synt{variable reference} must be a procedural type variable reference.
  2200. A method designator can only be used inside the method of an object. A
  2201. qualified method designator can be used outside object methods too.
  2202. The function that will get called is the function with a declared parameter
  2203. list that matches the actual parameter list. This means that
  2204. \begin{enumerate}
  2205. \item The number of actual parameters must equal the number of declared
  2206. parameters.
  2207. \item The types of the parameters must be compatible. For variable
  2208. reference parameters, the parameter types must be exactly the same.
  2209. \end{enumerate}
  2210. If no matching function is found, then the compiler will generate an error.
  2211. Depending on the fact of the function is overloaded (i.e. multiple functions
  2212. with the same name, but different parameter lists) the error will be
  2213. different.
  2214. There are cases when the compiler will not execute the function call in an
  2215. expression. This is the case when assigning a value to a procedural
  2216. type variable, as in the following example:
  2217. \begin{verbatim}
  2218. Type
  2219. FuncType = Function: Integer;
  2220. Var A : Integer;
  2221. Function AddOne : Integer;
  2222. begin
  2223. A := A+1;
  2224. AddOne := A;
  2225. end;
  2226. Var F : FuncType;
  2227. N : Integer;
  2228. begin
  2229. A := 0;
  2230. F := AddOne; { Assign AddOne to F, Don't call AddOne}
  2231. N := AddOne; { N := 1 !!}
  2232. end.
  2233. \end{verbatim}
  2234. In the above listing, the assigment to F will not cause the function AddOne
  2235. to be called. The assignment to N, however, will call AddOne.
  2236. A problem with this syntax is the following construction:
  2237. \begin{verbatim}
  2238. If F = AddOne Then
  2239. DoSomethingHorrible;
  2240. \end{verbatim}
  2241. Should the compiler compare the addresses of \var{F} and \var{AddOne},
  2242. or should it call both functions, and compare the result ? \fpc solves this
  2243. by deciding that a procedural variable is equivalent to a pointer. Thus the
  2244. compiler will give a type mismatch error, since AddOne is considered a
  2245. call to a function with integer result, and F is a pointer, Hence a type
  2246. mismatch occurs.
  2247. How then, should one compare whether \var{F} points to the function
  2248. \var{AddOne} ? To do this, one should use the address operator \var{@}:
  2249. \begin{verbatim}
  2250. If F = @AddOne Then
  2251. WriteLn ('Functions are equal');
  2252. \end{verbatim}
  2253. The left hand side of the boolean expression is an address. The right hand
  2254. side also, and so the compiler compares 2 addresses.
  2255. How to compare the values that both functions return ? By adding an empty
  2256. parameter list:
  2257. \begin{verbatim}
  2258. If F()=Addone then
  2259. WriteLn ('Functions return same values ');
  2260. \end{verbatim}
  2261. Remark that this behaviour is not compatible with Delphi syntax.
  2262. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2263. % Set constructors
  2264. \section{Set constructors}
  2265. When a set-type constant must be entered in an expression, a
  2266. set constructor must be given. In essence this is the same thing as when a
  2267. type is defined, only there is no identifier to identify the set with.
  2268. A set constructor is a comma separated list of expressions, enclosed in
  2269. square brackets.
  2270. \input{syntax/setconst.syn}
  2271. All set groups and set elements must be of the same ordinal type.
  2272. The empty set is denoted by \var{[]}, and it can be assigned to any type of
  2273. set. A set group with a range \var{[A..Z]} makes all values in the range a
  2274. set element. If the first range specifier has a bigger ordinal value than
  2275. the second the set is empty, e.g., \var{[Z..A]} denotes an empty set.
  2276. The following are valid set constructors:
  2277. \begin{verbatim}
  2278. [today,tomorrow]
  2279. [Monday..Friday,Sunday]
  2280. [ 2, 3*2, 6*2, 9*2 ]
  2281. ['A'..'Z','a'..'z','0'..'9']
  2282. \end{verbatim}
  2283. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2284. % Value typecasts
  2285. \section{Value typecasts}
  2286. Sometimes it is necessary to change the type of an expression, or a part of
  2287. the expression, to be able to be assignment compatible. This is done through
  2288. a value typecast. The syntax diagram for a value typecast is as follows:
  2289. \input{syntax/tcast.syn}
  2290. Value typecasts cannot be used on the left side of assignments, as variable
  2291. typecasts.
  2292. Here are some valid typecasts:
  2293. \begin{verbatim}
  2294. Byte('A')
  2295. Char(48)
  2296. boolean(1)
  2297. longint(@Buffer)
  2298. \end{verbatim}
  2299. The type size of the expression and the size of the type cast must be the
  2300. same. That is, the following doesn't work:
  2301. \begin{verbatim}
  2302. Integer('A')
  2303. Char(4875)
  2304. boolean(100)
  2305. Word(@Buffer)
  2306. \end{verbatim}
  2307. This is different from Delphi or Turbo Pascal behaviour.
  2308. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2309. % The @ operator
  2310. \section{The @ operator}
  2311. The address operator \var{@} returns the address of a variable, procedure
  2312. or function. It is used as follows:
  2313. \input{syntax/address.syn}
  2314. The \var{@} operator returns a typed pointer if the \var{\$T} switch is on.
  2315. If the \var{\$T} switch is off then the address operator returns an untyped
  2316. pointer, which is assigment compatible with all pointer types. The type of
  2317. the pointer is \var{\^{}T}, where \var{T} is the type of the variable
  2318. reference.
  2319. For example, the following will compile
  2320. \begin{verbatim}
  2321. Program tcast;
  2322. {$T-} { @ returns untyped pointer }
  2323. Type art = Array[1..100] of byte;
  2324. Var Buffer : longint;
  2325. PLargeBuffer : ^art;
  2326. begin
  2327. PLargeBuffer := @Buffer;
  2328. end.
  2329. \end{verbatim}
  2330. Changing the \var{\{\$T-\}} to \var{\{\$T+\}} will prevent the compiler from
  2331. compiling this. It will give a type mismatch error.
  2332. By default, the address operator returns an untyped pointer.
  2333. Applying the address operator to a function, method, or procedure identifier
  2334. will give a pointer to the entry point of that function. The result is an
  2335. untyped pointer.
  2336. By default, the address operator must be used if a value must be assigned
  2337. to a procedural type variable. This behaviour can be avoided by using the
  2338. \var{-So} or \var{-S2} switches, which result in a more compatible Delphi or
  2339. Turbo Pascal syntax.
  2340. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2341. % Operators
  2342. \section{Operators}
  2343. Operators can be classified according to the type of expression they
  2344. operate on. We will discuss them type by type.
  2345. \subsection{Arithmetic operators}
  2346. Arithmetic operators occur in arithmetic operations, i.e. in expressions
  2347. that contain integers or reals. There are 2 kinds of operators : Binary and
  2348. unary arithmetic operators.
  2349. Binary operators are listed in \seet{binaroperators}, unary operators are
  2350. listed in \seet{unaroperators}.
  2351. \begin{FPCltable}{ll}{Binary arithmetic operators}{binaroperators}
  2352. Operator & Operation \\ \hline
  2353. \var{+} & Addition\\
  2354. \var{-} & Subtraction\\
  2355. \var{*} & Multiplication \\
  2356. \var{/} & Division \\
  2357. \var{Div} & Integer division \\
  2358. \var{Mod} & Remainder \\ \hline
  2359. \end{FPCltable}
  2360. With the exception of \var{Div} and \var{Mod}, which accept only integer
  2361. expressions as operands, all operators accept real and integer expressions as
  2362. operands.
  2363. For binary operators, the result type will be integer if both operands are
  2364. integer type expressions. If one of the operands is a real type expression,
  2365. then the result is real.
  2366. As an exception : division (\var{/}) results always in real values.
  2367. \begin{FPCltable}{ll}{Unary arithmetic operators}{unaroperators}
  2368. Operator & Operation \\ \hline
  2369. \var{+} & Sign identity\\
  2370. \var{-} & Sign inversion \\ \hline
  2371. \end{FPCltable}
  2372. For unary operators, the result type is always equal to the expression type.
  2373. The division (\var{/}) and \var{Mod} operator will cause run-time errors if
  2374. the second argument is zero.
  2375. The sign of the result of a \var{Mod} operator is the same as the sign of
  2376. the left side operand of the \var{Mod} operator. In fact, the \var{Mod}
  2377. operator is equivalent to the following operation :
  2378. \begin{verbatim}
  2379. I mod J = I - (I div J) * J
  2380. \end{verbatim}
  2381. but it executes faster than the right hand side expression.
  2382. \subsection{Logical operators}
  2383. Logical operators act on the individual bits of ordinal expressions.
  2384. Logical operators require operands that are of an integer type, and produce
  2385. an integer type result. The possible logical operators are listed in
  2386. \seet{logicoperations}.
  2387. \begin{FPCltable}{ll}{Logical operators}{logicoperations}
  2388. Operator & Operation \\ \hline
  2389. \var{not} & Bitwise negation (unary) \\
  2390. \var{and} & Bitwise and \\
  2391. \var{or} & Bitwise or \\
  2392. \var{xor} & Bitwise xor \\
  2393. \var{shl} & Bitwise shift to the left \\
  2394. \var{shr} & Bitwise shift to the right \\ \hline
  2395. \end{FPCltable}
  2396. The following are valid logical expressions:
  2397. \begin{verbatim}
  2398. A shr 1 { same as A div 2, but faster}
  2399. Not 1 { equals -2 }
  2400. Not 0 { equals -1 }
  2401. Not -1 { equals 0 }
  2402. B shl 2 { same as B * 2 for integers }
  2403. 1 or 2 { equals 3 }
  2404. 3 xor 1 { equals 2 }
  2405. \end{verbatim}
  2406. \subsection{Boolean operators}
  2407. Boolean operators can be considered logical operations on a type with 1 bit
  2408. size. Therefore the \var{shl} and \var{shr} operations have little sense.
  2409. Boolean operators can only have boolean type operands, and the resulting
  2410. type is always boolean. The possible operators are listed in
  2411. \seet{booleanoperators}
  2412. \begin{FPCltable}{ll}{Boolean operators}{booleanoperators}
  2413. Operator & Operation \\ \hline
  2414. \var{not} & logical negation (unary) \\
  2415. \var{and} & logical and \\
  2416. \var{or} & logical or \\
  2417. \var{xor} & logical xor \\ \hline
  2418. \end{FPCltable}
  2419. \begin{remark} Boolean expressions are always evaluated with short-circuit
  2420. evaluation. This means that from the moment the result of the complete
  2421. expression is known, evaluation is stopped and the result is returned.
  2422. For instance, in the following expression:
  2423. \begin{verbatim}
  2424. B := True or MaybeTrue;
  2425. \end{verbatim}
  2426. The compiler will never look at the value of \var{MaybeTrue}, since it is
  2427. obvious that the expression will always be true. As a result of this
  2428. strategy, if \var{MaybeTrue} is a function, it will not get called !
  2429. (This can have surprising effects when used in conjunction with properties)
  2430. \end{remark}
  2431. \subsection{String operators}
  2432. There is only one string operator : \var{+}. It's action is to concatenate
  2433. the contents of the two strings (or characters) it stands between.
  2434. One cannot use \var{+} to concatenate null-terminated (\var{PChar}) strings.
  2435. The following are valid string operations:
  2436. \begin{verbatim}
  2437. 'This is ' + 'VERY ' + 'easy !'
  2438. Dirname+'\'
  2439. \end{verbatim}
  2440. The following is not:
  2441. \begin{verbatim}
  2442. Var Dirname = Pchar;
  2443. ...
  2444. Dirname := Dirname+'\';
  2445. \end{verbatim}
  2446. Because \var{Dirname} is a null-terminated string.
  2447. \subsection{Set operators}
  2448. The following operations on sets can be performed with operators:
  2449. Union, difference and intersection. The operators needed for this are listed
  2450. in \seet{setoperators}.
  2451. \begin{FPCltable}{ll}{Set operators}{setoperators}
  2452. Operator & Action \\ \hline
  2453. \var{+} & Union \\
  2454. \var{-} & Difference \\
  2455. \var{*} & Intersection \\ \hline
  2456. \end{FPCltable}
  2457. The set type of the operands must be the same, or an error will be
  2458. generated by the compiler.
  2459. \subsection{Relational operators}
  2460. The relational operators are listed in \seet{relationoperators}
  2461. \begin{FPCltable}{ll}{Relational operators}{relationoperators}
  2462. Operator & Action \\ \hline
  2463. \var{=} & Equal \\
  2464. \var{<>} & Not equal \\
  2465. \var{<} & Stricty less than\\
  2466. \var{>} & Strictly greater than\\
  2467. \var{<=} & Less than or equal \\
  2468. \var{>=} & Greater than or equal \\
  2469. \var{in} & Element of \\ \hline
  2470. \end{FPCltable}
  2471. Left and right operands must be of the same type. Only integer
  2472. and real types can be mixed in relational expressions.
  2473. Comparing strings is done on the basis of their ASCII code representation.
  2474. When comparing pointers, the addresses to which they point are compared.
  2475. This also is true for \var{PChar} type pointers. To compare the strings
  2476. the \var{Pchar} point to, the \var{StrComp} function
  2477. from the \file{strings} unit must be used.
  2478. The \var{in} returns \var{True} if the left operand (which must have the same
  2479. ordinal type as the set type, and which must be in the range 0..255) is an
  2480. element of the set which is the right operand, otherwise it returns \var{False}
  2481. \chapter{Statements}
  2482. \label{ch:Statements}
  2483. The heart of each algorithm are the actions it takes. These actions are
  2484. contained in the statements of a program or unit. Each statement can be
  2485. labeled and jumped to (within certain limits) with \var{Goto} statements.
  2486. This can be seen in the following syntax diagram:
  2487. \input{syntax/statement.syn}
  2488. A label can be an identifier or an integer digit.
  2489. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2490. % Simple statements
  2491. \section{Simple statements}
  2492. A simple statement cannot be decomposed in separate statements. There are
  2493. basically 4 kinds of simple statements:
  2494. \input{syntax/simstate.syn}
  2495. Of these statements, the {\em raise statement} will be explained in the
  2496. chapter on Exceptions (\seec{Exceptions})
  2497. \subsection{Assignments}
  2498. Assignments give a value to a variable, replacing any previous value the
  2499. variable might have had:
  2500. \input{syntax/assign.syn}
  2501. In addition to the standard Pascal assignment operator (\var{ := }), which
  2502. simply replaces the value of the varable with the value resulting from the
  2503. expression on the right of the { := } operator, \fpc
  2504. supports some c-style constructions. All available constructs are listed in
  2505. \seet{assignments}.
  2506. \begin{FPCltable}{lr}{Allowed C constructs in \fpc}{assignments}
  2507. Assignment & Result \\ \hline
  2508. a += b & Adds \var{b} to \var{a}, and stores the result in \var{a}.\\
  2509. a -= b & Substracts \var{b} from \var{a}, and stores the result in
  2510. \var{a}. \\
  2511. a *= b & Multiplies \var{a} with \var{b}, and stores the result in
  2512. \var{a}. \\
  2513. a /= b & Divides \var{a} through \var{b}, and stores the result in
  2514. \var{a}. \\ \hline
  2515. \end{FPCltable}
  2516. For these constructs to work, the \var{-Sc} command-line switch must
  2517. be specified.
  2518. \begin{remark}
  2519. These constructions are just for typing convenience, they
  2520. don't generate different code.
  2521. Here are some examples of valid assignment statements:
  2522. \begin{verbatim}
  2523. X := X+Y;
  2524. X+=Y; { Same as X := X+Y, needs -Sc command line switch}
  2525. X/=2; { Same as X := X/2, needs -Sc command line switch}
  2526. Done := False;
  2527. Weather := Good;
  2528. MyPi := 4* Tan(1);
  2529. \end{verbatim}
  2530. \end{remark}
  2531. \subsection{Procedure statements}
  2532. Procedure statements are calls to subroutines. There are
  2533. different possibilities for procedure calls: A normal procedure call, an
  2534. object method call (fully qualified or not), or even a call to a procedural
  2535. type variable. All types are present in the following diagram.
  2536. \input{syntax/procedure.syn}
  2537. The \fpc compiler will look for a procedure with the same name as given in
  2538. the procedure statement, and with a declared parameter list that matches the
  2539. actual parameter list.
  2540. The following are valid procedure statements:
  2541. \begin{verbatim}
  2542. Usage;
  2543. WriteLn('Pascal is an easy language !');
  2544. Doit();
  2545. \end{verbatim}
  2546. \subsection{Goto statements}
  2547. \fpc supports the \var{goto} jump statement. Its prototype syntax is
  2548. \input{syntax/goto.syn}
  2549. When using \var{goto} statements, the following must be kept in mind:
  2550. \begin{enumerate}
  2551. \item The jump label must be defined in the same block as the \var{Goto}
  2552. statement.
  2553. \item Jumping from outside a loop to the inside of a loop or vice versa can
  2554. have strange effects.
  2555. \item To be able to use the \var{Goto} statement, the \var{-Sg} compiler
  2556. switch must be used.
  2557. \end{enumerate}
  2558. \var{Goto} statements are considered bad practice and should be avoided as
  2559. much as possible. It is always possible to replace a \var{goto} statement by a
  2560. construction that doesn't need a \var{goto}, although this construction may
  2561. not be as clear as a goto statement.
  2562. For instance, the following is an allowed goto statement:
  2563. \begin{verbatim}
  2564. label
  2565. jumpto;
  2566. ...
  2567. Jumpto :
  2568. Statement;
  2569. ...
  2570. Goto jumpto;
  2571. ...
  2572. \end{verbatim}
  2573. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2574. % Structured statements
  2575. \section{Structured statements}
  2576. Structured statements can be broken into smaller simple statements, which
  2577. should be executed repeatedly, conditionally or sequentially:
  2578. \input{syntax/struct.syn}
  2579. Conditional statements come in 2 flavours :
  2580. \input{syntax/conditio.syn}
  2581. Repetitive statements come in 3 flavours:
  2582. \input{syntax/repetiti.syn}
  2583. The following sections deal with each of these statements.
  2584. \subsection{Compound statements}
  2585. Compound statements are a group of statements, separated by semicolons,
  2586. that are surrounded by the keywords \var{Begin} and \var{End}. The
  2587. Last statement doesn't need to be followed by a semicolon, although it is
  2588. allowed. A compound statement is a way of grouping statements together,
  2589. executing the statements sequentially. They are treated as one statement
  2590. in cases where Pascal syntax expects 1 statement, such as in
  2591. \var{if ... then} statements.
  2592. \input{syntax/compound.syn}
  2593. \subsection{The \var{Case} statement}
  2594. \fpc supports the \var{case} statement. Its syntax diagram is
  2595. \input{syntax/case.syn}
  2596. The constants appearing in the various case parts must be known at
  2597. compile-time, and can be of the following types : enumeration types,
  2598. Ordinal types (except boolean), and chars. The expression must be also of
  2599. this type, or a compiler error will occur. All case constants must
  2600. have the same type.
  2601. The compiler will evaluate the expression. If one of the case constants
  2602. values matches the value of the expression, the statement that follows
  2603. this constant is executed. After that, the program continues after the final
  2604. \var{end}.
  2605. If none of the case constants match the expression value, the statement
  2606. after the \var{else} keyword is executed. This can be an empty statement.
  2607. If no else part is present, and no case constant matches the expression
  2608. value, program flow continues after the final \var{end}.
  2609. The case statements can be compound statements
  2610. (i.e. a \var{begin..End} block).
  2611. \begin{remark}
  2612. Contrary to Turbo Pascal, duplicate case labels are not
  2613. allowed in \fpc, so the following code will generate an error when
  2614. compiling:
  2615. \begin{verbatim}
  2616. Var i : integer;
  2617. ...
  2618. Case i of
  2619. 3 : DoSomething;
  2620. 1..5 : DoSomethingElse;
  2621. end;
  2622. \end{verbatim}
  2623. The compiler will generate a \var{Duplicate case label} error when compiling
  2624. this, because the 3 also appears (implicitly) in the range \var{1..5}. This
  2625. is similar to Delphi syntax.
  2626. \end{remark}
  2627. The following are valid case statements:
  2628. \begin{verbatim}
  2629. Case C of
  2630. 'a' : WriteLn ('A pressed');
  2631. 'b' : WriteLn ('B pressed');
  2632. 'c' : WriteLn ('C pressed');
  2633. else
  2634. WriteLn ('unknown letter pressed : ',C);
  2635. end;
  2636. \end{verbatim}
  2637. Or
  2638. \begin{verbatim}
  2639. Case C of
  2640. 'a','e','i','o','u' : WriteLn ('vowel pressed');
  2641. 'y' : WriteLn ('This one depends on the language');
  2642. else
  2643. WriteLn ('Consonant pressed');
  2644. end;
  2645. \end{verbatim}
  2646. \begin{verbatim}
  2647. Case Number of
  2648. 1..10 : WriteLn ('Small number');
  2649. 11..100 : WriteLn ('Normal, medium number');
  2650. else
  2651. WriteLn ('HUGE number');
  2652. end;
  2653. \end{verbatim}
  2654. \subsection{The \var{If..then..else} statement}
  2655. The \var{If .. then .. else..} prototype syntax is
  2656. \input{syntax/ifthen.syn}
  2657. The expression between the \var{if} and \var{then} keywords must have a
  2658. boolean return type. If the expression evaluates to \var{True} then the
  2659. statement following \var{then} is executed.
  2660. If the expression evaluates to \var{False}, then the statement following
  2661. \var{else} is executed, if it is present.
  2662. Be aware of the fact that the boolean expression will be short-cut evaluated.
  2663. (Meaning that the evaluation will be stopped at the point where the
  2664. outcome is known with certainty)
  2665. Also, before the \var {else} keyword, no semicolon (\var{;}) is allowed,
  2666. but all statements can be compound statements.
  2667. In nested \var{If.. then .. else} constructs, some ambiguity may araise as
  2668. to which \var{else} statement pairs with which \var{if} statement. The rule
  2669. is that the \var{else } keyword matches the first \var{if} keyword not
  2670. already matched by an \var{else} keyword.
  2671. For example:
  2672. \begin{verbatim}
  2673. If exp1 Then
  2674. If exp2 then
  2675. Stat1
  2676. else
  2677. stat2;
  2678. \end{verbatim}
  2679. Despite it's appearance, the statement is syntactically equivalent to
  2680. \begin{verbatim}
  2681. If exp1 Then
  2682. begin
  2683. If exp2 then
  2684. Stat1
  2685. else
  2686. stat2
  2687. end;
  2688. \end{verbatim}
  2689. and not to
  2690. \begin{verbatim}
  2691. { NOT EQUIVALENT }
  2692. If exp1 Then
  2693. begin
  2694. If exp2 then
  2695. Stat1
  2696. end
  2697. else
  2698. stat2
  2699. \end{verbatim}
  2700. If it is this latter construct is needed, the \var{begin} and \var{end}
  2701. keywords must be present. When in doubt, it is better to add them.
  2702. The following is a valid statement:
  2703. \begin{verbatim}
  2704. If Today in [Monday..Friday] then
  2705. WriteLn ('Must work harder')
  2706. else
  2707. WriteLn ('Take a day off.');
  2708. \end{verbatim}
  2709. \subsection{The \var{For..to/downto..do} statement}
  2710. \fpc supports the \var{For} loop construction. A for loop is used in case
  2711. one wants to calculated something a fixed number of times.
  2712. The prototype syntax is as follows:
  2713. \input{syntax/for.syn}
  2714. \var{Statement} can be a compound statement.
  2715. When this statement is encountered, the control variable is initialized with
  2716. the initial value, and is compared with the final value.
  2717. What happens next depends on whether \var{to} or \var{downto} is used:
  2718. \begin{enumerate}
  2719. \item In the case \var{To} is used, if the initial value larger than the final
  2720. value then \var{Statement} will never be executed.
  2721. \item In the case \var{DownTo} is used, if the initial value larger than the final
  2722. value then \var{Statement} will never be executed.
  2723. \end{enumerate}
  2724. After this check, the statement after \var{Do} is executed. After the
  2725. execution of the statement, the control variable is increased or decreased
  2726. with 1, depending on whether \var{To} or \var{Downto} is used.
  2727. The control variable must be an ordinal type, no other
  2728. types can be used as counters in a loop.
  2729. \begin{remark}
  2730. Contrary to ANSI pascal specifications, \fpc first initializes
  2731. the counter variable, and only then calculates the upper bound.
  2732. \end{remark}
  2733. The following are valid loops:
  2734. \begin{verbatim}
  2735. For Day := Monday to Friday do Work;
  2736. For I := 100 downto 1 do
  2737. WriteLn ('Counting down : ',i);
  2738. For I := 1 to 7*dwarfs do KissDwarf(i);
  2739. \end{verbatim}
  2740. If the statement is a compound statement, then the \seep{Break} and
  2741. \seep{Continue} reserved words can be used to jump to the end or just
  2742. after the end of the \var{For} statement.
  2743. \subsection{The \var{Repeat..until} statement}
  2744. The \var{repeat} statement is used to execute a statement until a certain
  2745. condition is reached. The statement will be executed at least once.
  2746. The prototype syntax of the \var{Repeat..until} statement is
  2747. \input{syntax/repeat.syn}
  2748. This will execute the statements between \var{repeat} and \var{until} up to
  2749. the moment when \var{Expression} evaluates to \var{True}.
  2750. Since the \var{expression} is evaluated {\em after} the execution of the
  2751. statements, they are executed at least once.
  2752. Be aware of the fact that the boolean expression \var{Expression} will be
  2753. short-cut evaluated. (Meaning that the evaluation will be stopped at the
  2754. point where the outcome is known with certainty)
  2755. The following are valid \var{repeat} statements
  2756. \begin{verbatim}
  2757. repeat
  2758. WriteLn ('I =',i);
  2759. I := I+2;
  2760. until I>100;
  2761. repeat
  2762. X := X/2
  2763. until x<10e-3
  2764. \end{verbatim}
  2765. The \seep{Break} and \seep{Continue} reserved words can be used to jump to
  2766. the end or just after the end of the \var{repeat .. until } statement.
  2767. \subsection{The \var{While..do} statement}
  2768. A \var{while} statement is used to execute a statement as long as a certain
  2769. condition holds. This may imply that the statement is never executed.
  2770. The prototype syntax of the \var{While..do} statement is
  2771. \input{syntax/while.syn}
  2772. This will execute \var{Statement} as long as \var{Expression} evaluates to
  2773. \var{True}. Since \var{Expression} is evaluated {\em before} the execution
  2774. of \var{Statement}, it is possible that \var{Statement} isn't executed at
  2775. all. \var{Statement} can be a compound statement.
  2776. Be aware of the fact that the boolean expression \var{Expression} will be
  2777. short-cut evaluated. (Meaning that the evaluation will be stopped at the
  2778. point where the outcome is known with certainty)
  2779. The following are valid \var{while} statements:
  2780. \begin{verbatim}
  2781. I := I+2;
  2782. while i<=100 do
  2783. begin
  2784. WriteLn ('I =',i);
  2785. I := I+2;
  2786. end;
  2787. X := X/2;
  2788. while x>=10e-3 do
  2789. X := X/2;
  2790. \end{verbatim}
  2791. They correspond to the example loops for the \var{repeat} statements.
  2792. If the statement is a compound statement, then the \seep{Break} and
  2793. \seep{Continue} reserved words can be used to jump to the end or just
  2794. after the end of the \var{While} statement.
  2795. \subsection{The \var{With} statement}
  2796. \label{se:With}
  2797. The \var{with} statement serves to access the elements of a record
  2798. or object or class, without having to specify the name of the each time.
  2799. The syntax for a \var{with} statement is
  2800. \input{syntax/with.syn}
  2801. The variable reference must be a variable of a record, object or class type.
  2802. In the \var{with} statement, any variable reference, or method reference is
  2803. checked to see if it is a field or method of the record or object or class.
  2804. If so, then that field is accessed, or that method is called.
  2805. Given the declaration:
  2806. \begin{verbatim}
  2807. Type Passenger = Record
  2808. Name : String[30];
  2809. Flight : String[10];
  2810. end;
  2811. Var TheCustomer : Passenger;
  2812. \end{verbatim}
  2813. The following statements are completely equivalent:
  2814. \begin{verbatim}
  2815. TheCustomer.Name := 'Michael';
  2816. TheCustomer.Flight := 'PS901';
  2817. \end{verbatim}
  2818. and
  2819. \begin{verbatim}
  2820. With TheCustomer do
  2821. begin
  2822. Name := 'Michael';
  2823. Flight := 'PS901';
  2824. end;
  2825. \end{verbatim}
  2826. The statement
  2827. \begin{verbatim}
  2828. With A,B,C,D do Statement;
  2829. \end{verbatim}
  2830. is equivalent to
  2831. \begin{verbatim}
  2832. With A do
  2833. With B do
  2834. With C do
  2835. With D do Statement;
  2836. \end{verbatim}
  2837. This also is a clear example of the fact that the variables are tried {\em last
  2838. to first}, i.e., when the compiler encounters a variable reference, it will
  2839. first check if it is a field or method of the last variable. If not, then it
  2840. will check the last-but-one, and so on.
  2841. The following example shows this;
  2842. \begin{verbatim}
  2843. Program testw;
  2844. Type AR = record
  2845. X,Y : Longint;
  2846. end;
  2847. PAR = Record;
  2848. Var S,T : Ar;
  2849. begin
  2850. S.X := 1;S.Y := 1;
  2851. T.X := 2;T.Y := 2;
  2852. With S,T do
  2853. WriteLn (X,' ',Y);
  2854. end.
  2855. \end{verbatim}
  2856. The output of this program is
  2857. \begin{verbatim}
  2858. 2 2
  2859. \end{verbatim}
  2860. Showing thus that the \var{X,Y} in the \var{WriteLn} statement match the
  2861. \var{T} record variable.
  2862. \begin{remark}
  2863. When using a \var{With} statement with a pointer, or a class, it is not
  2864. permitted to change the pointer or the class in the \var{With} block.
  2865. With the definitions of the previous example, the following illustrates
  2866. what it is about:
  2867. \begin{verbatim}
  2868. Var p : PAR;
  2869. begin
  2870. With P^ do
  2871. begin
  2872. // Do some operations
  2873. P:=OtherP;
  2874. X:=0.0; // Wrong X will be used !!
  2875. end;
  2876. \end{verbatim}
  2877. The reason the pointer cannot be changed is that the address is stored
  2878. by the compiler in a temporary register. Changing the pointer won't change
  2879. the temporary address. The same is true for classes.
  2880. \end{remark}
  2881. \subsection{Exception Statements}
  2882. \fpc supports exceptions. Exceptions provide a convenient way to
  2883. program error and error-recovery mechanisms, and are
  2884. closely related to classes.
  2885. Exception support is explained in \seec{Exceptions}
  2886. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2887. % Assembler statements
  2888. \section{Assembler statements}
  2889. An assembler statement allows to insert assembler code right in the
  2890. pascal code.
  2891. \input{syntax/statasm.syn}
  2892. More information about assembler blocks can be found in the \progref.
  2893. The register list is used to indicate the registers that are modified by an
  2894. assembler statement in the assembler block. The compiler stores certain results in the
  2895. registers. If the registers are modified in an assembler statement, the compiler
  2896. should, sometimes, be told about it. The registers are denoted with their
  2897. Intel names for the I386 processor, i.e., \var{'EAX'}, \var{'ESI'} etc...
  2898. As an example, consider the following assembler code:
  2899. \begin{verbatim}
  2900. asm
  2901. Movl $1,%ebx
  2902. Movl $0,%eax
  2903. addl %eax,%ebx
  2904. end; ['EAX','EBX'];
  2905. \end{verbatim}
  2906. This will tell the compiler that it should save and restore the contents of
  2907. the \var{EAX} and \var{EBX} registers when it encounters this asm statement.
  2908. \fpc supports various styles of assembler syntax. By default, \var{AT\&T}
  2909. syntax is assumed for the 80386 and compatibles platform.
  2910. The default assembler style can be changed with the \var{\{\$asmmode xxx\}}
  2911. switch in the code, or the \var{-R} command-line option. More about this can
  2912. be found in the \progref.
  2913. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2914. % Using functions and procedures.
  2915. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2916. \chapter{Using functions and procedures}
  2917. \label{ch:Procedures}
  2918. \fpc supports the use of functions and procedures, but with some extras:
  2919. Function overloading is supported, as well as \var{Const} parameters and
  2920. open arrays.
  2921. \begin{remark} In many of the subsequent paragraphs the words \var{procedure}
  2922. and \var{function} will be used interchangeably. The statements made are
  2923. valid for both, except when indicated otherwise.
  2924. \end{remark}
  2925. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2926. % Procedure declaration
  2927. \section{Procedure declaration}
  2928. A procedure declaration defines an identifier and associates it with a
  2929. block of code. The procedure can then be called with a procedure statement.
  2930. \input{syntax/procedur.syn}
  2931. See \sees{Parameters} for the list of parameters.
  2932. A procedure declaration that is followed by a block implements the action of
  2933. the procedure in that block.
  2934. The following is a valid procedure :
  2935. \begin{verbatim}
  2936. Procedure DoSomething (Para : String);
  2937. begin
  2938. Writeln ('Got parameter : ',Para);
  2939. Writeln ('Parameter in upper case : ',Upper(Para));
  2940. end;
  2941. \end{verbatim}
  2942. Note that it is possible that a procedure calls itself.
  2943. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2944. % Function declaration
  2945. \section{Function declaration}
  2946. A function declaration defines an identifier and associates it with a
  2947. block of code. The block of code will return a result.
  2948. The function can then be called inside an expression, or with a procedure
  2949. statement, if extended syntax is on.
  2950. \input{syntax/function.syn}
  2951. The result type of a function can be any previously declared type.
  2952. contrary to Turbo pascal, where only simple types could be returned.
  2953. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2954. % Parameter lists
  2955. \section{Parameter lists}
  2956. \label{se:Parameters}
  2957. When arguments must be passed to a function or procedure, these parameters
  2958. must be declared in the formal parameter list of that function or procedure.
  2959. The parameter list is a declaration of identifiers that can be referred to
  2960. only in that procedure or function's block.
  2961. \input{syntax/params.syn}
  2962. Constant parameters and variable parameters can also be \var{untyped}
  2963. parameters if they have no type identifier.
  2964. \subsection{Value parameters}
  2965. Value parameters are declared as follows:
  2966. \input{syntax/paramval.syn}
  2967. When parameters are declared as value parameters, the procedure gets {\em
  2968. a copy} of the parameters that the calling block passes. Any modifications
  2969. to these parameters are purely local to the procedure's block, and do not
  2970. propagate back to the calling block.
  2971. A block that wishes to call a procedure with value parameters must pass
  2972. assignment compatible parameters to the procedure. This means that the types
  2973. should not match exactly, but can be converted (conversion code is inserted
  2974. by the compiler itself)
  2975. Care must be taken when using value parameters: Value parameters makes heavy
  2976. use of the stack, especially when using large parameters. The total size of
  2977. all parameters in the formal parameter list should be below 32K for
  2978. portability's sake (the Intel version limits this to 64K).
  2979. Open arrays can be passed as value parameters. See \sees{openarray} for
  2980. more information on using open arrays.
  2981. \subsection{Variable parameters}
  2982. \label{se:varparams}
  2983. Variable parameters are declared as follows:
  2984. \input{syntax/paramvar.syn}
  2985. When parameters are declared as variable parameters, the procedure or
  2986. function accesses immediatly the variable that the calling block passed in
  2987. its parameter list. The procedure gets a pointer to the variable that was
  2988. passed, and uses this pointer to access the variable's value.
  2989. From this, it follows that any changes made to the parameter, will
  2990. propagate back to the calling block. This mechanism can be used to pass
  2991. values back in procedures.
  2992. Because of this, the calling block must pass a parameter of {\em exactly}
  2993. the same type as the declared parameter's type. If it does not, the compiler
  2994. will generate an error.
  2995. Variable and constant parameters can be untyped. In that case the variable has no type,
  2996. and hence is incompatible with all other types. However, the address operator
  2997. can be used on it, or it can be can passed to a function that has also an
  2998. untyped parameter. If an untyped parameter is used in an assigment,
  2999. or a value must be assigned to it, a typecast must be used.
  3000. File type variables must always be passed as variable parameters.
  3001. Open arrays can be passed as variable parameters. See \sees{openarray} for
  3002. more information on using open arrays.
  3003. \subsection{Constant parameters}
  3004. In addition to variable parameters and value parameters \fpc also supports
  3005. Constant parameters. A constant parameter as can be specified as follows:
  3006. \input{syntax/paramcon.syn}
  3007. A constant argument is passed by reference if it's size is larger than a
  3008. pointer. It is passed by value if the size is equal or is less then the
  3009. size of a native pointer.
  3010. This means that the function or procedure receives a pointer to the passed
  3011. argument, but it cannot be assigned to, this will result in a
  3012. compiler error. Furthermore a const parameter cannot be passed on to another
  3013. function that requires a variable parameter.
  3014. The main use for this is reducing the stack size, hence improving
  3015. performance, and still retaining the semantics of passing by value...
  3016. Constant parameters can also be untyped. See \sees{varparams} for more
  3017. information about untyped parameters.
  3018. Open arrays can be passed as constant parameters. See \sees{openarray} for
  3019. more information on using open arrays.
  3020. \subsection{Open array parameters}
  3021. \label{se:openarray}
  3022. \fpc supports the passing of open arrays, i.e. a procedure can be declared
  3023. with an array of unspecified length as a parameter, as in Delphi.
  3024. Open array parameters can be accessed in the procedure or function as an
  3025. array that is declared with starting index 0, and last element
  3026. index \var{High(paremeter)}.
  3027. For example, the parameter
  3028. \begin{verbatim}
  3029. Row : Array of Integer;
  3030. \end{verbatim}
  3031. would be equivalent to
  3032. \begin{verbatim}
  3033. Row : Array[0..N-1] of Integer;
  3034. \end{verbatim}
  3035. Where \var{N} would be the actual size of the array that is passed to the
  3036. function. \var{N-1} can be calculated as \var{High(Row)}.
  3037. Open parameters can be passed by value, by reference or as a constant
  3038. parameter. In the latter cases the procedure receives a pointer to the
  3039. actual array. In the former case, it receives a copy of the array.
  3040. In a function or procedure, open arrays can only be passed to functions which
  3041. are also declared with open arrays as parameters, {\em not} to functions or
  3042. procedures which accept arrays of fixed length.
  3043. The following is an example of a function using an open array:
  3044. \begin{verbatim}
  3045. Function Average (Row : Array of integer) : Real;
  3046. Var I : longint;
  3047. Temp : Real;
  3048. begin
  3049. Temp := Row[0];
  3050. For I := 1 to High(Row) do
  3051. Temp := Temp + Row[i];
  3052. Average := Temp / (High(Row)+1);
  3053. end;
  3054. \end{verbatim}
  3055. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3056. % The array of const construct
  3057. \subsection{Array of const}
  3058. In Object Pascal or Delphi mode, \fpc supports the \var{Array of Const}
  3059. construction to pass parameters to a subroutine.
  3060. This is a special case of the \var{Open array} construction, where it is
  3061. allowed to pass any expression in an array to a function or procedure.
  3062. In the procedure, passed the arguments can be examined using a special
  3063. record:
  3064. \begin{verbatim}
  3065. Type
  3066. PVarRec = ^TVarRec;
  3067. TVarRec = record
  3068. case VType : Longint of
  3069. vtInteger : (VInteger: Longint);
  3070. vtBoolean : (VBoolean: Boolean);
  3071. vtChar : (VChar: Char);
  3072. vtExtended : (VExtended: PExtended);
  3073. vtString : (VString: PShortString);
  3074. vtPointer : (VPointer: Pointer);
  3075. vtPChar : (VPChar: PChar);
  3076. vtObject : (VObject: TObject);
  3077. vtClass : (VClass: TClass);
  3078. vtAnsiString : (VAnsiString: Pointer);
  3079. vtWideString : (VWideString: Pointer);
  3080. vtInt64 : (VInt64: PInt64);
  3081. end;
  3082. \end{verbatim}
  3083. Inside the procedure body, the array of const is equivalent to
  3084. an open array of TVarRec:
  3085. \begin{verbatim}
  3086. Procedure Testit (Args: Array of const);
  3087. Var I : longint;
  3088. begin
  3089. If High(Args)<0 then
  3090. begin
  3091. Writeln ('No aguments');
  3092. exit;
  3093. end;
  3094. Writeln ('Got ',High(Args)+1,' arguments :');
  3095. For i:=0 to High(Args) do
  3096. begin
  3097. write ('Argument ',i,' has type ');
  3098. case Args[i].vtype of
  3099. vtinteger :
  3100. Writeln ('Integer, Value :',args[i].vinteger);
  3101. vtboolean :
  3102. Writeln ('Boolean, Value :',args[i].vboolean);
  3103. vtchar :
  3104. Writeln ('Char, value : ',args[i].vchar);
  3105. vtextended :
  3106. Writeln ('Extended, value : ',args[i].VExtended^);
  3107. vtString :
  3108. Writeln ('ShortString, value :',args[i].VString^);
  3109. vtPointer :
  3110. Writeln ('Pointer, value : ',Longint(Args[i].VPointer));
  3111. vtPChar :
  3112. Writeln ('PCHar, value : ',Args[i].VPChar);
  3113. vtObject :
  3114. Writeln ('Object, name : ',Args[i].VObject.Classname);
  3115. vtClass :
  3116. Writeln ('Class reference, name :',Args[i].VClass.Classname);
  3117. vtAnsiString :
  3118. Writeln ('AnsiString, value :',AnsiString(Args[I].VAnsiStr
  3119. else
  3120. Writeln ('(Unknown) : ',args[i].vtype);
  3121. end;
  3122. end;
  3123. end;
  3124. \end{verbatim}
  3125. In code, it is possible to pass an arbitrary array of elements
  3126. to this procedure:
  3127. \begin{verbatim}
  3128. S:='Ansistring 1';
  3129. T:='AnsiString 2';
  3130. Testit ([]);
  3131. Testit ([1,2]);
  3132. Testit (['A','B']);
  3133. Testit ([TRUE,FALSE,TRUE]);
  3134. Testit (['String','Another string']);
  3135. Testit ([S,T]) ;
  3136. Testit ([P1,P2]);
  3137. Testit ([@testit,Nil]);
  3138. Testit ([ObjA,ObjB]);
  3139. Testit ([1.234,1.234]);
  3140. TestIt ([AClass]);
  3141. \end{verbatim}
  3142. If the procedure is declared with the \var{cdecl} modifier, then the
  3143. compiler will pass the array as a C compiler would pass it. This, in effect,
  3144. emulates the C construct of a variable number of arguments, as the following
  3145. example will show:
  3146. \begin{verbatim}
  3147. program testaocc;
  3148. {$mode objfpc}
  3149. Const
  3150. P : Pchar = 'example';
  3151. Fmt : PChar =
  3152. 'This %s uses printf to print numbers (%d) and strings.'#10;
  3153. // Declaration of standard C function printf:
  3154. procedure printf (fm : pchar; args : array of const);cdecl; external 'c';
  3155. begin
  3156. printf(Fmt,[P,123]);
  3157. end.
  3158. \end{verbatim}
  3159. Remark that this is not true for Delphi, so code relying on this feature
  3160. will not be portable.
  3161. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3162. % Function overloading
  3163. \section{Function overloading}
  3164. Function overloading simply means that the same function is defined more
  3165. than once, but each time with a different formal parameter list.
  3166. The parameter lists must differ at least in one of it's elements type.
  3167. When the compiler encounters a function call, it will look at the function
  3168. parameters to decide which one of the defined functions it should call.
  3169. This can be useful when the same function must be defined for different
  3170. types. For example, in the RTL, the \var{Dec} procedure could be
  3171. defined as:
  3172. \begin{verbatim}
  3173. ...
  3174. Dec(Var I : Longint;decrement : Longint);
  3175. Dec(Var I : Longint);
  3176. Dec(Var I : Byte;decrement : Longint);
  3177. Dec(Var I : Byte);
  3178. ...
  3179. \end{verbatim}
  3180. When the compiler encounters a call to the dec function, it will first search
  3181. which function it should use. It therefore checks the parameters in a
  3182. function call, and looks if there is a function definition which matches the
  3183. specified parameter list. If the compiler finds such a function, a call is
  3184. inserted to that function. If no such function is found, a compiler error is
  3185. generated.
  3186. functions that have a \var{cdecl} modifier cannot be overloaded.
  3187. (Technically, because this modifier prevents the mangling of
  3188. the function name by the compiler).
  3189. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3190. % forward defined functions
  3191. \section{Forward defined functions}
  3192. A function can be declared without having it followed by it's implementation,
  3193. by having it followed by the \var{forward} procedure. The effective
  3194. implementation of that function must follow later in the module.
  3195. The function can be used after a \var{forward} declaration as if it had been
  3196. implemented already.
  3197. The following is an example of a forward declaration.
  3198. \begin{verbatim}
  3199. Program testforward;
  3200. Procedure First (n : longint); forward;
  3201. Procedure Second;
  3202. begin
  3203. WriteLn ('In second. Calling first...');
  3204. First (1);
  3205. end;
  3206. Procedure First (n : longint);
  3207. begin
  3208. WriteLn ('First received : ',n);
  3209. end;
  3210. begin
  3211. Second;
  3212. end.
  3213. \end{verbatim}
  3214. A function can be defined as forward only once.
  3215. Likewise, in units, it is not allowed to have a forward declared function
  3216. of a function that has been declared in the interface part. The interface
  3217. declaration counts as a \var{forward} declaration.
  3218. The following unit will give an error when compiled:
  3219. \begin{verbatim}
  3220. Unit testforward;
  3221. interface
  3222. Procedure First (n : longint);
  3223. Procedure Second;
  3224. implementation
  3225. Procedure First (n : longint); forward;
  3226. Procedure Second;
  3227. begin
  3228. WriteLn ('In second. Calling first...');
  3229. First (1);
  3230. end;
  3231. Procedure First (n : longint);
  3232. begin
  3233. WriteLn ('First received : ',n);
  3234. end;
  3235. end.
  3236. \end{verbatim}
  3237. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3238. % External functions
  3239. \section{External functions}
  3240. \label{se:external}
  3241. The \var{external} modifier can be used to declare a function that resides in
  3242. an external object file. It allows to use the function in some code, and at
  3243. linking time, the object file containing the implementation of the function
  3244. or procedure must be linked in.
  3245. \input{syntax/external.syn}
  3246. It replaces, in effect, the function or procedure code block.
  3247. As an example:
  3248. \begin{verbatim}
  3249. program CmodDemo;
  3250. {$Linklib c}
  3251. Const P : PChar = 'This is fun !';
  3252. Function strlen (P : PChar) : Longint; cdecl; external;
  3253. begin
  3254. WriteLn ('Length of (',p,') : ',strlen(p))
  3255. end.
  3256. \end{verbatim}
  3257. \begin{remark}
  3258. The parameters in our declaration of the \var{external} function
  3259. should match exactly the ones in the declaration in the object file.
  3260. \end{remark}
  3261. If the \var{external} modifier is followed by a string constant:
  3262. \begin{verbatim}
  3263. external 'lname';
  3264. \end{verbatim}
  3265. Then this tells the compiler that the function resides in library
  3266. 'lname'. The compiler will then automatically link this library to
  3267. the program.
  3268. The name that the function has in the library can also be specified:
  3269. \begin{verbatim}
  3270. external 'lname' name 'Fname';
  3271. \end{verbatim}
  3272. This tells the compiler that the function resides in library 'lname',
  3273. but with name 'Fname'.The compiler will then automatically link this
  3274. library to the program, and use the correct name for the function.
  3275. Under \windows and \ostwo, the following form can also be used:
  3276. \begin{verbatim}
  3277. external 'lname' Index Ind;
  3278. \end{verbatim}
  3279. This tells the compiler that the function resides in library 'lname',
  3280. but with index \var{Ind}. The compiler will then automatically
  3281. link this library to the program, and use the correct index for the
  3282. function.
  3283. Finally, the external directive can be used to specify the external name
  3284. of the function :
  3285. \begin{verbatim}
  3286. {$L myfunc.o}
  3287. external name 'Fname';
  3288. \end{verbatim}
  3289. This tells the compiler that the function has the name 'Fname'. The
  3290. correct library or object file (in this case myfunc.o) must still be linked.
  3291. so that the function 'Fname' is included in the linking stage.
  3292. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3293. % Assembler functions
  3294. \section{Assembler functions}
  3295. Functions and procedures can be completely implemented in assembly
  3296. language. To indicate this, use the \var{assembler} keyword:
  3297. \input{syntax/asm.syn}
  3298. Contrary to Delphi, the assembler keyword must be present to indicate an
  3299. assembler function.
  3300. For more information about assembler functions, see the chapter on using
  3301. assembler in the \progref.
  3302. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3303. % Modifiers
  3304. \section{Modifiers}
  3305. A function or procedure declaration can contain modifiers. Here we list the
  3306. various possibilities:
  3307. \input{syntax/modifiers.syn}
  3308. \fpc doesn't support all Turbo Pascal modifiers, but
  3309. does support a number of additional modifiers. They are used mainly for assembler and
  3310. reference to C object files.
  3311. \subsection{alias}
  3312. The \var{alias} modifier allows the programmer to specify a different name for a
  3313. procedure or function. This is mostly useful for referring to this procedure
  3314. from assembly language constructs or from another object file. As an example,
  3315. consider the following program:
  3316. \begin{verbatim}
  3317. Program Aliases;
  3318. Procedure Printit;alias : 'DOIT';
  3319. begin
  3320. WriteLn ('In Printit (alias : "DOIT")');
  3321. end;
  3322. begin
  3323. asm
  3324. call DOIT
  3325. end;
  3326. end.
  3327. \end{verbatim}
  3328. \begin{remark} the specified alias is inserted straight into the assembly
  3329. code, thus it is case sensitive.
  3330. \end{remark}
  3331. The \var{alias} modifier does not make the symbol public to other modules,
  3332. unless the routine is also declared in the interface part of a unit, or
  3333. the \var{public} modifier is used to force it as public. Consider the
  3334. following:
  3335. \begin{verbatim}
  3336. unit testalias;
  3337. interface
  3338. procedure testroutine;
  3339. implementation
  3340. procedure testroutine;alias:'ARoutine';
  3341. begin
  3342. WriteLn('Hello world');
  3343. end;
  3344. end.
  3345. \end{verbatim}
  3346. This will make the routine \var{testroutine} available publicly to
  3347. external object files uunder the label name \var{ARoutine}.
  3348. \subsection{cdecl}
  3349. \label{se:cdecl}
  3350. The \var{cdecl} modifier can be used to declare a function that uses a C
  3351. type calling convention. This must be used when accessing functions residing in
  3352. an object file generated by standard C compilers. It allows to use the function in
  3353. the code, and at linking time, the object file containing the
  3354. \var{C} implementation of the function or procedure must be linked in.
  3355. As an example:
  3356. \begin{verbatim}
  3357. program CmodDemo;
  3358. {$LINKLIB c}
  3359. Const P : PChar = 'This is fun !';
  3360. Function strlen (P : PChar) : Longint; cdecl; external name 'strlen';
  3361. begin
  3362. WriteLn ('Length of (',p,') : ',strlen(p))
  3363. end.
  3364. \end{verbatim}
  3365. When compiling this, and linking to the C-library, the \var{strlen} function
  3366. can be called throughout the program. The \var{external} directive tells
  3367. the compiler that the function resides in an external object filebrary
  3368. with the 'strlen' name (see \ref{se:external}).
  3369. \begin{remark}
  3370. The parameters in our declaration of the \var{C} function should
  3371. match exactly the ones in the declaration in \var{C}.
  3372. \end{remark}
  3373. \subsection{export}
  3374. The export modifier is used to export names when creating a shared library
  3375. or an executable program. This means that the symbol will be publicly
  3376. available, and can be imported from other programs. For more information
  3377. on this modifier, consult the section on Programming dynamic libraries
  3378. in the \progref.
  3379. \subsection{inline}
  3380. \label{se:inline}
  3381. Procedures that are declared inline are copied to the places where they
  3382. are called. This has the effect that there is no actual procedure call,
  3383. the code of the procedure is just copied to where the procedure is needed,
  3384. this results in faster execution speed if the function or procedure is
  3385. used a lot.
  3386. By default, \var{inline} procedures are not allowed. Inline code must be enabled
  3387. using the command-line switch \var{-Si} or \var{\{\$inline on\}}
  3388. directive.
  3389. \begin{enumerate}
  3390. \item Inline code is NOT exported from a unit. This means that when
  3391. calling an inline procedure from another unit, a normal procedure call will be
  3392. performed. Only inside units, \var{Inline} procedures are really inlined.
  3393. \item Recursive inline functions are not allowed. i.e. an inline function
  3394. that calls itself is not allowed.
  3395. \end{enumerate}
  3396. \subsection{interrupt}
  3397. \label{se:interrupt}
  3398. The \var{interrupt} keyword is used to declare a routine which will
  3399. be used as an interrupt handler. On entry to this routine, all the registers
  3400. will be saved and on exit, all registers will be restored
  3401. and an interrupt or trap return will be executed (instead of the normal return
  3402. from subroutine instruction).
  3403. On platforms where a return from interrupt does not exist, the normal exit
  3404. code of routines will be done instead. For more information on the generated
  3405. code, consult the \progref.
  3406. \subsection{pascal}
  3407. \label{se:pascal}
  3408. The \var{pascal} modifier can be used to declare a function that uses the
  3409. classic pascal type calling convention (passing parameters from left to right).
  3410. For more information on the pascal calling convention, consult the \progref.
  3411. \subsection{popstack}
  3412. \label{se:popstack}
  3413. Popstack does the same as \var{cdecl}, namely it tells the \fpc compiler
  3414. that a function uses the C calling convention. In difference with the
  3415. \var{cdecl} modifier, it still mangles the name of the function as it would
  3416. for a normal pascal function.
  3417. With \var{popstack}, functions can be called by their pascal names in a
  3418. library.
  3419. \subsection{public}
  3420. The \var{Public} keyword is used to declare a function globally in a unit.
  3421. This is useful if the function should not be accessible from the unit
  3422. file (i.e. another unit/program using the unit doesn't see the function),
  3423. but must be accessible from the object file. as an example:
  3424. \begin{verbatim}
  3425. Unit someunit;
  3426. interface
  3427. Function First : Real;
  3428. Implementation
  3429. Function First : Real;
  3430. begin
  3431. First := 0;
  3432. end;
  3433. Function Second : Real; [Public];
  3434. begin
  3435. Second := 1;
  3436. end;
  3437. end.
  3438. \end{verbatim}
  3439. If another program or unit uses this unit, it will not be able to use the
  3440. function \var{Second}, since it isn't declared in the interface part.
  3441. However, it will be possible to access the function \var{Second} at the
  3442. assembly-language level, by using it's mangled name (see the \progref).
  3443. \subsection{register}
  3444. \label{se:register}
  3445. The \var{register} keyword is used for compatibility with Delphi. In
  3446. version 1.0.x of the compiler, this directive has no effect on the
  3447. generated code.
  3448. \subsection{saveregisters}
  3449. If this modifier is specified after a procedure or function, then the
  3450. \fpc compiler will save all registers on procedure entry, and restore
  3451. them when the procedure exits (except for registers where return values
  3452. are stored).
  3453. This modifier is not used under normal circumstances, except maybe when
  3454. calling assembler code.
  3455. \subsection{safecall}
  3456. This modifier ressembles closely the \var{stdcall} modifier. It sends
  3457. parameters from right to left on the stack.
  3458. More information about this modifier can be found in the \progref, in the
  3459. section on the calling mechanism and the chapter on linking.
  3460. \subsection{stdcall}
  3461. This modifier pushes the parameters from right to left on the stack,
  3462. it also aligns all the parameters to a default alignment.
  3463. More information about this modifier can be found in the \progref, in the
  3464. section on the calling mechanism and the chapter on linking.
  3465. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3466. % Unsupported Turbo Pascal modifiers
  3467. \section{Unsupported Turbo Pascal modifiers}
  3468. The modifiers that exist in Turbo pascal, but aren't supported by \fpc, are
  3469. listed in \seet{Modifs}.
  3470. \begin{FPCltable}{lr}{Unsupported modifiers}{Modifs}
  3471. Modifier & Why not supported ? \\ \hline
  3472. Near & \fpc is a 32-bit compiler.\\
  3473. Far & \fpc is a 32-bit compiler. \\
  3474. \end{FPCltable}
  3475. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3476. % Operator overloading
  3477. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3478. \chapter{Operator overloading}
  3479. \label{ch:operatoroverloading}
  3480. \section{Introduction}
  3481. \fpc supports operator overloading. This means that it is possible to
  3482. define the action of some operators on self-defined types, and thus allow
  3483. the use of these types in mathematical expressions.
  3484. Defining the action of an operator is much like the definition of a
  3485. function or procedure, only there are some restrictions on the possible
  3486. definitions, as will be shown in the subsequent.
  3487. Operator overloading is, in essence, a powerful notational tool;
  3488. but it is also not more than that, since the same results can be
  3489. obtained with regular function calls. When using operator overloading,
  3490. It is important to keep in mind that some implicit rules may produce
  3491. some unexpected results. This will be indicated.
  3492. \section{Operator declarations}
  3493. To define the action of an operator is much like defining a function:
  3494. \input{syntax/operator.syn}
  3495. The parameter list for a comparision operator or an arithmetic operator
  3496. must always contain 2 parameters. The result type of the comparision
  3497. operator must be \var{Boolean}.
  3498. \begin{remark}
  3499. When compiling in \var{Delphi} mode or \var{Objfpc} mode, the result
  3500. identifier may be dropped. The result can then be accessed through
  3501. the standard \var{Result} symbol.
  3502. If the result identifier is dropped and the compiler is not in one
  3503. of these modes, a syntax error will occur.
  3504. \end{remark}
  3505. The statement block contains the necessary statements to determine the
  3506. result of the operation. It can contain arbitrary large pieces of code;
  3507. it is executed whenever the operation is encountered in some expression.
  3508. The result of the statement block must always be defined; error conditions
  3509. are not checked by the compiler, and the code must take care of all possible
  3510. cases, throwing a run-time error if some error condition is encountered.
  3511. In the following, the three types of operator definitions will be examined.
  3512. As an example, throughout this chapter the following type will be used to
  3513. define overloaded operators on :
  3514. \begin{verbatim}
  3515. type
  3516. complex = record
  3517. re : real;
  3518. im : real;
  3519. end;
  3520. \end{verbatim}
  3521. this type will be used in all examples.
  3522. The sources of the Run-Time Library contain a unit \file{ucomplex},
  3523. which contains a complete calculus for complex numbers, based on
  3524. operator overloading.
  3525. \section{Assignment operators}
  3526. The assignment operator defines the action of a assignent of one type of
  3527. variable to another. The result type must match the type of the variable
  3528. at the left of the assignment statement, the single parameter to the
  3529. assignment operator must have the same type as the expression at the
  3530. right of the assignment operator.
  3531. This system can be used to declare a new type, and define an assignment for
  3532. that type. For instance, to be able to assign a newly defined type 'Complex'
  3533. \begin{verbatim}
  3534. Var
  3535. C,Z : Complex; // New type complex
  3536. begin
  3537. Z:=C; // assignments between complex types.
  3538. end;
  3539. \end{verbatim}
  3540. The following assignment operator would have to be defined:
  3541. \begin{verbatim}
  3542. Operator := (C : Complex) z : complex;
  3543. \end{verbatim}
  3544. To be able to assign a real type to a complex type as follows:
  3545. \begin{verbatim}
  3546. var
  3547. R : real;
  3548. C : complex;
  3549. begin
  3550. C:=R;
  3551. end;
  3552. \end{verbatim}
  3553. the following assignment operator must be defined:
  3554. \begin{verbatim}
  3555. Operator := (r : real) z : complex;
  3556. \end{verbatim}
  3557. As can be seen from this statement, it defines the action of the operator
  3558. \var{:=} with at the right a real expression, and at the left a complex
  3559. expression.
  3560. an example implementation of this could be as follows:
  3561. \begin{verbatim}
  3562. operator := (r : real) z : complex;
  3563. begin
  3564. z.re:=r;
  3565. z.im:=0.0;
  3566. end;
  3567. \end{verbatim}
  3568. As can be seen in the example, the result identifier (\var{z} in this case)
  3569. is used to store the result of the assignment. When compiling in Delphi mode
  3570. or objfpc mode, the use of the special identifier \var{Result} is also
  3571. allowed, and can be substituted for the \var{z}, so the above would be
  3572. equivalent to
  3573. \begin{verbatim}
  3574. operator := (r : real) z : complex;
  3575. begin
  3576. Result.re:=r;
  3577. Result.im:=0.0;
  3578. end;
  3579. \end{verbatim}
  3580. The assignment operator is also used to convert types from one type to
  3581. another. The compiler will consider all overloaded assignment operators
  3582. till it finds one that matches the types of the left hand and right hand
  3583. expressions. If no such operator is found, a 'type mismatch' error
  3584. is given.
  3585. \begin{remark}
  3586. The assignment operator is not commutative; the compiler will never reverse
  3587. the role of the two arguments. in other words, given the above definition of
  3588. the assignment operator, the following is {\em not} possible:
  3589. \begin{verbatim}
  3590. var
  3591. R : real;
  3592. C : complex;
  3593. begin
  3594. R:=C;
  3595. end;
  3596. \end{verbatim}
  3597. if the reverse assignment should be possible (this is not so for reals and
  3598. complex numbers) then the assigment operator must be defined for that as well.
  3599. \end{remark}
  3600. \begin{remark}
  3601. The assignment operator is also used in implicit type conversions. This can
  3602. have unwanted effects. Consider the following definitions:
  3603. \begin{verbatim}
  3604. operator := (r : real) z : complex;
  3605. function exp(c : complex) : complex;
  3606. \end{verbatim}
  3607. then the following assignment will give a type mismatch:
  3608. \begin{verbatim}
  3609. Var
  3610. r1,r2 : real;
  3611. begin
  3612. r1:=exp(r2);
  3613. end;
  3614. \end{verbatim}
  3615. because the compiler will encounter the definition of the \var{exp} function
  3616. with the complex argument. It implicitly converts r2 to a complex, so it can
  3617. use the above \var{exp} function. The result of this function is a complex,
  3618. which cannot be assigned to r1, so the compiler will give a 'type mismatch'
  3619. error. The compiler will not look further for another \var{exp} which has
  3620. the correct arguments.
  3621. It is possible to avoid this particular problem by specifying
  3622. \begin{verbatim}
  3623. r1:=system.exp(r2);
  3624. \end{verbatim}
  3625. An experimental solution for this problem exists in the compiler, but is
  3626. not enabled by default. Maybe someday it will be.
  3627. \end{remark}
  3628. \section{Arithmetic operators}
  3629. Arithmetic operators define the action of a binary operator. Possible
  3630. operations are:
  3631. \begin{description}
  3632. \item[multiplication] to multiply two types, the \var{*} multiplication
  3633. operator must be overloaded.
  3634. \item[division] to divide two types, the \var{/} division
  3635. operator must be overloaded.
  3636. \item[addition] to add two types, the \var{+} addition
  3637. operator must be overloaded.
  3638. \item[substraction] to substract two types, the \var{-} substraction
  3639. operator must be overloaded.
  3640. \item[exponentiation] to exponentiate two types, the \var{**} exponentiation
  3641. operator must be overloaded.
  3642. \end{description}
  3643. The definition of an arithmetic operator takes two parameters. The first
  3644. parameter must be of the type that occurs at the left of the operator,
  3645. the second parameter must be of the type that is at the right of the
  3646. arithmetic operator. The result type must match the type that results
  3647. after the arithmetic operation.
  3648. To compile an expression as
  3649. \begin{verbatim}
  3650. var
  3651. R : real;
  3652. C,Z : complex;
  3653. begin
  3654. C:=R*Z;
  3655. end;
  3656. \end{verbatim}
  3657. one needs a definition of the multiplication operator as:
  3658. \begin{verbatim}
  3659. Operator * (r : real; z1 : complex) z : complex;
  3660. begin
  3661. z.re := z1.re * r;
  3662. z.im := z1.im * r;
  3663. end;
  3664. \end{verbatim}
  3665. As can be seen, the first operator is a real, and the second is
  3666. a complex. The result type is complex.
  3667. Multiplication and addition of reals and complexes are commutative
  3668. operations. The compiler, however, has no notion of this fact so even
  3669. if a multiplication between a real and a complex is defined, the
  3670. compiler will not use that definition when it encounters a complex
  3671. and a real (in that order). It is necessary to define both operations.
  3672. So, given the above definition of the multiplication,
  3673. the compiler will not accept the following statement:
  3674. \begin{verbatim}
  3675. var
  3676. R : real;
  3677. C,Z : complex;
  3678. begin
  3679. C:=Z*R;
  3680. end;
  3681. \end{verbatim}
  3682. since the types of \var{Z} and \var{R} don't match the types in the
  3683. operator definition.
  3684. The reason for this behaviour is that it is possible that a multiplication
  3685. is not always commutative. e.g. the multiplication of a \var{(n,m)} with a
  3686. \var{(m,n)} matrix will result in a \var{(n,n)} matrix, while the
  3687. mutiplication of a \var{(m,n)} with a \var{(n,m)} matrix is a \var{(m,m)}
  3688. matrix, which needn't be the same in all cases.
  3689. \section{Comparision operator}
  3690. The comparision operator can be overloaded to compare two different types
  3691. or to compare two equal types that are not basic types. The result type of
  3692. a comparision operator is always a boolean.
  3693. The comparision operators that can be overloaded are:
  3694. \begin{description}
  3695. \item[equal to] (=) to determine if two variables are equal.
  3696. \item[less than] ($<$) to determine if one variable is less than another.
  3697. \item[greater than] ($>$) to determine if one variable is greater than another.
  3698. \item[greater than or equal to] ($>=$) to determine if one variable is greater than
  3699. or equal to another.
  3700. \item[less than or equal to] ($<=$) to determine if one variable is greater
  3701. than or equal to another.
  3702. \end{description}
  3703. There is no separate operator for {\em unequal to} ($<>$). To evaluate a
  3704. statement that contans the {\em unequal to} operator, the compiler uses the
  3705. {\em equal to} operator (=), and negates the result.
  3706. As an example, the following opetrator allows to compare two complex
  3707. numbers:
  3708. \begin{verbatim}
  3709. operator = (z1, z2 : complex) b : boolean;
  3710. \end{verbatim}
  3711. the above definition allows comparisions of the following form:
  3712. \begin{verbatim}
  3713. Var
  3714. C1,C2 : Complex;
  3715. begin
  3716. If C1=C2 then
  3717. Writeln('C1 and C2 are equal');
  3718. end;
  3719. \end{verbatim}
  3720. The comparision operator definition needs 2 parameters, with the types that
  3721. the operator is meant to compare. Here also, the compiler doesn't apply
  3722. commutativity; if the two types are different, then it necessary to
  3723. define 2 comparision operators.
  3724. In the case of complex numbers, it is, for instance necessary to define
  3725. 2 comparsions: one with the complex type first, and one with the real type
  3726. first.
  3727. Given the definitions
  3728. \begin{verbatim}
  3729. operator = (z1 : complex;r : real) b : boolean;
  3730. operator = (r : real; z1 : complex) b : boolean;
  3731. \end{verbatim}
  3732. the following two comparisions are possible:
  3733. \begin{verbatim}
  3734. Var
  3735. R,S : Real;
  3736. C : Complex;
  3737. begin
  3738. If (C=R) or (S=C) then
  3739. Writeln ('Ok');
  3740. end;
  3741. \end{verbatim}
  3742. Note that the order of the real and complex type in the two comparisions
  3743. is reversed.
  3744. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3745. % Programs, Units, Blocks
  3746. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3747. \chapter{Programs, units, blocks}
  3748. A Pascal program consists of modules called \var{units}. A unit can be used
  3749. to group pieces of code together, or to give someone code without giving
  3750. the sources.
  3751. Both programs and units consist of code blocks, which are mixtures of
  3752. statements, procedures, and variable or type declarations.
  3753. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3754. % Programs
  3755. \section{Programs}
  3756. A pascal program consists of the program header, followed possibly by a
  3757. 'uses' clause, and a block.
  3758. \input{syntax/program.syn}
  3759. The program header is provided for backwards compatibility, and is ignored
  3760. by the compiler.
  3761. The uses clause serves to identify all units that are needed by the program.
  3762. The system unit doesn't have to be in this list, since it is always loaded
  3763. by the compiler.
  3764. The order in which the units appear is significant, it determines in
  3765. which order they are initialized. Units are initialized in the same order
  3766. as they appear in the uses clause. Identifiers are searched in the opposite
  3767. order, i.e. when the compiler searches for an identifier, then it looks
  3768. first in the last unit in the uses clause, then the last but one, and so on.
  3769. This is important in case two units declare different types with the same
  3770. identifier.
  3771. When the compiler looks for unit files, it adds the extension \file{.ppu}
  3772. (\file{.ppw} for Win32 platforms) to the name of the unit. On \linux and in
  3773. operating systems where filenames are case sensitive, when looking for a unit,
  3774. the unit name is first looked for in the original case, and when not found,
  3775. converted to all lowercase and searched for.
  3776. If a unit name is longer than 8 characters, the compiler will first look for
  3777. a unit name with this length, and then it will truncate the name to 8
  3778. characters and look for it again. For compatibility reasons, this is also
  3779. true on platforms that suport long file names.
  3780. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3781. % Units
  3782. \section{Units}
  3783. A unit contains a set of declarations, procedures and functions that can be
  3784. used by a program or another unit.
  3785. The syntax for a unit is as follows:
  3786. \input{syntax/unit.syn}
  3787. The interface part declares all identifiers that must be exported from the
  3788. unit. This can be constant, type or variable identifiers, and also procedure
  3789. or function identifier declarations. Declarations inside the
  3790. implementation part are {\em not} accessible outside the unit. The
  3791. implementation must contain a function declaration for each function or
  3792. procedure that is declared in the interface part. If a function is declared
  3793. in the interface part, but no declaration of that function is present in the
  3794. implementation part, then the compiler will give an error.
  3795. When a program uses a unit (say \file{unitA}) and this units uses a second
  3796. unit, say \file{unitB}, then the program depends indirectly also on
  3797. \var{unitB}. This means that the compiler must have access to \file{unitB} when
  3798. trying to compile the program. If the unit is not present at compile time,
  3799. an error occurs.
  3800. Note that the identifiers from a unit on which a program depends indirectly,
  3801. are not accessible to the program. To have access to the identifiers of a
  3802. unit, the unit must be in the uses clause of the program or unit where the
  3803. identifiers are needed.
  3804. Units can be mutually dependent, that is, they can reference each other in
  3805. their uses clauses. This is allowed, on the condition that at least one of
  3806. the references is in the implementation section of the unit. This also holds
  3807. for indirect mutually dependent units.
  3808. If it is possible to start from one interface uses clause of a unit, and to return
  3809. there via uses clauses of interfaces only, then there is circular unit
  3810. dependence, and the compiler will generate an error.
  3811. As and example : the following is not allowed:
  3812. \begin{verbatim}
  3813. Unit UnitA;
  3814. interface
  3815. Uses UnitB;
  3816. implementation
  3817. end.
  3818. Unit UnitB
  3819. interface
  3820. Uses UnitA;
  3821. implementation
  3822. end.
  3823. \end{verbatim}
  3824. But this is allowed :
  3825. \begin{verbatim}
  3826. Unit UnitA;
  3827. interface
  3828. Uses UnitB;
  3829. implementation
  3830. end.
  3831. Unit UnitB
  3832. implementation
  3833. Uses UnitA;
  3834. end.
  3835. \end{verbatim}
  3836. Because \file{UnitB} uses \file{UnitA} only in it's implentation section.
  3837. In general, it is a bad idea to have circular unit dependencies, even if it is
  3838. only in implementation sections.
  3839. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3840. % Blocks
  3841. \section{Blocks}
  3842. Units and programs are made of blocks. A block is made of declarations of
  3843. labels, constants, types variables and functions or procedures. Blocks can
  3844. be nested in certain ways, i.e., a procedure or function declaration can
  3845. have blocks in themselves.
  3846. A block looks like the following:
  3847. \input{syntax/block.syn}
  3848. Labels that can be used to identify statements in a block are declared in
  3849. the label declaration part of that block. Each label can only identify one
  3850. statement.
  3851. Constants that are to be used only in one block should be declared in that
  3852. block's constant declaration part.
  3853. Variables that are to be used only in one block should be declared in that
  3854. block's constant declaration part.
  3855. Types that are to be used only in one block should be declared in that
  3856. block's constant declaration part.
  3857. Lastly, functions and procedures that will be used in that block can be
  3858. declared in the procedure/function declaration part.
  3859. After the different declaration parts comes the statement part. This
  3860. contains any actions that the block should execute.
  3861. All identifiers declared before the statement part can be used in that
  3862. statement part.
  3863. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3864. % Scope
  3865. \section{Scope}
  3866. Identifiers are valid from the point of their declaration until the end of
  3867. the block in which the declaration occurred. The range where the identifier
  3868. is known is the {\em scope} of the identifier. The exact scope of an
  3869. identifier depends on the way it was defined.
  3870. \subsection{Block scope}
  3871. The {\em scope} of a variable declared in the declaration part of a block,
  3872. is valid from the point of declaration until the end of the block.
  3873. If a block contains a second block, in which the identfier is
  3874. redeclared, then inside this block, the second declaration will be valid.
  3875. Upon leaving the inner block, the first declaration is valid again.
  3876. Consider the following example:
  3877. \begin{verbatim}
  3878. Program Demo;
  3879. Var X : Real;
  3880. { X is real variable }
  3881. Procedure NewDeclaration
  3882. Var X : Integer; { Redeclare X as integer}
  3883. begin
  3884. // X := 1.234; {would give an error when trying to compile}
  3885. X := 10; { Correct assigment}
  3886. end;
  3887. { From here on, X is Real again}
  3888. begin
  3889. X := 2.468;
  3890. end.
  3891. \end{verbatim}
  3892. In this example, inside the procedure, X denotes an integer variable.
  3893. It has it's own storage space, independent of the variable \var{X} outside
  3894. the procedure.
  3895. \subsection{Record scope}
  3896. The field identifiers inside a record definition are valid in the following
  3897. places:
  3898. \begin{enumerate}
  3899. \item to the end of the record definition.
  3900. \item field designators of a variable of the given record type.
  3901. \item identifiers inside a \var{With} statement that operates on a variable
  3902. of the given record type.
  3903. \end{enumerate}
  3904. \subsection{Class scope}
  3905. A component identifier is valid in the following places:
  3906. \begin{enumerate}
  3907. \item From the point of declaration to the end of the class definition.
  3908. \item In all descendent types of this class, unless it is in the private
  3909. part of the class declaration.
  3910. \item In all method declaration blocks of this class and descendent classes.
  3911. \item In a with statement that operators on a variable of the given class's
  3912. definition.
  3913. \end{enumerate}
  3914. Note that method designators are also considered identifiers.
  3915. \subsection{Unit scope}
  3916. All identifiers in the interface part of a unit are valid from the point of
  3917. declaration, until the end of the unit. Furthermore, the identifiers are
  3918. known in programs or units that have the unit in their uses clause.
  3919. Identifiers from indirectly dependent units are {\em not} available.
  3920. Identifiers declared in the implementation part of a unit are valid from the
  3921. point of declaration to the end of the unit.
  3922. The system unit is automatically used in all units and programs.
  3923. It's identifiers are therefore always known, in each pascal program, library
  3924. or unit.
  3925. The rules of unit scope imply that an identifier of a
  3926. unit can be redefined. To have access to an identifier of another unit that was redeclared in
  3927. the current unit, precede it with that other units name, as in the following
  3928. example:
  3929. \begin{verbatim}
  3930. unit unitA;
  3931. interface
  3932. Type
  3933. MyType = Real;
  3934. implementation
  3935. end.
  3936. Program prog;
  3937. Uses UnitA;
  3938. { Redeclaration of MyType}
  3939. Type MyType = Integer;
  3940. Var A : Mytype; { Will be Integer }
  3941. B : UnitA.MyType { Will be real }
  3942. begin
  3943. end.
  3944. \end{verbatim}
  3945. This is especially useful when redeclaring the system unit's identifiers.
  3946. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3947. % Libraries
  3948. \section{Libraries}
  3949. \fpc supports making of dynamic libraries (DLLs under Win32 and \ostwo) trough
  3950. the use of the \var{Library} keyword.
  3951. A Library is just like a unit or a program:
  3952. \input{syntax/library.syn}
  3953. By default, functions and procedures that are declared and implemented in
  3954. library are not available to a programmer that wishes to use this library.
  3955. In order to make functions or procedures available from the library,
  3956. they must be exported in an export clause:
  3957. \input{syntax/exports.syn}
  3958. Under Win32, an index clause can be added to an exports entry.
  3959. an index entry must be a positive number larger or equal than 1.
  3960. Optionally, an exports entry can have a name specifier. If present, the name
  3961. specifier gives the exact name (case sensitive) of the function in the
  3962. library.
  3963. If neither of these constructs is present, the functions or procedures
  3964. are exported with the exact names as specified in the exports clause.
  3965. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3966. % Exceptions
  3967. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3968. \chapter{Exceptions}
  3969. \label{ch:Exceptions}
  3970. Exceptions provide a convenient way to program error and error-recovery
  3971. mechanisms, and are closely related to classes.
  3972. Exception support is based on 3 constructs:
  3973. \begin{description}
  3974. \item [Raise\ ] statements. To raise an exeption. This is usually done to signal an
  3975. error condition.
  3976. \item [Try ... Except\ ] blocks. These block serve to catch exceptions
  3977. raised within the scope of the block, and to provide exception-recovery
  3978. code.
  3979. \item [Try ... Finally\ ] blocks. These block serve to force code to be
  3980. executed irrespective of an exception occurrence or not. They generally
  3981. serve to clean up memory or close files in case an exception occurs.
  3982. The compiler generates many implicit \var{Try ... Finally} blocks around
  3983. procedure, to force memory consistence.
  3984. \end{description}
  3985. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3986. % The raise statement
  3987. \section{The raise statement}
  3988. The \var{raise} statement is as follows:
  3989. \input{syntax/raise.syn}
  3990. This statement will raise an exception. If it is specified, the exception
  3991. instance must be an initialized instance of a class, which is the raise
  3992. type. The address exception is optional. If itis not specified, the compiler
  3993. will provide the address by itself.
  3994. If the exception instance is omitted, then the current exception is
  3995. re-raised. This construct can only be used in an exception handling
  3996. block (see further).
  3997. \begin{remark} Control {\em never} returns after an exception block. The
  3998. control is transferred to the first \var{try...finally} or
  3999. \var{try...except} statement that is encountered when unwinding the stack.
  4000. If no such statement is found, the \fpc Run-Time Library will generate a
  4001. run-time error 217 (see also \sees{exceptclasses}).
  4002. \end{remark}
  4003. As an example: The following division checks whether the denominator is
  4004. zero, and if so, raises an exception of type \var{EDivException}
  4005. \begin{verbatim}
  4006. Type EDivException = Class(Exception);
  4007. Function DoDiv (X,Y : Longint) : Integer;
  4008. begin
  4009. If Y=0 then
  4010. Raise EDivException.Create ('Division by Zero would occur');
  4011. Result := X Div Y;
  4012. end;
  4013. \end{verbatim}
  4014. The class \var{Exception} is defined in the \file{Sysutils} unit of the rtl.
  4015. (\sees{exceptclasses})
  4016. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4017. % The try...except statement
  4018. \section{The try...except statement}
  4019. A \var{try...except} exception handling block is of the following form :
  4020. \input{syntax/try.syn}
  4021. If no exception is raised during the execution of the \var{statement list},
  4022. then all statements in the list will be executed sequentially, and the
  4023. except block will be skipped, transferring program flow to the statement
  4024. after the final \var{end}.
  4025. If an exception occurs during the execution of the \var{statement list}, the
  4026. program flow will be transferred to the except block. Statements in the
  4027. statement list between the place where the exception was raised and the
  4028. exception block are ignored.
  4029. In the exception handling block, the type of the exception is checked,
  4030. and if there is an exception handler where the class type matches the
  4031. exception object type, or is a parent type of
  4032. the exception object type, then the statement following the corresponding
  4033. \var{Do} will be executed. The first matching type is used. After the
  4034. \var{Do} block was executed, the program continues after the \var{End}
  4035. statement.
  4036. The identifier in an exception handling statement is optional, and declares
  4037. an exception object. It can be used to manipulate the exception object in
  4038. the exception handling code. The scope of this declaration is the statement
  4039. block foillowing the \var{Do} keyword.
  4040. If none of the \var{On} handlers matches the exception object type, then the
  4041. statement list after \var{else} is executed. If no such list is
  4042. found, then the exception is automatically re-raised. This process allows
  4043. to nest \var{try...except} blocks.
  4044. If, on the other hand, the exception was caught, then the exception object is
  4045. destroyed at the end of the exception handling block, before program flow
  4046. continues. The exception is destroyed through a call to the object's
  4047. \var{Destroy} destructor.
  4048. As an example, given the previous declaration of the \var{DoDiv} function,
  4049. consider the following
  4050. \begin{verbatim}
  4051. Try
  4052. Z := DoDiv (X,Y);
  4053. Except
  4054. On EDivException do Z := 0;
  4055. end;
  4056. \end{verbatim}
  4057. If \var{Y} happens to be zero, then the DoDiv function code will raise an
  4058. exception. When this happens, program flow is transferred to the except
  4059. statement, where the Exception handler will set the value of \var{Z} to
  4060. zero. If no exception is raised, then program flow continues past the last
  4061. \var{end} statement.
  4062. To allow error recovery, the \var{Try ... Finally} block is supported.
  4063. A \var{Try...Finally} block ensures that the statements following the
  4064. \var{Finally} keyword are guaranteed to be executed, even if an exception
  4065. occurs.
  4066. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4067. % The try...finally statement
  4068. \section{The try...finally statement}
  4069. A \var{Try..Finally} statement has the following form:
  4070. \input{syntax/finally.syn}
  4071. If no exception occurs inside the \var{statement List}, then the program
  4072. runs as if the \var{Try}, \var{Finally} and \var{End} keywords were not
  4073. present.
  4074. If, however, an exception occurs, the program flow is immediatly
  4075. transferred from the point where the excepion was raised to the first
  4076. statement of the \var{Finally statements}.
  4077. All statements after the finally keyword will be executed, and then
  4078. the exception will be automatically re-raised. Any statements between the
  4079. place where the exception was raised and the first statement of the
  4080. \var{Finally Statements} are skipped.
  4081. As an example consider the following routine:
  4082. \begin{verbatim}
  4083. Procedure Doit (Name : string);
  4084. Var F : Text;
  4085. begin
  4086. Try
  4087. Assign (F,Name);
  4088. Rewrite (name);
  4089. ... File handling ...
  4090. Finally
  4091. Close(F);
  4092. end;
  4093. \end{verbatim}
  4094. If during the execution of the file handling an execption occurs, then
  4095. program flow will continue at the \var{close(F)} statement, skipping any
  4096. file operations that might follow between the place where the exception
  4097. was raised, and the \var{Close} statement.
  4098. If no exception occurred, all file operations will be executed, and the file
  4099. will be closed at the end.
  4100. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4101. % Exception handling nesting
  4102. \section{Exception handling nesting}
  4103. It is possible to nest \var{Try...Except} blocks with \var{Try...Finally}
  4104. blocks. Program flow will be done according to a \var{lifo} (last in, first
  4105. out) principle: The code of the last encountered \var{Try...Except} or
  4106. \var{Try...Finally} block will be executed first. If the exception is not
  4107. caught, or it was a finally statement, program flow will be transferred to
  4108. the last-but-one block, {\em ad infinitum}.
  4109. If an exception occurs, and there is no exception handler present, then a
  4110. runerror 217 will be generated. When using the \file{sysutils} unit, a default
  4111. handler is installed which will show the exception object message, and the
  4112. address where the exception occurred, after which the program will exit with
  4113. a \var{Halt} instruction.
  4114. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4115. % Exception classes
  4116. \section{Exception classes}
  4117. \label{se:exceptclasses}
  4118. The \file{sysutils} unit contains a great deal of exception handling.
  4119. It defines the following exception types:
  4120. \begin{verbatim}
  4121. Exception = class(TObject)
  4122. private
  4123. fmessage : string;
  4124. fhelpcontext : longint;
  4125. public
  4126. constructor create(const msg : string);
  4127. constructor createres(indent : longint);
  4128. property helpcontext : longint read fhelpcontext write fhelpcontext;
  4129. property message : string read fmessage write fmessage;
  4130. end;
  4131. ExceptClass = Class of Exception;
  4132. { mathematical exceptions }
  4133. EIntError = class(Exception);
  4134. EDivByZero = class(EIntError);
  4135. ERangeError = class(EIntError);
  4136. EIntOverflow = class(EIntError);
  4137. EMathError = class(Exception);
  4138. \end{verbatim}
  4139. The sysutils unit also installs an exception handler. If an exception is
  4140. unhandled by any exception handling block, this handler is called by the
  4141. Run-Time library. Basically, it prints the exception address, and it prints
  4142. the message of the Exception object, and exits with a exit code of 217.
  4143. If the exception object is not a descendent object of the \var{Exception}
  4144. object, then the class name is printed instead of the exception message.
  4145. It is recommended to use the \var{Exception} object or a descendant class for
  4146. all \var{raise} statements, since then the message field of the
  4147. exception object can be used.
  4148. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4149. % Using Assembler
  4150. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4151. \chapter{Using assembler}
  4152. \fpc supports the use of assembler in code, but not inline
  4153. assembler macros. To have more information on the processor
  4154. specific assembler syntax and its limitations, see the \progref.
  4155. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4156. % Assembler statements
  4157. \section{Assembler statements }
  4158. The following is an example of assembler inclusion in pascal code.
  4159. \begin{verbatim}
  4160. ...
  4161. Statements;
  4162. ...
  4163. Asm
  4164. the asm code here
  4165. ...
  4166. end;
  4167. ...
  4168. Statements;
  4169. \end{verbatim}
  4170. The assembler instructions between the \var{Asm} and \var{end} keywords will
  4171. be inserted in the assembler generated by the compiler.
  4172. Conditionals can be used ib assembler, the compiler will recognise it,
  4173. and treat it as any other conditionals.
  4174. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4175. % Assembler procedures and functions
  4176. \section{Assembler procedures and functions}
  4177. Assembler procedures and functions are declared using the
  4178. \var{Assembler} directive. This permits the code generator to make a number
  4179. of code generation optimizations.
  4180. The code generator does not generate any stack frame (entry and exit
  4181. code for the routine) if it contains no local variables and no
  4182. parameters. In the case of functions, ordinal values must be returned
  4183. in the accumulator. In the case of floating point values, these depend
  4184. on the target processor and emulation options.
  4185. %
  4186. % System unit reference guide.
  4187. %
  4188. \part{Reference : The System unit}
  4189. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4190. % The system unit
  4191. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4192. \chapter{The system unit}
  4193. \label{ch:refchapter}
  4194. \FPCexampledir{refex}
  4195. The system unit contains the standard supported functions of \fpc. It is the
  4196. same for all platforms. Basically it is the same as the system unit provided
  4197. with Borland or Turbo Pascal.
  4198. Functions are listed in alphabetical order. Arguments of functions or
  4199. procedures that are optional are put between square brackets.
  4200. The pre-defined constants and variables are listed in the first section.
  4201. The second section contains an overview of all functions, grouped by
  4202. functionality, and the last section contains the supported functions
  4203. and procedures.
  4204. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4205. % Types, Constants and Variables
  4206. \section{Types, Constants and Variables}
  4207. \subsection{Types}
  4208. The following integer types are defined in the System unit:
  4209. \begin{verbatim}
  4210. Shortint = -128..127;
  4211. SmallInt = -32768..32767;
  4212. Longint = $80000000..$7fffffff;
  4213. byte = 0..255;
  4214. word = 0..65535;
  4215. dword = longword;
  4216. cardinal = longword;
  4217. Integer = smallint;
  4218. \end{verbatim}
  4219. The following types are used for the functions that need compiler magic
  4220. such as \seep{Val} or \seep{Str}:
  4221. \begin{verbatim}
  4222. StrLenInt = LongInt;
  4223. ValSInt = Longint;
  4224. ValUInt = Cardinal;
  4225. ValReal = Extended;
  4226. \end{verbatim}
  4227. The \var{Real48} type is defined to emulate the old \tp \var{Real} type:
  4228. \begin{verbatim}
  4229. Real48 = Array[0..5] of byte;
  4230. \end{verbatim}
  4231. The assignment operator has been overloaded so this type can be assigned
  4232. to the \fpc native \var{Double} and \var{Extended} types.
  4233. \seefl{Real2Double}{RealToDouble}.
  4234. The following character types are defined for Delphi compatibility:
  4235. \begin{verbatim}
  4236. TAnsiChar = Char;
  4237. AnsiChar = TAnsiChar;
  4238. \end{verbatim}
  4239. And the following pointer types as well:
  4240. \begin{verbatim}
  4241. PChar = ^char;
  4242. pPChar = ^PChar;
  4243. PAnsiChar = PChar;
  4244. PQWord = ^QWord;
  4245. PInt64 = ^Int64;
  4246. pshortstring = ^shortstring;
  4247. plongstring = ^longstring;
  4248. pansistring = ^ansistring;
  4249. pwidestring = ^widestring;
  4250. pextended = ^extended;
  4251. ppointer = ^pointer;
  4252. \end{verbatim}
  4253. For the \seef{SetJmp} and \seep{LongJmp} calls, the following jump bufer
  4254. type is defined (for the I386 processor):
  4255. \begin{verbatim}
  4256. jmp_buf = record
  4257. ebx,esi,edi : Longint;
  4258. bp,sp,pc : Pointer;
  4259. end;
  4260. PJmp_buf = ^jmp_buf;
  4261. \end{verbatim}
  4262. The following records and pointers can be used to scan the
  4263. entries in the string message handler tables:
  4264. \begin{verbatim}
  4265. tmsgstrtable = record
  4266. name : pshortstring;
  4267. method : pointer;
  4268. end;
  4269. pmsgstrtable = ^tmsgstrtable;
  4270. tstringmessagetable = record
  4271. count : dword;
  4272. msgstrtable : array[0..0] of tmsgstrtable;
  4273. end;
  4274. pstringmessagetable = ^tstringmessagetable;
  4275. \end{verbatim}
  4276. The base class for all classes is defined as:
  4277. \begin{verbatim}
  4278. Type
  4279. TObject = Class
  4280. Public
  4281. constructor create;
  4282. destructor destroy;virtual;
  4283. class function newinstance : tobject;virtual;
  4284. procedure freeinstance;virtual;
  4285. function safecallexception(exceptobject : tobject;
  4286. exceptaddr : pointer) : longint;virtual;
  4287. procedure defaulthandler(var message);virtual;
  4288. procedure free;
  4289. class function initinstance(instance : pointer) : tobject;
  4290. procedure cleanupinstance;
  4291. function classtype : tclass;
  4292. class function classinfo : pointer;
  4293. class function classname : shortstring;
  4294. class function classnameis(const name : string) : boolean;
  4295. class function classparent : tclass;
  4296. class function instancesize : longint;
  4297. class function inheritsfrom(aclass : tclass) : boolean;
  4298. class function inheritsfrom(aclass : tclass) : boolean;
  4299. class function stringmessagetable : pstringmessagetable;
  4300. procedure dispatch(var message);
  4301. procedure dispatchstr(var message);
  4302. class function methodaddress(const name : shortstring) : pointer;
  4303. class function methodname(address : pointer) : shortstring;
  4304. function fieldaddress(const name : shortstring) : pointer;
  4305. procedure AfterConstruction;virtual;
  4306. procedure BeforeDestruction;virtual;
  4307. procedure DefaultHandlerStr(var message);virtual;
  4308. end;
  4309. TClass = Class Of TObject;
  4310. PClass = ^TClass;
  4311. \end{verbatim}
  4312. Unhandled exceptions can be treated using a constant of the
  4313. \var{TExceptProc} type:
  4314. \begin{verbatim}
  4315. TExceptProc = Procedure (Obj : TObject; Addr,Frame: Pointer);
  4316. \end{verbatim}
  4317. \var{Obj} is the exception object that was used to raise the exception,
  4318. \var{Addr} and \var{Frame} contain the exact address and stack frame
  4319. where the exception was raised.
  4320. The \var{TVarRec} type is used to access the elements passed in a \var{Array
  4321. of Const} argument to a function or procedure:
  4322. \begin{verbatim}
  4323. Type
  4324. PVarRec = ^TVarRec;
  4325. TVarRec = record
  4326. case VType : Longint of
  4327. vtInteger : (VInteger: Longint);
  4328. vtBoolean : (VBoolean: Boolean);
  4329. vtChar : (VChar: Char);
  4330. vtExtended : (VExtended: PExtended);
  4331. vtString : (VString: PShortString);
  4332. vtPointer : (VPointer: Pointer);
  4333. vtPChar : (VPChar: PChar);
  4334. vtObject : (VObject: TObject);
  4335. vtClass : (VClass: TClass);
  4336. vtAnsiString : (VAnsiString: Pointer);
  4337. vtWideString : (VWideString: Pointer);
  4338. vtInt64 : (VInt64: PInt64);
  4339. end;
  4340. \end{verbatim}
  4341. The heap manager uses the \var{TMemoryManager} type:
  4342. \begin{verbatim}
  4343. PMemoryManager = ^TMemoryManager;
  4344. TMemoryManager = record
  4345. Getmem : Function(Size:Longint):Pointer;
  4346. Freemem : Function(var p:pointer):Longint;
  4347. FreememSize : Function(var p:pointer;Size:Longint):Longint;
  4348. AllocMem : Function(Size:longint):Pointer;
  4349. ReAllocMem : Function(var p:pointer;Size:longint):Pointer;
  4350. MemSize : function(p:pointer):Longint;
  4351. MemAvail : Function:Longint;
  4352. MaxAvail : Function:Longint;
  4353. HeapSize : Function:Longint;
  4354. end;
  4355. \end{verbatim}
  4356. More information on using this record can be found in \progref.
  4357. \subsection{Constants}
  4358. The following constants define the maximum values that can be used with
  4359. various types:
  4360. \begin{verbatim}
  4361. MaxSIntValue = High(ValSInt);
  4362. MaxUIntValue = High(ValUInt);
  4363. maxint = maxsmallint;
  4364. maxLongint = $7fffffff;
  4365. maxSmallint = 32767;
  4366. \end{verbatim}
  4367. The following constants for file-handling are defined in the system unit:
  4368. \begin{verbatim}
  4369. Const
  4370. fmclosed = $D7B0;
  4371. fminput = $D7B1;
  4372. fmoutput = $D7B2;
  4373. fminout = $D7B3;
  4374. fmappend = $D7B4;
  4375. filemode : byte = 2;
  4376. \end{verbatim}
  4377. The \var{filemode} variable is used when a non-text file is opened using
  4378. \var{Reset}. It indicates how the file will be opened. \var{filemode} can have one of
  4379. the following values:
  4380. \begin{description}
  4381. \item[0] The file is opened for reading.
  4382. \item[1] The file is opened for writing.
  4383. \item[2] The file is opened for reading and writing.
  4384. \end{description}
  4385. The default value is 2. Other values are possible but are operating system specific.
  4386. Further, the following non processor specific general-purpose constants
  4387. are also defined:
  4388. \begin{verbatim}
  4389. const
  4390. erroraddr : pointer = nil;
  4391. errorcode : word = 0;
  4392. { max level in dumping on error }
  4393. max_frame_dump : word = 20;
  4394. \end{verbatim}
  4395. \begin{remark}
  4396. Processor specific global constants are named Testxxxx where xxxx
  4397. represents the processor number (such as Test8086, Test68000),
  4398. and are used to determine on what generation of processor the program
  4399. is running on.
  4400. \end{remark}
  4401. The following constants are defined to access VMT entries:
  4402. \begin{verbatim}
  4403. vmtInstanceSize = 0;
  4404. vmtParent = 8;
  4405. vmtClassName = 12;
  4406. vmtDynamicTable = 16;
  4407. vmtMethodTable = 20;
  4408. vmtFieldTable = 24;
  4409. vmtTypeInfo = 28;
  4410. vmtInitTable = 32;
  4411. vmtAutoTable = 36;
  4412. vmtIntfTable = 40;
  4413. vmtMsgStrPtr = 44;
  4414. vmtMethodStart = 48;
  4415. vmtDestroy = vmtMethodStart;
  4416. vmtNewInstance = vmtMethodStart+4;
  4417. vmtFreeInstance = vmtMethodStart+8;
  4418. vmtSafeCallException = vmtMethodStart+12;
  4419. vmtDefaultHandler = vmtMethodStart+16;
  4420. vmtAfterConstruction = vmtMethodStart+20;
  4421. vmtBeforeDestruction = vmtMethodStart+24;
  4422. vmtDefaultHandlerStr = vmtMethodStart+28;
  4423. \end{verbatim}
  4424. The constant names should be used, and never their values, because
  4425. the VMT table can change, breaking code that uses direct values.
  4426. The following constants will be used for the planned \var{variant} support:
  4427. \begin{verbatim}
  4428. varEmpty = $0000;
  4429. varNull = $0001;
  4430. varSmallint = $0002;
  4431. varInteger = $0003;
  4432. varSingle = $0004;
  4433. varDouble = $0005;
  4434. varCurrency = $0006;
  4435. varDate = $0007;
  4436. varOleStr = $0008;
  4437. varDispatch = $0009;
  4438. varError = $000A;
  4439. varBoolean = $000B;
  4440. varVariant = $000C;
  4441. varUnknown = $000D;
  4442. varByte = $0011;
  4443. varString = $0100;
  4444. varAny = $0101;
  4445. varTypeMask = $0FFF;
  4446. varArray = $2000;
  4447. varByRef = $4000;
  4448. \end{verbatim}
  4449. The following constants are used in the \var{TVarRec} record:
  4450. \begin{verbatim}
  4451. vtInteger = 0;
  4452. vtBoolean = 1;
  4453. vtChar = 2;
  4454. vtExtended = 3;
  4455. vtString = 4;
  4456. vtPointer = 5;
  4457. vtPChar = 6;
  4458. vtObject = 7;
  4459. vtClass = 8;
  4460. vtWideChar = 9;
  4461. vtPWideChar = 10;
  4462. vtAnsiString = 11;
  4463. vtCurrency = 12;
  4464. vtVariant = 13;
  4465. vtInterface = 14;
  4466. vtWideString = 15;
  4467. vtInt64 = 16;
  4468. vtQWord = 17;
  4469. \end{verbatim}
  4470. The \var{ExceptProc} is called when an unhandled exception occurs:
  4471. \begin{verbatim}
  4472. Const
  4473. ExceptProc : TExceptProc = Nil;
  4474. \end{verbatim}
  4475. It is set in the \file{objpas} unit, but it can be set by the programmer
  4476. to change the default exception handling.
  4477. The following constants are defined to describe the operating system's file
  4478. system:
  4479. \begin{verbatim}
  4480. LineEnding = #10;
  4481. LFNSupport = true;
  4482. DirectorySeparator = '/';
  4483. DriveSeparator = ':';
  4484. PathSeparator = ':';
  4485. FileNameCaseSensitive : Boolean = True;
  4486. \end{verbatim}
  4487. (the shown values are for \unix platforms, but will be different on other
  4488. platforms)
  4489. The meaning of the constants is the following:
  4490. \begin{description}
  4491. \item[LineEnding] End of line marker. This constant is used when writing end
  4492. of lines to text files.
  4493. \item[LFNSupport] This is \var{True} if the system supports long file names,
  4494. i.e. filenames that are not restricted to 8.3 characters.
  4495. \item[DirectorySeparator] The character that is used as a directory
  4496. separator, i.e. it appears between various parts of a path to a file.
  4497. \item[DriveSeparator] On systems that support drive letters, this character
  4498. separates the drive indication from the rest of a filename.
  4499. \item[PathSeparator] This character can be found between elements in a
  4500. series of paths (such as the contents of the \var{PATH} environment
  4501. variable.
  4502. \item[FileNameCaseSensitive] Indicates whether filenames are case sensitive.
  4503. \end{description}
  4504. When programming cross-platform, use these constants instead of hard-coded
  4505. characters. This will enhance portability of an application.
  4506. \subsection{Variables}
  4507. The following variables are defined and initialized in the system unit:
  4508. \begin{verbatim}
  4509. var
  4510. output,input,stderr : text;
  4511. exitproc : pointer;
  4512. exitcode : word;
  4513. stackbottom : Cardinal;
  4514. \end{verbatim}
  4515. The variables \var{ExitProc}, \var{exitcode} are used in the \fpc exit
  4516. scheme. It works similarly to the one in Turbo Pascal:
  4517. When a program halts (be it through the call of the \var{Halt} function or
  4518. \var{Exit} or through a run-time error), the exit mechanism checks the value
  4519. of \var{ExitProc}. If this one is non-\var{Nil}, it is set to \var{Nil}, and
  4520. the procedure is called. If the exit procedure exits, the value of ExitProc
  4521. is checked again. If it is non-\var{Nil} then the above steps are repeated.
  4522. So when an exit procedure must be installed, the old value of \var{ExitProc}
  4523. should be saved (it may be non-\var{Nil}, since other units could have set
  4524. it). In the exit procedure the value of \var{ExitProc} should be restored to
  4525. the previous value, such that if it was non-\var{Nil} the exit-procedure can be
  4526. called.
  4527. \FPCexample{ex98}
  4528. The \var{ErrorAddr} and \var{ExitCode} can be used to check for
  4529. error-conditions. If \var{ErrorAddr} is non-\var{Nil}, a run-time error has
  4530. occurred. If so, \var{ExitCode} contains the error code. If \var{ErrorAddr} is
  4531. \var{Nil}, then {ExitCode} contains the argument to \var{Halt} or 0 if the
  4532. program terminated normally.
  4533. \var{ExitCode} is always passed to the operating system as the exit-code of
  4534. the current process.
  4535. \begin{remark}
  4536. The maximum error code under \linux and \unix like operating systems is 127.
  4537. \end{remark}
  4538. Under \file{GO32}, the following constants are also defined :
  4539. \begin{verbatim}
  4540. const
  4541. seg0040 = $0040;
  4542. segA000 = $A000;
  4543. segB000 = $B000;
  4544. segB800 = $B800;
  4545. \end{verbatim}
  4546. These constants allow easy access to the bios/screen segment via mem/absolute.
  4547. The randomize function uses a seed stored in the \var{RandSeed} variable:
  4548. \begin{verbatim}
  4549. RandSeed : Cardinal;
  4550. \end{verbatim}
  4551. This variable is initialized in the initialization code of the system unit.
  4552. Other variables indicate the state of the application.
  4553. \begin{verbatim}
  4554. IsLibrary : boolean;
  4555. IsMultiThread : boolean;
  4556. \end{verbatim}
  4557. The \var{IsLibrary} variable is set to true if this module is a
  4558. shared library instead of an application. The \var{IsMultiThread}
  4559. variable is set to True if the application has spawned other
  4560. threads, otherwise, and by default, it is set to False.
  4561. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4562. % Functions and Procedures by category
  4563. \section{Function list by category}
  4564. What follows is a listing of the available functions, grouped by category.
  4565. For each function there is a reference to the page where the
  4566. function can be found:
  4567. \subsection{File handling}
  4568. Functions concerning input and output from and to file.
  4569. \begin{funclist}
  4570. \procref{Append}{Open a file in append mode}
  4571. \procref{Assign}{Assign a name to a file}
  4572. \procref{Blockread}{Read data from a file into memory}
  4573. \procref{Blockwrite}{Write data from memory to a file}
  4574. \procref{Close}{Close a file}
  4575. \funcref{Eof}{Check for end of file}
  4576. \funcref{Eoln}{Check for end of line}
  4577. \procref{Erase}{Delete file from disk}
  4578. \funcref{Filepos}{Position in file}
  4579. \funcref{Filesize}{Size of file}
  4580. \procref{Flush}{Write file buffers to disk}
  4581. \funcref{IOresult}{Return result of last file IO operation}
  4582. \procref{Read}{Read from file into variable}
  4583. \procref{Readln}{Read from file into variable and goto next line}
  4584. \procref{Rename}{Rename file on disk}
  4585. \procref{Reset}{Open file for reading}
  4586. \procref{Rewrite}{Open file for writing}
  4587. \procref{Seek}{Set file position}
  4588. \funcref{SeekEof}{Set file position to end of file}
  4589. \funcref{SeekEoln}{Set file position to end of line}
  4590. \procref{SetTextBuf}{Set size of file buffer}
  4591. \procref{Truncate}{Truncate the file at position}
  4592. \procref{Write}{Write variable to file}
  4593. \procref{WriteLn}{Write variable to file and append newline}
  4594. \end{funclist}
  4595. \subsection{Memory management}
  4596. Functions concerning memory issues.
  4597. \begin{funclist}
  4598. \funcref{Addr}{Return address of variable}
  4599. \funcref{Assigned}{Check if a pointer is valid}
  4600. \funcref{CompareByte}{Compare 2 memory buffers byte per byte}
  4601. \funcref{CompareChar}{Compare 2 memory buffers byte per byte}
  4602. \funcref{CompareDWord}{Compare 2 memory buffers byte per byte}
  4603. \funcref{CompareWord}{Compare 2 memory buffers byte per byte}
  4604. \funcref{CSeg}{Return code segment}
  4605. \procref{Dispose}{Free dynamically allocated memory}
  4606. \funcref{DSeg}{Return data segment}
  4607. \procref{FillByte}{Fill memory region with 8-bit pattern}
  4608. \procref{Fillchar}{Fill memory region with certain character}
  4609. \procref{FillDWord}{Fill memory region with 32-bit pattern}
  4610. \procref{Fillword}{Fill memory region with 16-bit pattern}
  4611. \procref{Freemem}{Release allocated memory}
  4612. \procref{Getmem}{Allocate new memory}
  4613. \procref{GetMemoryManager}{Return current memory manager}
  4614. \funcref{High}{Return highest index of open array or enumerated}
  4615. \funcref{IsMemoryManagerSet}{Is the memory manager set}
  4616. \funcref{Low}{Return lowest index of open array or enumerated}
  4617. \procref{Mark}{Mark current memory position}
  4618. \funcref{Maxavail}{Return size of largest free memory block}
  4619. \funcref{Memavail}{Return total available memory}
  4620. \procref{Move}{Move data from one location in memory to another}
  4621. \procrefl{MoveChar0}{MoveCharNull}{Move data till first zero character}
  4622. \procref{New}{Dynamically allocate memory for variable}
  4623. \funcref{Ofs}{Return offset of variable}
  4624. \funcref{Ptr}{Combine segment and offset to pointer}
  4625. \funcref{ReAllocMem}{Resize a memory block on the heap}
  4626. \procref{Release}{Release memory above mark point}
  4627. \funcref{Seg}{Return segment}
  4628. \procref{SetMemoryManager}{Set a memory manager}
  4629. \funcref{Sptr}{Return current stack pointer}
  4630. \funcref{SSeg}{Return stack segment register value}
  4631. \end{funclist}
  4632. \subsection{Mathematical routines}
  4633. Functions connected to calculating and coverting numbers.
  4634. \begin{funclist}
  4635. \funcref{Abs}{Calculate absolute value}
  4636. \funcref{Arctan}{Calculate inverse tangent}
  4637. \funcref{Cos}{Calculate cosine of angle}
  4638. \procref{Dec}{Decrease value of variable}
  4639. \funcref{Exp}{Exponentiate}
  4640. \funcref{Frac}{Return fractional part of floating point value}
  4641. \funcref{Hi}{Return high byte/word of value}
  4642. \procref{Inc}{Increase value of variable}
  4643. \funcref{Int}{Calculate integer part of floating point value}
  4644. \funcref{Ln}{Calculate logarithm}
  4645. \funcref{Lo}{Return low byte/word of value}
  4646. \funcref{Odd}{Is a value odd or even ? }
  4647. \funcref{Pi}{Return the value of pi}
  4648. \funcref{Power}{Raise float to integer power}
  4649. \funcref{Random}{Generate random number}
  4650. \procref{Randomize}{Initialize random number generator}
  4651. \funcref{Round}{Round floating point value to nearest integer number}
  4652. \funcref{Sin}{Calculate sine of angle}
  4653. \funcref{Sqr}{Calculate the square of a value}
  4654. \funcref{Sqrt}{Calculate the square root of a value}
  4655. \funcref{Swap}{Swap high and low bytes/words of a variable}
  4656. \funcref{Trunc}{Truncate a floating point value}
  4657. \end{funclist}
  4658. \subsection{String handling}
  4659. All things connected to string handling.
  4660. \begin{funclist}
  4661. \funcref{BinStr}{Construct binary representation of integer}
  4662. \funcref{Chr}{Convert ASCII code to character}
  4663. \funcref{Concat}{Concatenate two strings}
  4664. \funcref{Copy}{Copy part of a string}
  4665. \procref{Delete}{Delete part of a string}
  4666. \funcref{HexStr}{Construct hexadecimal representation of integer}
  4667. \procref{Insert}{Insert one string in another}
  4668. \funcref{Length}{Return length of string}
  4669. \funcref{Lowercase}{Convert string to all-lowercase}
  4670. \funcref{OctStr}{Construct octal representation of integer}
  4671. \funcref{Pos}{Calculate position of one string in another}
  4672. \procref{SetLength}{Set length of a string}
  4673. \procref{SetString}{Set contents and length of a string}
  4674. \procref{Str}{Convert number to string representation}
  4675. \funcref{StringOfChar}{Create string consisting of a number of characters}
  4676. \funcref{Upcase}{Convert string to all-uppercase}
  4677. \procref{Val}{Convert string to number}
  4678. \end{funclist}
  4679. \subsection{Operating System functions}
  4680. Functions that are connected to the operating system.
  4681. \begin{funclist}
  4682. \procref{Chdir}{Change working directory}
  4683. \procref{Getdir}{Return current working directory}
  4684. \procref{Halt}{Halt program execution}
  4685. \funcref{Paramcount}{Number of parameters with which program was called}
  4686. \funcref{Paramstr}{Retrieve parameters with which program was called}
  4687. \procref{Mkdir}{Make a directory}
  4688. \procref{Rmdir}{Remove a directory}
  4689. \procref{Runerror}{Abort program execution with error condition}
  4690. \end{funclist}
  4691. \subsection{Miscellaneous functions}
  4692. Functions that do not belong in one of the other categories.
  4693. \begin{funclist}
  4694. \procref{Assert}{Conditionally abort program with error}
  4695. \procref{Break}{Abort current loop}
  4696. \procref{Continue}{Next cycle in current loop}
  4697. \procref{Exclude}{Exclude an element from a set}
  4698. \procref{Exit}{Exit current function or procedure}
  4699. \procref{Include}{Include an element into a set}
  4700. \procref{LongJmp}{Jump to execution point}
  4701. \funcref{Ord}{Return ordinal value of enumerated type}
  4702. \funcref{Pred}{Return previous value of ordinal type}
  4703. \funcref{SetJmp}{Mark execution point for jump}
  4704. \funcref{SizeOf}{Return size of variable or type}
  4705. \funcref{Succ}{Return next value of ordinal type}
  4706. \end{funclist}
  4707. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4708. % Functions and Procedures
  4709. \section{Functions and Procedures}
  4710. \begin{function}{Abs}
  4711. \Declaration
  4712. Function Abs (X : Every numerical type) : Every numerical type;
  4713. \Description
  4714. \var{Abs} returns the absolute value of a variable. The result of the
  4715. function has the same type as its argument, which can be any numerical
  4716. type.
  4717. \Errors
  4718. None.
  4719. \SeeAlso
  4720. \seef{Round}
  4721. \end{function}
  4722. \FPCexample{ex1}
  4723. \begin{function}{Addr}
  4724. \Declaration
  4725. Function Addr (X : Any type) : Pointer;
  4726. \Description
  4727. \var{Addr} returns a pointer to its argument, which can be any type, or a
  4728. function or procedure name. The returned pointer isn't typed.
  4729. The same result can be obtained by the \var{@} operator, which can return a
  4730. typed pointer (\progref).
  4731. \Errors
  4732. None
  4733. \SeeAlso
  4734. \seef{SizeOf}
  4735. \end{function}
  4736. \FPCexample{ex2}
  4737. \begin{procedure}{Append}
  4738. \Declaration
  4739. Procedure Append (Var F : Text);
  4740. \Description
  4741. \var{Append} opens an existing file in append mode. Any data written to
  4742. \var{F} will be appended to the file. Only text files can be opened in
  4743. append mode. After a call to \var{Append}, the file \var{F} becomes
  4744. write-only.
  4745. File sharing is not taken into account when calling \var{Append}.
  4746. \Errors
  4747. If the file doesn't exist when appending, a run-time error will be generated.
  4748. This behaviour has changed on Windows and Linux platforms, where in versions
  4749. prior to 1.0.6, the file would be created in append mode.
  4750. \SeeAlso
  4751. \seep{Rewrite},\seep{Close}, \seep{Reset}
  4752. \end{procedure}
  4753. \FPCexample{ex3}
  4754. \begin{function}{Arctan}
  4755. \Declaration
  4756. Function Arctan (X : Real) : Real;
  4757. \Description
  4758. \var{Arctan} returns the Arctangent of \var{X}, which can be any Real type.
  4759. The resulting angle is in radial units.
  4760. \Errors
  4761. None
  4762. \SeeAlso
  4763. \seef{Sin}, \seef{Cos}
  4764. \end{function}
  4765. \FPCexample{ex4}
  4766. \begin{procedure}{Assert}
  4767. \Declaration
  4768. Procedure Assert(expr : Boolean [; const msg: string]);
  4769. \Description
  4770. With assertions on, \var{Assert} tests if \var{expr} is
  4771. false, and if so, aborts the application with a Runtime error
  4772. 227 and an optional error message in \var{msg}.
  4773. If \var{expr} is true, program execution continues normally.
  4774. If assertions are not enabled at compile time, this routine does
  4775. nothing, and no code is generated for the \var{Assert} call.
  4776. Enabling and disabling assertions at compile time is done via
  4777. the \var{\$C} or \var{\$ASSERTIONS} compiler switches. These are
  4778. global switches.
  4779. The default behavior of the assert call can be changed by
  4780. setting a new handler in the \var{AssertErrorProc} variable.
  4781. Sysutils overrides the default handler to raise a \var{EAssertionFailed}
  4782. exception.
  4783. \Errors
  4784. None.
  4785. \SeeAlso
  4786. \seep{Halt}, \seep{Runerror}
  4787. \end{procedure}
  4788. \begin{procedure}{Assign}
  4789. \Declaration
  4790. Procedure Assign (Var F; Name : String);
  4791. \Description
  4792. \var{Assign} assigns a name to \var{F}, which can be any file type.
  4793. This call doesn't open the file, it just assigns a name to a file variable,
  4794. and marks the file as closed.
  4795. \Errors
  4796. None.
  4797. \SeeAlso
  4798. \seep{Reset}, \seep{Rewrite}, \seep{Append}
  4799. \end{procedure}
  4800. \FPCexample{ex5}
  4801. \begin{function}{Assigned}
  4802. \Declaration
  4803. Function Assigned (P : Pointer) : Boolean;
  4804. \Description
  4805. \var{Assigned} returns \var{True} if \var{P} is non-nil
  4806. and retuns \var{False} of \var{P} is nil.
  4807. The main use of Assigned is that Procedural variables, method variables and
  4808. class-type variables also can be passed to \var{Assigned}.
  4809. \Errors
  4810. None
  4811. \SeeAlso
  4812. \seep{New}
  4813. \end{function}
  4814. \FPCexample{ex96}
  4815. \begin{function}{BinStr}
  4816. \Declaration
  4817. Function BinStr (Value : longint; cnt : byte) : String;
  4818. \Description
  4819. \var{BinStr} returns a string with the binary representation
  4820. of \var{Value}. The string has at most \var{cnt} characters.
  4821. (i.e. only the \var{cnt} rightmost bits are taken into account)
  4822. To have a complete representation of any longint-type value, 32
  4823. bits are needed, i.e. \var{cnt=32}
  4824. \Errors
  4825. None.
  4826. \SeeAlso
  4827. \seep{Str},\seep{Val},\seef{HexStr}, \seef{OctStr}
  4828. \end{function}
  4829. \FPCexample{ex82}
  4830. \begin{procedure}{Blockread}
  4831. \Declaration
  4832. Procedure Blockread (Var F : File; Var Buffer; Var Count : Longint [; var
  4833. Result : Longint]);
  4834. \Description
  4835. \var{Blockread} reads \var{count} or less records from file \var{F}. A
  4836. record is a block of bytes with size specified by the \seep{Rewrite} or
  4837. \seep{Reset} statement.
  4838. The result is placed in \var{Buffer}, which must contain enough room for
  4839. \var{Count} records. The function cannot read partial records.
  4840. If \var{Result} is specified, it contains the number of records actually
  4841. read. If \var{Result} isn't specified, and less than \var{Count} records were
  4842. read, a run-time error is generated. This behavior can be controlled by the
  4843. \var{\{\$i\}} switch.
  4844. \Errors
  4845. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  4846. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  4847. to check for errors.
  4848. \SeeAlso
  4849. \seep{Blockwrite}, \seep{Close}, \seep{Reset}, \seep{Assign}
  4850. \end{procedure}
  4851. \FPCexample{ex6}
  4852. \begin{procedure}{Blockwrite}
  4853. \Declaration
  4854. Procedure Blockwrite (Var F : File; Var Buffer; Var Count : Longint);
  4855. \Description
  4856. \var{BlockWrite} writes \var{count} records from \var{buffer} to the file
  4857. \var{F}.A record is a block of bytes with size specified by the \seep{Rewrite} or
  4858. \seep{Reset} statement.
  4859. If the records couldn't be written to disk, a run-time error is generated.
  4860. This behavior can be controlled by the \var{\{\$i\}} switch.
  4861. \Errors
  4862. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  4863. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  4864. to check for errors.
  4865. \SeeAlso
  4866. \seep{Blockread},\seep{Close}, \seep{Rewrite}, \seep{Assign}
  4867. \end{procedure}
  4868. For the example, see \seep{Blockread}.
  4869. \begin{procedure}{Break}
  4870. \Declaration
  4871. Procedure Break;
  4872. \Description
  4873. \var{Break} jumps to the statement following the end of the current
  4874. repetitive statement. The code between the \var{Break} call and
  4875. the end of the repetitive statement is skipped.
  4876. The condition of the repetitive statement is NOT evaluated.
  4877. This can be used with \var{For}, var{repeat} and \var{While} statements.
  4878. Note that while this is a procedure, \var{Break} is a reserved word
  4879. and hence cannot be redefined.
  4880. \Errors
  4881. None.
  4882. \SeeAlso
  4883. \seep{Continue}, \seep{Exit}
  4884. \end{procedure}
  4885. \FPCexample{ex87}
  4886. \begin{procedure}{Chdir}
  4887. \Declaration
  4888. Procedure Chdir (const S : string);
  4889. \Description
  4890. \var{Chdir} changes the working directory of the process to \var{S}.
  4891. \Errors
  4892. \Errors
  4893. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  4894. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  4895. to check for errors.
  4896. \SeeAlso
  4897. \seep{Mkdir}, \seep{Rmdir}
  4898. \end{procedure}
  4899. \FPCexample{ex7}
  4900. \begin{function}{Chr}
  4901. \Declaration
  4902. Function Chr (X : byte) : Char;
  4903. \Description
  4904. \var{Chr} returns the character which has ASCII value \var{X}.
  4905. \Errors
  4906. None.
  4907. \SeeAlso
  4908. \seef{Ord}, \seep{Str}
  4909. \end{function}
  4910. \FPCexample{ex8}
  4911. \begin{procedure}{Close}
  4912. \Declaration
  4913. Procedure Close (Var F : Anyfiletype);
  4914. \Description
  4915. \var{Close} flushes the buffer of the file \var{F} and closes \var{F}.
  4916. After a call to \var{Close}, data can no longer be read from or written to
  4917. \var{F}.
  4918. To reopen a file closed with \var{Close}, it isn't necessary to assign the
  4919. file again. A call to \seep{Reset} or \seep{Rewrite} is sufficient.
  4920. \Errors
  4921. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  4922. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  4923. to check for errors.
  4924. \SeeAlso
  4925. \seep{Assign}, \seep{Reset}, \seep{Rewrite}, \seep{Flush}
  4926. \end{procedure}
  4927. \FPCexample{ex9}
  4928. \begin{function}{CompareByte}
  4929. \Declaration
  4930. function CompareByte(var buf1,buf2;len:longint):longint;
  4931. \Description
  4932. \var{CompareByte} compares two memory regions \var{buf1},\var{buf2} on a
  4933. byte-per-byte basis for a total of \var{len} bytes.
  4934. The function returns one of the following values:
  4935. \begin{description}
  4936. \item[-1] if \var{buf1} and \var{buf2} contain different bytes
  4937. in the first \var{len} bytes, and the first such byte is smaller in \var{buf1}
  4938. than the byte at the same position in \var{buf2}.
  4939. \item[0] if the first \var{len} bytes in \var{buf1} and \var{buf2} are
  4940. equal.
  4941. \item [1] if \var{buf1} and \var{buf2} contain different bytes
  4942. in the first \var{len} bytes, and the first such byte is larger in \var{buf1}
  4943. than the byte at the same position in \var{buf2}.
  4944. \end{description}
  4945. \Errors
  4946. None.
  4947. \SeeAlso
  4948. \seef{CompareChar},\seef{CompareWord},\seef{CompareDWord}
  4949. \end{function}
  4950. \FPCexample{ex99}
  4951. \begin{function}{CompareChar}
  4952. \Declaration
  4953. function CompareChar(var buf1,buf2;len:longint):longint;
  4954. function CompareChar0(var buf1,buf2;len:longint):longint;
  4955. \Description
  4956. \var{CompareChar} compares two memory regions \var{buf1},\var{buf2} on a
  4957. character-per-character basis for a total of \var{len} characters.
  4958. The \var{CompareChar0} variant compares \var{len} bytes, or until
  4959. a zero character is found.
  4960. The function returns one of the following values:
  4961. \begin{description}
  4962. \item[-1] if \var{buf1} and \var{buf2} contain different characters
  4963. in the first \var{len} positions, and the first such character is smaller in \var{buf1}
  4964. than the character at the same position in \var{buf2}.
  4965. \item[0] if the first \var{len} characters in \var{buf1} and \var{buf2} are
  4966. equal.
  4967. \item [1] if \var{buf1} and \var{buf2} contain different characters
  4968. in the first \var{len} positions, and the first such character is larger in
  4969. \var{buf1} than the character at the same position in \var{buf2}.
  4970. \end{description}
  4971. \Errors
  4972. None.
  4973. \SeeAlso
  4974. \seef{CompareByte},\seef{CompareWord},\seef{CompareDWord}
  4975. \end{function}
  4976. \FPCexample{ex100}
  4977. \begin{function}{CompareDWord}
  4978. \Declaration
  4979. function CompareDWord(var buf1,buf2;len:longint):longint;
  4980. \Description
  4981. \var{CompareDWord} compares two memory regions \var{buf1},\var{buf2} on a
  4982. DWord-per-DWord basis for a total of \var{len} DWords. (A DWord is 4 bytes).
  4983. The function returns one of the following values:
  4984. \begin{description}
  4985. \item[-1] if \var{buf1} and \var{buf2} contain different DWords
  4986. in the first \var{len} DWords, and the first such DWord is smaller in \var{buf1}
  4987. than the DWord at the same position in \var{buf2}.
  4988. \item[0] if the first \var{len} DWords in \var{buf1} and \var{buf2} are
  4989. equal.
  4990. \item [1] if \var{buf1} and \var{buf2} contain different DWords
  4991. in the first \var{len} DWords, and the first such DWord is larger in \var{buf1}
  4992. than the DWord at the same position in \var{buf2}.
  4993. \end{description}
  4994. \Errors
  4995. None.
  4996. \SeeAlso
  4997. \seef{CompareChar},\seef{CompareByte},\seef{CompareWord},
  4998. \end{function}
  4999. \FPCexample{ex101}
  5000. \begin{function}{CompareWord}
  5001. \Declaration
  5002. function CompareWord(var buf1,buf2;len:longint):longint;
  5003. \Description
  5004. \var{CompareWord} compares two memory regions \var{buf1},\var{buf2} on a
  5005. Word-per-Word basis for a total of \var{len} Words. (A Word is 2 bytes).
  5006. The function returns one of the following values:
  5007. \begin{description}
  5008. \item[-1] if \var{buf1} and \var{buf2} contain different Words
  5009. in the first \var{len} Words, and the first such Word is smaller in \var{buf1}
  5010. than the Word at the same position in \var{buf2}.
  5011. \item[0] if the first \var{len} Words in \var{buf1} and \var{buf2} are
  5012. equal.
  5013. \item [1] if \var{buf1} and \var{buf2} contain different Words
  5014. in the first \var{len} Words, and the first such Word is larger in \var{buf1}
  5015. than the Word at the same position in \var{buf2}.
  5016. \end{description}
  5017. \Errors
  5018. None.
  5019. \SeeAlso
  5020. \seef{CompareChar},\seef{CompareByte},\seef{CompareWord},
  5021. \end{function}
  5022. \FPCexample{ex102}
  5023. \begin{function}{Concat}
  5024. \Declaration
  5025. Function Concat (S1,S2 [,S3, ... ,Sn]) : String;
  5026. \Description
  5027. \var{Concat} concatenates the strings \var{S1},\var{S2} etc. to one long
  5028. string. The resulting string is truncated at a length of 255 bytes.
  5029. The same operation can be performed with the \var{+} operation.
  5030. \Errors
  5031. None.
  5032. \SeeAlso
  5033. \seef{Copy}, \seep{Delete}, \seep{Insert}, \seef{Pos}, \seef{Length}
  5034. \end{function}
  5035. \FPCexample{ex10}
  5036. \begin{procedure}{Continue}
  5037. \Declaration
  5038. Procedure Continue;
  5039. \Description
  5040. \var{Continue} jumps to the end of the current repetitive statement.
  5041. The code between the \var{Continue} call and the end of the repetitive
  5042. statement is skipped. The condition of the repetitive statement is then
  5043. checked again.
  5044. This can be used with \var{For}, var{repeat} and \var{While} statements.
  5045. Note that while this is a procedure, \var{Continue} is a reserved word
  5046. and hence cannot be redefined.
  5047. \Errors
  5048. None.
  5049. \SeeAlso
  5050. \seep{Break}, \seep{Exit}
  5051. \end{procedure}
  5052. \FPCexample{ex86}
  5053. \begin{function}{Copy}
  5054. \Declaration
  5055. Function Copy (Const S : String;Index : Integer;Count : Integer) : String;
  5056. \Description
  5057. \var{Copy} returns a string which is a copy if the \var{Count} characters
  5058. in \var{S}, starting at position \var{Index}. If \var{Count} is larger than
  5059. the length of the string \var{S}, the result is truncated.
  5060. If \var{Index} is larger than the length of the string \var{S}, then an
  5061. empty string is returned.
  5062. \Errors
  5063. None.
  5064. \SeeAlso
  5065. \seep{Delete}, \seep{Insert}, \seef{Pos}
  5066. \end{function}
  5067. \FPCexample{ex11}
  5068. \begin{function}{Cos}
  5069. \Declaration
  5070. Function Cos (X : Real) : Real;
  5071. \Description
  5072. \var{Cos} returns the cosine of \var{X}, where X is an angle, in radians.
  5073. If the absolute value of the argument is larger than \var{2\^{}63}, then the
  5074. result is undefined.
  5075. \Errors
  5076. None.
  5077. \SeeAlso
  5078. \seef{Arctan}, \seef{Sin}
  5079. \end{function}
  5080. \FPCexample{ex12}
  5081. \begin{function}{CSeg}
  5082. \Declaration
  5083. Function CSeg : Word;
  5084. \Description
  5085. \var{CSeg} returns the Code segment register. In \fpc, it returns always a
  5086. zero, since \fpc is a 32 bit compiler.
  5087. \Errors
  5088. None.
  5089. \SeeAlso
  5090. \seef{DSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}
  5091. \end{function}
  5092. \FPCexample{ex13}
  5093. \begin{procedure}{Dec}
  5094. \Declaration
  5095. Procedure Dec (Var X : Any ordinal type[; Decrement : Any ordinal type]);
  5096. \Description
  5097. \var{Dec} decreases the value of \var{X} with \var{Decrement}.
  5098. If \var{Decrement} isn't specified, then 1 is taken as a default.
  5099. \Errors
  5100. A range check can occur, or an underflow error, if an attempt it made
  5101. to decrease \var{X} below its minimum value.
  5102. \SeeAlso
  5103. \seep{Inc}
  5104. \end{procedure}
  5105. \FPCexample{ex14}
  5106. \begin{procedure}{Delete}
  5107. \Declaration
  5108. Procedure Delete (var S : string;Index : Integer;Count : Integer);
  5109. \Description
  5110. \var{Delete} removes \var{Count} characters from string \var{S}, starting
  5111. at position \var{Index}. All characters after the delected characters are
  5112. shifted \var{Count} positions to the left, and the length of the string is adjusted.
  5113. \Errors
  5114. None.
  5115. \SeeAlso
  5116. \seef{Copy},\seef{Pos},\seep{Insert}
  5117. \end{procedure}
  5118. \FPCexample{ex15}
  5119. \begin{procedure}{Dispose}
  5120. \Declaration
  5121. Procedure Dispose (P : pointer);\\
  5122. Procedure Dispose (P : Typed Pointer; Des : Procedure);
  5123. \Description
  5124. The first form \var{Dispose} releases the memory allocated with a call to
  5125. \seep{New}. The pointer \var{P} must be typed. The released memory is
  5126. returned to the heap.
  5127. The second form of \var{Dispose} accepts as a first parameter a pointer
  5128. to an object type, and as a second parameter the name of a destructor
  5129. of this object. The destructor will be called, and the memory allocated
  5130. for the object will be freed.
  5131. \Errors
  5132. An runtime error will occur if the pointer doesn't point to a location in the
  5133. heap.
  5134. \SeeAlso
  5135. \seep{New}, \seep{Getmem}, \seep{Freemem}
  5136. \end{procedure}
  5137. \FPCexample{ex16}
  5138. \begin{function}{DSeg}
  5139. \Declaration
  5140. Function DSeg : Word;
  5141. \Description
  5142. \var{DSeg} returns the data segment register. In \fpc, it returns always a
  5143. zero, since \fpc is a 32 bit compiler.
  5144. \Errors
  5145. None.
  5146. \SeeAlso
  5147. \seef{CSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}
  5148. \end{function}
  5149. \FPCexample{ex17}
  5150. \begin{function}{Eof}
  5151. \Declaration
  5152. Function Eof [(F : Any file type)] : Boolean;
  5153. \Description
  5154. \var{Eof} returns \var{True} if the file-pointer has reached the end of the
  5155. file, or if the file is empty. In all other cases \var{Eof} returns
  5156. \var{False}.
  5157. If no file \var{F} is specified, standard input is assumed.
  5158. \Errors
  5159. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  5160. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  5161. to check for errors.
  5162. \SeeAlso
  5163. \seef{Eoln}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}
  5164. \end{function}
  5165. \FPCexample{ex18}
  5166. \begin{function}{Eoln}
  5167. \Declaration
  5168. Function Eoln [(F : Text)] : Boolean;
  5169. \Description
  5170. \var{Eof} returns \var{True} if the file pointer has reached the end of a
  5171. line, which is demarcated by a line-feed character (ASCII value 10), or if
  5172. the end of the file is reached.
  5173. In all other cases \var{Eof} returns \var{False}.
  5174. If no file \var{F} is specified, standard input is assumed.
  5175. It can only be used on files of type \var{Text}.
  5176. \Errors
  5177. None.
  5178. \SeeAlso
  5179. \seef{Eof}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}
  5180. \end{function}
  5181. \FPCexample{ex19}
  5182. \begin{procedure}{Erase}
  5183. \Declaration
  5184. Procedure Erase (Var F : Any file type);
  5185. \Description
  5186. \var{Erase} removes an unopened file from disk. The file should be
  5187. assigned with \var{Assign}, but not opened with \var{Reset} or \var{Rewrite}
  5188. \Errors
  5189. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  5190. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  5191. to check for errors.
  5192. \SeeAlso
  5193. \seep{Assign}
  5194. \end{procedure}
  5195. \FPCexample{ex20}
  5196. \begin{procedure}{Exclude}
  5197. \Declaration
  5198. Procedure Exclude (Var S : Any set type; E : Set element);
  5199. \Description
  5200. \var{Exclude} removes \var{E} from the set \var{S} if it is
  5201. included inthe set. E should be of the same type as the base type
  5202. of the set \var{S}.
  5203. Thus, the two following statements do the same thing:
  5204. \begin{verbatim}
  5205. S:=S-[E];
  5206. Exclude(S,E);
  5207. \end{verbatim}
  5208. \Errors
  5209. If the type of the element \var{E} is not equal to the base type of the
  5210. set \var{S}, the compiler will generate an error.
  5211. \SeeAlso
  5212. \seep{Include}
  5213. \end{procedure}
  5214. \FPCexample{ex111}
  5215. \begin{procedure}{Exit}
  5216. \Declaration
  5217. Procedure Exit ([Var X : return type )];
  5218. \Description
  5219. \var{Exit} exits the current subroutine, and returns control to the calling
  5220. routine. If invoked in the main program routine, exit stops the program.
  5221. The optional argument \var{X} allows to specify a return value, in the case
  5222. \var{Exit} is invoked in a function. The function result will then be
  5223. equal to \var{X}.
  5224. \Errors
  5225. None.
  5226. \SeeAlso
  5227. \seep{Halt}
  5228. \end{procedure}
  5229. \FPCexample{ex21}
  5230. \begin{function}{Exp}
  5231. \Declaration
  5232. Function Exp (Var X : Real) : Real;
  5233. \Description
  5234. \var{Exp} returns the exponent of \var{X}, i.e. the number \var{e} to the
  5235. power \var{X}.
  5236. \Errors
  5237. None.
  5238. \SeeAlso
  5239. \seef{Ln}, \seef{Power}
  5240. \end{function}
  5241. \FPCexample{ex22}
  5242. \begin{function}{Filepos}
  5243. \Declaration
  5244. Function Filepos (Var F : Any file type) : Longint;
  5245. \Description
  5246. \var{Filepos} returns the current record position of the file-pointer in file
  5247. \var{F}. It cannot be invoked with a file of type \var{Text}. A compiler error
  5248. will be generated if this is attempted.
  5249. \Errors
  5250. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  5251. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  5252. to check for errors.
  5253. \SeeAlso
  5254. \seef{Filesize}
  5255. \end{function}
  5256. \FPCexample{ex23}
  5257. \begin{function}{Filesize}
  5258. \Declaration
  5259. Function Filesize (Var F : Any file type) : Longint;
  5260. \Description
  5261. \var{Filesize} returns the total number of records in file \var{F}.
  5262. It cannot be invoked with a file of type \var{Text}. (under \linux and \unix, this
  5263. also means that it cannot be invoked on pipes).
  5264. If \var{F} is empty, 0 is returned.
  5265. \Errors
  5266. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  5267. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  5268. to check for errors.
  5269. \SeeAlso
  5270. \seef{Filepos}
  5271. \end{function}
  5272. \FPCexample{ex24}
  5273. \begin{procedure}{FillByte}
  5274. \Declaration
  5275. Procedure FillByte(var X;Count:longint;Value:byte);
  5276. \Description
  5277. \var{FillByte} fills the memory starting at \var{X} with \var{Count} bytes
  5278. with value equal to \var{Value}.
  5279. This is useful for quickly zeroing out a memory location. When the size of
  5280. the memory location to be filled out is a multiple of 2 bytes, it is better
  5281. to use \seep{Fillword}, and if it is a multiple of 4 bytes it is better
  5282. to use \seep{FillDWord}, these routines are optimized for their respective sizes.
  5283. \Errors
  5284. No checking on the size of \var{X} is done.
  5285. \SeeAlso
  5286. \seep{Fillchar}, \seep{FillDWord}, \seep{Fillword}, \seep{Move}
  5287. \end{procedure}
  5288. \FPCexample{ex102}
  5289. \begin{procedure}{Fillchar}
  5290. \Declaration
  5291. Procedure Fillchar (Var X;Count : Longint;Value : char or byte);;
  5292. \Description
  5293. \var{Fillchar} fills the memory starting at \var{X} with \var{Count} bytes
  5294. or characters with value equal to \var{Value}.
  5295. \Errors
  5296. No checking on the size of \var{X} is done.
  5297. \SeeAlso
  5298. \seep{Fillword}, \seep{Move}, \seep{FillByte}, \seep{FillDWord}
  5299. \end{procedure}
  5300. \FPCexample{ex25}
  5301. \begin{procedure}{FillDWord}
  5302. \Declaration
  5303. Procedure FillDWord (Var X;Count : Longint;Value : DWord);;
  5304. \Description
  5305. \var{Fillword} fills the memory starting at \var{X} with \var{Count} DWords
  5306. with value equal to \var{Value}. A DWord is 4 bytes in size.
  5307. \Errors
  5308. No checking on the size of \var{X} is done.
  5309. \SeeAlso
  5310. \seep{FillByte}, \seep{Fillchar}, \seep{Fillword}, \seep{Move}
  5311. \end{procedure}
  5312. \FPCexample{ex103}
  5313. \begin{procedure}{Fillword}
  5314. \Declaration
  5315. Procedure Fillword (Var X;Count : Longint;Value : Word);;
  5316. \Description
  5317. \var{Fillword} fills the memory starting at \var{X} with \var{Count} words
  5318. with value equal to \var{Value}. A word is 2 bytes in size.
  5319. \Errors
  5320. No checking on the size of \var{X} is done.
  5321. \SeeAlso
  5322. \seep{Fillchar}, \seep{Move}
  5323. \end{procedure}
  5324. \FPCexample{ex76}
  5325. \begin{procedure}{Flush}
  5326. \Declaration
  5327. Procedure Flush (Var F : Text);
  5328. \Description
  5329. \var{Flush} empties the internal buffer of an opened file \var{F} and writes the
  5330. contents to disk. The file is \textit{not} closed as a result of this call.
  5331. \Errors
  5332. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  5333. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  5334. to check for errors.
  5335. \SeeAlso
  5336. \seep{Close}
  5337. \end{procedure}
  5338. \FPCexample{ex26}
  5339. \begin{function}{Frac}
  5340. \Declaration
  5341. Function Frac (X : Real) : Real;
  5342. \Description
  5343. \var{Frac} returns the non-integer part of \var{X}.
  5344. \Errors
  5345. None.
  5346. \SeeAlso
  5347. \seef{Round}, \seef{Int}
  5348. \end{function}
  5349. \FPCexample{ex27}
  5350. \begin{procedure}{Freemem}
  5351. \Declaration
  5352. Procedure Freemem (Var P : pointer; Count : Longint);
  5353. \Description
  5354. \var{Freemem} releases the memory occupied by the pointer \var{P}, of size
  5355. \var{Count} (in bytes), and returns it to the heap. \var{P} should point to the memory
  5356. allocated to a dynamic variable.
  5357. \Errors
  5358. An error will occur when \var{P} doesn't point to the heap.
  5359. \SeeAlso
  5360. \seep{Getmem}, \seep{New}, \seep{Dispose}
  5361. \end{procedure}
  5362. \FPCexample{ex28}
  5363. \begin{procedure}{Getdir}
  5364. \Declaration
  5365. Procedure Getdir (drivenr : byte;var dir : string);
  5366. \Description
  5367. \var{Getdir} returns in \var{dir} the current directory on the drive
  5368. \var{drivenr}, where {drivenr} is 1 for the first floppy drive, 3 for the
  5369. first hard disk etc. A value of 0 returns the directory on the current disk.
  5370. On \linux and \unix systems, \var{drivenr} is ignored, as there is only one
  5371. directory tree.
  5372. \Errors
  5373. An error is returned under \dos, if the drive requested isn't ready.
  5374. \SeeAlso
  5375. \seep{Chdir}
  5376. \end{procedure}
  5377. \FPCexample{ex29}
  5378. \begin{procedure}{Getmem}
  5379. \Declaration
  5380. Procedure Getmem (var p : pointer;size : Longint);
  5381. \Description
  5382. \var{Getmem} reserves \var{Size} bytes memory on the heap, and returns a
  5383. pointer to this memory in \var{p}. If no more memory is available, nil is
  5384. returned.
  5385. \Errors
  5386. None.
  5387. \SeeAlso
  5388. \seep{Freemem}, \seep{Dispose}, \seep{New}
  5389. \end{procedure}
  5390. For an example, see \seep{Freemem}.
  5391. \begin{procedure}{GetMemoryManager}
  5392. \Declaration
  5393. procedure GetMemoryManager(var MemMgr: TMemoryManager);
  5394. \Description
  5395. \var{GetMemoryManager} stores the current Memory Manager record in
  5396. \var{MemMgr}.
  5397. \Errors
  5398. None.
  5399. \SeeAlso
  5400. \seep{SetMemoryManager}, \seef{IsMemoryManagerSet}.
  5401. \end{procedure}
  5402. For an example, see \progref.
  5403. \begin{procedure}{Halt}
  5404. \Declaration
  5405. Procedure Halt [(Errnum : byte)];
  5406. \Description
  5407. \var{Halt} stops program execution and returns control to the calling
  5408. program. The optional argument \var{Errnum} specifies an exit value. If
  5409. omitted, zero is returned.
  5410. \Errors
  5411. None.
  5412. \SeeAlso
  5413. \seep{Exit}
  5414. \end{procedure}
  5415. \FPCexample{ex30}
  5416. \begin{function}{HexStr}
  5417. \Declaration
  5418. Function HexStr (Value : longint; cnt : byte) : String;
  5419. Function HexStr (Value : int64; cnt : byte) : String;
  5420. \Description
  5421. \var{HexStr} returns a string with the hexadecimal representation
  5422. of \var{Value}. The string has exactly \var{cnt} charaters.
  5423. (i.e. only the \var{cnt} rightmost nibbles are taken into account)
  5424. To have a complete representation of a Longint-type value, 8
  5425. nibbles are needed, i.e. \var{cnt=8}.
  5426. \Errors
  5427. None.
  5428. \SeeAlso
  5429. \seep{Str}, \seep{Val}, \seef{BinStr}
  5430. \end{function}
  5431. \FPCexample{ex81}
  5432. \begin{function}{Hi}
  5433. \Declaration
  5434. Function Hi (X : Ordinal type) : Word or byte;
  5435. \Description
  5436. \var{Hi} returns the high byte or word from \var{X}, depending on the size
  5437. of X. If the size of X is 4, then the high word is returned. If the size is
  5438. 2 then the high byte is returned.
  5439. \var{Hi} cannot be invoked on types of size 1, such as byte or char.
  5440. \Errors
  5441. None
  5442. \SeeAlso
  5443. \seef{Lo}
  5444. \end{function}
  5445. \FPCexample{ex31}
  5446. \begin{function}{High}
  5447. \Declaration
  5448. Function High (Type identifier or variable reference) : Ordinal;
  5449. \Description
  5450. The return value of \var{High} depends on it's argument:
  5451. \begin{enumerate}
  5452. \item If the argument is an ordinal type, \var{High} returns the highest
  5453. value in the range of the given ordinal type.
  5454. \item If the argument is an array type or an array type variable then
  5455. \var{High} returns the highest possible value of it's index.
  5456. \item If the argument is an open array identifier in a function or
  5457. procedure, then \var{High} returns the highest index of the array, as if the
  5458. array has a zero-based index.
  5459. \end{enumerate}
  5460. The return type is always the same type as the type of the argument
  5461. (This can lead to some nasty surprises !).
  5462. \Errors
  5463. None.
  5464. \SeeAlso
  5465. \seef{Low}, \seef{Ord}, \seef{Pred}, \seef{Succ}
  5466. \end{function}
  5467. \FPCexample{ex80}
  5468. \begin{procedure}{Inc}
  5469. \Declaration
  5470. Procedure Inc (Var X : Any ordinal type[; Increment : Any ordinal type]);
  5471. \Description
  5472. \var{Inc} increases the value of \var{X} with \var{Increment}.
  5473. If \var{Increment} isn't specified, then 1 is taken as a default.
  5474. \Errors
  5475. If range checking is on, then A range check can occur, or an overflow
  5476. error, when an attempt is made to increase \var{X} over its maximum value.
  5477. \SeeAlso
  5478. \seep{Dec}
  5479. \end{procedure}
  5480. \FPCexample{ex32}
  5481. \begin{procedure}{Include}
  5482. \Declaration
  5483. Procedure Include (Var S : Any set type; E : Set element);
  5484. \Description
  5485. \var{Include} includes \var{E} in the set \var{S} if it is
  5486. not yet part of the set. E should be of the same type as the base type
  5487. of the set \var{S}.
  5488. Thus, the two following statements do the same thing:
  5489. \begin{verbatim}
  5490. S:=S+[E];
  5491. Include(S,E);
  5492. \end{verbatim}
  5493. \Errors
  5494. If the type of the element \var{E} is not equal to the base type of the
  5495. set \var{S}, the compiler will generate an error.
  5496. \SeeAlso
  5497. \seep{Exclude}
  5498. \end{procedure}
  5499. For an example, see \seep{Exclude}
  5500. \begin{function}{IndexByte}
  5501. \Declaration
  5502. function IndexByte(var buf;len:longint;b:byte):longint;
  5503. \Description
  5504. \var{IndexByte} searches the memory at \var{buf} for maximally \var{len}
  5505. positions for the byte \var{b} and returns it's position if it found one.
  5506. If \var{b} is not found then -1 is returned.
  5507. The position is zero-based.
  5508. \Errors
  5509. \var{Buf} and \var{Len} are not checked to see if they are valid values.
  5510. \SeeAlso
  5511. \seef{IndexChar}, \seef{IndexDWord}, \seef{IndexWord}, \seef{CompareByte}
  5512. \end{function}
  5513. \FPCexample{ex105}
  5514. \begin{function}{IndexChar}
  5515. \Declaration
  5516. function IndexChar(var buf;len:longint;b:char):longint;
  5517. \Declaration
  5518. function IndexChar0(var buf;len:longint;b:char):longint;
  5519. \Description
  5520. \var{IndexChar} searches the memory at \var{buf} for maximally \var{len}
  5521. positions for the character \var{b} and returns it's position if it found one.
  5522. If \var{b} is not found then -1 is returned.
  5523. The position is zero-based. The \var{IndexChar0} variant stops looking if
  5524. a null character is found, and returns -1 in that case.
  5525. \Errors
  5526. \var{Buf} and \var{Len} are not checked to see if they are valid values.
  5527. \SeeAlso
  5528. \seef{IndexByte}, \seef{IndexDWord}, \seef{IndexWord}, \seef{CompareChar}
  5529. \end{function}
  5530. \FPCexample{ex108}
  5531. \begin{function}{IndexDWord}
  5532. \Declaration
  5533. function IndexDWord(var buf;len:longint;DW:DWord):longint;
  5534. \Description
  5535. \var{IndexChar} searches the memory at \var{buf} for maximally \var{len}
  5536. positions for the DWord \var{DW} and returns it's position if it found one.
  5537. If \var{DW} is not found then -1 is returned.
  5538. The position is zero-based.
  5539. \Errors
  5540. \var{Buf} and \var{Len} are not checked to see if they are valid values.
  5541. \SeeAlso
  5542. \seef{IndexByte}, \seef{IndexChar}, \seef{IndexWord}, \seef{CompareDWord}
  5543. \end{function}
  5544. \FPCexample{ex106}
  5545. \begin{function}{IndexWord}
  5546. \Declaration
  5547. function IndexWord(var buf;len:longint;W:word):longint;
  5548. \Description
  5549. \var{IndexChar} searches the memory at \var{buf} for maximally \var{len}
  5550. positions for the Word \var{W} and returns it's position if it found one.
  5551. If \var{W} is not found then -1 is returned.
  5552. \Errors
  5553. \var{Buf} and \var{Len} are not checked to see if they are valid values.
  5554. \SeeAlso
  5555. \seef{IndexByte}, \seef{IndexDWord}, \seef{IndexChar}, \seef{CompareWord}
  5556. \end{function}
  5557. \FPCexample{ex107}
  5558. \begin{procedure}{Insert}
  5559. \Declaration
  5560. Procedure Insert (Const Source : String;var S : String;Index : Integer);
  5561. \Description
  5562. \var{Insert} inserts string \var{Source} in string \var{S}, at position
  5563. \var{Index}, shifting all characters after \var{Index} to the right. The
  5564. resulting string is truncated at 255 characters, if needed. (i.e. for
  5565. shortstrings)
  5566. \Errors
  5567. None.
  5568. \SeeAlso
  5569. \seep{Delete}, \seef{Copy}, \seef{Pos}
  5570. \end{procedure}
  5571. \FPCexample{ex33}
  5572. \begin{function}{IsMemoryManagerSet}
  5573. \Declaration
  5574. function IsMemoryManagerSet: Boolean;
  5575. \Description
  5576. \var{IsMemoryManagerSet} will return \var{True} if the memory manager has
  5577. been set to another value than the system heap manager, it will return
  5578. \var{False} otherwise.
  5579. \Errors
  5580. None.
  5581. \SeeAlso
  5582. \seep{SetMemoryManager}, \seep{GetMemoryManager}
  5583. \end{function}
  5584. \begin{function}{Int}
  5585. \Declaration
  5586. Function Int (X : Real) : Real;
  5587. \Description
  5588. \var{Int} returns the integer part of any Real \var{X}, as a Real.
  5589. \Errors
  5590. None.
  5591. \SeeAlso
  5592. \seef{Frac}, \seef{Round}
  5593. \end{function}
  5594. \FPCexample{ex34}
  5595. \begin{function}{IOresult}
  5596. \Declaration
  5597. Function IOresult : Word;
  5598. \Description
  5599. IOresult contains the result of any input/output call, when the
  5600. \var{\{\$i-\}} compiler directive is active, disabling IO checking.
  5601. When the flag is read, it is reset to zero.
  5602. If \var{IOresult} is zero, the operation completed successfully. If
  5603. non-zero, an error occurred. The following errors can occur:
  5604. \dos errors :
  5605. \begin{description}
  5606. \item [2\ ] File not found.
  5607. \item [3\ ] Path not found.
  5608. \item [4\ ] Too many open files.
  5609. \item [5\ ] Access denied.
  5610. \item [6\ ] Invalid file handle.
  5611. \item [12\ ] Invalid file-access mode.
  5612. \item [15\ ] Invalid disk number.
  5613. \item [16\ ] Cannot remove current directory.
  5614. \item [17\ ] Cannot rename across volumes.
  5615. \end{description}
  5616. I/O errors :
  5617. \begin{description}
  5618. \item [100\ ] Error when reading from disk.
  5619. \item [101\ ] Error when writing to disk.
  5620. \item [102\ ] File not assigned.
  5621. \item [103\ ] File not open.
  5622. \item [104\ ] File not opened for input.
  5623. \item [105\ ] File not opened for output.
  5624. \item [106\ ] Invalid number.
  5625. \end{description}
  5626. Fatal errors :
  5627. \begin{description}
  5628. \item [150\ ] Disk is write protected.
  5629. \item [151\ ] Unknown device.
  5630. \item [152\ ] Drive not ready.
  5631. \item [153\ ] Unknown command.
  5632. \item [154\ ] CRC check failed.
  5633. \item [155\ ] Invalid drive specified..
  5634. \item [156\ ] Seek error on disk.
  5635. \item [157\ ] Invalid media type.
  5636. \item [158\ ] Sector not found.
  5637. \item [159\ ] Printer out of paper.
  5638. \item [160\ ] Error when writing to device.
  5639. \item [161\ ] Error when reading from device.
  5640. \item [162\ ] Hardware failure.
  5641. \end{description}
  5642. \Errors
  5643. None.
  5644. \SeeAlso
  5645. All I/O functions.
  5646. \end{function}
  5647. \FPCexample{ex35}
  5648. \begin{function}{Length}
  5649. \Declaration
  5650. Function Length (S : String) : Integer;
  5651. \Description
  5652. \var{Length} returns the length of the string \var{S}, which is limited
  5653. to 255 for shortstrings. If the strings \var{S} is empty, 0 is returned.
  5654. {\em Note:} The length of the string \var{S} is stored in \var{S[0]} for
  5655. shortstrings only. The \var{Length} fuction should always be used on
  5656. ansistrings and widestrings.
  5657. \Errors
  5658. None.
  5659. \SeeAlso
  5660. \seef{Pos}
  5661. \end{function}
  5662. \FPCexample{ex36}
  5663. \begin{function}{Ln}
  5664. \Declaration
  5665. Function Ln (X : Real) : Real;
  5666. \Description
  5667. \var{Ln} returns the natural logarithm of the Real parameter \var{X}.
  5668. \var{X} must be positive.
  5669. \Errors
  5670. An run-time error will occur when \var{X} is negative.
  5671. \SeeAlso
  5672. \seef{Exp}, \seef{Power}
  5673. \end{function}
  5674. \FPCexample{ex37}
  5675. \begin{function}{Lo}
  5676. \Declaration
  5677. Function Lo (O : Word or Longint) : Byte or Word;
  5678. \Description
  5679. \var{Lo} returns the low byte of its argument if this is of type
  5680. \var{Integer} or
  5681. \var{Word}. It returns the low word of its argument if this is of type
  5682. \var{Longint} or \var{Cardinal}.
  5683. \Errors
  5684. None.
  5685. \SeeAlso
  5686. \seef{Ord}, \seef{Chr}, \seef{Hi}
  5687. \end{function}
  5688. \FPCexample{ex38}
  5689. \begin{procedure}{LongJmp}
  5690. \Declaration
  5691. Procedure LongJmp (Var env : Jmp\_Buf; Value : Longint);
  5692. \Description
  5693. \var{LongJmp} jumps to the adress in the \var{env} \var{jmp\_buf},
  5694. and restores the registers that were stored in it at the corresponding
  5695. \seef{SetJmp} call.
  5696. In effect, program flow will continue at the \var{SetJmp} call, which will
  5697. return \var{value} instead of 0. If a \var{value} equal to zero is passed,
  5698. it will be converted to 1 before passing it on. The call will not return, so it must be
  5699. used with extreme care.
  5700. This can be used for error recovery, for instance when a segmentation fault
  5701. occurred.
  5702. \Errors
  5703. None.
  5704. \SeeAlso
  5705. \seef{SetJmp}
  5706. \end{procedure}
  5707. For an example, see \seef{SetJmp}
  5708. \begin{function}{Low}
  5709. \Declaration
  5710. Function Low (Type identifier or variable reference) : Longint;
  5711. \Description
  5712. The return value of \var{Low} depends on it's argument:
  5713. \begin{enumerate}
  5714. \item If the argument is an ordinal type, \var{Low} returns the lowest
  5715. value in the range of the given ordinal type.
  5716. \item If the argument is an array type or an array type variable then
  5717. \var{Low} returns the lowest possible value of it's index.
  5718. \end{enumerate}
  5719. The return type is always the same type as the type of the argument
  5720. \Errors
  5721. None.
  5722. \SeeAlso
  5723. \seef{High}, \seef{Ord}, \seef{Pred}, \seef{Succ}
  5724. \end{function}
  5725. for an example, see \seef{High}.
  5726. \begin{function}{Lowercase}
  5727. \Declaration
  5728. Function Lowercase (C : Char or String) : Char or String;
  5729. \Description
  5730. \var{Lowercase} returns the lowercase version of its argument \var{C}.
  5731. If its argument is a string, then the complete string is converted to
  5732. lowercase. The type of the returned value is the same as the type of the
  5733. argument.
  5734. \Errors
  5735. None.
  5736. \SeeAlso
  5737. \seef{Upcase}
  5738. \end{function}
  5739. \FPCexample{ex73}
  5740. \begin{procedure}{Mark}
  5741. \Declaration
  5742. Procedure Mark (Var P : Pointer);
  5743. \Description
  5744. This routine is here for compatibility with Turbo Pascal, but
  5745. it is not implemented and currently does nothing.
  5746. \Errors
  5747. None.
  5748. \SeeAlso
  5749. \seep{Getmem}, \seep{Freemem}, \seep{New}, \seep{Dispose}, \seef{Maxavail}
  5750. \end{procedure}
  5751. \begin{function}{Maxavail}
  5752. \Declaration
  5753. Function Maxavail : Longint;
  5754. \Description
  5755. \var{Maxavail} returns the size, in bytes, of the biggest free memory block in
  5756. the heap.
  5757. \begin{remark}
  5758. The heap grows dynamically if more memory is needed than is available.
  5759. \end{remark}
  5760. \Errors
  5761. None.
  5762. \SeeAlso
  5763. \seep{Release}, \seef{Memavail},\seep{Freemem}, \seep{Getmem}
  5764. \end{function}
  5765. \FPCexample{ex40}
  5766. \begin{function}{Memavail}
  5767. \Declaration
  5768. Function Memavail : Longint;
  5769. \Description
  5770. \var{Memavail} returns the size, in bytes, of the free heap memory.
  5771. \begin{remark}
  5772. The heap grows dynamically if more memory is needed than is available. The
  5773. heap size is not equal to the size of the memory available to the
  5774. operating system, it is internal to the programs created by \fpc.
  5775. \end{remark}
  5776. \Errors
  5777. None.
  5778. \SeeAlso
  5779. \seef{Maxavail},\seep{Freemem}, \seep{Getmem}
  5780. \end{function}
  5781. \FPCexample{ex41}
  5782. \begin{procedure}{Mkdir}
  5783. \Declaration
  5784. Procedure Mkdir (const S : string);
  5785. \Description
  5786. \var{Mkdir} creates a new directory \var{S}.
  5787. \Errors
  5788. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  5789. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  5790. to check for errors.
  5791. \SeeAlso
  5792. \seep{Chdir}, \seep{Rmdir}
  5793. \end{procedure}
  5794. For an example, see \seep{Rmdir}.
  5795. \begin{procedure}{Move}
  5796. \Declaration
  5797. Procedure Move (var Source,Dest;Count : Longint);
  5798. \Description
  5799. \var{Move} moves \var{Count} bytes from \var{Source} to \var{Dest}.
  5800. \Errors
  5801. If either \var{Dest} or \var{Source} is outside the accessible memory for
  5802. the process, then a run-time error will be generated.
  5803. \SeeAlso
  5804. \seep{Fillword}, \seep{Fillchar}
  5805. \end{procedure}
  5806. \FPCexample{ex42}
  5807. \begin{procedurel}{MoveChar0}{MoveCharNull}
  5808. \Declaration
  5809. procedure MoveChar0(var Src,Dest;Count:longint);
  5810. \Description
  5811. \var{MoveChar0} moves \var{Count} bytes from \var{Src} to \var{Dest}, and
  5812. stops moving if a zero character is found.
  5813. \Errors
  5814. No checking is done to see if \var{Count} stays within the memory allocated
  5815. to the process.
  5816. \SeeAlso
  5817. \seep{Move}
  5818. \end{procedurel}
  5819. \FPCexample{ex109}
  5820. \begin{procedure}{New}
  5821. \Declaration
  5822. Procedure New (Var P : Pointer[, Constructor]);
  5823. \Description
  5824. \var{New} allocates a new instance of the type pointed to by \var{P}, and
  5825. puts the address in \var{P}.
  5826. If P is an object, then it is possible to
  5827. specify the name of the constructor with which the instance will be created.
  5828. \Errors
  5829. If not enough memory is available, \var{Nil} will be returned.
  5830. \SeeAlso
  5831. \seep{Dispose}, \seep{Freemem}, \seep{Getmem}, \seef{Memavail},
  5832. \seef{Maxavail}
  5833. \end{procedure}
  5834. For an example, see \seep{Dispose}.
  5835. \begin{function}{Odd}
  5836. \Declaration
  5837. Function Odd (X : Longint) : Boolean;
  5838. \Description
  5839. \var{Odd} returns \var{True} if \var{X} is odd, or \var{False} otherwise.
  5840. \Errors
  5841. None.
  5842. \SeeAlso
  5843. \seef{Abs}, \seef{Ord}
  5844. \end{function}
  5845. \FPCexample{ex43}
  5846. \begin{function}{OctStr}
  5847. \Declaration
  5848. Function OctStr (Value : longint; cnt : byte) : String;
  5849. Function OctStr (Value : int64; cnt : byte) : String;
  5850. \Description
  5851. \var{OctStr} returns a string with the octal representation
  5852. of \var{Value}. The string has exactly \var{cnt} charaters.
  5853. \Errors
  5854. None.
  5855. \SeeAlso
  5856. \seep{Str}, \seep{Val}, \seef{BinStr}, \seef{HexStr}
  5857. \end{function}
  5858. \FPCexample{ex112}
  5859. \begin{function}{Ofs}
  5860. \Declaration
  5861. Function Ofs (Var X) : Longint;
  5862. \Description
  5863. \var{Ofs} returns the offset of the address of a variable.
  5864. This function is only supported for compatibility. In \fpc, it
  5865. returns always the complete address of the variable, since \fpc is a 32 bit
  5866. compiler.
  5867. \Errors
  5868. None.
  5869. \SeeAlso
  5870. \seef{DSeg}, \seef{CSeg}, \seef{Seg}, \seef{Ptr}
  5871. \end{function}
  5872. \FPCexample{ex44}
  5873. \begin{function}{Ord}
  5874. \Declaration
  5875. Function Ord (X : Any ordinal type) : Longint;
  5876. \Description
  5877. \var{Ord} returns the Ordinal value of a ordinal-type variable \var{X}.
  5878. \Errors
  5879. None.
  5880. \SeeAlso
  5881. \seef{Chr}, \seef{Succ}, \seef{Pred}, \seef{High}, \seef{Low}
  5882. \end{function}
  5883. \FPCexample{ex45}
  5884. \begin{function}{Paramcount}
  5885. \Declaration
  5886. Function Paramcount : Longint;
  5887. \Description
  5888. \var{Paramcount} returns the number of command-line arguments. If no
  5889. arguments were given to the running program, \var{0} is returned.
  5890. \Errors
  5891. None.
  5892. \SeeAlso
  5893. \seef{Paramstr}
  5894. \end{function}
  5895. \FPCexample{ex46}
  5896. \begin{function}{Paramstr}
  5897. \Declaration
  5898. Function Paramstr (L : Longint) : String;
  5899. \Description
  5900. \var{Paramstr} returns the \var{L}-th command-line argument. \var{L} must
  5901. be between \var{0} and \var{Paramcount}, these values included.
  5902. The zeroth argument is the path and file name with which the program was
  5903. started.
  5904. The command-line parameters will be truncated to a length of 255,
  5905. even though the operating system may support bigger command-lines.
  5906. The \var{Objpas} unit (used in \var{objfpc} or \var{delphi} mode) define versions
  5907. of \var{Paramstr} which return the full-length command-line arguments.
  5908. When the complete command-line must be accessed, the \var{argv} pointer
  5909. should be used to retrieve the real values of the command-line parameters.
  5910. \Errors
  5911. None.
  5912. \SeeAlso
  5913. \seef{Paramcount}
  5914. \end{function}
  5915. For an example, see \seef{Paramcount}.
  5916. \begin{function}{Pi}
  5917. \Declaration
  5918. Function Pi : Real;
  5919. \Description
  5920. \var{Pi} returns the value of Pi (3.1415926535897932385).
  5921. \Errors
  5922. None.
  5923. \SeeAlso
  5924. \seef{Cos}, \seef{Sin}
  5925. \end{function}
  5926. \FPCexample{ex47}
  5927. \begin{function}{Pos}
  5928. \Declaration
  5929. Function Pos (Const Substr : String;Const S : String) : Integer;
  5930. \Description
  5931. \var{Pos} returns the index of \var{Substr} in \var{S}, if \var{S} contains
  5932. \var{Substr}. In case \var{Substr} isn't found, \var{0} is returned.
  5933. The search is case-sensitive.
  5934. \Errors
  5935. None
  5936. \SeeAlso
  5937. \seef{Length}, \seef{Copy}, \seep{Delete}, \seep{Insert}
  5938. \end{function}
  5939. \FPCexample{ex48}
  5940. \begin{function}{Power}
  5941. \Declaration
  5942. Function Power (base,expon : Real) : Real;
  5943. \Description
  5944. \var{Power} returns the value of \var{base} to the power \var{expon}.
  5945. \var{Base} and \var{expon} can be of type Longint, in which case the
  5946. result will also be a Longint.
  5947. The function actually returns \var{Exp(expon*Ln(base))}
  5948. \Errors
  5949. None.
  5950. \SeeAlso
  5951. \seef{Exp}, \seef{Ln}
  5952. \end{function}
  5953. \FPCexample{ex78}
  5954. \begin{function}{Pred}
  5955. \Declaration
  5956. Function Pred (X : Any ordinal type) : Same type;
  5957. \Description
  5958. \var{Pred} returns the element that precedes the element that was passed
  5959. to it. If it is applied to the first value of the ordinal type, and the
  5960. program was compiled with range checking on (\var{\{\$R+\}}, then a run-time
  5961. error will be generated.
  5962. \Errors
  5963. Run-time error 201 is generated when the result is out of
  5964. range.
  5965. \SeeAlso
  5966. \seef{Ord}, \seef{Pred}, \seef{High}, \seef{Low}
  5967. \end{function}
  5968. for an example, see \seef{Ord}
  5969. \begin{function}{Ptr}
  5970. \Declaration
  5971. Function Ptr (Sel,Off : Longint) : Pointer;
  5972. \Description
  5973. \var{Ptr} returns a pointer, pointing to the address specified by
  5974. segment \var{Sel} and offset \var{Off}.
  5975. \begin{remark}
  5976. \begin{enumerate}
  5977. \item In the 32-bit flat-memory model supported by \fpc, this
  5978. function is obsolete.
  5979. \item The returned address is simply the offset.
  5980. \end{enumerate}
  5981. \end{remark}
  5982. \Errors
  5983. None.
  5984. \SeeAlso
  5985. \seef{Addr}
  5986. \end{function}
  5987. \FPCexample{ex59}
  5988. \begin{function}{Random}
  5989. \Declaration
  5990. Function Random [(L : Longint)] : Longint or Real;
  5991. \Description
  5992. \var{Random} returns a random number larger or equal to \var{0} and
  5993. strictly less than \var{L}.
  5994. If the argument \var{L} is omitted, a Real number between 0 and 1 is returned.
  5995. (0 included, 1 excluded)
  5996. \Errors
  5997. None.
  5998. \SeeAlso
  5999. \seep{Randomize}
  6000. \end{function}
  6001. \FPCexample{ex49}
  6002. \begin{procedure}{Randomize}
  6003. \Declaration
  6004. Procedure Randomize ;
  6005. \Description
  6006. \var{Randomize} initializes the random number generator of \fpc, by giving
  6007. a value to \var{Randseed}, calculated with the system clock.
  6008. \Errors
  6009. None.
  6010. \SeeAlso
  6011. \seef{Random}
  6012. \end{procedure}
  6013. For an example, see \seef{Random}.
  6014. \begin{procedure}{Read}
  6015. \Declaration
  6016. Procedure Read ([Var F : Any file type], V1 [, V2, ... , Vn]);
  6017. \Description
  6018. \var{Read} reads one or more values from a file \var{F}, and stores the
  6019. result in \var{V1}, \var{V2}, etc.; If no file \var{F} is specified, then
  6020. standard input is read.
  6021. If \var{F} is of type \var{Text}, then the variables \var{V1, V2} etc. must be
  6022. of type \var{Char}, \var{Integer}, \var{Real}, \var{String} or \var{PChar}.
  6023. If \var{F} is a typed file, then each of the variables must be of the type
  6024. specified in the declaration of \var{F}. Untyped files are not allowed as an
  6025. argument.
  6026. \Errors
  6027. If no data is available, a run-time error is generated. This behavior can
  6028. be controlled with the \var{\{\$i\}} compiler switch.
  6029. \SeeAlso
  6030. \seep{Readln}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}
  6031. \end{procedure}
  6032. \FPCexample{ex50}
  6033. \begin{procedure}{Readln}
  6034. \Declaration
  6035. Procedure Readln [Var F : Text], V1 [, V2, ... , Vn]);
  6036. \Description
  6037. \var{Read} reads one or more values from a file \var{F}, and stores the
  6038. result in \var{V1}, \var{V2}, etc. After that it goes to the next line in
  6039. the file (defined by the \var{LineFeed (\#10)} character).
  6040. If no file \var{F} is specified, then standard input is read.
  6041. The variables \var{V1, V2} etc. must be of type \var{Char}, \var{Integer},
  6042. \var{Real}, \var{String} or \var{PChar}.
  6043. \Errors
  6044. If no data is available, a run-time error is generated. This behavior can
  6045. be controlled with the \var{\{\$i\}} compiler switch.
  6046. \SeeAlso
  6047. \seep{Read}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}
  6048. \end{procedure}
  6049. For an example, see \seep{Read}.
  6050. \begin{functionl}{Real2Double}{RealToDouble}
  6051. \Declaration
  6052. Function Real2Double(r : real48) : double;
  6053. \Description
  6054. The \var{Real2Double} function converts a \tp style real (6 bytes long) to
  6055. a native \fpc double type. It can be used e.g. to read old binary TP files with
  6056. FPC and convert them to Free Pacal binary files.
  6057. Note that the assignment operator has been overloaded so a \var{Real48} type
  6058. can be assigned directly to a double or extended.
  6059. \Errors
  6060. None.
  6061. \SeeAlso
  6062. \end{functionl}
  6063. \FPCexample{ex110}
  6064. \begin{procedure}{Release}
  6065. \Declaration
  6066. Procedure Release (Var P : pointer);
  6067. \Description
  6068. This routine is here for compatibility with Turbo Pascal, but
  6069. it is not implemented and currently does nothing.
  6070. \Errors
  6071. None.
  6072. \SeeAlso
  6073. \seep{Mark}, \seef{Memavail}, \seef{Maxavail}, \seep{Getmem}, \seep{Freemem}
  6074. \seep{New}, \seep{Dispose}
  6075. \end{procedure}
  6076. \begin{procedure}{Rename}
  6077. \Declaration
  6078. Procedure Rename (Var F : Any Filetype; Const S : String);
  6079. \Description
  6080. \var{Rename} changes the name of the assigned file \var{F} to \var{S}.
  6081. \var{F}
  6082. must be assigned, but not opened.
  6083. \Errors
  6084. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  6085. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  6086. to check for errors.
  6087. \SeeAlso
  6088. \seep{Erase}
  6089. \end{procedure}
  6090. \FPCexample{ex77}
  6091. \begin{procedure}{Reset}
  6092. \Declaration
  6093. Procedure Reset (Var F : Any File Type[; L : Longint]);
  6094. \Description
  6095. \var{Reset} opens a file \var{F} for reading. \var{F} can be any file type.
  6096. If \var{F} is a text file, or refers to standard I/O (e.g : '') then it is
  6097. opened read-only, otherwise it is opened using the mode specified in
  6098. \var{filemode}.
  6099. If \var{F} is an untyped file, the record size can be specified in
  6100. the optional parameter \var{L}. A default value of 128 is used.
  6101. File sharing is not taken into account when calling \var{Reset}.
  6102. \Errors
  6103. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  6104. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  6105. to check for errors.
  6106. \SeeAlso
  6107. \seep{Rewrite}, \seep{Assign}, \seep{Close}, \seep{Append}
  6108. \end{procedure}
  6109. \FPCexample{ex51}
  6110. \begin{procedure}{Rewrite}
  6111. \Declaration
  6112. Procedure Rewrite (Var F : Any File Type[; L : Longint]);
  6113. \Description
  6114. \var{Rewrite} opens a file \var{F} for writing. \var{F} can be any file type.
  6115. If \var{F} is an untyped or typed file, then it is opened for reading and
  6116. writing. If \var{F} is an untyped file, the record size can be specified in
  6117. the optional parameter \var{L}. Default a value of 128 is used.
  6118. if \var{Rewrite} finds a file with the same name as \var{F}, this file is
  6119. truncated to length \var{0}. If it doesn't find such a file, a new file is
  6120. created.
  6121. Contrary to \tp, \fpc opens the file with mode \var{fmoutput}. If it should
  6122. be opened in \var{fminout} mode, an extra call to \seep{Reset} is needed.
  6123. File sharing is not taken into account when calling \var{Rewrite}.
  6124. \Errors
  6125. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  6126. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  6127. to check for errors.
  6128. \SeeAlso
  6129. \seep{Reset}, \seep{Assign}, \seep{Close}, \seep{Flush}, \seep{Append}
  6130. \end{procedure}
  6131. \FPCexample{ex52}
  6132. \begin{procedure}{Rmdir}
  6133. \Declaration
  6134. Procedure Rmdir (const S : string);
  6135. \Description
  6136. \var{Rmdir} removes the directory \var{S}.
  6137. \Errors
  6138. \Errors
  6139. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  6140. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  6141. to check for errors.
  6142. \SeeAlso
  6143. \seep{Chdir}, \seep{Mkdir}
  6144. \end{procedure}
  6145. \FPCexample{ex53}
  6146. \begin{function}{Round}
  6147. \Declaration
  6148. Function Round (X : Real) : Longint;
  6149. \Description
  6150. \var{Round} rounds \var{X} to the closest integer, which may be bigger or
  6151. smaller than \var{X}.
  6152. \Errors
  6153. None.
  6154. \SeeAlso
  6155. \seef{Frac}, \seef{Int}, \seef{Trunc}
  6156. \end{function}
  6157. \FPCexample{ex54}
  6158. \begin{procedure}{Runerror}
  6159. \Declaration
  6160. Procedure Runerror (ErrorCode : Word);
  6161. \Description
  6162. \var{Runerror} stops the execution of the program, and generates a
  6163. run-time error \var{ErrorCode}.
  6164. \Errors
  6165. None.
  6166. \SeeAlso
  6167. \seep{Exit}, \seep{Halt}
  6168. \end{procedure}
  6169. \FPCexample{ex55}
  6170. \begin{procedure}{Seek}
  6171. \Declaration
  6172. Procedure Seek (Var F; Count : Longint);
  6173. \Description
  6174. \var{Seek} sets the file-pointer for file \var{F} to record Nr. \var{Count}.
  6175. The first record in a file has \var{Count=0}. F can be any file type, except
  6176. \var{Text}. If \var{F} is an untyped file, with no record size specified in
  6177. \seep{Reset} or \seep{Rewrite}, 128 is assumed.
  6178. \Errors
  6179. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  6180. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  6181. to check for errors.
  6182. \SeeAlso
  6183. \seef{Eof}, \seef{SeekEof}, \seef{SeekEoln}
  6184. \end{procedure}
  6185. \FPCexample{ex56}
  6186. \begin{function}{SeekEof}
  6187. \Declaration
  6188. Function SeekEof [(Var F : text)] : Boolean;
  6189. \Description
  6190. \var{SeekEof} returns \var{True} is the file-pointer is at the end of the
  6191. file. It ignores all whitespace.
  6192. Calling this function has the effect that the file-position is advanced
  6193. until the first non-whitespace character or the end-of-file marker is
  6194. reached.
  6195. If the end-of-file marker is reached, \var{True} is returned. Otherwise,
  6196. False is returned.
  6197. If the parameter \var{F} is omitted, standard \var{Input} is assumed.
  6198. \Errors
  6199. A run-time error is generated if the file \var{F} isn't opened.
  6200. \SeeAlso
  6201. \seef{Eof}, \seef{SeekEoln}, \seep{Seek}
  6202. \end{function}
  6203. \FPCexample{ex57}
  6204. \begin{function}{SeekEoln}
  6205. \Declaration
  6206. Function SeekEoln [(Var F : text)] : Boolean;
  6207. \Description
  6208. \var{SeekEoln} returns \var{True} is the file-pointer is at the end of the
  6209. current line. It ignores all whitespace.
  6210. Calling this function has the effect that the file-position is advanced
  6211. until the first non-whitespace character or the end-of-line marker is
  6212. reached.
  6213. If the end-of-line marker is reached, \var{True} is returned. Otherwise,
  6214. False is returned.
  6215. The end-of-line marker is defined as \var{\#10}, the LineFeed character.
  6216. If the parameter \var{F} is omitted, standard \var{Input} is assumed.
  6217. \Errors
  6218. A run-time error is generated if the file \var{F} isn't opened.
  6219. \SeeAlso
  6220. \seef{Eof}, \seef{SeekEof}, \seep{Seek}
  6221. \end{function}
  6222. \FPCexample{ex58}
  6223. \begin{function}{Seg}
  6224. \Declaration
  6225. Function Seg (Var X) : Longint;
  6226. \Description
  6227. \var{Seg} returns the segment of the address of a variable.
  6228. This function is only supported for compatibility. In \fpc, it
  6229. returns always 0, since \fpc is a 32 bit compiler, segments have no meaning.
  6230. \Errors
  6231. None.
  6232. \SeeAlso
  6233. \seef{DSeg}, \seef{CSeg}, \seef{Ofs}, \seef{Ptr}
  6234. \end{function}
  6235. \FPCexample{ex60}
  6236. \begin{procedure}{SetMemoryManager}
  6237. \Declaration
  6238. procedure SetMemoryManager(const MemMgr: TMemoryManager);
  6239. \Description
  6240. \var{SetMemoryManager} sets the current memory manager record to
  6241. \var{MemMgr}.
  6242. \Errors
  6243. None.
  6244. \SeeAlso
  6245. \seep{GetMemoryManager}, \seef{IsMemoryManagerSet}
  6246. \end{procedure}
  6247. For an example, see \progref.
  6248. \begin{function}{SetJmp}
  6249. \Declaration
  6250. Function SetJmp (Var Env : Jmp\_Buf) : Longint;
  6251. \Description
  6252. \var{SetJmp} fills \var{env} with the necessary data for a jump back to the
  6253. point where it was called. It returns zero if called in this way.
  6254. If the function returns nonzero, then it means that a call to \seep{LongJmp}
  6255. with \var{env} as an argument was made somewhere in the program.
  6256. \Errors
  6257. None.
  6258. \SeeAlso
  6259. \seep{LongJmp}
  6260. \end{function}
  6261. \FPCexample{ex79}
  6262. \begin{procedure}{SetLength}
  6263. \Declaration
  6264. Procedure SetLength(var S : String; Len : Longint);
  6265. \Description
  6266. \var{SetLength} sets the length of the string \var{S} to \var{Len}. \var{S}
  6267. can be an ansistring, a short string or a widestring.
  6268. For \var{ShortStrings}, \var{Len} can maximally be 255. For \var{AnsiStrings}
  6269. it can have any value. For \var{AnsiString} strings, \var{SetLength} {\em
  6270. must} be used to set the length of the string.
  6271. \Errors
  6272. None.
  6273. \SeeAlso
  6274. \seef{Length}
  6275. \end{procedure}
  6276. \FPCexample{ex85}
  6277. \begin{procedure}{SetString}
  6278. \Declaration
  6279. Procedure SetString(var S : String; Buf : PChar; Len : Longint);
  6280. \Description
  6281. \var{SetString} sets the length of the string \var{S} to \var{Len} and
  6282. if \var{Buf} is non-nil, copies \var{Len} characters from \var{Buf}
  6283. into \var{S}. \var{S} can be an ansistring, a short string or a widestring.
  6284. For \var{ShortStrings}, \var{Len} can maximally be 255.
  6285. \Errors
  6286. None.
  6287. \SeeAlso
  6288. \seep{SetLength}
  6289. \end{procedure}
  6290. \begin{procedure}{SetTextBuf}
  6291. \Declaration
  6292. Procedure SetTextBuf (Var f : Text; Var Buf[; Size : Word]);
  6293. \Description
  6294. \var{SetTextBuf} assigns an I/O buffer to a text file. The new buffer is
  6295. located at \var{Buf} and is \var{Size} bytes long. If \var{Size} is omitted,
  6296. then \var{SizeOf(Buf)} is assumed.
  6297. The standard buffer of any text file is 128 bytes long. For heavy I/O
  6298. operations this may prove too slow. The \var{SetTextBuf} procedure allows
  6299. to set a bigger buffer for the IO of the application, thus reducing the number of
  6300. system calls, and thus reducing the load on the system resources.
  6301. The maximum size of the newly assigned buffer is 65355 bytes.
  6302. \begin{remark}
  6303. \begin{itemize}
  6304. \item Never assign a new buffer to an opened file. A
  6305. new buffer can be assigned immediately after a call to \seep{Rewrite}, \seep{Reset} or
  6306. \var{Append}, but not after the file was read from/written to. This may cause
  6307. loss of data. If a new buffer must be assigned after read/write
  6308. operations have been performed, the file should be flushed first.
  6309. This will ensure that the current buffer is emptied.
  6310. \item Take care that the assigned buffer is always valid. If a local variable is
  6311. assigned as a buffer, then after the program exits the local program block,
  6312. the buffer will no longer be valid, and stack problems may occur.
  6313. \end{itemize}
  6314. \end{remark}
  6315. \Errors
  6316. No checking on \var{Size} is done.
  6317. \SeeAlso
  6318. \seep{Assign}, \seep{Reset}, \seep{Rewrite}, \seep{Append}
  6319. \end{procedure}
  6320. \FPCexample{ex61}
  6321. \begin{function}{Sin}
  6322. \Declaration
  6323. Function Sin (X : Real) : Real;
  6324. \Description
  6325. \var{Sin} returns the sine of its argument \var{X}, where \var{X} is an
  6326. angle in radians.
  6327. If the absolute value of the argument is larger than \var{2\^{}63}, then the
  6328. result is undefined.
  6329. \Errors
  6330. None.
  6331. \SeeAlso
  6332. \seef{Cos}, \seef{Pi}, \seef{Exp}, \seef{Ln}
  6333. \end{function}
  6334. \FPCexample{ex62}
  6335. \begin{function}{SizeOf}
  6336. \Declaration
  6337. Function SizeOf (X : Any Type) : Longint;
  6338. \Description
  6339. \var{SizeOf} returns the size, in bytes, of any variable or type-identifier.
  6340. \begin{remark}
  6341. This isn't really a RTL function. Its result is calculated at
  6342. compile-time, and hard-coded in the executable.
  6343. \end{remark}
  6344. \Errors
  6345. None.
  6346. \SeeAlso
  6347. \seef{Addr}
  6348. \end{function}
  6349. \FPCexample{ex63}
  6350. \begin{function}{Sptr}
  6351. \Declaration
  6352. Function Sptr : Pointer;
  6353. \Description
  6354. \var{Sptr} returns the current stack pointer.
  6355. \Errors
  6356. None.
  6357. \SeeAlso
  6358. \seef{SSeg}
  6359. \end{function}
  6360. \FPCexample{ex64}
  6361. \begin{function}{Sqr}
  6362. \Declaration
  6363. Function Sqr (X : Real) : Real;
  6364. \Description
  6365. \var{Sqr} returns the square of its argument \var{X}.
  6366. \Errors
  6367. None.
  6368. \SeeAlso
  6369. \seef{Sqrt}, \seef{Ln}, \seef{Exp}
  6370. \end{function}
  6371. \FPCexample{ex65}
  6372. \begin{function}{Sqrt}
  6373. \Declaration
  6374. Function Sqrt (X : Real) : Real;
  6375. \Description
  6376. \var{Sqrt} returns the square root of its argument \var{X}, which must be
  6377. positive.
  6378. \Errors
  6379. If \var{X} is negative, then a run-time error is generated.
  6380. \SeeAlso
  6381. \seef{Sqr}, \seef{Ln}, \seef{Exp}
  6382. \end{function}
  6383. \FPCexample{ex66}
  6384. \begin{function}{SSeg}
  6385. \Declaration
  6386. Function SSeg : Longint;
  6387. \Description
  6388. \var{SSeg} returns the Stack Segment. This function is only
  6389. supported for compatibility reasons, as \var{Sptr} returns the
  6390. correct contents of the stackpointer.
  6391. \Errors
  6392. None.
  6393. \SeeAlso
  6394. \seef{Sptr}
  6395. \end{function}
  6396. \FPCexample{ex67}
  6397. \begin{procedure}{Str}
  6398. \Declaration
  6399. Procedure Str (Var X[:NumPlaces[:Decimals]]; Var S : String);
  6400. \Description
  6401. \var{Str} returns a string which represents the value of X. X can be any
  6402. numerical type.
  6403. The optional \var{NumPLaces} and \var{Decimals} specifiers control the
  6404. formatting of the string.
  6405. \Errors
  6406. None.
  6407. \SeeAlso
  6408. \seep{Val}
  6409. \end{procedure}
  6410. \FPCexample{ex68}
  6411. \begin{function}{StringOfChar}
  6412. \Declaration
  6413. Function StringOfChar(c : char;l : Integer) : String;
  6414. \Description
  6415. \var{StringOfChar} creates a new String of length \var{l} and fills
  6416. it with the character \var{c}.
  6417. It is equivalent to the following calls:
  6418. \begin{verbatim}
  6419. SetLength(StringOfChar,l);
  6420. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  6421. \end{verbatim}
  6422. \Errors
  6423. None.
  6424. \SeeAlso
  6425. \seep{SetLength}
  6426. \end{function}
  6427. \FPCexample{ex97}
  6428. \begin{function}{Succ}
  6429. \Declaration
  6430. Function Succ (X : Any ordinal type) : Same type;
  6431. \Description
  6432. \var{Succ} returns the element that succeeds the element that was passed
  6433. to it. If it is applied to the last value of the ordinal type, and the
  6434. program was compiled with range checking on (\var{\{\$R+\}}), then a run-time
  6435. error will be generated.
  6436. \Errors
  6437. Run-time error 201 is generated when the result is out of
  6438. range.
  6439. \SeeAlso
  6440. \seef{Ord}, \seef{Pred}, \seef{High}, \seef{Low}
  6441. \end{function}
  6442. for an example, see \seef{Ord}.
  6443. \begin{function}{Swap}
  6444. \Declaration
  6445. Function Swap (X) : Type of X;
  6446. \Description
  6447. \var{Swap} swaps the high and low order bytes of \var{X} if \var{X} is of
  6448. type \var{Word} or \var{Integer}, or swaps the high and low order words of
  6449. \var{X} if \var{X} is of type \var{Longint} or \var{Cardinal}.
  6450. The return type is the type of \var{X}
  6451. \Errors
  6452. None.
  6453. \SeeAlso
  6454. \seef{Lo}, \seef{Hi}
  6455. \end{function}
  6456. \FPCexample{ex69}
  6457. \begin{function}{Trunc}
  6458. \Declaration
  6459. Function Trunc (X : Real) : Longint;
  6460. \Description
  6461. \var{Trunc} returns the integer part of \var{X},
  6462. which is always smaller than (or equal to) \var{X} in absolute value.
  6463. \Errors
  6464. None.
  6465. \SeeAlso
  6466. \seef{Frac}, \seef{Int}, \seef{Round}
  6467. \end{function}
  6468. \FPCexample{ex70}
  6469. \begin{procedure}{Truncate}
  6470. \Declaration
  6471. Procedure Truncate (Var F : file);
  6472. \Description
  6473. \var{Truncate} truncates the (opened) file \var{F} at the current file
  6474. position.
  6475. \Errors
  6476. Depending on the state of the \var{\{\$I\}} switch, a runtime error can be
  6477. generated if there is an error. In the \var{\{\$I-\}} state, use \var{IOResult}
  6478. to check for errors.
  6479. \SeeAlso
  6480. \seep{Append}, \seef{Filepos},
  6481. \seep{Seek}
  6482. \end{procedure}
  6483. \FPCexample{ex71}
  6484. \begin{function}{Upcase}
  6485. \Declaration
  6486. Function Upcase (C : Char or string) : Char or String;
  6487. \Description
  6488. \var{Upcase} returns the uppercase version of its argument \var{C}.
  6489. If its argument is a string, then the complete string is converted to
  6490. uppercase. The type of the returned value is the same as the type of the
  6491. argument.
  6492. \Errors
  6493. None.
  6494. \SeeAlso
  6495. \seef{Lowercase}
  6496. \end{function}
  6497. \FPCexample{ex72}
  6498. \begin{procedure}{Val}
  6499. \Declaration
  6500. Procedure Val (const S : string;var V;var Code : word);
  6501. \Description
  6502. \var{Val} converts the value represented in the string \var{S} to a numerical
  6503. value, and stores this value in the variable \var{V}, which
  6504. can be of type \var{Longint}, \var{Real} and \var{Byte}.
  6505. If the conversion isn't succesfull, then the parameter \var{Code} contains
  6506. the index of the character in \var{S} which prevented the conversion.
  6507. The string \var{S} is allowed to contain spaces in the beginning.
  6508. The string \var{S} can contain a number in decimal, hexadecimal, binary
  6509. or octal format, as described in the language reference.
  6510. \Errors
  6511. If the conversion doesn't succeed, the value of \var{Code} indicates the
  6512. position where the conversion went wrong.
  6513. \SeeAlso
  6514. \seep{Str}
  6515. \end{procedure}
  6516. \FPCexample{ex74}
  6517. \begin{procedure}{Write}
  6518. \Declaration
  6519. Procedure Write ([Var F : Any filetype;] V1 [; V2; ... , Vn)];
  6520. \Description
  6521. \var{Write} writes the contents of the variables \var{V1}, \var{V2} etc. to
  6522. the file \var{F}. \var{F} can be a typed file, or a \var{Text} file.
  6523. If \var{F} is a typed file, then the variables \var{V1}, \var{V2} etc. must
  6524. be of the same type as the type in the declaration of \var{F}. Untyped files
  6525. are not allowed.
  6526. If the parameter \var{F} is omitted, standard output is assumed.
  6527. If \var{F} is of type \var{Text}, then the necessary conversions are done
  6528. such that the output of the variables is in human-readable format.
  6529. This conversion is done for all numerical types. Strings are printed exactly
  6530. as they are in memory, as well as \var{PChar} types.
  6531. The format of the numerical conversions can be influenced through
  6532. the following modifiers:
  6533. \var{ OutputVariable : NumChars [: Decimals ] }
  6534. This will print the value of \var{OutputVariable} with a minimum of
  6535. \var{NumChars} characters, from which \var{Decimals} are reserved for the
  6536. decimals. If the number cannot be represented with \var{NumChars} characters,
  6537. \var{NumChars} will be increased, until the representation fits. If the
  6538. representation requires less than \var{NumChars} characters then the output
  6539. is filled up with spaces, to the left of the generated string, thus
  6540. resulting in a right-aligned representation.
  6541. If no formatting is specified, then the number is written using its natural
  6542. length, with nothing in front of it if it's positive, and a minus sign if
  6543. it's negative.
  6544. Real numbers are, by default, written in scientific notation.
  6545. \Errors
  6546. If an error occurs, a run-time error is generated. This behavior can be
  6547. controlled with the \var{\{\$i\}} switch.
  6548. \SeeAlso
  6549. \seep{WriteLn}, \seep{Read}, \seep{Readln}, \seep{Blockwrite}
  6550. \end{procedure}
  6551. \begin{procedure}{WriteLn}
  6552. \Declaration
  6553. Procedure WriteLn [([Var F : Text;] [V1 [; V2; ... , Vn)]];
  6554. \Description
  6555. \var{WriteLn} does the same as \seep{Write} for text files, and emits a
  6556. Carriage Return - LineFeed character pair after that.
  6557. If the parameter \var{F} is omitted, standard output is assumed.
  6558. If no variables are specified, a Carriage Return - LineFeed character pair
  6559. is emitted, resulting in a new line in the file \var{F}.
  6560. \begin{remark}
  6561. Under \linux and \unix, the Carriage Return character is omitted, as
  6562. customary in Unix environments.
  6563. \end{remark}
  6564. \Errors
  6565. If an error occurs, a run-time error is generated. This behavior can be
  6566. controlled with the \var{\{\$i\}} switch.
  6567. \SeeAlso
  6568. \seep{Write}, \seep{Read}, \seep{Readln}, \seep{Blockwrite}
  6569. \end{procedure}
  6570. \FPCexample{ex75}
  6571. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6572. % The objpas unit
  6573. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6574. \chapter{The OBJPAS unit}
  6575. The \file{objpas} unit is meant for compatibility with Object Pascal as
  6576. implemented by Delphi. The unit is loaded automatically by the \fpc compiler
  6577. whenever the \var{Delphi} or \var{objfpc} more is entered, either through
  6578. the command line switches \var{-Sd} or \var{-Sh} or with the \var{\{\$MODE
  6579. DELPHI\}} or \var{\{\$MODE OBJFPC\}} directives.
  6580. It redefines some basic pascal types, introduces some functions for
  6581. compatibility with Delphi's system unit, and introduces some methods for the
  6582. management of the resource string tables.
  6583. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6584. % Tytpes
  6585. \section{Types}
  6586. The \file{objpas} unit redefines two integer types, for compatibity with
  6587. Delphi:
  6588. \begin{verbatim}
  6589. type
  6590. smallint = system.integer;
  6591. integer = system.longint;
  6592. \end{verbatim}
  6593. The resource string tables can be managed with a callback function which the
  6594. user must provide: \var{TResourceIterator}.
  6595. \begin{verbatim}
  6596. Type
  6597. TResourceIterator =
  6598. Function (Name,Value : AnsiString;Hash : Longint):AnsiString;
  6599. \end{verbatim}
  6600. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6601. % Functions and procedures
  6602. \section{Functions and Procedures}
  6603. \begin{procedure}{AssignFile}
  6604. \Declaration
  6605. Procedure AssignFile(Var f: FileType;Name: Character type);
  6606. \Description
  6607. \var{AssignFile} is completely equivalent to the system unit's \seep{Assign}
  6608. function: It assigns \var{Name} to a function of any type (\var{FileType}
  6609. can be \var{Text} or a typed or untyped \var{File} variable). \var{Name} can
  6610. be a string, a single character or a \var{PChar}.
  6611. It is most likely introduced to avoid confusion between the regular
  6612. \seep{Assign} function and the \var{Assign} method of \var{TPersistent}
  6613. in the Delphi VCL.
  6614. \Errors
  6615. None.
  6616. \SeeAlso
  6617. \seep{CloseFile}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}, \seep{Append}
  6618. \end{procedure}
  6619. \FPCexample{ex88}
  6620. \begin{procedure}{CloseFile}
  6621. \Declaration
  6622. Procedure CloseFile(Var F: FileType);
  6623. \Description
  6624. \var{CloseFile} flushes and closes a file \var{F} of any file type.
  6625. \var{F} can be \var{Text} or a typed or untyped \var{File} variable.
  6626. After a call to \var{CloseFile}, any attempt to write to the file \var{F}
  6627. will result in an error.
  6628. It is most likely introduced to avoid confusion between the regular
  6629. \seep{Close} function and the \var{Close} method of \var{TForm}
  6630. in the Delphi VCL.
  6631. \Errors
  6632. None.
  6633. \SeeAlso
  6634. \seep{Close}, \seep{AssignFile}, \seep{Reset}, \seep{Rewrite}, \seep{Append}
  6635. \end{procedure}
  6636. for an example, see \seep{AssignFile}.
  6637. \begin{procedurel}{Freemem}{objpasfreemem}
  6638. \Declaration
  6639. Procedure FreeMem(Var p:pointer[;Size:Longint]);
  6640. \Description
  6641. \var{FreeMem} releases the memory reserved by a call to
  6642. \seepl{GetMem}{objpasgetmem}. The (optional) \var{Size} parameter is
  6643. ignored, since the object pascal version of \var{GetMem} stores the amount
  6644. of memory that was requested.
  6645. Be sure not to release memory that was not obtained with the \var{Getmem}
  6646. call in \file{Objpas}. Normally, this should not happen, since objpas
  6647. changes the default memory manager to it's own memory manager.
  6648. \Errors
  6649. None.
  6650. \SeeAlso
  6651. \seep{Freemem}, \seepl{GetMem}{objpasgetmem}, \seep{Getmem}
  6652. \end{procedurel}
  6653. \FPCexample{ex89}
  6654. \begin{procedurel}{Getmem}{objpasgetmem}
  6655. \Declaration
  6656. Procedure Getmem(Var P:pointer;Size:Longint);
  6657. \Description
  6658. \var{GetMem} reserves \var{Size} bytes of memory on the heap and returns
  6659. a pointer to it in \var{P}. \var{Size} is stored at offset -4 of the
  6660. result, and is used to release the memory again. \var{P} can be a typed or
  6661. untyped pointer.
  6662. Be sure to release this memory with the \seepl{FreeMem}{objpasfreemem} call
  6663. defined in the \file{objpas} unit.
  6664. \Errors
  6665. In case no more memory is available, and no more memory could be obtained
  6666. from the system a run-time error is triggered.
  6667. \SeeAlso
  6668. \seepl{FreeMem}{objpasfreemem}, \seep{Getmem}.
  6669. \end{procedurel}
  6670. For an example, see \seepl{FreeMem}{objpasfreemem}.
  6671. \begin{function}{GetResourceStringCurrentValue}
  6672. \Declaration
  6673. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  6674. \Description
  6675. \var{GetResourceStringCurrentValue} returns the current value of the
  6676. resourcestring in table \var{TableIndex} with index \var{StringIndex}.
  6677. The current value depends on the system of internationalization that was
  6678. used, and which language is selected when the program is executed.
  6679. \Errors
  6680. If either \var{TableIndex} or \var{StringIndex} are out of range, then
  6681. a empty string is returned.
  6682. \SeeAlso
  6683. \seep{SetResourceStrings},
  6684. \seef{GetResourceStringDefaultValue},
  6685. \seef{GetResourceStringHash},
  6686. \seef{GetResourceStringName},
  6687. \seef{ResourceStringTableCount},
  6688. \seef{ResourceStringCount}
  6689. \end{function}
  6690. \FPCexample{ex90}
  6691. \begin{function}{GetResourceStringDefaultValue}
  6692. \Declaration
  6693. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString
  6694. \Description
  6695. \var{GetResourceStringDefaultValue} returns the default value of the
  6696. resourcestring in table \var{TableIndex} with index \var{StringIndex}.
  6697. The default value is the value of the string that appears in the source code
  6698. of the programmer, and is compiled into the program.
  6699. \Errors
  6700. If either \var{TableIndex} or \var{StringIndex} are out of range, then
  6701. a empty string is returned.
  6702. \Errors
  6703. \SeeAlso
  6704. \seep{SetResourceStrings},
  6705. \seef{GetResourceStringCurrentValue},
  6706. \seef{GetResourceStringHash},
  6707. \seef{GetResourceStringName},
  6708. \seef{ResourceStringTableCount},
  6709. \seef{ResourceStringCount}
  6710. \end{function}
  6711. \FPCexample{ex91}
  6712. \begin{function}{GetResourceStringHash}
  6713. \Declaration
  6714. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  6715. \Description
  6716. \var{GetResourceStringHash} returns the hash value associated with the
  6717. resource string in table \var{TableIndex}, with index \var{StringIndex}.
  6718. The hash value is calculated from the default value of the resource string
  6719. in a manner that gives the same result as the GNU \file{gettext} mechanism.
  6720. It is stored in the resourcestring tables, so retrieval is faster than
  6721. actually calculating the hash for each string.
  6722. \Errors
  6723. If either \var{TableIndex} or \var{StringIndex} is zero, 0 is returned.
  6724. \SeeAlso
  6725. \seef{Hash}
  6726. \seep{SetResourceStrings},
  6727. \seef{GetResourceStringDefaultValue},
  6728. \seef{GetResourceStringHash},
  6729. \seef{GetResourceStringName},
  6730. \seef{ResourceStringTableCount},
  6731. \seef{ResourceStringCount}
  6732. \end{function}
  6733. For an example, see \seef{Hash}.
  6734. \begin{function}{GetResourceStringName}
  6735. \Declaration
  6736. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  6737. \Description
  6738. \var{GetResourceStringName} returns the name of the resourcestring in table
  6739. \var{TableIndex} with index \var{StringIndex}. The name of the string is
  6740. always the unit name in which the string was declared, followed by a period
  6741. and the name of the constant, all in lowercase.
  6742. If a unit \file{MyUnit} declares a resourcestring \var{MyTitle} then the
  6743. name returned will be \var{myunit.mytitle}. A resourcestring in the program file
  6744. will have the name of the program prepended.
  6745. The name returned by this function is also the name that is stored in the
  6746. resourcestring file generated by the compiler.
  6747. Strictly speaking, this information isn't necessary for the functioning
  6748. of the program, it is provided only as a means to easier translation of
  6749. strings.
  6750. \Errors
  6751. If either \var{TableIndex} or \var{StringIndex} is zero, an empty string
  6752. is returned.
  6753. \SeeAlso
  6754. \seep{SetResourceStrings},
  6755. \seef{GetResourceStringDefaultValue},
  6756. \seef{GetResourceStringHash},
  6757. \seef{GetResourceStringName},
  6758. \seef{ResourceStringTableCount},
  6759. \seef{ResourceStringCount}
  6760. \end{function}
  6761. \FPCexample{ex92}
  6762. \begin{function}{Hash}
  6763. \Declaration
  6764. Function Hash(S : AnsiString) : longint;
  6765. \Description
  6766. \var{Hash} calculates the hash value of the string \var{S} in a manner that
  6767. is compatible with the GNU gettext hash value for the string. It is the same
  6768. value that is stored in the Resource string tables, and which can be
  6769. retrieved with the \seef{GetResourceStringHash} function call.
  6770. \Errors
  6771. None. In case the calculated hash value should be 0, the returned result
  6772. will be -1.
  6773. \SeeAlso
  6774. \seef{GetResourceStringHash},
  6775. \end{function}
  6776. \FPCexample{ex93}
  6777. \begin{functionl}{Paramstr}{objpasparamstr}
  6778. \Declaration
  6779. Function ParamStr(Param : Integer) : Ansistring;
  6780. \Description
  6781. \var{ParamStr} returns the \var{Param}-th command-line parameter as an
  6782. AnsiString. The system unit \seef{Paramstr} function limits the result to
  6783. 255 characters.
  6784. The zeroeth command-line parameter contains the path of the executable,
  6785. except on \linux, where it is the command as typed on the command-line.
  6786. \Errors
  6787. In case \var{Param} is an invalid value, an empty string is returned.
  6788. \SeeAlso
  6789. \seef{Paramstr}
  6790. \end{functionl}
  6791. For an example, see \seef{Paramstr}.
  6792. \begin{function}{ReAllocMem}
  6793. \Declaration
  6794. function ReAllocMem(var p:pointer;Size:Longint):pointer;
  6795. \Description
  6796. \var{ReAllocMem} resizes the memory pointed to by \var{P} so it has size
  6797. \var{Size}. The value of \var{P} may change during this operation.
  6798. The contents of the memory pointed to by \var{P} (if any) will be copied to
  6799. the new location, but may be truncated if the newly allocated memory block
  6800. is smaller in size. If a larger block is allocated, only the used memory is
  6801. initialized, extra memory will not be zeroed out.
  6802. Note that \var{P} may be nil, in that case the behaviour of \var{ReAllocMem}
  6803. is equivalent to \seep{Getmem}.
  6804. \Errors
  6805. If no memory is available then a run-time error will occur.
  6806. \SeeAlso
  6807. \seep{Getmem}, \seep{Freemem}
  6808. \end{function}
  6809. \begin{procedure}{ResetResourceTables}
  6810. \Declaration
  6811. Procedure ResetResourceTables;
  6812. \Description
  6813. \var{ResetResourceTables} resets all resource strings to their default
  6814. (i.e. as in the source code) values.
  6815. Normally, this should never be called from a user's program. It is called
  6816. in the initialization code of the \file{objpas} unit. However, if the
  6817. resourcetables get messed up for some reason, this procedure will fix them
  6818. again.
  6819. \Errors
  6820. None.
  6821. \SeeAlso
  6822. \seep{SetResourceStrings},
  6823. \seef{GetResourceStringDefaultValue},
  6824. \seef{GetResourceStringHash},
  6825. \seef{GetResourceStringName},
  6826. \seef{ResourceStringTableCount},
  6827. \seef{ResourceStringCount}
  6828. \end{procedure}
  6829. \begin{function}{ResourceStringCount}
  6830. \Declaration
  6831. Function ResourceStringCount(TableIndex : longint) : longint;
  6832. \Description
  6833. \var{ResourceStringCount} returns the number of resourcestrings in
  6834. the table with index \var{TableIndex}. The strings in a particular table
  6835. are numbered from \var{0} to \var{ResourceStringCount-1}, i.e. they're zero
  6836. based.
  6837. \Errors
  6838. If an invalid \var{TableIndex} is given, \var{-1} is returned.
  6839. \SeeAlso
  6840. \seep{SetResourceStrings},
  6841. \seef{GetResourceStringCurrentValue},
  6842. \seef{GetResourceStringDefaultValue},
  6843. \seef{GetResourceStringHash},
  6844. \seef{GetResourceStringName},
  6845. \seef{ResourceStringTableCount},
  6846. \end{function}
  6847. For an example, see \seef{GetResourceStringDefaultValue}
  6848. \begin{function}{ResourceStringTableCount}
  6849. \Declaration
  6850. Function ResourceStringTableCount : Longint;
  6851. \Description
  6852. \var{ResourceStringTableCount} returns the number of resource string tables;
  6853. this may be zero if no resource strings are used in a program.
  6854. The tables are numbered from 0 to \var{ResourceStringTableCount-1}, i.e.
  6855. they're zero based.
  6856. \Errors
  6857. \SeeAlso
  6858. \seep{SetResourceStrings},
  6859. \seef{GetResourceStringDefaultValue},
  6860. \seef{GetResourceStringHash},
  6861. \seef{GetResourceStringName},
  6862. \seef{ResourceStringCount}
  6863. \end{function}
  6864. For an example, see \seef{GetResourceStringDefaultValue}
  6865. \begin{procedure}{SetResourceStrings}
  6866. \Declaration
  6867. TResourceIterator = Function (Name,Value : AnsiString;Hash : Longint):AnsiString;
  6868. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  6869. \Description
  6870. \var{SetResourceStrings} calls \var{SetFunction} for all resourcestrings
  6871. in the resourcestring tables and sets the resourcestring's current value
  6872. to the value returned by \var{SetFunction}.
  6873. The \var{Name},\var{Value} and \var{Hash} parameters passed to the iterator
  6874. function are the values stored in the tables.
  6875. \Errors
  6876. None.
  6877. \SeeAlso
  6878. \seef{GetResourceStringCurrentValue},
  6879. \seef{GetResourceStringDefaultValue},
  6880. \seef{GetResourceStringHash},
  6881. \seef{GetResourceStringName},
  6882. \seef{ResourceStringTableCount},
  6883. \seef{ResourceStringCount}
  6884. \end{procedure}
  6885. \FPCexample{ex95}
  6886. \begin{function}{SetResourceStringValue}
  6887. \Declaration
  6888. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  6889. \Description
  6890. \var{SetResourceStringValue} assigns \var{Value} to the resource string in
  6891. table \var{TableIndex} with index \var{StringIndex}.
  6892. \Errors
  6893. \SeeAlso
  6894. \seep{SetResourceStrings},
  6895. \seef{GetResourceStringCurrentValue},
  6896. \seef{GetResourceStringDefaultValue},
  6897. \seef{GetResourceStringHash},
  6898. \seef{GetResourceStringName},
  6899. \seef{ResourceStringTableCount},
  6900. \seef{ResourceStringCount}
  6901. \end{function}
  6902. \FPCexample{ex94}
  6903. %
  6904. % The index.
  6905. %
  6906. \printindex
  6907. \end{document}