ref.tex 260 KB

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