ref.tex 249 KB

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