ref.tex 188 KB

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