ref.tex 273 KB

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