ref.tex 217 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459
  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. Var S,T : Ar;
  2776. begin
  2777. S.X := 1;S.Y := 1;
  2778. T.X := 2;T.Y := 2;
  2779. With S,T do
  2780. WriteLn (X,' ',Y);
  2781. end.
  2782. \end{verbatim}
  2783. The output of this program is
  2784. \begin{verbatim}
  2785. 2 2
  2786. \end{verbatim}
  2787. Showing thus that the \var{X,Y} in the \var{WriteLn} statement match the
  2788. \var{T} record variable.
  2789. \subsection{Exception Statements}
  2790. As of version 0.99.7, \fpc supports exceptions. Exceptions provide a
  2791. convenient way to program error and error-recovery mechanisms, and are
  2792. closely related to classes.
  2793. Exception support is explained in \seec{Exceptions}
  2794. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2795. % Assembler statements
  2796. \section{Assembler statements}
  2797. An assembler statement allows you to insert assembler code right in your
  2798. pascal code.
  2799. \input{syntax/statasm.syn}
  2800. More information about assembler blocks can be found in the \progref.
  2801. The register list is used to indicate the registers that are modified by an
  2802. assembler statement in your code. The compiler stores certain results in the
  2803. registers. If you modify the registers in an assembler statement, the compiler
  2804. should, sometimes, be told about it. The registers are denoted with their
  2805. Intel names for the I386 processor, i.e., \var{'EAX'}, \var{'ESI'} etc...
  2806. As an example, consider the following assembler code:
  2807. \begin{verbatim}
  2808. asm
  2809. Movl $1,%ebx
  2810. Movl $0,%eax
  2811. addl %eax,%ebx
  2812. end; ['EAX','EBX'];
  2813. \end{verbatim}
  2814. This will tell the compiler that it should save and restore the contents of
  2815. the \var{EAX} and \var{EBX} registers when it encounters this asm statement.
  2816. \chapter{Using functions and procedures}
  2817. \label{ch:Procedures}
  2818. \fpc supports the use of functions and procedures, but with some extras:
  2819. Function overloading is supported, as well as \var{Const} parameters and
  2820. open arrays.
  2821. \begin{remark} In many of the subsequent paragraphs the words \var{procedure}
  2822. and \var{function} will be used interchangeably. The statements made are
  2823. valid for both, except when indicated otherwise.
  2824. \end{remark}
  2825. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2826. % Procedure declaration
  2827. \section{Procedure declaration}
  2828. A procedure declaration defines an identifier and associates it with a
  2829. block of code. The procedure can then be called with a procedure statement.
  2830. \input{syntax/procedur.syn}
  2831. See \sees{Parameters} for the list of parameters.
  2832. A procedure declaration that is followed by a block implements the action of
  2833. the procedure in that block.
  2834. The following is a valid procedure :
  2835. \begin{verbatim}
  2836. Procedure DoSomething (Para : String);
  2837. begin
  2838. Writeln ('Got parameter : ',Para);
  2839. Writeln ('Parameter in upper case : ',Upper(Para));
  2840. end;
  2841. \end{verbatim}
  2842. Note that it is possible that a procedure calls itself.
  2843. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2844. % Function declaration
  2845. \section{Function declaration}
  2846. A function declaration defines an identifier and associates it with a
  2847. block of code. The block of code will return a result.
  2848. The function can then be called inside an expression, or with a procedure
  2849. statement, if extended syntax is on.
  2850. \input{syntax/function.syn}
  2851. The result type of a function can be any previously declared type.
  2852. contrary to Turbo pascal, where only simple types could be returned.
  2853. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2854. % Parameter lists
  2855. \section{Parameter lists}
  2856. \label{se:Parameters}
  2857. When you need to pass arguments to a function or procedure, these parameters
  2858. must be declared in the formal parameter list of that function or procedure.
  2859. The parameter list is a declaration of identifiers that can be referred to
  2860. only in that procedure or function's block.
  2861. \input{syntax/params.syn}
  2862. Constant parameters and variable parameters can also be \var{untyped}
  2863. parameters if they have no type identifier.
  2864. \subsection{Value parameters}
  2865. Value parameters are declared as follows:
  2866. \input{syntax/paramval.syn}
  2867. When you declare parameters as value parameters, the procedure gets {\em
  2868. a copy} of the parameters that the calling block passes. Any modifications
  2869. to these parameters are purely local to the procedure's block, and do not
  2870. propagate back to the calling block.
  2871. A block that wishes to call a procedure with value parameters must pass
  2872. assignment compatible parameters to the procedure. This means that the types
  2873. should not match exactly, but can be converted (conversion code is inserted
  2874. by the compiler itself)
  2875. Take care that using value parameters makes heavy use of the stack,
  2876. especially if you pass large parameters. The total size of all parameters in
  2877. the formal parameter list should be below 32K for portability's sake (the
  2878. Intel version limits this to 64K).
  2879. You can pass open arrays as value parameters. See \sees{openarray} for
  2880. more information on using open arrays.
  2881. \subsection{Variable parameters}
  2882. \label{se:varparams}
  2883. Variable parameters are declared as follows:
  2884. \input{syntax/paramvar.syn}
  2885. When you declare parameters as variable parameters, the procedure or
  2886. function accesses immediatly the variable that the calling block passed in
  2887. its parameter list. The procedure gets a pointer to the variable that was
  2888. passed, and uses this pointer to access the variable's value.
  2889. From this, it follows that any changes that you make to the parameter, will
  2890. proagate back to the calling block. This mechanism can be used to pass
  2891. values back in procedures.
  2892. Because of this, the calling block must pass a parameter of {\em exactly}
  2893. the same type as the declared parameter's type. If it does not, the compiler
  2894. will generate an error.
  2895. Variable parameters can be untyped. In that case the variable has no type,
  2896. and hence is incompatible with all other types. However, you can use the
  2897. address operator on it, or you can pass it to a function that has also an
  2898. untyped parameter. If you want to use an untyped parameter in an assigment,
  2899. or you want to assign to it, you must use a typecast.
  2900. File type variables must always be passed as variable parameters.
  2901. You can pass open arrays as variable parameters. See \sees{openarray} for
  2902. more information on using open arrays.
  2903. \subsection{Constant parameters}
  2904. In addition to variable parameters and value parameters \fpc also supports
  2905. Constant parameters. You can specify a constant parameter as follows:
  2906. \input{syntax/paramcon.syn}
  2907. A constant argument is passed by reference if it's size is larger than a
  2908. longint. It is passed by value if the size equals 4 or less.
  2909. This means that the function or procedure receives a pointer to the passed
  2910. argument, but you are not allowed to assign to it, this will result in a
  2911. compiler error. Likewise, you cannot pass a const parameter on to another
  2912. function that requires a variable parameter.
  2913. The main use for this is reducing the stack size, hence improving
  2914. performance, and still retaining the semantics of passing by value...
  2915. Constant parameters can also be untyped. See \sees{varparams} for more
  2916. information about untyped parameters.
  2917. You can pass open arrays as constant parameters. See \sees{openarray} for
  2918. more information on using open arrays.
  2919. \subsection{Open array parameters}
  2920. \label{se:openarray}
  2921. \fpc supports the passing of open arrays, i.e. you can declare a procedure
  2922. with an array of unspecified length as a parameter, as in Delphi.
  2923. Open array parameters can be accessed in the procedure or function as an
  2924. array that is declared with starting index 0, and last element
  2925. index \var{High(paremeter)}.
  2926. For example, the parameter
  2927. \begin{verbatim}
  2928. Row : Array of Integer;
  2929. \end{verbatim}
  2930. would be equivalent to
  2931. \begin{verbatim}
  2932. Row : Array[0..N-1] of Integer;
  2933. \end{verbatim}
  2934. Where \var{N} would be the actual size of the array that is passed to the
  2935. function. \var{N-1} can be calculated as \var{High(Row)}.
  2936. Open parameters can be passed by value, by reference or as a constant
  2937. parameter. In the latter cases the procedure receives a pointer to the
  2938. actual array. In the former case, it receives a copy of the array.
  2939. In a function or procedure, you can pass open arrays only to functions which
  2940. are also declared with open arrays as parameters, {\em not} to functions or
  2941. procedures which accept arrays of fixed length.
  2942. The following is an example of a function using an open array:
  2943. \begin{verbatim}
  2944. Function Average (Row : Array of integer) : Real;
  2945. Var I : longint;
  2946. Temp : Real;
  2947. begin
  2948. Temp := Row[0];
  2949. For I := 1 to High(Row) do
  2950. Temp := Temp + Row[i];
  2951. Average := Temp / (High(Row)+1);
  2952. end;
  2953. \end{verbatim}
  2954. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2955. % Function overloading
  2956. \section{Function overloading}
  2957. Function overloading simply means that you can define the same function more
  2958. than once, but each time with a different formal parameter list.
  2959. The parameter lists must differ at least in one of it's elements type.
  2960. When the compiler encounters a function call, it will look at the function
  2961. parameters to decide which one of the defined functions it should call.
  2962. This can be useful if you want to define the same function for different
  2963. types. For example, in the RTL, the \var{Dec} procedure is
  2964. is defined as:
  2965. \begin{verbatim}
  2966. ...
  2967. Dec(Var I : Longint;decrement : Longint);
  2968. Dec(Var I : Longint);
  2969. Dec(Var I : Byte;decrement : Longint);
  2970. Dec(Var I : Byte);
  2971. ...
  2972. \end{verbatim}
  2973. When the compiler encounters a call to the dec function, it will first search
  2974. which function it should use. It therefore checks the parameters in your
  2975. function call, and looks if there is a function definition which matches the
  2976. specified parameter list. If the compiler finds such a function, a call is
  2977. inserted to that function. If no such function is found, a compiler error is
  2978. generated.
  2979. You cannot have overloaded functions that have a \var{cdecl} or \var{export}
  2980. modifier (Technically, because these two modifiers prevent the mangling of
  2981. the function name by the compiler).
  2982. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2983. % forward defined functions
  2984. \section{Forward defined functions}
  2985. You can define a function without having it followed by it's implementation,
  2986. by having it followed by the \var{forward} procedure. The effective
  2987. implementation of that function must follow later in the module.
  2988. The function can be used after a \var{forward} declaration as if it had been
  2989. implemented already.
  2990. The following is an example of a forward declaration.
  2991. \begin{verbatim}
  2992. Program testforward;
  2993. Procedure First (n : longint); forward;
  2994. Procedure Second;
  2995. begin
  2996. WriteLn ('In second. Calling first...');
  2997. First (1);
  2998. end;
  2999. Procedure First (n : longint);
  3000. begin
  3001. WriteLn ('First received : ',n);
  3002. end;
  3003. begin
  3004. Second;
  3005. end.
  3006. \end{verbatim}
  3007. You cannot define a function twice as forward (nor is there any reason why
  3008. you would want to do that).
  3009. Likewise, in units, you cannot have a forward declared function of a
  3010. function that has been declared in the interface part. The interface
  3011. declaration counts as a \var{forward} declaration.
  3012. The following unit will give an error when compiled:
  3013. \begin{verbatim}
  3014. Unit testforward;
  3015. interface
  3016. Procedure First (n : longint);
  3017. Procedure Second;
  3018. implementation
  3019. Procedure First (n : longint); forward;
  3020. Procedure Second;
  3021. begin
  3022. WriteLn ('In second. Calling first...');
  3023. First (1);
  3024. end;
  3025. Procedure First (n : longint);
  3026. begin
  3027. WriteLn ('First received : ',n);
  3028. end;
  3029. end.
  3030. \end{verbatim}
  3031. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3032. % External functions
  3033. \section{External functions}
  3034. \label{se:external}
  3035. The \var{external} modifier can be used to declare a function that resides in
  3036. an external object file. It allows you to use the function in
  3037. your code, and at linking time, you must link the object file containing the
  3038. implementation of the function or procedure.
  3039. \input{syntax/external.syn}
  3040. It replaces, in effect, the function or procedure code block. As such, it
  3041. can be present only in an implementation block of a unit, or in a program.
  3042. As an example:
  3043. \begin{verbatim}
  3044. program CmodDemo;
  3045. {$Linklib c}
  3046. Const P : PChar = 'This is fun !';
  3047. Function strlen (P : PChar) : Longint; cdecl; external;
  3048. begin
  3049. WriteLn ('Length of (',p,') : ',strlen(p))
  3050. end.
  3051. \end{verbatim}
  3052. \begin{remark}
  3053. The parameters in our declaration of the \var{external} function
  3054. should match exactly the ones in the declaration in the object file.
  3055. \end{remark}
  3056. If the \var{external} modifier is followed by a string constant:
  3057. \begin{verbatim}
  3058. external 'lname';
  3059. \end{verbatim}
  3060. Then this tells the compiler that the function resides in library
  3061. 'lname'. The compiler will then automatically link this library to
  3062. your program.
  3063. You can also specify the name that the function has in the library:
  3064. \begin{verbatim}
  3065. external 'lname' name Fname;
  3066. \end{verbatim}
  3067. This tells the compiler that the function resides in library 'lname',
  3068. but with name 'Fname'. The compiler will then automatically link this
  3069. library to your program, and use the correct name for the function.
  3070. Under \windows and \ostwo, you can also use the following form:
  3071. \begin{verbatim}
  3072. external 'lname' Index Ind;
  3073. \end{verbatim}
  3074. This tells the compiler that the function resides in library 'lname',
  3075. but with index \var{Ind}. The compiler will then automatically
  3076. link this library to your program, and use the correct index for the
  3077. function.
  3078. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3079. % Assembler functions
  3080. \section{Assembler functions}
  3081. Functions and procedures can be completely implemented in assembly
  3082. language. To indicate this, you use the \var{assembler} keyword:
  3083. \input{syntax/asm.syn}
  3084. Contrary to Delphi, the assembler keyword must be present to indicate an
  3085. assembler function.
  3086. For more information about assembler functions, see the chapter on using
  3087. assembler in the \progref.
  3088. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3089. % Modifiers
  3090. \section{Modifiers}
  3091. A function or procedure declaration can contain modifiers. Here we list the
  3092. various possibilities:
  3093. \input{syntax/modifiers.syn}
  3094. \fpc doesn't support all Turbo Pascal modifiers, but
  3095. does support a number of additional modifiers. They are used mainly for assembler and
  3096. reference to C object files. More on the use of modifiers can be found in
  3097. the \progref.
  3098. \subsection{Public}
  3099. The \var{Public} keyword is used to declare a function globally in a unit.
  3100. This is useful if you don't want a function to be accessible from the unit
  3101. file, but you do want the function to be accessible from the object file.
  3102. as an example:
  3103. \begin{verbatim}
  3104. Unit someunit;
  3105. interface
  3106. Function First : Real;
  3107. Implementation
  3108. Function First : Real;
  3109. begin
  3110. First := 0;
  3111. end;
  3112. Function Second : Real; [Public];
  3113. begin
  3114. Second := 1;
  3115. end;
  3116. end.
  3117. \end{verbatim}
  3118. If another program or unit uses this unit, it will not be able to use the
  3119. function \var{Second}, since it isn't declared in the interface part.
  3120. However, it will be possible to access the function \var{Second} at the
  3121. assembly-language level, by using it's mangled name (see the \progref).
  3122. \subsection{cdecl}
  3123. \label{se:cdecl}
  3124. The \var{cdecl} modifier can be used to declare a function that uses a C
  3125. type calling convention. This must be used if you wish to acces functions in
  3126. an object file generated by a C compiler. It allows you to use the function in
  3127. your code, and at linking time, you must link the object file containing the
  3128. \var{C} implementation of the function or procedure.
  3129. As an example:
  3130. \begin{verbatim}
  3131. program CmodDemo;
  3132. {$LINKLIB c}
  3133. Const P : PChar = 'This is fun !';
  3134. Function strlen (P : PChar) : Longint; cdecl; external;
  3135. begin
  3136. WriteLn ('Length of (',p,') : ',strlen(p))
  3137. end.
  3138. \end{verbatim}
  3139. When compiling this, and linking to the C-library, you will be able to call
  3140. the \var{strlen} function throughout your program. The \var{external}
  3141. directive tells the compiler that the function resides in an external
  3142. object filebrary (see \ref{se:external}).
  3143. \begin{remark}
  3144. The parameters in our declaration of the \var{C} function should
  3145. match exactly the ones in the declaration in \var{C}. Since \var{C} is case
  3146. sensitive, this means also that the name of the
  3147. function must be exactly the same. the \fpc compiler will use the name {\em
  3148. exactly} as it is typed in the declaration.
  3149. \end{remark}
  3150. \subsection{popstack}
  3151. \label{se:popstack}
  3152. Popstack does the same as \var{cdecl}, namely it tells the \fpc compiler
  3153. that a function uses the C calling convention. In difference with the
  3154. \var{cdecl} modifier, it still mangles the name of the function as it would
  3155. for a normal pascal function.
  3156. With \var{popstack} you could access functions by their pascal names in a
  3157. library.
  3158. \subsection{Export}
  3159. Sometimes you must provide a callback function for a C library, or you want
  3160. your routines to be callable from a C program. Since \fpc and C use
  3161. different calling schemes for functions and procedures\footnote{More
  3162. techically: In C the calling procedure must clear the stack. In \fpc, the
  3163. subroutine clears the stack.}, the compiler must be told to generate code
  3164. that can be called from a C routine. This is where the \var{Export} modifier
  3165. comes in. Contrary to the other modifiers, it must be specified separately,
  3166. as follows:
  3167. \begin{verbatim}
  3168. function DoSquare (X : Longint) : Longint; export;
  3169. begin
  3170. ...
  3171. end;
  3172. \end{verbatim}
  3173. The square brackets around the modifier are not allowed in this case.
  3174. \begin{remark}
  3175. as of version 0.9.8, \fpc supports the Delphi \var{cdecl} modifier.
  3176. This modifier works in the same way as the \var{export} modifier.
  3177. More information about these modifiers can be found in the \progref, in the
  3178. section on the calling mechanism and the chapter on linking.
  3179. \end{remark}
  3180. \subsection{StdCall}
  3181. As of version 0.9.8, \fpc supports the Delphi \var{stdcall} modifier.
  3182. This modifier does actually nothing, since the \fpc compiler by default
  3183. pushes parameters from right to left on the stack, which is what the
  3184. modifier does under Delphi (which pushes parameters on the stack from left to
  3185. right).
  3186. More information about this modifier can be found in the \progref, in the
  3187. section on the calling mechanism and the chapter on linking.
  3188. \subsection{Alias}
  3189. The \var{Alias} modifier allows you to specify a different name for a
  3190. procedure or function. This is mostly useful for referring to this procedure
  3191. from assembly language constructs. As an example, consider the following
  3192. program:
  3193. \begin{verbatim}
  3194. Program Aliases;
  3195. Procedure Printit; [Alias : 'DOIT'];
  3196. begin
  3197. WriteLn ('In Printit (alias : "DOIT")');
  3198. end;
  3199. begin
  3200. asm
  3201. call DOIT
  3202. end;
  3203. end.
  3204. \end{verbatim}
  3205. \begin{remark} the specified alias is inserted straight into the assembly
  3206. code, thus it is case sensitive.
  3207. \end{remark}
  3208. The \var{Alias} modifier, combined with the \var{Public} modifier, make a
  3209. powerful tool for making externally accessible object files.
  3210. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3211. % Unsupported Turbo Pascal modifiers
  3212. \section{Unsupported Turbo Pascal modifiers}
  3213. The modifiers that exist in Turbo pascal, but aren't supported by \fpc, are
  3214. listed in \seet{Modifs}.
  3215. \begin{FPCltable}{lr}{Unsupported modifiers}{Modifs}
  3216. Modifier & Why not supported ? \\ \hline
  3217. Near & \fpc is a 32-bit compiler.\\
  3218. Far & \fpc is a 32-bit compiler. \\
  3219. %External & Replaced by \var{C} modifier. \\ \hline
  3220. \end{FPCltable}
  3221. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3222. % Operator overloading
  3223. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3224. \chapter{Operator overloading}
  3225. \label{ch:operatoroverloading}
  3226. \section{Introduction}
  3227. \fpc supports operator overloading. This means that it is possible to
  3228. define the action of some operators on self-defined types, and thus allow
  3229. the use of these types in mathematical expressions.
  3230. Defining the action of an operator is much like the definition of a
  3231. function or procedure, only there are some restrictions on the possible
  3232. definitions, as will be shown in the subsequent.
  3233. Operator overloading is, in essence, a powerful notational tool;
  3234. but it is also not more than that, since the same results can be
  3235. obtained with regular function calls. When using operator overloading,
  3236. It is important to keep in mind that some implicit rules may produce
  3237. some unexpected results. This will be indicated.
  3238. \section{Operator declarations}
  3239. To define the action of an operator is much like defining a function:
  3240. \input{syntax/operator.syn}
  3241. The parameter list for a comparision operator or an arithmetic operator
  3242. must always contain 2 parameters. The result type of the comparision
  3243. operator must be \var{Boolean}.
  3244. The statement block contains the necessary statements to determine the
  3245. result of the operation. It can contain arbitrary large pieces of code;
  3246. it is executed whenever the operation is encountered in some expression.
  3247. The result of the statement block must always be defined; error conditions
  3248. are not checked bythe compiler, and the code must take care of all possible
  3249. cases, throwing a run-time error if some error condition is encountered.
  3250. In the following, the three types of operator definitions will be examined.
  3251. As an example, throughout this chapter the following type will be used to
  3252. define overloaded operators on :
  3253. \begin{verbatim}
  3254. type
  3255. complex = record
  3256. re : real;
  3257. im : real;
  3258. end;
  3259. \end{verbatim}
  3260. this type will be used in all examples.
  3261. The sources of the Run-Time Library contain a unit \file{ucomplex},
  3262. which contains a complete calculus for complex numbers, based on
  3263. operator overloading.
  3264. \section{Assignment operators}
  3265. The assignment operator defines the action of a assignent of one type of
  3266. variable to another. The result type must match the type of the variable
  3267. at the left of the assignment statement, the single parameter to the
  3268. assignment operator must have the same type as the expression at the
  3269. right of the assignment operator.
  3270. This system can be used to declare a new type, and define an assignment for
  3271. that type. For instance, to be able to assign a newly defined type 'Complex'
  3272. \begin{verbatim}
  3273. Var
  3274. C,Z : Complex; // New type complex
  3275. begin
  3276. Z:=C; // assignments between complex types.
  3277. end;
  3278. \end{verbatim}
  3279. You would have to define the following assignment operator:
  3280. \begin{verbatim}
  3281. Operator := (C : Complex) z : complex;
  3282. \end{verbatim}
  3283. To be able to assign a real type to a complex type as follows:
  3284. \begin{verbatim}
  3285. var
  3286. R : real;
  3287. C : complex;
  3288. begin
  3289. C:=R;
  3290. end;
  3291. \end{verbatim}
  3292. the following assignment operator must be defined:
  3293. \begin{verbatim}
  3294. Operator := (r : real) z : complex;
  3295. \end{verbatim}
  3296. As can be seen from this statement, it defines the action of the operator
  3297. \var{:=} with at the right a real expression, and at the left a complex
  3298. expression.
  3299. an example implementation of this could be as follows:
  3300. \begin{verbatim}
  3301. operator := (r : real) z : complex;
  3302. begin
  3303. z.re:=r;
  3304. z.im:=0.0;
  3305. end;
  3306. \end{verbatim}
  3307. As can be seen in the example, the result identifier (\var{z} in this case)
  3308. is used to store the result of the assignment. When compiling in Delphi mode
  3309. or objfpc mode, the use of the special identifier \var{Result} is also
  3310. allowed, and can be substituted for the \var{z}, so the above would be
  3311. equivalent to
  3312. \begin{verbatim}
  3313. operator := (r : real) z : complex;
  3314. begin
  3315. Result.re:=r;
  3316. Result.im:=0.0;
  3317. end;
  3318. \end{verbatim}
  3319. The assignment operator is also used to convert types from one type to
  3320. another. The compiler will consider all overloaded assignment operators
  3321. till it finds one that matches the types of the left hand and right hand
  3322. expressions. If no such operator is found, a 'type mismatch' error
  3323. is given.
  3324. \begin{remark}
  3325. The assignment operator is not commutative; the compiler will never reverse
  3326. the role of the two arguments. in other words, given the above definition of
  3327. the assignment operator, the following is {\em not} possible:
  3328. \begin{verbatim}
  3329. var
  3330. R : real;
  3331. C : complex;
  3332. begin
  3333. R:=C;
  3334. end;
  3335. \end{verbatim}
  3336. if the reverse assignment should be possible (this is not so for reals and
  3337. complex numbers) then the assigment operator must be defined for that as well.
  3338. \end{remark}
  3339. \begin{remark}
  3340. The assignment operator is also used in implicit type conversions. This can
  3341. have unwanted effects. Consider the following definitions:
  3342. \begin{verbatim}
  3343. operator := (r : real) z : complex;
  3344. function exp(c : complex) : complex;
  3345. \end{verbatim}
  3346. then the following assignment will give a type mismatch:
  3347. \begin{verbatim}
  3348. Var
  3349. r1,r2 : real;
  3350. begin
  3351. r1:=exp(r2);
  3352. end;
  3353. \end{verbatim}
  3354. because the compiler will encounter the definition of the \var{exp} function
  3355. with the complex argument. It implicitly converts r2 to a complex, so it can
  3356. use the above \var{exp} function. The result of this function is a complex,
  3357. which cannot be assigned to r1, so the compiler will give a 'type mismatch'
  3358. error. The compiler will not look further for another \var{exp} which has
  3359. the correct arguments.
  3360. It is possible to avoid this particular problem by specifying
  3361. \begin{verbatim}
  3362. r1:=system.exp(r2);
  3363. \end{verbatim}
  3364. An experimental solution for this problem exists in the compiler, but is
  3365. not enabled by default. Maybe someday it will be.
  3366. \end{remark}
  3367. \section{Arithmetic operators}
  3368. Arithmetic operators define the action of a binary operator. Possible
  3369. operations are:
  3370. \begin{description}
  3371. \item[multiplication] to multiply two types, the \var{*} multiplication
  3372. operator must be overloaded.
  3373. \item[division] to divide two types, the \var{/} division
  3374. operator must be overloaded.
  3375. \item[addition] to add two types, the \var{+} addition
  3376. operator must be overloaded.
  3377. \item[substraction] to substract two types, the \var{-} substraction
  3378. operator must be overloaded.
  3379. \item[exponentiation] to exponentiate two types, the \var{**} exponentiation
  3380. operator must be overloaded.
  3381. \end{description}
  3382. The definition of an arithmetic operator takes two parameters. The first
  3383. parameter must be of the type that occurs at the left of the operator,
  3384. the second parameter must be of the type that is at the right of the
  3385. arithmetic operator. The result type must match the type that results
  3386. after the arithmetic operation.
  3387. To compile an expression as
  3388. \begin{verbatim}
  3389. var
  3390. R : real;
  3391. C,Z : complex;
  3392. begin
  3393. C:=R*Z;
  3394. end;
  3395. \end{verbatim}
  3396. one needs a definition of the multiplication operator as:
  3397. \begin{verbatim}
  3398. Operator * (r : real; z1 : complex) z : complex;
  3399. begin
  3400. z.re := z1.re * r;
  3401. z.im := z1.im * r;
  3402. end;
  3403. \end{verbatim}
  3404. As can be seen, the first operator is a real, and the second is
  3405. a complex. The result type is complex.
  3406. Multiplication and addition of reals and complexes are commutative
  3407. operations. The compiler, however, has no notion of this fact so even
  3408. if a multiplication between a real and a complex is defined, the
  3409. compiler will not use that definition when it encounters a complex
  3410. and a real (in that order). It is necessary to define both operations.
  3411. So, given the above definition of the multiplication,
  3412. the compiler will not accept the following statement:
  3413. \begin{verbatim}
  3414. var
  3415. R : real;
  3416. C,Z : complex;
  3417. begin
  3418. C:=Z*R;
  3419. end;
  3420. \end{verbatim}
  3421. since the types of \var{Z} and \var{R} don't match the types in the
  3422. operator definition.
  3423. The reason for this behaviour is that it is possible that a multiplication
  3424. is not always commutative. e.g. the multiplication of a \var{(n,m)} with a
  3425. \var{(m,n)} matrix will result in a \var{(n,n)} matrix, while the
  3426. mutiplication of a \var{(m,n)} with a \var{(n,m)} matrix is a \var{(m,m)}
  3427. matrix, which needn't be the same in all cases.
  3428. \section{Comparision operator}
  3429. The comparision operator can be overloaded to compare two different types
  3430. or to compare two equal types that are not basic types. The result type of
  3431. a comparision operator is always a boolean.
  3432. The comparision operators that can be overloaded are:
  3433. \begin{description}
  3434. \item[equal to] (=) to determine if two variables are equal.
  3435. \item[less than] ($<$) to determine if one variable is less than another.
  3436. \item[greater than] ($>$) to determine if one variable is greater than another.
  3437. \item[greater than or equal to] ($>=$) to determine if one variable is greater than
  3438. or equal to another.
  3439. \item[less than or equal to] ($<=$) to determine if one variable is greater
  3440. than or equal to another.
  3441. \end{description}
  3442. There is no separate operator for {\em unequal to} ($<>$). To evaluate a
  3443. statement that contans the {\em unequal to} operator, the compiler uses the
  3444. {\em equal to} operator (=), and negates the result.
  3445. As an example, the following opetrator allows to compare two complex
  3446. numbers:
  3447. \begin{verbatim}
  3448. operator = (z1, z2 : complex) b : boolean;
  3449. \end{verbatim}
  3450. the above definition allows comparisions of the following form:
  3451. \begin{verbatim}
  3452. Var
  3453. C1,C2 : Complex;
  3454. begin
  3455. If C1=C2 then
  3456. Writeln('C1 and C2 are equal');
  3457. end;
  3458. \end{verbatim}
  3459. The comparision operator definition needs 2 parameters, with the types that
  3460. the operator is meant to compare. Here also, the compiler doesn't apply
  3461. commutativity; if the two types are different, then it necessary to
  3462. define 2 comparision operators.
  3463. In the case of complex numbers, it is, for instance necessary to define
  3464. 2 comparsions: one with the complex type first, and one with the real type
  3465. first.
  3466. Given the definitions
  3467. \begin{verbatim}
  3468. operator = (z1 : complex;r : real) b : boolean;
  3469. operator = (r : real; z1 : complex) b : boolean;
  3470. \end{verbatim}
  3471. the following two comparisions are possible:
  3472. \begin{verbatim}
  3473. Var
  3474. R,S : Real;
  3475. C : Complex;
  3476. begin
  3477. If (C=R) or (S=C) then
  3478. Writeln ('Ok');
  3479. end;
  3480. \end{verbatim}
  3481. Note that the order of the real and complex type in the two comparisions
  3482. is reversed.
  3483. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3484. % Programs, Units, Blocks
  3485. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3486. \chapter{Programs, units, blocks}
  3487. A Pascal program consists of modules called \var{units}. A unit can be used
  3488. to group pieces of code together, or to give someone code without giving
  3489. the sources.
  3490. Both programs and units consist of code blocks, which are mixtures of
  3491. statements, procedures, and variable or type declarations.
  3492. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3493. % Programs
  3494. \section{Programs}
  3495. A pascal program consists of the program header, followed possibly by a
  3496. 'uses' clause, and a block.
  3497. \input{syntax/program.syn}
  3498. The program header is provided for backwards compatibility, and is ignored
  3499. by the compiler.
  3500. The uses clause serves to identify all units that are needed by the program.
  3501. The system unit doesn't have to be in this list, since it is always loaded
  3502. by the compiler.
  3503. The order in which the units appear is significant, it determines in
  3504. which order they are initialized. Units are initialized in the same order
  3505. as they appear in the uses clause. Identifiers are searched in the opposite
  3506. order, i.e. when the compiler searches for an identifier, then it looks
  3507. first in the last unit in the uses clause, then the last but one, and so on.
  3508. This is important in case two units declare different types with the same
  3509. identifier.
  3510. When the compiler looks for unit files, it adds the extension \file{.ppu}
  3511. (\file{.ppw} for Win32 platforms) to the name of the unit. On \linux, unit names
  3512. are converted to all lowercase when looking for a unit.
  3513. If a unit name is longer than 8 characters, the compiler will first look for
  3514. a unit name with this length, and then it will truncate the name to 8
  3515. characters and look for it again. For compatibility reasons, this is also
  3516. true on platforms that suport long file names.
  3517. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3518. % Units
  3519. \section{Units}
  3520. A unit contains a set of declarations, procedures and functions that can be
  3521. used by a program or another unit.
  3522. The syntax for a unit is as follows:
  3523. \input{syntax/unit.syn}
  3524. The interface part declares all identifiers that must be exported from the
  3525. unit. This can be constant, type or variable identifiers, and also procedure
  3526. or function identifier declarations. Declarations inside the
  3527. implementation part are {\em not} accessible outside the unit. The
  3528. implementation must contain a function declaration for each function or
  3529. procedure that is declared in the interface part. If a function is declared
  3530. in the interface part, but no declaration of that function is present in the
  3531. implementation part, then the compiler will give an error.
  3532. When a program uses a unit (say \file{unitA}) and this units uses a second
  3533. unit, say \file{unitB}, then the program depends indirectly also on
  3534. \var{unitB}. This means that the compiler must have access to \file{unitB} when
  3535. trying to compile the program. If the unit is not present at compile time,
  3536. an error occurs.
  3537. Note that the identifiers from a unit on which a program depends indirectly,
  3538. are not accessible to the program. To have access to the identifiers of a
  3539. unit, you must put that unit in the uses clause of the program or unit where
  3540. you want to yuse the identifier.
  3541. Units can be mutually dependent, that is, they can reference each other in
  3542. their uses clauses. This is allowed, on the condition that at least one of
  3543. the references is in the implementation section of the unit. This also holds
  3544. for indirect mutually dependent units.
  3545. If it is possible to start from one interface uses clause of a unit, and to return
  3546. there via uses clauses of interfaces only, then there is circular unit
  3547. dependence, and the compiler will generate an error.
  3548. As and example : the following is not allowed:
  3549. \begin{verbatim}
  3550. Unit UnitA;
  3551. interface
  3552. Uses UnitB;
  3553. implementation
  3554. end.
  3555. Unit UnitB
  3556. interface
  3557. Uses UnitA;
  3558. implementation
  3559. end.
  3560. \end{verbatim}
  3561. But this is allowed :
  3562. \begin{verbatim}
  3563. Unit UnitA;
  3564. interface
  3565. Uses UnitB;
  3566. implementation
  3567. end.
  3568. Unit UnitB
  3569. implementation
  3570. Uses UnitA;
  3571. end.
  3572. \end{verbatim}
  3573. Because \file{UnitB} uses \file{UnitA} only in it's implentation section.
  3574. In general, it is a bad idea to have circular unit dependencies, even if it is
  3575. only in implementation sections.
  3576. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3577. % Blocks
  3578. \section{Blocks}
  3579. Units and programs are made of blocks. A block is made of declarations of
  3580. labels, constants, types variables and functions or procedures. Blocks can
  3581. be nested in certain ways, i.e., a procedure or function declaration can
  3582. have blocks in themselves.
  3583. A block looks like the following:
  3584. \input{syntax/block.syn}
  3585. Labels that can be used to identify statements in a block are declared in
  3586. the label declaration part of that block. Each label can only identify one
  3587. statement.
  3588. Constants that are to be used only in one block should be declared in that
  3589. block's constant declaration part.
  3590. Variables that are to be used only in one block should be declared in that
  3591. block's constant declaration part.
  3592. Types that are to be used only in one block should be declared in that
  3593. block's constant declaration part.
  3594. Lastly, functions and procedures that will be used in that block can be
  3595. declared in the procedure/function declaration part.
  3596. After the different declaration parts comes the statement part. This
  3597. contains any actions that the block should execute.
  3598. All identifiers declared before the statement part can be used in that
  3599. statement part.
  3600. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3601. % Scope
  3602. \section{Scope}
  3603. Identifiers are valid from the point of their declaration until the end of
  3604. the block in which the declaration occurred. The range where the identifier
  3605. is known is the {\em scope} of the identifier. The exact scope of an
  3606. identifier depends on the way it was defined.
  3607. \subsection{Block scope}
  3608. The {\em scope} of a variable declared in the declaration part of a block,
  3609. is valid from the point of declaration until the end of the block.
  3610. If a block contains a second block, in which the identfier is
  3611. redeclared, then inside this block, the second declaration will be valid.
  3612. Upon leaving the inner block, the first declaration is valid again.
  3613. Consider the following example:
  3614. \begin{verbatim}
  3615. Program Demo;
  3616. Var X : Real;
  3617. { X is real variable }
  3618. Procedure NewDeclaration
  3619. Var X : Integer; { Redeclare X as integer}
  3620. begin
  3621. // X := 1.234; {would give an error when trying to compile}
  3622. X := 10; { Correct assigment}
  3623. end;
  3624. { From here on, X is Real again}
  3625. begin
  3626. X := 2.468;
  3627. end.
  3628. \end{verbatim}
  3629. In this example, inside the procedure, X denotes an integer variable.
  3630. It has it's own storage space, independent of the variable \var{X} outside
  3631. the procedure.
  3632. \subsection{Record scope}
  3633. The field identifiers inside a record definition are valid in the following
  3634. places:
  3635. \begin{enumerate}
  3636. \item to the end of the record definition.
  3637. \item field designators of a variable of the given record type.
  3638. \item identifiers inside a \var{With} statement that operates on a variable
  3639. of the given record type.
  3640. \end{enumerate}
  3641. \subsection{Class scope}
  3642. A component identifier is valid in the following places:
  3643. \begin{enumerate}
  3644. \item From the point of declaration to the end of the class definition.
  3645. \item In all descendent types of this class, unless it is in the private
  3646. part of the class declaration.
  3647. \item In all method declaration blocks of this class and descendent classes.
  3648. \item In a with statement that operators on a variable of the given class's
  3649. definition.
  3650. \end{enumerate}
  3651. Note that method designators are also considered identifiers.
  3652. \subsection{Unit scope}
  3653. All identifiers in the interface part of a unit are valid from the point of
  3654. declaration, until the end of the unit. Furthermore, the identifiers are
  3655. known in programs or units that have the unit in their uses clause.
  3656. Identifiers from indirectly dependent units are {\em not} available.
  3657. Identifiers declared in the implementation part of a unit are valid from the
  3658. point of declaration to the end of the unit.
  3659. The system unit is automatically used in all units and programs.
  3660. It's identifiers are therefore always known, in each program or unit
  3661. you make.
  3662. The rules of unit scope implie that you can redefine an identifier of a
  3663. unit. To have access to an identifier of another unit that was redeclared in
  3664. the current unit, precede it with that other units name, as in the following
  3665. example:
  3666. \begin{verbatim}
  3667. unit unitA;
  3668. interface
  3669. Type
  3670. MyType = Real;
  3671. implementation
  3672. end.
  3673. Program prog;
  3674. Uses UnitA;
  3675. { Redeclaration of MyType}
  3676. Type MyType = Integer;
  3677. Var A : Mytype; { Will be Integer }
  3678. B : UnitA.MyType { Will be real }
  3679. begin
  3680. end.
  3681. \end{verbatim}
  3682. This is especially useful if you redeclare the system unit's identifiers.
  3683. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3684. % Libraries
  3685. \section{Libraries}
  3686. \fpc supports making of dynamic libraries (DLLs under Win32 and \ostwo) trough
  3687. the use of the \var{Library} keyword.
  3688. A Library is just like a unit or a program:
  3689. \input{syntax/library.syn}
  3690. By default, functions and procedures that are declared and implemented in
  3691. library are not available to a programmer that wishes to use your library.
  3692. In order to make functions or procedures available from the library,
  3693. you must export them in an export clause:
  3694. \input{syntax/exports.syn}
  3695. Under Win32, an index clause can be added to an exports entry.
  3696. an index entry must be a positive number larger or equal than 1.
  3697. It is best to use low index values, although nothing forces you to
  3698. do this.
  3699. Optionally, an exports entry can have a name specifier. If present, the name
  3700. specifier gives the exact name (case sensitive) of the function in the
  3701. library.
  3702. If neither of these constructs is present, the functions or procedures
  3703. are exported with the exact names as specified in the exports clause.
  3704. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3705. % Exceptions
  3706. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3707. \chapter{Exceptions}
  3708. \label{ch:Exceptions}
  3709. As of version 0.99.7, \fpc supports exceptions. Exceptions provide a
  3710. convenient way to program error and error-recovery mechanisms, and are
  3711. closely related to classes.
  3712. Exception support is based on 3 constructs:
  3713. \begin{description}
  3714. \item [Raise\ ] statements. To raise an exeption. This is usually done to signal an
  3715. error condition.
  3716. \item [Try ... Except\ ] blocks. These block serve to catch exceptions
  3717. raised within the scope of the block, and to provide exception-recovery
  3718. code.
  3719. \item [Try ... Finally\ ] blocks. These block serve to force code to be
  3720. executed irrespective of an exception occurrence or not. They generally
  3721. serve to clean up memory or close files in case an exception occurs.
  3722. The compiler generates many implicit \var{Try ... Finally} blocks around
  3723. procedure, to force memory consistence.
  3724. \end{description}
  3725. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3726. % The raise statement
  3727. \section{The raise statement}
  3728. The \var{raise} statement is as follows:
  3729. \input{syntax/raise.syn}
  3730. This statement will raise an exception. If it is specified, the exception
  3731. instance must be an initialized instance of a class, which is the raise
  3732. type. The address exception is optional. If itis not specified, the compiler
  3733. will provide the address by itself.
  3734. If the exception instance is omitted, then the current exception is
  3735. re-raised. This construct can only be used in an exception handling
  3736. block (see further).
  3737. \begin{remark} Control {\em never} returns after an exception block. The
  3738. control is transferred to the first \var{try...finally} or
  3739. \var{try...except} statement that is encountered when unwinding the stack.
  3740. If no such statement is found, the \fpc Run-Time Library will generate a
  3741. run-time error 217 (see also \sees{exceptclasses}).
  3742. \end{remark}
  3743. As an example: The following division checks whether the denominator is
  3744. zero, and if so, raises an exception of type \var{EDivException}
  3745. \begin{verbatim}
  3746. Type EDivException = Class(Exception);
  3747. Function DoDiv (X,Y : Longint) : Integer;
  3748. begin
  3749. If Y=0 then
  3750. Raise EDivException.Create ('Division by Zero would occur');
  3751. Result := X Div Y;
  3752. end;
  3753. \end{verbatim}
  3754. The class \var{Exception} is defined in the \file{Sysutils} unit of the rtl.
  3755. (\sees{exceptclasses})
  3756. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3757. % The try...except statement
  3758. \section{The try...except statement}
  3759. A \var{try...except} exception handling block is of the following form :
  3760. \input{syntax/try.syn}
  3761. If no exception is raised during the execution of the \var{statement list},
  3762. then all statements in the list will be executed sequentially, and the
  3763. except block will be skipped, transferring program flow to the statement
  3764. after the final \var{end}.
  3765. If an exception occurs during the execution of the \var{statement list}, the
  3766. program flow will be transferred to the except block. Statements in the
  3767. statement list between the place where the exception was raised and the
  3768. exception block are ignored.
  3769. In the exception handling block, the type of the exception is checked,
  3770. and if there is an exception handler where the class type matches the
  3771. exception object type, or is a parent type of
  3772. the exception object type, then the statement following the corresponding
  3773. \var{Do} will be executed. The first matching type is used. After the
  3774. \var{Do} block was executed, the program continues after the \var{End}
  3775. statement.
  3776. The identifier in an exception handling statement is optional, and declares
  3777. an exception object. It can be used to manipulate the exception object in
  3778. the exception handling code. The scope of this declaration is the statement
  3779. block foillowing the \var{Do} keyword.
  3780. If none of the \var{On} handlers matches the exception object type, then the
  3781. statement list after \var{else} is executed. If no such list is
  3782. found, then the exception is automatically re-raised. This process allows
  3783. to nest \var{try...except} blocks.
  3784. If, on the other hand, the exception was caught, then the exception object is
  3785. destroyed at the end of the exception handling block, before program flow
  3786. continues. The exception is destroyed through a call to the object's
  3787. \var{Destroy} destructor.
  3788. As an example, given the previous declaration of the \var{DoDiv} function,
  3789. consider the following
  3790. \begin{verbatim}
  3791. Try
  3792. Z := DoDiv (X,Y);
  3793. Except
  3794. On EDivException do Z := 0;
  3795. end;
  3796. \end{verbatim}
  3797. If \var{Y} happens to be zero, then the DoDiv function code will raise an
  3798. exception. When this happens, program flow is transferred to the except
  3799. statement, where the Exception handler will set the value of \var{Z} to
  3800. zero. If no exception is raised, then program flow continues past the last
  3801. \var{end} statement.
  3802. To allow error recovery, the \var{Try ... Finally} block is supported.
  3803. A \var{Try...Finally} block ensures that the statements following the
  3804. \var{Finally} keyword are guaranteed to be executed, even if an exception
  3805. occurs.
  3806. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3807. % The try...finally statement
  3808. \section{The try...finally statement}
  3809. A \var{Try..Finally} statement has the following form:
  3810. \input{syntax/finally.syn}
  3811. If no exception occurs inside the \var{statement List}, then the program
  3812. runs as if the \var{Try}, \var{Finally} and \var{End} keywords were not
  3813. present.
  3814. If, however, an exception occurs, the program flow is immediatly
  3815. transferred from the point where the excepion was raised to the first
  3816. statement of the \var{Finally statements}.
  3817. All statements after the finally keyword will be executed, and then
  3818. the exception will be automatically re-raised. Any statements between the
  3819. place where the exception was raised and the first statement of the
  3820. \var{Finally Statements} are skipped.
  3821. As an example consider the following routine:
  3822. \begin{verbatim}
  3823. Procedure Doit (Name : string);
  3824. Var F : Text;
  3825. begin
  3826. Try
  3827. Assign (F,Name);
  3828. Rewrite (name);
  3829. ... File handling ...
  3830. Finally
  3831. Close(F);
  3832. end;
  3833. \end{verbatim}
  3834. If during the execution of the file handling an execption occurs, then
  3835. program flow will continue at the \var{close(F)} statement, skipping any
  3836. file operations that might follow between the place where the exception
  3837. was raised, and the \var{Close} statement.
  3838. If no exception occurred, all file operations will be executed, and the file
  3839. will be closed at the end.
  3840. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3841. % Exception handling nesting
  3842. \section{Exception handling nesting}
  3843. It is possible to nest \var{Try...Except} blocks with \var{Try...Finally}
  3844. blocks. Program flow will be done according to a \var{lifo} (last in, first
  3845. out) principle: The code of the last encountered \var{Try...Except} or
  3846. \var{Try...Finally} block will be executed first. If the exception is not
  3847. caught, or it was a finally statement, program flow will be transferred to
  3848. the last-but-one block, {\em ad infinitum}.
  3849. If an exception occurs, and there is no exception handler present, then a
  3850. runerror 217 will be generated. If you use the \file{sysutils} unit, a default
  3851. handler is installed which will show the exception object message, and the
  3852. address where the exception occurred, after which the program will exit with
  3853. a \var{Halt} instruction.
  3854. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3855. % Exception classes
  3856. \section{Exception classes}
  3857. \label{se:exceptclasses}
  3858. The \file{sysutils} unit contains a great deal of exception handling.
  3859. It defines the following exception types:
  3860. \begin{verbatim}
  3861. Exception = class(TObject)
  3862. private
  3863. fmessage : string;
  3864. fhelpcontext : longint;
  3865. public
  3866. constructor create(const msg : string);
  3867. constructor createres(indent : longint);
  3868. property helpcontext : longint read fhelpcontext write fhelpcontext;
  3869. property message : string read fmessage write fmessage;
  3870. end;
  3871. ExceptClass = Class of Exception;
  3872. { mathematical exceptions }
  3873. EIntError = class(Exception);
  3874. EDivByZero = class(EIntError);
  3875. ERangeError = class(EIntError);
  3876. EIntOverflow = class(EIntError);
  3877. EMathError = class(Exception);
  3878. \end{verbatim}
  3879. The sysutils unit also installs an exception handler. If an exception is
  3880. unhandled by any exception handling block, this handler is called by the
  3881. Run-Time library. Basically, it prints the exception address, and it prints
  3882. the message of the Exception object, and exits with a exit code of 217.
  3883. If the exception object is not a descendent object of the \var{Exception}
  3884. object, then the class name is printed instead of the exception message.
  3885. It is recommended to use the \var{Exception} object or a descendant class for
  3886. all \var{raise} statements, since then you can use the message field of the
  3887. exception object.
  3888. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3889. % Using Assembler
  3890. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3891. \chapter{Using assembler}
  3892. \fpc supports the use of assembler in your code, but not inline
  3893. assembler macros. To have more information on the processor
  3894. specific assembler syntax and its limitations, see the \progref.
  3895. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3896. % Assembler statements
  3897. \section{Assembler statements }
  3898. The following is an example of assembler inclusion in your code.
  3899. \begin{verbatim}
  3900. ...
  3901. Statements;
  3902. ...
  3903. Asm
  3904. your asm code here
  3905. ...
  3906. end;
  3907. ...
  3908. Statements;
  3909. \end{verbatim}
  3910. The assembler instructions between the \var{Asm} and \var{end} keywords will
  3911. be inserted in the assembler generated by the compiler.
  3912. You can still use conditionals in your assembler, the compiler will
  3913. recognise it, and treat it as any other conditionals.
  3914. \begin{remark}
  3915. Before version 0.99.1, \fpc did not support reference to variables by
  3916. their names in the assembler parts of your code.
  3917. \end{remark}
  3918. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3919. % Assembler procedures and functions
  3920. \section{Assembler procedures and functions}
  3921. Assembler procedures and functions are declared using the
  3922. \var{Assembler} directive. The \var{Assembler} keyword is supported
  3923. as of version 0.9.7. This permits the code generator to make a number
  3924. of code generation optimizations.
  3925. The code generator does not generate any stack frame (entry and exit
  3926. code for the routine) if it contains no local variables and no
  3927. parameters. In the case of functions, ordinal values must be returned
  3928. in the accumulator. In the case of floating point values, these depend
  3929. on the target processor and emulation options.
  3930. \begin{remark} From version 0.99.1 to 0.99.5 (\emph{excluding}
  3931. FPC 0.99.5a), the \var{Assembler} directive did not have the
  3932. same effect as in Turbo Pascal, so beware! The stack frame would be
  3933. omitted if there were no local variables, in this case if the assembly
  3934. routine had any parameters, they would be referenced directly via the stack
  3935. pointer. This was \emph{ NOT} like Turbo Pascal where the stack frame is only
  3936. omitted if there are no parameters \emph{ and } no local variables. As
  3937. stated earlier, starting from version 0.99.5a, \fpc now has the same
  3938. behaviour as Turbo Pascal.
  3939. \end{remark}
  3940. %
  3941. % System unit reference guide.
  3942. %
  3943. \part{Reference : The System unit}
  3944. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3945. % The system unit
  3946. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3947. \chapter{The system unit}
  3948. \label{ch:refchapter}
  3949. \FPCexampledir{refex}
  3950. The system unit contains the standard supported functions of \fpc. It is the
  3951. same for all platforms. Basically it is the same as the system unit provided
  3952. with Borland or Turbo Pascal.
  3953. Functions are listed in alphabetical order. Arguments of functions or
  3954. procedures that are optional are put between square brackets.
  3955. The pre-defined constants and variables are listed in the first section. The
  3956. second section contains the supported functions and procedures.
  3957. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3958. % Types, Constants and Variables
  3959. \section{Types, Constants and Variables}
  3960. \subsection{Types}
  3961. The following integer types are defined in the System unit:
  3962. \begin{verbatim}
  3963. shortint = -128..127;
  3964. Longint = $80000000..$7fffffff;
  3965. integer = -32768..32767;
  3966. byte = 0..255;
  3967. word = 0..65535;
  3968. \end{verbatim}
  3969. And the following pointer types:
  3970. \begin{verbatim}
  3971. PChar = ^char;
  3972. pPChar = ^PChar;
  3973. \end{verbatim}
  3974. For the \seef{SetJmp} and \seep{LongJmp} calls, the following jump bufer
  3975. type is defined (for the I386 processor):
  3976. \begin{verbatim}
  3977. jmp_buf = record
  3978. ebx,esi,edi : Longint;
  3979. bp,sp,pc : Pointer;
  3980. end;
  3981. PJmp_buf = ^jmp_buf;
  3982. \end{verbatim}
  3983. \subsection{Constants}
  3984. The following constants for file-handling are defined in the system unit:
  3985. \begin{verbatim}
  3986. Const
  3987. fmclosed = $D7B0;
  3988. fminput = $D7B1;
  3989. fmoutput = $D7B2;
  3990. fminout = $D7B3;
  3991. fmappend = $D7B4;
  3992. filemode : byte = 2;
  3993. \end{verbatim}
  3994. Further, the following non processor specific general-purpose constants
  3995. are also defined:
  3996. \begin{lstlisting}{}
  3997. const
  3998. erroraddr : pointer = nil;
  3999. errorcode : word = 0;
  4000. { max level in dumping on error }
  4001. max_frame_dump : word = 20;
  4002. \end{lstlisting}{}
  4003. \begin{remark}
  4004. Processor specific global constants are named Testxxxx where xxxx
  4005. represents the processor number (such as Test8086, Test68000),
  4006. and are used to determine on what generation of processor the program
  4007. is running on.
  4008. \end{remark}
  4009. \subsection{Variables}
  4010. The following variables are defined and initialized in the system unit:
  4011. \begin{verbatim}
  4012. var
  4013. output,input,stderr : text;
  4014. exitproc : pointer;
  4015. exitcode : word;
  4016. stackbottom : Longint;
  4017. loweststack : Longint;
  4018. \end{verbatim}
  4019. The variables \var{ExitProc}, \var{exitcode} are used in the \fpc exit
  4020. scheme. It works similarly to the one in Turbo Pascal:
  4021. When a program halts (be it through the call of the \var{Halt} function or
  4022. \var{Exit} or through a run-time error), the exit mechanism checks the value
  4023. of \var{ExitProc}. If this one is non-\var{Nil}, it is set to \var{Nil}, and
  4024. the procedure is called. If the exit procedure exits, the value of ExitProc
  4025. is checked again. If it is non-\var{Nil} then the above steps are repeated.
  4026. So if you want to install your exit procedure, you should save the old value
  4027. of \var{ExitProc} (may be non-\var{Nil}, since other units could have set it before
  4028. you did). In your exit procedure you then restore the value of
  4029. \var{ExitProc}, such that if it was non-\var{Nil} the exit-procedure can be
  4030. called.
  4031. The \var{ErrorAddr} and \var{ExitCode} can be used to check for
  4032. error-conditions. If \var{ErrorAddr} is non-\var{Nil}, a run-time error has
  4033. occurred. If so, \var{ExitCode} contains the error code. If \var{ErrorAddr} is
  4034. \var{Nil}, then {ExitCode} contains the argument to \var{Halt} or 0 if the
  4035. program terminated normally.
  4036. \var{ExitCode} is always passed to the operating system as the exit-code of
  4037. your process.
  4038. Under \file{GO32}, the following constants are also defined :
  4039. \begin{verbatim}
  4040. const
  4041. seg0040 = $0040;
  4042. segA000 = $A000;
  4043. segB000 = $B000;
  4044. segB800 = $B800;
  4045. \end{verbatim}
  4046. These constants allow easy access to the bios/screen segment via mem/absolute.
  4047. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4048. % Functions and Procedures
  4049. \section{Functions and Procedures}
  4050. \begin{function}{Abs}
  4051. \Declaration
  4052. Function Abs (X : Every numerical type) : Every numerical type;
  4053. \Description
  4054. \var{Abs} returns the absolute value of a variable. The result of the
  4055. function has the same type as its argument, which can be any numerical
  4056. type.
  4057. \Errors
  4058. None.
  4059. \SeeAlso
  4060. \seef{Round}
  4061. \end{function}
  4062. \FPCexample{ex1}
  4063. \begin{function}{Addr}
  4064. \Declaration
  4065. Function Addr (X : Any type) : Pointer;
  4066. \Description
  4067. \var{Addr} returns a pointer to its argument, which can be any type, or a
  4068. function or procedure name. The returned pointer isn't typed.
  4069. The same result can be obtained by the \var{@} operator, which can return a
  4070. typed pointer (\progref).
  4071. \Errors
  4072. None
  4073. \SeeAlso
  4074. \seef{SizeOf}
  4075. \end{function}
  4076. \FPCexample{ex2}
  4077. \begin{procedure}{Append}
  4078. \Declaration
  4079. Procedure Append (Var F : Text);
  4080. \Description
  4081. \var{Append} opens an existing file in append mode. Any data written to
  4082. \var{F} will be appended to the file. If the file didn't exist, it will be
  4083. created, contrary to the Turbo Pascal implementation of \var{Append}, where
  4084. a file needed to exist in order to be opened by
  4085. \var{Append}.
  4086. Only text files can be opened in append mode.
  4087. \Errors
  4088. If the file can't be created, a run-time error will be generated.
  4089. \SeeAlso
  4090. \seep{Rewrite},\seep{Close}, \seep{Reset}
  4091. \end{procedure}
  4092. \FPCexample{ex3}
  4093. \begin{function}{Arctan}
  4094. \Declaration
  4095. Function Arctan (X : Real) : Real;
  4096. \Description
  4097. \var{Arctan} returns the Arctangent of \var{X}, which can be any Real type.
  4098. The resulting angle is in radial units.
  4099. \Errors
  4100. None
  4101. \SeeAlso
  4102. \seef{Sin}, \seef{Cos}
  4103. \end{function}
  4104. \FPCexample{ex4}
  4105. \begin{procedure}{Assign}
  4106. \Declaration
  4107. Procedure Assign (Var F; Name : String);
  4108. \Description
  4109. \var{Assign} assigns a name to \var{F}, which can be any file type.
  4110. This call doesn't open the file, it just assigns a name to a file variable,
  4111. and marks the file as closed.
  4112. \Errors
  4113. None.
  4114. \SeeAlso
  4115. \seep{Reset}, \seep{Rewrite}, \seep{Append}
  4116. \end{procedure}
  4117. \FPCexample{ex5}
  4118. \begin{function}{Assigned}
  4119. \Declaration
  4120. Function Assigned (P : Pointer) : Boolean;
  4121. \Description
  4122. \var{Assigned} returns \var{True} if \var{P} is non-nil
  4123. and retuns \var{False} of \var{P} is nil.
  4124. The main use of Assigned is that Procedural variables, method variables and
  4125. class-type variables also can be passed to \var{Assigned}.
  4126. \Errors
  4127. None
  4128. \SeeAlso
  4129. \seep{New}
  4130. \end{function}
  4131. \FPCexample{ex96}
  4132. \begin{function}{BinStr}
  4133. \Declaration
  4134. Function BinStr (Value : longint; cnt : byte) : String;
  4135. \Description
  4136. \var{BinStr} returns a string with the binary representation
  4137. of \var{Value}. The string has at most \var{cnt} characters.
  4138. (i.e. only the \var{cnt} rightmost bits are taken into account)
  4139. To have a complete representation of any longint-type value, you need 32
  4140. bits, i.e. \var{cnt=32}
  4141. \Errors
  4142. None.
  4143. \SeeAlso
  4144. \seep{Str},\seep{Val},\seef{HexStr}
  4145. \end{function}
  4146. \FPCexample{ex82}
  4147. \begin{procedure}{Blockread}
  4148. \Declaration
  4149. Procedure Blockread (Var F : File; Var Buffer; Var Count : Longint [; var
  4150. Result : Longint]);
  4151. \Description
  4152. \var{Blockread} reads \var{count} or less records from file \var{F}. A
  4153. record is a block of bytes with size specified by the \seep{Rewrite} or
  4154. \seep{Reset} statement.
  4155. The result is placed in \var{Buffer}, which must contain enough room for
  4156. \var{Count} records. The function cannot read partial records.
  4157. If \var{Result} is specified, it contains the number of records actually
  4158. read. If \var{Result} isn't specified, and less than \var{Count} records were
  4159. read, a run-time error is generated. This behavior can be controlled by the
  4160. \var{\{\$i\}} switch.
  4161. \Errors
  4162. If \var{Result} isn't specified, then a run-time error is generated if less
  4163. than \var{count} records were read.
  4164. \SeeAlso
  4165. \seep{Blockwrite}, \seep{Close}, \seep{Reset}, \seep{Assign}
  4166. \end{procedure}
  4167. \FPCexample{ex6}
  4168. \begin{procedure}{Blockwrite}
  4169. \Declaration
  4170. Procedure Blockwrite (Var F : File; Var Buffer; Var Count : Longint);
  4171. \Description
  4172. \var{BlockWrite} writes \var{count} records from \var{buffer} to the file
  4173. \var{F}.A record is a block of bytes with size specified by the \seep{Rewrite} or
  4174. \seep{Reset} statement.
  4175. If the records couldn't be written to disk, a run-time error is generated.
  4176. This behavior can be controlled by the \var{\{\$i\}} switch.
  4177. \Errors
  4178. A run-time error is generated if, for some reason, the records couldn't be
  4179. written to disk.
  4180. \SeeAlso
  4181. \seep{Blockread},\seep{Close}, \seep{Rewrite}, \seep{Assign}
  4182. \end{procedure}
  4183. For the example, see \seep{Blockread}.
  4184. \begin{procedure}{Break}
  4185. \Declaration
  4186. Procedure Break;
  4187. \Description
  4188. \var{Break} jumps to the statement following the end of the current
  4189. repetitive statement. The code between the \var{Break} call and
  4190. the end of the repetitive statement is skipped.
  4191. The condition of the repetitive statement is NOT evaluated.
  4192. This can be used with \var{For}, var{repeat} and \var{While} statements.
  4193. Note that while this is a procedure, \var{Break} is a reserved word
  4194. and hence cannot be redefined.
  4195. \Errors
  4196. None.
  4197. \SeeAlso
  4198. \seep{Continue}, \seep{Exit}
  4199. \end{procedure}
  4200. \FPCexample{ex87}
  4201. \begin{procedure}{Chdir}
  4202. \Declaration
  4203. Procedure Chdir (const S : string);
  4204. \Description
  4205. \var{Chdir} changes the working directory of the process to \var{S}.
  4206. \Errors
  4207. If the directory \var{S} doesn't exist, a run-time error is generated.
  4208. \SeeAlso
  4209. \seep{Mkdir}, \seep{Rmdir}
  4210. \end{procedure}
  4211. \FPCexample{ex7}
  4212. \begin{function}{Chr}
  4213. \Declaration
  4214. Function Chr (X : byte) : Char;
  4215. \Description
  4216. \var{Chr} returns the character which has ASCII value \var{X}.
  4217. \Errors
  4218. None.
  4219. \SeeAlso
  4220. \seef{Ord}, \seep{Str}
  4221. \end{function}
  4222. \FPCexample{ex8}
  4223. \begin{procedure}{Close}
  4224. \Declaration
  4225. Procedure Close (Var F : Anyfiletype);
  4226. \Description
  4227. \var{Close} flushes the buffer of the file \var{F} and closes \var{F}.
  4228. After a call to \var{Close}, data can no longer be read from or written to
  4229. \var{F}.
  4230. To reopen a file closed with \var{Close}, it isn't necessary to assign the
  4231. file again. A call to \seep{Reset} or \seep{Rewrite} is sufficient.
  4232. \Errors
  4233. None.
  4234. \SeeAlso
  4235. \seep{Assign}, \seep{Reset}, \seep{Rewrite}, \seep{Flush}
  4236. \end{procedure}
  4237. \FPCexample{ex9}
  4238. \begin{function}{Concat}
  4239. \Declaration
  4240. Function Concat (S1,S2 [,S3, ... ,Sn]) : String;
  4241. \Description
  4242. \var{Concat} concatenates the strings \var{S1},\var{S2} etc. to one long
  4243. string. The resulting string is truncated at a length of 255 bytes.
  4244. The same operation can be performed with the \var{+} operation.
  4245. \Errors
  4246. None.
  4247. \SeeAlso
  4248. \seef{Copy}, \seep{Delete}, \seep{Insert}, \seef{Pos}, \seef{Length}
  4249. \end{function}
  4250. \FPCexample{ex10}
  4251. \begin{procedure}{Continue}
  4252. \Declaration
  4253. Procedure Continue;
  4254. \Description
  4255. \var{Continue} jumps to the end of the current repetitive statement.
  4256. The code between the \var{Continue} call and the end of the repetitive
  4257. statement is skipped. The condition of the repetitive statement is then
  4258. checked again.
  4259. This can be used with \var{For}, var{repeat} and \var{While} statements.
  4260. Note that while this is a procedure, \var{Continue} is a reserved word
  4261. and hence cannot be redefined.
  4262. \Errors
  4263. None.
  4264. \SeeAlso
  4265. \seep{Break}, \seep{Exit}
  4266. \end{procedure}
  4267. \FPCexample{ex86}
  4268. \begin{function}{Copy}
  4269. \Declaration
  4270. Function Copy (Const S : String;Index : Integer;Count : Byte) : String;
  4271. \Description
  4272. \var{Copy} returns a string which is a copy if the \var{Count} characters
  4273. in \var{S}, starting at position \var{Index}. If \var{Count} is larger than
  4274. the length of the string \var{S}, the result is truncated.
  4275. If \var{Index} is larger than the length of the string \var{S}, then an
  4276. empty string is returned.
  4277. \Errors
  4278. None.
  4279. \SeeAlso
  4280. \seep{Delete}, \seep{Insert}, \seef{Pos}
  4281. \end{function}
  4282. \FPCexample{ex11}
  4283. \begin{function}{Cos}
  4284. \Declaration
  4285. Function Cos (X : Real) : Real;
  4286. \Description
  4287. \var{Cos} returns the cosine of \var{X}, where X is an angle, in radians.
  4288. \Errors
  4289. None.
  4290. \SeeAlso
  4291. \seef{Arctan}, \seef{Sin}
  4292. \end{function}
  4293. \FPCexample{ex12}
  4294. \begin{function}{CSeg}
  4295. \Declaration
  4296. Function CSeg : Word;
  4297. \Description
  4298. \var{CSeg} returns the Code segment register. In \fpc, it returns always a
  4299. zero, since \fpc is a 32 bit compiler.
  4300. \Errors
  4301. None.
  4302. \SeeAlso
  4303. \seef{DSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}
  4304. \end{function}
  4305. \FPCexample{ex13}
  4306. \begin{procedure}{Dec}
  4307. \Declaration
  4308. Procedure Dec (Var X : Any ordinal type[; Decrement : Longint]);
  4309. \Description
  4310. \var{Dec} decreases the value of \var{X} with \var{Decrement}.
  4311. If \var{Decrement} isn't specified, then 1 is taken as a default.
  4312. \Errors
  4313. A range check can occur, or an underflow error, if you try to decrease \var{X}
  4314. below its minimum value.
  4315. \SeeAlso
  4316. \seep{Inc}
  4317. \end{procedure}
  4318. \FPCexample{ex14}
  4319. \begin{procedure}{Delete}
  4320. \Declaration
  4321. Procedure Delete (var S : string;Index : Integer;Count : Integer);
  4322. \Description
  4323. \var{Delete} removes \var{Count} characters from string \var{S}, starting
  4324. at position \var{Index}. All characters after the delected characters are
  4325. shifted \var{Count} positions to the left, and the length of the string is adjusted.
  4326. \Errors
  4327. None.
  4328. \SeeAlso
  4329. \seef{Copy},\seef{Pos},\seep{Insert}
  4330. \end{procedure}
  4331. \FPCexample{ex15}
  4332. \begin{procedure}{Dispose}
  4333. \Declaration
  4334. Procedure Dispose (P : pointer);\\
  4335. Procedure Dispiose (P : Typed Pointer; Des : Procedure);
  4336. \Description
  4337. The first form \var{Dispose} releases the memory allocated with a call to
  4338. \seep{New}. The pointer \var{P} must be typed. The released memory is
  4339. returned to the heap.
  4340. The second form of \var{Dispose} accepts as a first parameter a pointer
  4341. to an object type, and as a second parameter the name of a destructor
  4342. of this object. The destructor will be called, and the memory allocated
  4343. for the object will be freed.
  4344. \Errors
  4345. An error will occur if the pointer doesn't point to a location in the
  4346. heap.
  4347. \SeeAlso
  4348. \seep{New}, \seep{Getmem}, \seep{Freemem}
  4349. \end{procedure}
  4350. \FPCexample{ex16}
  4351. \begin{function}{DSeg}
  4352. \Declaration
  4353. Function DSeg : Word;
  4354. \Description
  4355. \var{DSeg} returns the data segment register. In \fpc, it returns always a
  4356. zero, since \fpc is a 32 bit compiler.
  4357. \Errors
  4358. None.
  4359. \SeeAlso
  4360. \seef{CSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}
  4361. \end{function}
  4362. \FPCexample{ex17}
  4363. \begin{function}{Eof}
  4364. \Declaration
  4365. Function Eof [(F : Any file type)] : Boolean;
  4366. \Description
  4367. \var{Eof} returns \var{True} if the file-pointer has reached the end of the
  4368. file, or if the file is empty. In all other cases \var{Eof} returns
  4369. \var{False}.
  4370. If no file \var{F} is specified, standard input is assumed.
  4371. \Errors
  4372. None.
  4373. \SeeAlso
  4374. \seef{Eoln}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}
  4375. \end{function}
  4376. \FPCexample{ex18}
  4377. \begin{function}{Eoln}
  4378. \Declaration
  4379. Function Eoln [(F : Text)] : Boolean;
  4380. \Description
  4381. \var{Eof} returns \var{True} if the file pointer has reached the end of a
  4382. line, which is demarcated by a line-feed character (ASCII value 10), or if
  4383. the end of the file is reached.
  4384. In all other cases \var{Eof} returns \var{False}.
  4385. If no file \var{F} is specified, standard input is assumed.
  4386. It can only be used on files of type \var{Text}.
  4387. \Errors
  4388. None.
  4389. \SeeAlso
  4390. \seef{Eof}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}
  4391. \end{function}
  4392. \FPCexample{ex19}
  4393. \begin{procedure}{Erase}
  4394. \Declaration
  4395. Procedure Erase (Var F : Any file type);
  4396. \Description
  4397. \var{Erase} removes an unopened file from disk. The file should be
  4398. assigned with \var{Assign}, but not opened with \var{Reset} or \var{Rewrite}
  4399. \Errors
  4400. A run-time error will be generated if the specified file doesn't exist, or
  4401. is opened by the program.
  4402. \SeeAlso
  4403. \seep{Assign}
  4404. \end{procedure}
  4405. \FPCexample{ex20}
  4406. \begin{procedure}{Exit}
  4407. \Declaration
  4408. Procedure Exit ([Var X : return type )];
  4409. \Description
  4410. \var{Exit} exits the current subroutine, and returns control to the calling
  4411. routine. If invoked in the main program routine, exit stops the program.
  4412. The optional argument \var{X} allows to specify a return value, in the case
  4413. \var{Exit} is invoked in a function. The function result will then be
  4414. equal to \var{X}.
  4415. \Errors
  4416. None.
  4417. \SeeAlso
  4418. \seep{Halt}
  4419. \end{procedure}
  4420. \FPCexample{ex21}
  4421. \begin{function}{Exp}
  4422. \Declaration
  4423. Function Exp (Var X : Real) : Real;
  4424. \Description
  4425. \var{Exp} returns the exponent of \var{X}, i.e. the number \var{e} to the
  4426. power \var{X}.
  4427. \Errors
  4428. None.
  4429. \SeeAlso
  4430. \seef{Ln}, \seef{Power}
  4431. \end{function}
  4432. \FPCexample{ex22}
  4433. \begin{function}{Filepos}
  4434. \Declaration
  4435. Function Filepos (Var F : Any file type) : Longint;
  4436. \Description
  4437. \var{Filepos} returns the current record position of the file-pointer in file
  4438. \var{F}. It cannot be invoked with a file of type \var{Text}. If you try to
  4439. do this, a compiler error will be generated.
  4440. \Errors
  4441. None.
  4442. \SeeAlso
  4443. \seef{Filesize}
  4444. \end{function}
  4445. \FPCexample{ex23}
  4446. \begin{function}{Filesize}
  4447. \Declaration
  4448. Function Filesize (Var F : Any file type) : Longint;
  4449. \Description
  4450. \var{Filesize} returns the total number of records in file \var{F}.
  4451. It cannot be invoked with a file of type \var{Text}. (under \linux, this
  4452. also means that it cannot be invoked on pipes.)
  4453. If \var{F} is empty, 0 is returned.
  4454. \Errors
  4455. None.
  4456. \SeeAlso
  4457. \seef{Filepos}
  4458. \end{function}
  4459. \FPCexample{ex24}
  4460. \begin{procedure}{Fillchar}
  4461. \Declaration
  4462. Procedure Fillchar (Var X;Count : Longint;Value : char or byte);;
  4463. \Description
  4464. \var{Fillchar} fills the memory starting at \var{X} with \var{Count} bytes
  4465. or characters with value equal to \var{Value}.
  4466. \Errors
  4467. No checking on the size of \var{X} is done.
  4468. \SeeAlso
  4469. \seep{Fillword}, \seep{Move}
  4470. \end{procedure}
  4471. \FPCexample{ex25}
  4472. \begin{procedure}{Fillword}
  4473. \Declaration
  4474. Procedure Fillword (Var X;Count : Longint;Value : Word);;
  4475. \Description
  4476. \var{Fillword} fills the memory starting at \var{X} with \var{Count} words
  4477. with value equal to \var{Value}.
  4478. \Errors
  4479. No checking on the size of \var{X} is done.
  4480. \SeeAlso
  4481. \seep{Fillchar}, \seep{Move}
  4482. \end{procedure}
  4483. \FPCexample{ex76}
  4484. \begin{procedure}{Flush}
  4485. \Declaration
  4486. Procedure Flush (Var F : Text);
  4487. \Description
  4488. \var{Flush} empties the internal buffer of an opened file \var{F} and writes the
  4489. contents to disk. The file is \textit{not} closed as a result of this call.
  4490. \Errors
  4491. If the disk is full, a run-time error will be generated.
  4492. \SeeAlso
  4493. \seep{Close}
  4494. \end{procedure}
  4495. \FPCexample{ex26}
  4496. \begin{function}{Frac}
  4497. \Declaration
  4498. Function Frac (X : Real) : Real;
  4499. \Description
  4500. \var{Frac} returns the non-integer part of \var{X}.
  4501. \Errors
  4502. None.
  4503. \SeeAlso
  4504. \seef{Round}, \seef{Int}
  4505. \end{function}
  4506. \FPCexample{ex27}
  4507. \begin{procedure}{Freemem}
  4508. \Declaration
  4509. Procedure Freemem (Var P : pointer; Count : Longint);
  4510. \Description
  4511. \var{Freemem} releases the memory occupied by the pointer \var{P}, of size
  4512. \var{Count} (in bytes), and returns it to the heap. \var{P} should point to the memory
  4513. allocated to a dynamical variable.
  4514. \Errors
  4515. An error will occur when \var{P} doesn't point to the heap.
  4516. \SeeAlso
  4517. \seep{Getmem}, \seep{New}, \seep{Dispose}
  4518. \end{procedure}
  4519. \FPCexample{ex28}
  4520. \begin{procedure}{Getdir}
  4521. \Declaration
  4522. Procedure Getdir (drivenr : byte;var dir : string);
  4523. \Description
  4524. \var{Getdir} returns in \var{dir} the current directory on the drive
  4525. \var{drivenr}, where {drivenr} is 1 for the first floppy drive, 3 for the
  4526. first hard disk etc. A value of 0 returns the directory on the current disk.
  4527. On \linux, \var{drivenr} is ignored, as there is only one directory tree.
  4528. \Errors
  4529. An error is returned under \dos, if the drive requested isn't ready.
  4530. \SeeAlso
  4531. \seep{Chdir}
  4532. \end{procedure}
  4533. \FPCexample{ex29}
  4534. \begin{procedure}{Getmem}
  4535. \Declaration
  4536. Procedure Getmem (var p : pointer;size : Longint);
  4537. \Description
  4538. \var{Getmem} reserves \var{Size} bytes memory on the heap, and returns a
  4539. pointer to this memory in \var{p}. If no more memory is available, nil is
  4540. returned.
  4541. \Errors
  4542. None.
  4543. \SeeAlso
  4544. \seep{Freemem}, \seep{Dispose}, \seep{New}
  4545. \end{procedure}
  4546. For an example, see \seep{Freemem}.
  4547. \begin{procedure}{Halt}
  4548. \Declaration
  4549. Procedure Halt [(Errnum : byte)];
  4550. \Description
  4551. \var{Halt} stops program execution and returns control to the calling
  4552. program. The optional argument \var{Errnum} specifies an exit value. If
  4553. omitted, zero is returned.
  4554. \Errors
  4555. None.
  4556. \SeeAlso
  4557. \seep{Exit}
  4558. \end{procedure}
  4559. \FPCexample{ex30}
  4560. \begin{function}{HexStr}
  4561. \Declaration
  4562. Function HexStr (Value : longint; cnt : byte) : String;
  4563. \Description
  4564. \var{HexStr} returns a string with the hexadecimal representation
  4565. of \var{Value}. The string has at most \var{cnt} charaters.
  4566. (i.e. only the \var{cnt} rightmost nibbles are taken into account)
  4567. To have a complete representation of a Longint-type value, you need 8
  4568. nibbles, i.e. \var{cnt=8}.
  4569. \Errors
  4570. None.
  4571. \SeeAlso
  4572. \seep{Str}, \seep{Val}, \seef{BinStr}
  4573. \end{function}
  4574. \FPCexample{ex81}
  4575. \begin{function}{Hi}
  4576. \Declaration
  4577. Function Hi (X : Ordinal type) : Word or byte;
  4578. \Description
  4579. \var{Hi} returns the high byte or word from \var{X}, depending on the size
  4580. of X. If the size of X is 4, then the high word is returned. If the size is
  4581. 2 then the high byte is returned.
  4582. \var{Hi} cannot be invoked on types of size 1, such as byte or char.
  4583. \Errors
  4584. None
  4585. \SeeAlso
  4586. \seef{Lo}
  4587. \end{function}
  4588. \FPCexample{ex31}
  4589. \begin{function}{High}
  4590. \Declaration
  4591. Function High (Type identifier or variable reference) : Longint;
  4592. \Description
  4593. The return value of \var{High} depends on it's argument:
  4594. \begin{enumerate}
  4595. \item If the argument is an ordinal type, \var{High} returns the lowest
  4596. value in the range of the given ordinal type.
  4597. \item If the argument is an array type or an array type variable then
  4598. \var{High} returns the highest possible value of it's index.
  4599. \item If the argument is an open array identifier in a function or
  4600. procedure, then \var{High} returns the highest index of the array, as if the
  4601. array has a zero-based index.
  4602. \end{enumerate}
  4603. \Errors
  4604. None.
  4605. \SeeAlso
  4606. \seef{Low}, \seef{Ord}, \seef{Pred}, \seef{Succ}
  4607. \end{function}
  4608. \FPCexample{ex80}
  4609. \begin{procedure}{Inc}
  4610. \Declaration
  4611. Procedure Inc (Var X : Any ordinal type[; Increment : Longint]);
  4612. \Description
  4613. \var{Inc} increases the value of \var{X} with \var{Increment}.
  4614. If \var{Increment} isn't specified, then 1 is taken as a default.
  4615. \Errors
  4616. If range checking is on, then A range check can occur, or an overflow
  4617. error, if you try to increase \var{X} over its maximum value.
  4618. \SeeAlso
  4619. \seep{Dec}
  4620. \end{procedure}
  4621. \FPCexample{ex32}
  4622. \begin{procedure}{Insert}
  4623. \Declaration
  4624. Procedure Insert (Const Source : String;var S : String;Index : Longint);
  4625. \Description
  4626. \var{Insert} inserts string \var{Source} in string \var{S}, at position
  4627. \var{Index}, shifting all characters after \var{Index} to the right. The
  4628. resulting string is truncated at 255 characters, if needed. (i.e. for
  4629. shortstrings)
  4630. \Errors
  4631. None.
  4632. \SeeAlso
  4633. \seep{Delete}, \seef{Copy}, \seef{Pos}
  4634. \end{procedure}
  4635. \FPCexample{ex33}
  4636. \begin{function}{Int}
  4637. \Declaration
  4638. Function Int (X : Real) : Real;
  4639. \Description
  4640. \var{Int} returns the integer part of any Real \var{X}, as a Real.
  4641. \Errors
  4642. None.
  4643. \SeeAlso
  4644. \seef{Frac}, \seef{Round}
  4645. \end{function}
  4646. \FPCexample{ex34}
  4647. \begin{function}{IOresult}
  4648. \Declaration
  4649. Function IOresult : Word;
  4650. \Description
  4651. IOresult contains the result of any input/output call, when the
  4652. \var{\{\$i-\}} compiler directive is active, disabling IO checking.
  4653. When the flag is read, it is reset to zero.
  4654. If \var{IOresult} is zero, the operation completed successfully. If
  4655. non-zero, an error occurred. The following errors can occur:
  4656. \dos errors :
  4657. \begin{description}
  4658. \item [2\ ] File not found.
  4659. \item [3\ ] Path not found.
  4660. \item [4\ ] Too many open files.
  4661. \item [5\ ] Access denied.
  4662. \item [6\ ] Invalid file handle.
  4663. \item [12\ ] Invalid file-access mode.
  4664. \item [15\ ] Invalid disk number.
  4665. \item [16\ ] Cannot remove current directory.
  4666. \item [17\ ] Cannot rename across volumes.
  4667. \end{description}
  4668. I/O errors :
  4669. \begin{description}
  4670. \item [100\ ] Error when reading from disk.
  4671. \item [101\ ] Error when writing to disk.
  4672. \item [102\ ] File not assigned.
  4673. \item [103\ ] File not open.
  4674. \item [104\ ] File not opened for input.
  4675. \item [105\ ] File not opened for output.
  4676. \item [106\ ] Invalid number.
  4677. \end{description}
  4678. Fatal errors :
  4679. \begin{description}
  4680. \item [150\ ] Disk is write protected.
  4681. \item [151\ ] Unknown device.
  4682. \item [152\ ] Drive not ready.
  4683. \item [153\ ] Unknown command.
  4684. \item [154\ ] CRC check failed.
  4685. \item [155\ ] Invalid drive specified..
  4686. \item [156\ ] Seek error on disk.
  4687. \item [157\ ] Invalid media type.
  4688. \item [158\ ] Sector not found.
  4689. \item [159\ ] Printer out of paper.
  4690. \item [160\ ] Error when writing to device.
  4691. \item [161\ ] Error when reading from device.
  4692. \item [162\ ] Hardware failure.
  4693. \end{description}
  4694. \Errors
  4695. None.
  4696. \SeeAlso
  4697. All I/O functions.
  4698. \end{function}
  4699. \FPCexample{ex35}
  4700. \begin{function}{Length}
  4701. \Declaration
  4702. Function Length (S : String) : Byte;
  4703. \Description
  4704. \var{Length} returns the length of the string \var{S}, which is limited
  4705. to 255 for shortstrings. If the strings \var{S} is empty, 0 is returned.
  4706. {\em Note:} The length of the string \var{S} is stored in \var{S[0]} for
  4707. shortstrings only. Ansistrings have their length stored elsewhere,
  4708. the \var{Length} fuction should always be used on ansistrings.
  4709. \Errors
  4710. None.
  4711. \SeeAlso
  4712. \seef{Pos}
  4713. \end{function}
  4714. \FPCexample{ex36}
  4715. \begin{function}{Ln}
  4716. \Declaration
  4717. Function Ln (X : Real) : Real;
  4718. \Description
  4719. \var{Ln} returns the natural logarithm of the Real parameter \var{X}.
  4720. \var{X} must be positive.
  4721. \Errors
  4722. An run-time error will occur when \var{X} is negative.
  4723. \SeeAlso
  4724. \seef{Exp}, \seef{Power}
  4725. \end{function}
  4726. \FPCexample{ex37}
  4727. \begin{function}{Lo}
  4728. \Declaration
  4729. Function Lo (O : Word or Longint) : Byte or Word;
  4730. \Description
  4731. \var{Lo} returns the low byte of its argument if this is of type
  4732. \var{Integer} or
  4733. \var{Word}. It returns the low word of its argument if this is of type
  4734. \var{Longint} or \var{Cardinal}.
  4735. \Errors
  4736. None.
  4737. \SeeAlso
  4738. \seef{Ord}, \seef{Chr}, \seef{Hi}
  4739. \end{function}
  4740. \FPCexample{ex38}
  4741. \begin{procedure}{LongJmp}
  4742. \Declaration
  4743. Procedure LongJmp (Var env : Jmp\_Buf; Value : Longint);
  4744. \Description
  4745. \var{LongJmp} jumps to the adress in the \var{env} \var{jmp\_buf},
  4746. and resores the registers that were stored in it at the corresponding
  4747. \seef{SetJmp} call.
  4748. In effect, program flow will continue at the \var{SetJmp} call, which will
  4749. return \var{value} instead of 0. If you pas a \var{value} equal to zero, it will be
  4750. converted to 1 before passing it on. The call will not return, so it must be
  4751. used with extreme care.
  4752. This can be used for error recovery, for instance when a segmentation fault
  4753. occurred.
  4754. \Errors
  4755. None.
  4756. \SeeAlso
  4757. \seef{SetJmp}
  4758. \end{procedure}
  4759. For an example, see \seef{SetJmp}
  4760. \begin{function}{Low}
  4761. \Declaration
  4762. Function Low (Type identifier or variable reference) : Longint;
  4763. \Description
  4764. The return value of \var{Low} depends on it's argument:
  4765. \begin{enumerate}
  4766. \item If the argument is an ordinal type, \var{Low} returns the lowest
  4767. value in the range of the given ordinal type.
  4768. \item If the argument is an array type or an array type variable then
  4769. \var{Low} returns the lowest possible value of it's index.
  4770. \end{enumerate}
  4771. \Errors
  4772. None.
  4773. \SeeAlso
  4774. \seef{High}, \seef{Ord}, \seef{Pred}, \seef{Succ}
  4775. \end{function}
  4776. for an example, see \seef{High}.
  4777. \begin{function}{Lowercase}
  4778. \Declaration
  4779. Function Lowercase (C : Char or String) : Char or String;
  4780. \Description
  4781. \var{Lowercase} returns the lowercase version of its argument \var{C}.
  4782. If its argument is a string, then the complete string is converted to
  4783. lowercase. The type of the returned value is the same as the type of the
  4784. argument.
  4785. \Errors
  4786. None.
  4787. \SeeAlso
  4788. \seef{Upcase}
  4789. \end{function}
  4790. \FPCexample{ex73}
  4791. \begin{procedure}{Mark}
  4792. \Declaration
  4793. Procedure Mark (Var P : Pointer);
  4794. \Description
  4795. \var{Mark} copies the current heap-pointer to \var{P}.
  4796. \Errors
  4797. None.
  4798. \SeeAlso
  4799. \seep{Getmem}, \seep{Freemem}, \seep{New}, \seep{Dispose}, \seef{Maxavail}
  4800. \end{procedure}
  4801. \FPCexample{ex39}
  4802. \begin{function}{Maxavail}
  4803. \Declaration
  4804. Function Maxavail : Longint;
  4805. \Description
  4806. \var{Maxavail} returns the size, in bytes, of the biggest free memory block in
  4807. the heap.
  4808. \begin{remark}
  4809. The heap grows dynamically if more memory is needed than is available.
  4810. \end{remark}
  4811. \Errors
  4812. None.
  4813. \SeeAlso
  4814. \seep{Release}, \seef{Memavail},\seep{Freemem}, \seep{Getmem}
  4815. \end{function}
  4816. \FPCexample{ex40}
  4817. \begin{function}{Memavail}
  4818. \Declaration
  4819. Function Memavail : Longint;
  4820. \Description
  4821. \var{Memavail} returns the size, in bytes, of the free heap memory.
  4822. \begin{remark}
  4823. The heap grows dynamically if more memory is needed than is available.
  4824. \end{remark}
  4825. \Errors
  4826. None.
  4827. \SeeAlso
  4828. \seef{Maxavail},\seep{Freemem}, \seep{Getmem}
  4829. \end{function}
  4830. \FPCexample{ex41}
  4831. \begin{procedure}{Mkdir}
  4832. \Declaration
  4833. Procedure Mkdir (const S : string);
  4834. \Description
  4835. \var{Mkdir} creates a new directory \var{S}.
  4836. \Errors
  4837. If a parent-directory of directory \var{S} doesn't exist, a run-time error is generated.
  4838. \SeeAlso
  4839. \seep{Chdir}, \seep{Rmdir}
  4840. \end{procedure}
  4841. For an example, see \seep{Rmdir}.
  4842. \begin{procedure}{Move}
  4843. \Declaration
  4844. Procedure Move (var Source,Dest;Count : Longint);
  4845. \Description
  4846. \var{Move} moves \var{Count} bytes from \var{Source} to \var{Dest}.
  4847. \Errors
  4848. If either \var{Dest} or \var{Source} is outside the accessible memory for
  4849. the process, then a run-time error will be generated. With older versions of
  4850. the compiler, a segmentation-fault will occur.
  4851. \SeeAlso
  4852. \seep{Fillword}, \seep{Fillchar}
  4853. \end{procedure}
  4854. \FPCexample{ex42}
  4855. \begin{procedure}{New}
  4856. \Declaration
  4857. Procedure New (Var P : Pointer[, Constructor]);
  4858. \Description
  4859. \var{New} allocates a new instance of the type pointed to by \var{P}, and
  4860. puts the address in \var{P}.
  4861. If P is an object, then it is possible to
  4862. specify the name of the constructor with which the instance will be created.
  4863. \Errors
  4864. If not enough memory is available, \var{Nil} will be returned.
  4865. \SeeAlso
  4866. \seep{Dispose}, \seep{Freemem}, \seep{Getmem}, \seef{Memavail},
  4867. \seef{Maxavail}
  4868. \end{procedure}
  4869. For an example, see \seep{Dispose}.
  4870. \begin{function}{Odd}
  4871. \Declaration
  4872. Function Odd (X : Longint) : Boolean;
  4873. \Description
  4874. \var{Odd} returns \var{True} if \var{X} is odd, or \var{False} otherwise.
  4875. \Errors
  4876. None.
  4877. \SeeAlso
  4878. \seef{Abs}, \seef{Ord}
  4879. \end{function}
  4880. \FPCexample{ex43}
  4881. \begin{function}{Ofs}
  4882. \Declaration
  4883. Function Ofs Var X : Longint;
  4884. \Description
  4885. \var{Ofs} returns the offset of the address of a variable.
  4886. This function is only supported for compatibility. In \fpc, it
  4887. returns always the complete address of the variable, since \fpc is a 32 bit
  4888. compiler.
  4889. \Errors
  4890. None.
  4891. \SeeAlso
  4892. \seef{DSeg}, \seef{CSeg}, \seef{Seg}, \seef{Ptr}
  4893. \end{function}
  4894. \FPCexample{ex44}
  4895. \begin{function}{Ord}
  4896. \Declaration
  4897. Function Ord (X : Any ordinal type) : Longint;
  4898. \Description
  4899. \var{Ord} returns the Ordinal value of a ordinal-type variable \var{X}.
  4900. \Errors
  4901. None.
  4902. \SeeAlso
  4903. \seef{Chr}, \seef{Succ}, \seef{Pred}, \seef{High}, \seef{Low}
  4904. \end{function}
  4905. \FPCexample{ex45}
  4906. \begin{function}{Paramcount}
  4907. \Declaration
  4908. Function Paramcount : Longint;
  4909. \Description
  4910. \var{Paramcount} returns the number of command-line arguments. If no
  4911. arguments were given to the running program, \var{0} is returned.
  4912. \Errors
  4913. None.
  4914. \SeeAlso
  4915. \seef{Paramstr}
  4916. \end{function}
  4917. \FPCexample{ex46}
  4918. \begin{function}{Paramstr}
  4919. \Declaration
  4920. Function Paramstr (L : Longint) : String;
  4921. \Description
  4922. \var{Paramstr} returns the \var{L}-th command-line argument. \var{L} must
  4923. be between \var{0} and \var{Paramcount}, these values included.
  4924. The zeroth argument is the name with which the program was started.
  4925. In all cases, the command-line will be truncated to a length of 255,
  4926. even though the operating system may support bigger command-lines. If you
  4927. want to access the complete command-line, you must use the \var{argv} pointer
  4928. to access the Real values of the command-line parameters.
  4929. \Errors
  4930. None.
  4931. \SeeAlso
  4932. \seef{Paramcount}
  4933. \end{function}
  4934. For an example, see \seef{Paramcount}.
  4935. \begin{function}{Pi}
  4936. \Declaration
  4937. Function Pi : Real;
  4938. \Description
  4939. \var{Pi} returns the value of Pi (3.1415926535897932385).
  4940. \Errors
  4941. None.
  4942. \SeeAlso
  4943. \seef{Cos}, \seef{Sin}
  4944. \end{function}
  4945. \FPCexample{ex47}
  4946. \begin{function}{Pos}
  4947. \Declaration
  4948. Function Pos (Const Substr : String;Const S : String) : Byte;
  4949. \Description
  4950. \var{Pos} returns the index of \var{Substr} in \var{S}, if \var{S} contains
  4951. \var{Substr}. In case \var{Substr} isn't found, \var{0} is returned.
  4952. The search is case-sensitive.
  4953. \Errors
  4954. None
  4955. \SeeAlso
  4956. \seef{Length}, \seef{Copy}, \seep{Delete}, \seep{Insert}
  4957. \end{function}
  4958. \FPCexample{ex48}
  4959. \begin{function}{Power}
  4960. \Declaration
  4961. Function Power (base,expon : Real) : Real;
  4962. \Description
  4963. \var{Power} returns the value of \var{base} to the power \var{expon}.
  4964. \var{Base} and \var{expon} can be of type Longint, in which case the
  4965. result will also be a Longint.
  4966. The function actually returns \var{Exp(expon*Ln(base))}
  4967. \Errors
  4968. None.
  4969. \SeeAlso
  4970. \seef{Exp}, \seef{Ln}
  4971. \end{function}
  4972. \FPCexample{ex78}
  4973. \begin{function}{Pred}
  4974. \Declaration
  4975. Function Pred (X : Any ordinal type) : Same type;
  4976. \Description
  4977. \var{Pred} returns the element that precedes the element that was passed
  4978. to it. If it is applied to the first value of the ordinal type, and the
  4979. program was compiled with range checking on (\var{\{\$R+\}}, then a run-time
  4980. error will be generated.
  4981. \Errors
  4982. Run-time error 201 is generated when the result is out of
  4983. range.
  4984. \SeeAlso
  4985. \seef{Ord}, \seef{Pred}, \seef{High}, \seef{Low}
  4986. \end{function}
  4987. for an example, see \seef{Ord}
  4988. \begin{function}{Ptr}
  4989. \Declaration
  4990. Function Ptr (Sel,Off : Longint) : Pointer;
  4991. \Description
  4992. \var{Ptr} returns a pointer, pointing to the address specified by
  4993. segment \var{Sel} and offset \var{Off}.
  4994. \begin{remark}
  4995. \begin{enumerate}
  4996. \item In the 32-bit flat-memory model supported by \fpc, this
  4997. function is obsolete.
  4998. \item The returned address is simply the offset. If you recompile
  4999. the RTL with \var{-dDoMapping} defined, then the compiler returns the
  5000. following : \var{ptr := pointer(\$e0000000+sel shl 4+off)} under \dos, or
  5001. \var{ptr := pointer(sel shl 4+off)} on other OSes.
  5002. \end{enumerate}
  5003. \end{remark}
  5004. \Errors
  5005. None.
  5006. \SeeAlso
  5007. \seef{Addr}
  5008. \end{function}
  5009. \FPCexample{ex59}
  5010. \begin{function}{Random}
  5011. \Declaration
  5012. Function Random [(L : Longint)] : Longint or Real;
  5013. \Description
  5014. \var{Random} returns a random number larger or equal to \var{0} and
  5015. strictly less than \var{L}.
  5016. If the argument \var{L} is omitted, a Real number between 0 and 1 is returned.
  5017. (0 included, 1 excluded)
  5018. \Errors
  5019. None.
  5020. \SeeAlso
  5021. \seep{Randomize}
  5022. \end{function}
  5023. \FPCexample{ex49}
  5024. \begin{procedure}{Randomize}
  5025. \Declaration
  5026. Procedure Randomize ;
  5027. \Description
  5028. \var{Randomize} initializes the random number generator of \fpc, by giving
  5029. a value to \var{Randseed}, calculated with the system clock.
  5030. \Errors
  5031. None.
  5032. \SeeAlso
  5033. \seef{Random}
  5034. \end{procedure}
  5035. For an example, see \seef{Random}.
  5036. \begin{procedure}{Read}
  5037. \Declaration
  5038. Procedure Read ([Var F : Any file type], V1 [, V2, ... , Vn]);
  5039. \Description
  5040. \var{Read} reads one or more values from a file \var{F}, and stores the
  5041. result in \var{V1}, \var{V2}, etc.; If no file \var{F} is specified, then
  5042. standard input is read.
  5043. If \var{F} is of type \var{Text}, then the variables \var{V1, V2} etc. must be
  5044. of type \var{Char}, \var{Integer}, \var{Real}, \var{String} or \var{PChar}.
  5045. If \var{F} is a typed file, then each of the variables must be of the type
  5046. specified in the declaration of \var{F}. Untyped files are not allowed as an
  5047. argument.
  5048. \Errors
  5049. If no data is available, a run-time error is generated. This behavior can
  5050. be controlled with the \var{\{\$i\}} compiler switch.
  5051. \SeeAlso
  5052. \seep{Readln}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}
  5053. \end{procedure}
  5054. \FPCexample{ex50}
  5055. \begin{procedure}{Readln}
  5056. \Declaration
  5057. Procedure Readln [Var F : Text], V1 [, V2, ... , Vn]);
  5058. \Description
  5059. \var{Read} reads one or more values from a file \var{F}, and stores the
  5060. result in \var{V1}, \var{V2}, etc. After that it goes to the next line in
  5061. the file (defined by the \var{LineFeed (\#10)} character).
  5062. If no file \var{F} is specified, then standard input is read.
  5063. The variables \var{V1, V2} etc. must be of type \var{Char}, \var{Integer},
  5064. \var{Real}, \var{String} or \var{PChar}.
  5065. \Errors
  5066. If no data is available, a run-time error is generated. This behavior can
  5067. be controlled with the \var{\{\$i\}} compiler switch.
  5068. \SeeAlso
  5069. \seep{Read}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}
  5070. \end{procedure}
  5071. For an example, see \seep{Read}.
  5072. \begin{procedure}{Release}
  5073. \Declaration
  5074. Procedure Release (Var P : pointer);
  5075. \Description
  5076. \var{Release} sets the top of the Heap to the location pointed to by
  5077. \var{P}. All memory at a location higher than \var{P} is marked empty.
  5078. \Errors
  5079. A run-time error will be generated if \var{P} points to memory outside the
  5080. heap.
  5081. \SeeAlso
  5082. \seep{Mark}, \seef{Memavail}, \seef{Maxavail}, \seep{Getmem}, \seep{Freemem}
  5083. \seep{New}, \seep{Dispose}
  5084. \end{procedure}
  5085. For an example, see \seep{Mark}.
  5086. \begin{procedure}{Rename}
  5087. \Declaration
  5088. Procedure Rename (Var F : Any Filetype; Const S : String);
  5089. \Description
  5090. \var{Rename} changes the name of the assigned file \var{F} to \var{S}.
  5091. \var{F}
  5092. must be assigned, but not opened.
  5093. \Errors
  5094. A run-time error will be generated if \var{F} isn't assigned,
  5095. or doesn't exist.
  5096. \SeeAlso
  5097. \seep{Erase}
  5098. \end{procedure}
  5099. \FPCexample{ex77}
  5100. \begin{procedure}{Reset}
  5101. \Declaration
  5102. Procedure Reset (Var F : Any File Type[; L : Longint]);
  5103. \Description
  5104. \var{Reset} opens a file \var{F} for reading. \var{F} can be any file type.
  5105. If \var{F} is an untyped or typed file, then it is opened for reading and
  5106. writing. If \var{F} is an untyped file, the record size can be specified in
  5107. the optional parameter \var{L}. Default a value of 128 is used.
  5108. \Errors
  5109. If the file cannot be opened for reading, then a run-time error is
  5110. generated. This behavior can be changed by the \var{\{\$i\} } compiler switch.
  5111. \SeeAlso
  5112. \seep{Rewrite}, \seep{Assign}, \seep{Close}, \seep{Append}
  5113. \end{procedure}
  5114. \FPCexample{ex51}
  5115. \begin{procedure}{Rewrite}
  5116. \Declaration
  5117. Procedure Rewrite (Var F : Any File Type[; L : Longint]);
  5118. \Description
  5119. \var{Rewrite} opens a file \var{F} for writing. \var{F} can be any file type.
  5120. If \var{F} is an untyped or typed file, then it is opened for reading and
  5121. writing. If \var{F} is an untyped file, the record size can be specified in
  5122. the optional parameter \var{L}. Default a value of 128 is used.
  5123. if \var{Rewrite} finds a file with the same name as \var{F}, this file is
  5124. truncated to length \var{0}. If it doesn't find such a file, a new file is
  5125. created.
  5126. \Errors
  5127. If the file cannot be opened for writing, then a run-time error is
  5128. generated. This behavior can be changed by the \var{\{\$i\} } compiler switch.
  5129. \SeeAlso
  5130. \seep{Reset}, \seep{Assign}, \seep{Close}, \seep{Flush}, \seep{Append}
  5131. \end{procedure}
  5132. \FPCexample{ex52}
  5133. \begin{procedure}{Rmdir}
  5134. \Declaration
  5135. Procedure Rmdir (const S : string);
  5136. \Description
  5137. \var{Rmdir} removes the directory \var{S}.
  5138. \Errors
  5139. If \var{S} doesn't exist, or isn't empty, a run-time error is generated.
  5140. \SeeAlso
  5141. \seep{Chdir}, \seep{Mkdir}
  5142. \end{procedure}
  5143. \FPCexample{ex53}
  5144. \begin{function}{Round}
  5145. \Declaration
  5146. Function Round (X : Real) : Longint;
  5147. \Description
  5148. \var{Round} rounds \var{X} to the closest integer, which may be bigger or
  5149. smaller than \var{X}.
  5150. \Errors
  5151. None.
  5152. \SeeAlso
  5153. \seef{Frac}, \seef{Int}, \seef{Trunc}
  5154. \end{function}
  5155. \FPCexample{ex54}
  5156. \begin{procedure}{Runerror}
  5157. \Declaration
  5158. Procedure Runerror (ErrorCode : Word);
  5159. \Description
  5160. \var{Runerror} stops the execution of the program, and generates a
  5161. run-time error \var{ErrorCode}.
  5162. \Errors
  5163. None.
  5164. \SeeAlso
  5165. \seep{Exit}, \seep{Halt}
  5166. \end{procedure}
  5167. \FPCexample{ex55}
  5168. \begin{procedure}{Seek}
  5169. \Declaration
  5170. Procedure Seek (Var F; Count : Longint);
  5171. \Description
  5172. \var{Seek} sets the file-pointer for file \var{F} to record Nr. \var{Count}.
  5173. The first record in a file has \var{Count=0}. F can be any file type, except
  5174. \var{Text}. If \var{F} is an untyped file, with no record size specified in
  5175. \seep{Reset} or \seep{Rewrite}, 128 is assumed.
  5176. \Errors
  5177. A run-time error is generated if \var{Count} points to a position outside
  5178. the file, or the file isn't opened.
  5179. \SeeAlso
  5180. \seef{Eof}, \seef{SeekEof}, \seef{SeekEoln}
  5181. \end{procedure}
  5182. \FPCexample{ex56}
  5183. \begin{function}{SeekEof}
  5184. \Declaration
  5185. Function SeekEof [(Var F : text)] : Boolean;
  5186. \Description
  5187. \var{SeekEof} returns \var{True} is the file-pointer is at the end of the
  5188. file. It ignores all whitespace.
  5189. Calling this function has the effect that the file-position is advanced
  5190. until the first non-whitespace character or the end-of-file marker is
  5191. reached.
  5192. If the end-of-file marker is reached, \var{True} is returned. Otherwise,
  5193. False is returned.
  5194. If the parameter \var{F} is omitted, standard \var{Input} is assumed.
  5195. \Errors
  5196. A run-time error is generated if the file \var{F} isn't opened.
  5197. \SeeAlso
  5198. \seef{Eof}, \seef{SeekEoln}, \seep{Seek}
  5199. \end{function}
  5200. \FPCexample{ex57}
  5201. \begin{function}{SeekEoln}
  5202. \Declaration
  5203. Function SeekEoln [(Var F : text)] : Boolean;
  5204. \Description
  5205. \var{SeekEoln} returns \var{True} is the file-pointer is at the end of the
  5206. current line. It ignores all whitespace.
  5207. Calling this function has the effect that the file-position is advanced
  5208. until the first non-whitespace character or the end-of-line marker is
  5209. reached.
  5210. If the end-of-line marker is reached, \var{True} is returned. Otherwise,
  5211. False is returned.
  5212. The end-of-line marker is defined as \var{\#10}, the LineFeed character.
  5213. If the parameter \var{F} is omitted, standard \var{Input} is assumed.
  5214. \Errors
  5215. A run-time error is generated if the file \var{F} isn't opened.
  5216. \SeeAlso
  5217. \seef{Eof}, \seef{SeekEof}, \seep{Seek}
  5218. \end{function}
  5219. \FPCexample{ex58}
  5220. \begin{function}{Seg}
  5221. \Declaration
  5222. Function Seg Var X : Longint;
  5223. \Description
  5224. \var{Seg} returns the segment of the address of a variable.
  5225. This function is only supported for compatibility. In \fpc, it
  5226. returns always 0, since \fpc is a 32 bit compiler, segments have no meaning.
  5227. \Errors
  5228. None.
  5229. \SeeAlso
  5230. \seef{DSeg}, \seef{CSeg}, \seef{Ofs}, \seef{Ptr}
  5231. \end{function}
  5232. \FPCexample{ex60}
  5233. \begin{function}{SetJmp}
  5234. \Declaration
  5235. Function SetJmp (Var Env : Jmp\_Buf) : Longint;
  5236. \Description
  5237. \var{SetJmp} fills \var{env} with the necessary data for a jump back to the
  5238. point where it was called. It returns zero if called in this way.
  5239. If the function returns nonzero, then it means that a call to \seep{LongJmp}
  5240. with \var{env} as an argument was made somewhere in the program.
  5241. \Errors
  5242. None.
  5243. \SeeAlso
  5244. \seep{LongJmp}
  5245. \end{function}
  5246. \FPCexample{ex79}
  5247. \begin{procedure}{SetLength}
  5248. \Declaration
  5249. Procedure SetLength(var S : String; Len : Longint);
  5250. \Description
  5251. \var{SetLength} sets the length of the string \var{S} to \var{Len}. \var{S}
  5252. can be an ansistring or a short string.
  5253. For \var{ShortStrings}, \var{Len} can maximally be 255. For \var{AnsiStrings}
  5254. it can have any value. For \var{AnsiString} strings, \var{SetLength} {\em
  5255. must} be used to set the length of the string.
  5256. \Errors
  5257. None.
  5258. \SeeAlso
  5259. \seef{Length}
  5260. \end{procedure}
  5261. \FPCexample{ex85}
  5262. \begin{procedure}{SetTextBuf}
  5263. \Declaration
  5264. Procedure SetTextBuf (Var f : Text; Var Buf[; Size : Word]);
  5265. \Description
  5266. \var{SetTextBuf} assigns an I/O buffer to a text file. The new buffer is
  5267. located at \var{Buf} and is \var{Size} bytes long. If \var{Size} is omitted,
  5268. then \var{SizeOf(Buf)} is assumed.
  5269. The standard buffer of any text file is 128 bytes long. For heavy I/0
  5270. operations this may prove too slow. The \var{SetTextBuf} procedure allows
  5271. you to set a bigger buffer for your application, thus reducing the number of
  5272. system calls, and thus reducing the load on the system resources.
  5273. The maximum size of the newly assigned buffer is 65355 bytes.
  5274. \begin{remark}
  5275. \begin{itemize}
  5276. \item Never assign a new buffer to an opened file. You can assign a
  5277. new buffer immediately after a call to \seep{Rewrite}, \seep{Reset} or
  5278. \var{Append}, but not after you read from/wrote to the file. This may cause
  5279. loss of data. If you still want to assign a new buffer after read/write
  5280. operations have been performed, flush the file first. This will ensure that
  5281. the current buffer is emptied.
  5282. \item Take care that the buffer you assign is always valid. If you
  5283. assign a local variable as a buffer, then after your program exits the local
  5284. program block, the buffer will no longer be valid, and stack problems may
  5285. occur.
  5286. \end{itemize}
  5287. \end{remark}
  5288. \Errors
  5289. No checking on \var{Size} is done.
  5290. \SeeAlso
  5291. \seep{Assign}, \seep{Reset}, \seep{Rewrite}, \seep{Append}
  5292. \end{procedure}
  5293. \FPCexample{ex61}
  5294. \begin{function}{Sin}
  5295. \Declaration
  5296. Function Sin (X : Real) : Real;
  5297. \Description
  5298. \var{Sin} returns the sine of its argument \var{X}, where \var{X} is an
  5299. angle in radians.
  5300. \Errors
  5301. None.
  5302. \SeeAlso
  5303. \seef{Cos}, \seef{Pi}, \seef{Exp}, \seef{Ln}
  5304. \end{function}
  5305. \FPCexample{ex62}
  5306. \begin{function}{SizeOf}
  5307. \Declaration
  5308. Function SizeOf (X : Any Type) : Longint;
  5309. \Description
  5310. \var{SizeOf} returns the size, in bytes, of any variable or type-identifier.
  5311. \begin{remark}
  5312. This isn't really a RTL function. It's result is calculated at
  5313. compile-time, and hard-coded in your executable.
  5314. \end{remark}
  5315. \Errors
  5316. None.
  5317. \SeeAlso
  5318. \seef{Addr}
  5319. \end{function}
  5320. \FPCexample{ex63}
  5321. \begin{function}{Sptr}
  5322. \Declaration
  5323. Function Sptr : Pointer;
  5324. \Description
  5325. \var{Sptr} returns the current stack pointer.
  5326. \Errors
  5327. None.
  5328. \SeeAlso
  5329. \seef{SSeg}
  5330. \end{function}
  5331. \FPCexample{ex64}
  5332. \begin{function}{Sqr}
  5333. \Declaration
  5334. Function Sqr (X : Real) : Real;
  5335. \Description
  5336. \var{Sqr} returns the square of its argument \var{X}.
  5337. \Errors
  5338. None.
  5339. \SeeAlso
  5340. \seef{Sqrt}, \seef{Ln}, \seef{Exp}
  5341. \end{function}
  5342. \FPCexample{ex65}
  5343. \begin{function}{Sqrt}
  5344. \Declaration
  5345. Function Sqrt (X : Real) : Real;
  5346. \Description
  5347. \var{Sqrt} returns the square root of its argument \var{X}, which must be
  5348. positive.
  5349. \Errors
  5350. If \var{X} is negative, then a run-time error is generated.
  5351. \SeeAlso
  5352. \seef{Sqr}, \seef{Ln}, \seef{Exp}
  5353. \end{function}
  5354. \FPCexample{ex66}
  5355. \begin{function}{SSeg}
  5356. \Declaration
  5357. Function SSeg : Longint;
  5358. \Description
  5359. \var{SSeg} returns the Stack Segment. This function is only
  5360. supported for compatibility reasons, as \var{Sptr} returns the
  5361. correct contents of the stackpointer.
  5362. \Errors
  5363. None.
  5364. \SeeAlso
  5365. \seef{Sptr}
  5366. \end{function}
  5367. \FPCexample{ex67}
  5368. \begin{procedure}{Str}
  5369. \Declaration
  5370. Procedure Str (Var X[:NumPlaces[:Decimals]]; Var S : String);
  5371. \Description
  5372. \var{Str} returns a string which represents the value of X. X can be any
  5373. numerical type.
  5374. The optional \var{NumPLaces} and \var{Decimals} specifiers control the
  5375. formatting of the string.
  5376. \Errors
  5377. None.
  5378. \SeeAlso
  5379. \seep{Val}
  5380. \end{procedure}
  5381. \FPCexample{ex68}
  5382. \begin{function}{Succ}
  5383. \Declaration
  5384. Function Succ (X : Any ordinal type) : Same type;
  5385. \Description
  5386. \var{Succ} returns the element that succeeds the element that was passed
  5387. to it. If it is applied to the last value of the ordinal type, and the
  5388. program was compiled with range checking on (\var{\{\$R+\}}), then a run-time
  5389. error will be generated.
  5390. \Errors
  5391. Run-time error 201 is generated when the result is out of
  5392. range.
  5393. \SeeAlso
  5394. \seef{Ord}, \seef{Pred}, \seef{High}, \seef{Low}
  5395. \end{function}
  5396. for an example, see \seef{Ord}.
  5397. \begin{function}{Swap}
  5398. \Declaration
  5399. Function Swap (X) : Type of X;
  5400. \Description
  5401. \var{Swap} swaps the high and low order bytes of \var{X} if \var{X} is of
  5402. type \var{Word} or \var{Integer}, or swaps the high and low order words of
  5403. \var{X} if \var{X} is of type \var{Longint} or \var{Cardinal}.
  5404. The return type is the type of \var{X}
  5405. \Errors
  5406. None.
  5407. \SeeAlso
  5408. \seef{Lo}, \seef{Hi}
  5409. \end{function}
  5410. \FPCexample{ex69}
  5411. \begin{function}{Trunc}
  5412. \Declaration
  5413. Function Trunc (X : Real) : Longint;
  5414. \Description
  5415. \var{Trunc} returns the integer part of \var{X},
  5416. which is always smaller than (or equal to) \var{X} in absolute value.
  5417. \Errors
  5418. None.
  5419. \SeeAlso
  5420. \seef{Frac}, \seef{Int}, \seef{Round}
  5421. \end{function}
  5422. \FPCexample{ex70}
  5423. \begin{procedure}{Truncate}
  5424. \Declaration
  5425. Procedure Truncate (Var F : file);
  5426. \Description
  5427. \var{Truncate} truncates the (opened) file \var{F} at the current file
  5428. position.
  5429. \Errors
  5430. Errors are reported by IOresult.
  5431. \SeeAlso
  5432. \seep{Append}, \seef{Filepos},
  5433. \seep{Seek}
  5434. \end{procedure}
  5435. \FPCexample{ex71}
  5436. \begin{function}{Upcase}
  5437. \Declaration
  5438. Function Upcase (C : Char or string) : Char or String;
  5439. \Description
  5440. \var{Upcase} returns the uppercase version of its argument \var{C}.
  5441. If its argument is a string, then the complete string is converted to
  5442. uppercase. The type of the returned value is the same as the type of the
  5443. argument.
  5444. \Errors
  5445. None.
  5446. \SeeAlso
  5447. \seef{Lowercase}
  5448. \end{function}
  5449. \FPCexample{ex72}
  5450. \begin{procedure}{Val}
  5451. \Declaration
  5452. Procedure Val (const S : string;var V;var Code : word);
  5453. \Description
  5454. \var{Val} converts the value represented in the string \var{S} to a numerical
  5455. value, and stores this value in the variable \var{V}, which
  5456. can be of type \var{Longint}, \var{Real} and \var{Byte}.
  5457. If the conversion isn't succesfull, then the parameter \var{Code} contains
  5458. the index of the character in \var{S} which prevented the conversion.
  5459. The string \var{S} isn't allowed to contain spaces.
  5460. \Errors
  5461. If the conversion doesn't succeed, the value of \var{Code} indicates the
  5462. position where the conversion went wrong.
  5463. \SeeAlso
  5464. \seep{Str}
  5465. \end{procedure}
  5466. \FPCexample{ex74}
  5467. \begin{procedure}{Write}
  5468. \Declaration
  5469. Procedure Write ([Var F : Any filetype;] V1 [; V2; ... , Vn)];
  5470. \Description
  5471. \var{Write} writes the contents of the variables \var{V1}, \var{V2} etc. to
  5472. the file \var{F}. \var{F} can be a typed file, or a \var{Text} file.
  5473. If \var{F} is a typed file, then the variables \var{V1}, \var{V2} etc. must
  5474. be of the same type as the type in the declaration of \var{F}. Untyped files
  5475. are not allowed.
  5476. If the parameter \var{F} is omitted, standard output is assumed.
  5477. If \var{F} is of type \var{Text}, then the necessary conversions are done
  5478. such that the output of the variables is in human-readable format.
  5479. This conversion is done for all numerical types. Strings are printed exactly
  5480. as they are in memory, as well as \var{PChar} types.
  5481. The format of the numerical conversions can be influenced through
  5482. the following modifiers:
  5483. \var{ OutputVariable : NumChars [: Decimals ] }
  5484. This will print the value of \var{OutputVariable} with a minimum of
  5485. \var{NumChars} characters, from which \var{Decimals} are reserved for the
  5486. decimals. If the number cannot be represented with \var{NumChars} characters,
  5487. \var{NumChars} will be increased, until the representation fits. If the
  5488. representation requires less than \var{NumChars} characters then the output
  5489. is filled up with spaces, to the left of the generated string, thus
  5490. resulting in a right-aligned representation.
  5491. If no formatting is specified, then the number is written using its natural
  5492. length, with nothing in front of it if it's positive, and a minus sign if
  5493. it's negative.
  5494. Real numbers are, by default, written in scientific notation.
  5495. \Errors
  5496. If an error occurs, a run-time error is generated. This behavior can be
  5497. controlled with the \var{\{\$i\}} switch.
  5498. \SeeAlso
  5499. \seep{WriteLn}, \seep{Read}, \seep{Readln}, \seep{Blockwrite}
  5500. \end{procedure}
  5501. \begin{procedure}{WriteLn}
  5502. \Declaration
  5503. Procedure WriteLn [([Var F : Text;] [V1 [; V2; ... , Vn)]];
  5504. \Description
  5505. \var{WriteLn} does the same as \seep{Write} for text files, and emits a
  5506. Carriage Return - LineFeed character pair after that.
  5507. If the parameter \var{F} is omitted, standard output is assumed.
  5508. If no variables are specified, a Carriage Return - LineFeed character pair
  5509. is emitted, resulting in a new line in the file \var{F}.
  5510. \begin{remark}
  5511. Under \linux, the Carriage Return character is omitted, as
  5512. customary in Unix environments.
  5513. \end{remark}
  5514. \Errors
  5515. If an error occurs, a run-time error is generated. This behavior can be
  5516. controlled with the \var{\{\$i\}} switch.
  5517. \SeeAlso
  5518. \seep{Write}, \seep{Read}, \seep{Readln}, \seep{Blockwrite}
  5519. \end{procedure}
  5520. \FPCexample{ex75}
  5521. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5522. % The objpas unit
  5523. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5524. \chapter{The OBJPAS unit}
  5525. The \file{objpas} unit is meant for compatibility with Object Pascal as
  5526. implemented by Delphi. The unit is loaded automatically by the \fpc compiler
  5527. whenever the \var{Delphi} or \var{objfpc} more is entered, either through
  5528. the command line switches \var{-Sd} or \var{-Sh} or with the \var{\{\$MODE
  5529. DELPHI\}} or \var{\{\$MODE OBJFPC\}} directives.
  5530. It redefines some basic pascal types, introduces some functions for
  5531. compatibility with Delphi's system unit, and introduces some methods for the
  5532. management of the resource string tables.
  5533. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5534. % Tytpes
  5535. \section{Types}
  5536. The \file{objpas} unit redefines two integer types, for compatibity with
  5537. Delphi:
  5538. \begin{verbatim}
  5539. type
  5540. smallint = system.integer;
  5541. integer = system.longint;
  5542. \end{verbatim}
  5543. The resource string tables can be managed with a callback function which the
  5544. user must provide: \var{TResourceIterator}.
  5545. \begin{verbatim}
  5546. Type
  5547. TResourceIterator =
  5548. Function (Name,Value : AnsiString;Hash : Longint):AnsiString;
  5549. \end{verbatim}
  5550. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5551. % Functions and procedures
  5552. \section{Functions and Procedures}
  5553. \begin{procedure}{AssignFile}
  5554. \Declaration
  5555. Procedure AssignFile(Var f: FileType;Name: Character type);
  5556. \Description
  5557. \var{AssignFile} is completely equivalent to the system unit's \seep{Assign}
  5558. function: It assigns \var{Name} to a function of any type (\var{FileType}
  5559. can be \var{Text} or a typed or untyped \var{File} variable). \var{Name} can
  5560. be a string, a single character or a \var{PChar}.
  5561. It is most likely introduced to avoid confusion between the regular
  5562. \seep{Assign} function and the \var{Assign} method of \var{TPersistent}
  5563. in the Delphi VCL.
  5564. \Errors
  5565. None.
  5566. \SeeAlso
  5567. \seep{CloseFile}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}, \seep{Append}
  5568. \end{procedure}
  5569. \FPCexample{ex88}
  5570. \begin{procedure}{CloseFile}
  5571. \Declaration
  5572. Procedure CloseFile(Var F: FileType);
  5573. \Description
  5574. \var{CloseFile} flushes and closes a file \var{F} of any file type.
  5575. \var{F} can be \var{Text} or a typed or untyped \var{File} variable.
  5576. After a call to \var{CloseFile}, any attempt to write to the file \var{F}
  5577. will result in an error.
  5578. It is most likely introduced to avoid confusion between the regular
  5579. \seep{Close} function and the \var{Close} method of \var{TForm}
  5580. in the Delphi VCL.
  5581. \Errors
  5582. None.
  5583. \SeeAlso
  5584. \seep{Close}, \seep{AssignFile}, \seep{Reset}, \seep{Rewrite}, \seep{Append}
  5585. \end{procedure}
  5586. for an example, see \seep{AssignFile}.
  5587. \begin{procedurel}{Freemem}{objpasfreemem}
  5588. \Declaration
  5589. Procedure FreeMem(Var p:pointer[;Size:Longint]);
  5590. \Description
  5591. \var{FreeMem} releases the memory reserved by a call to
  5592. \seepl{GetMem}{objpasgetmem}. The (optional) \var{Size} parameter is
  5593. ignored, since the object pascal version of \var{GetMem} stores the amount
  5594. of memory that was requested.
  5595. be sure not to release memory that was not obtained with the \var{Getmem}
  5596. call in \file{Objpas}. Normally, this should not happen, since objpas
  5597. changes the default memory manager to it's own memory manager.
  5598. \Errors
  5599. None.
  5600. \SeeAlso
  5601. \seep{Freemem}, \seepl{GetMem}{objpasgetmem}, \seep{Getmem}
  5602. \end{procedurel}
  5603. \FPCexample{ex89}
  5604. \begin{procedurel}{Getmem}{objpasgetmem}
  5605. \Declaration
  5606. Procedure Getmem(Var P:pointer;Size:Longint);
  5607. \Description
  5608. \var{GetMem} reserves \var{Size} bytes of memory on the heap and returns
  5609. a pointer to it in \var{P}. \var{Size} is stored at offset -4 of the
  5610. result, and is used to release the memory again. \var{P} can be a typed or
  5611. untyped pointer.
  5612. Be sure to release this memory with the \seepl{FreeMem}{objpasfreemem} call
  5613. defined in the \file{objpas} unit.
  5614. \Errors
  5615. In case no more memory is available, and no more memory could be obtained
  5616. from the system a run-time error is triggered.
  5617. \SeeAlso
  5618. \seepl{FreeMem}{objpasfreemem}, \seep{Getmem}.
  5619. \end{procedurel}
  5620. For an example, see \seepl{FreeMem}{objpasfreemem}.
  5621. \begin{function}{GetResourceStringCurrentValue}
  5622. \Declaration
  5623. Function GetResourceStringCurrentValue(TableIndex,StringIndex : Longint) : AnsiString;
  5624. \Description
  5625. \var{GetResourceStringCurrentValue} returns the current value of the
  5626. resourcestring in table \var{TableIndex} with index \var{StringIndex}.
  5627. The current value depends on the system of internationalization that was
  5628. used, and which language is selected when the program is executed.
  5629. \Errors
  5630. If either \var{TableIndex} or \var{StringIndex} are out of range, then
  5631. a empty string is returned.
  5632. \SeeAlso
  5633. \seep{SetResourceStrings},
  5634. \seef{GetResourceStringDefaultValue},
  5635. \seef{GetResourceStringHash},
  5636. \seef{GetResourceStringName},
  5637. \seef{ResourceStringTableCount},
  5638. \seef{ResourceStringCount}
  5639. \end{function}
  5640. \FPCexample{ex90}
  5641. \begin{function}{GetResourceStringDefaultValue}
  5642. \Declaration
  5643. Function GetResourceStringDefaultValue(TableIndex,StringIndex : Longint) : AnsiString
  5644. \Description
  5645. \var{GetResourceStringDefaultValue} returns the default value of the
  5646. resourcestring in table \var{TableIndex} with index \var{StringIndex}.
  5647. The default value is the value of the string that appears in the source code
  5648. of the programmer, and is compiled into the program.
  5649. \Errors
  5650. If either \var{TableIndex} or \var{StringIndex} are out of range, then
  5651. a empty string is returned.
  5652. \Errors
  5653. \SeeAlso
  5654. \seep{SetResourceStrings},
  5655. \seef{GetResourceStringCurrentValue},
  5656. \seef{GetResourceStringHash},
  5657. \seef{GetResourceStringName},
  5658. \seef{ResourceStringTableCount},
  5659. \seef{ResourceStringCount}
  5660. \end{function}
  5661. \FPCexample{ex91}
  5662. \begin{function}{GetResourceStringHash}
  5663. \Declaration
  5664. Function GetResourceStringHash(TableIndex,StringIndex : Longint) : Longint;
  5665. \Description
  5666. \var{GetResourceStringHash} returns the hash value associated with the
  5667. resource string in table \var{TableIndex}, with index \var{StringIndex}.
  5668. The hash value is calculated from the default value of the resource string
  5669. in a manner that gives the same result as the GNU \file{gettext} mechanism.
  5670. It is stored in the resourcestring tables, so retrieval is faster than
  5671. actually calculating the hash for each string.
  5672. \Errors
  5673. If either \var{TableIndex} or \var{StringIndex} is zero, 0 is returned.
  5674. \SeeAlso
  5675. \seef{Hash}
  5676. \seep{SetResourceStrings},
  5677. \seef{GetResourceStringDefaultValue},
  5678. \seef{GetResourceStringHash},
  5679. \seef{GetResourceStringName},
  5680. \seef{ResourceStringTableCount},
  5681. \seef{ResourceStringCount}
  5682. \end{function}
  5683. For an example, see \seef{Hash}.
  5684. \begin{function}{GetResourceStringName}
  5685. \Declaration
  5686. Function GetResourceStringName(TableIndex,StringIndex : Longint) : Ansistring;
  5687. \Description
  5688. \var{GetResourceStringName} returns the name of the resourcestring in table
  5689. \var{TableIndex} with index \var{StringIndex}. The name of the string is
  5690. always the unit name in which the string was declared, followed by a period
  5691. and the name of the constant, all in lowercase.
  5692. If a unit \file{MyUnit} declares a resourcestring \var{MyTitle} then the
  5693. name returned will be \var{myunit.mytitle}. A resourcestring in the program file
  5694. will have the name of the program prepended.
  5695. The name returned by this function is also the name that is stored in the
  5696. resourcestring file generated by the compiler.
  5697. Strictly speaking, this information isn't necessary for the functioning
  5698. of the program, it is provided only as a means to easier translation of
  5699. strings.
  5700. \Errors
  5701. If either \var{TableIndex} or \var{StringIndex} is zero, an empty string
  5702. is returned.
  5703. \SeeAlso
  5704. \seep{SetResourceStrings},
  5705. \seef{GetResourceStringDefaultValue},
  5706. \seef{GetResourceStringHash},
  5707. \seef{GetResourceStringName},
  5708. \seef{ResourceStringTableCount},
  5709. \seef{ResourceStringCount}
  5710. \end{function}
  5711. \FPCexample{ex92}
  5712. \begin{function}{Hash}
  5713. \Declaration
  5714. Function Hash(S : AnsiString) : longint;
  5715. \Description
  5716. \var{Hash} calculates the hash value of the string \var{S} in a manner that
  5717. is compatible with the GNU gettext hash value for the string. It is the same
  5718. value that is stored in the Resource string tables, and which can be
  5719. retrieved with the \seef{GetResourceStringHash} function call.
  5720. \Errors
  5721. None. In case the calculated hash value should be 0, the returned result
  5722. will be -1.
  5723. \SeeAlso
  5724. \seef{GetResourceStringHash},
  5725. \end{function}
  5726. \FPCexample{ex93}
  5727. \begin{functionl}{Paramstr}{objpasparamstr}
  5728. \Declaration
  5729. Function ParamStr(Param : Integer) : Ansistring;
  5730. \Description
  5731. \var{ParamStr} returns the \var{Param}-th command-line parameter as an
  5732. AnsiString. The system unit \seef{Paramstr} function limits the result to
  5733. 255 characters.
  5734. The zeroeth command-line parameter contains the path of the executable,
  5735. except on \linux, where it is the command as typed on the command-line.
  5736. \Errors
  5737. In case \var{Param} is an invalid value, an empty string is returned.
  5738. \SeeAlso
  5739. \seef{Paramstr}
  5740. \end{functionl}
  5741. For an example, see \seef{Paramstr}.
  5742. \begin{procedure}{ResetResourceTables}
  5743. \Declaration
  5744. Procedure ResetResourceTables;
  5745. \Description
  5746. \var{ResetResourceTables} resets all resource strings to their default
  5747. (i.e. as in the source code) values.
  5748. Normally, this should never be called from a user's program. It is called
  5749. in the initialization code of the \file{objpas} unit. However, if the
  5750. resourcetables get messed up for some reason, this procedure will fix them
  5751. again.
  5752. \Errors
  5753. None.
  5754. \SeeAlso
  5755. \seep{SetResourceStrings},
  5756. \seef{GetResourceStringDefaultValue},
  5757. \seef{GetResourceStringHash},
  5758. \seef{GetResourceStringName},
  5759. \seef{ResourceStringTableCount},
  5760. \seef{ResourceStringCount}
  5761. \end{procedure}
  5762. \begin{function}{ResourceStringCount}
  5763. \Declaration
  5764. Function ResourceStringCount(TableIndex : longint) : longint;
  5765. \Description
  5766. \var{ResourceStringCount} returns the number of resourcestrings in
  5767. the table with index \var{TableIndex}. The strings in a particular table
  5768. are numbered from \var{0} to \var{ResourceStringCount-1}, i.e. they're zero
  5769. based.
  5770. \Errors
  5771. If an invalid \var{TableIndex} is given, \var{-1} is returned.
  5772. \SeeAlso
  5773. \seep{SetResourceStrings},
  5774. \seef{GetResourceStringCurrentValue},
  5775. \seef{GetResourceStringDefaultValue},
  5776. \seef{GetResourceStringHash},
  5777. \seef{GetResourceStringName},
  5778. \seef{ResourceStringTableCount},
  5779. \end{function}
  5780. For an example, see \seef{GetResourceStringDefaultValue}
  5781. \begin{function}{ResourceStringTableCount}
  5782. \Declaration
  5783. Function ResourceStringTableCount : Longint;
  5784. \Description
  5785. \var{ResourceStringTableCount} returns the number of resource string tables;
  5786. this may be zero if no resource strings are used in a program.
  5787. The tables are numbered from 0 to \var{ResourceStringTableCount-1}, i.e.
  5788. they're zero based.
  5789. \Errors
  5790. \SeeAlso
  5791. \seep{SetResourceStrings},
  5792. \seef{GetResourceStringDefaultValue},
  5793. \seef{GetResourceStringHash},
  5794. \seef{GetResourceStringName},
  5795. \seef{ResourceStringCount}
  5796. \end{function}
  5797. For an example, see \seef{GetResourceStringDefaultValue}
  5798. \begin{procedure}{SetResourceStrings}
  5799. \Declaration
  5800. TResourceIterator = Function (Name,Value : AnsiString;Hash : Longint):AnsiString;
  5801. Procedure SetResourceStrings (SetFunction : TResourceIterator);
  5802. \Description
  5803. \var{SetResourceStrings} calls \var{SetFunction} for all resourcestrings
  5804. in the resourcestring tables and sets the resourcestring's current value
  5805. to the value returned by \var{SetFunction}.
  5806. The \var{Name},\var{Value} and \var{Hash} parameters passed to the iterator
  5807. function are the values stored in the tables.
  5808. \Errors
  5809. None.
  5810. \SeeAlso
  5811. \seep{SetResourceStrings},
  5812. \seef{GetResourceStringCurrentValue},
  5813. \seef{GetResourceStringDefaultValue},
  5814. \seef{GetResourceStringHash},
  5815. \seef{GetResourceStringName},
  5816. \seef{ResourceStringTableCount},
  5817. \seef{ResourceStringCount}
  5818. \end{procedure}
  5819. \FPCexample{ex95}
  5820. \begin{function}{SetResourceStringValue}
  5821. \Declaration
  5822. Function SetResourceStringValue(TableIndex,StringIndex : longint; Value : Ansistring) : Boolean;
  5823. \Description
  5824. \var{SetResourceStringValue} assigns \var{Value} to the resource string in
  5825. table \var{TableIndex} with index \var{StringIndex}.
  5826. \Errors
  5827. \SeeAlso
  5828. \seep{SetResourceStrings},
  5829. \seef{GetResourceStringCurrentValue},
  5830. \seef{GetResourceStringDefaultValue},
  5831. \seef{GetResourceStringHash},
  5832. \seef{GetResourceStringName},
  5833. \seef{ResourceStringTableCount},
  5834. \seef{ResourceStringCount}
  5835. \end{function}
  5836. \FPCexample{ex94}
  5837. %
  5838. % The index.
  5839. %
  5840. \printindex
  5841. \end{document}