ref.tex 258 KB

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