ref.tex 216 KB

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