ref.tex 286 KB

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