ref.tex 278 KB

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