ref.tex 256 KB

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