ref.tex 244 KB

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