ref.tex 284 KB

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