ref.tex 230 KB

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