ref.tex 251 KB

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