ref.tex 221 KB

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