ref.tex 258 KB

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