ref.tex 252 KB

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