ref.tex 288 KB

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