ref.tex 245 KB

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