ref.tex 236 KB

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