ref.tex 236 KB

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