ref.tex 247 KB

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