ref.tex 245 KB

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