ref.tex 205 KB

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