2
0

ref.tex 253 KB

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