2
0

ref.tex 236 KB

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