ref.tex 252 KB

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