symdef.pas 172 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547
  1. {
  2. Symbol table implementation for the definitions
  3. Copyright (c) 1998-2005 by Florian Klaempfl, Pierre Muller
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symdef;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { ppu }
  28. ppu,
  29. { node }
  30. node,
  31. { aasm }
  32. aasmbase,aasmtai,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. { tstoreddef }
  42. tstoreddef = class(tdef)
  43. protected
  44. typesymderef : tderef;
  45. public
  46. {$ifdef EXTDEBUG}
  47. fileinfo : tfileposinfo;
  48. {$endif}
  49. { generic support }
  50. genericdef : tstoreddef;
  51. genericdefderef : tderef;
  52. generictokenbuf : tdynamicarray;
  53. constructor create(dt:tdeftyp);
  54. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  55. destructor destroy;override;
  56. function getcopy : tstoreddef;virtual;
  57. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  58. procedure buildderef;override;
  59. procedure buildderefimpl;override;
  60. procedure deref;override;
  61. procedure derefimpl;override;
  62. function size:aint;override;
  63. function getvardef:longint;override;
  64. function alignment:shortint;override;
  65. function is_publishable : boolean;override;
  66. function needs_inittable : boolean;override;
  67. function rtti_mangledname(rt:trttitype):string;override;
  68. function in_currentunit: boolean;
  69. { regvars }
  70. function is_intregable : boolean;
  71. function is_fpuregable : boolean;
  72. { generics }
  73. procedure initgeneric;
  74. private
  75. savesize : aint;
  76. end;
  77. tfiletyp = (ft_text,ft_typed,ft_untyped);
  78. tfiledef = class(tstoreddef)
  79. filetyp : tfiletyp;
  80. typedfiledef : tdef;
  81. typedfiledefderef : tderef;
  82. constructor createtext;
  83. constructor createuntyped;
  84. constructor createtyped(def : tdef);
  85. constructor ppuload(ppufile:tcompilerppufile);
  86. function getcopy : tstoreddef;override;
  87. procedure ppuwrite(ppufile:tcompilerppufile);override;
  88. procedure buildderef;override;
  89. procedure deref;override;
  90. function GetTypeName:string;override;
  91. function getmangledparaname:string;override;
  92. procedure setsize;
  93. end;
  94. tvariantdef = class(tstoreddef)
  95. varianttype : tvarianttype;
  96. constructor create(v : tvarianttype);
  97. constructor ppuload(ppufile:tcompilerppufile);
  98. function getcopy : tstoreddef;override;
  99. function GetTypeName:string;override;
  100. procedure ppuwrite(ppufile:tcompilerppufile);override;
  101. function getvardef:longint;override;
  102. procedure setsize;
  103. function is_publishable : boolean;override;
  104. function needs_inittable : boolean;override;
  105. end;
  106. tformaldef = class(tstoreddef)
  107. typed:boolean;
  108. constructor create(Atyped:boolean);
  109. constructor ppuload(ppufile:tcompilerppufile);
  110. procedure ppuwrite(ppufile:tcompilerppufile);override;
  111. function GetTypeName:string;override;
  112. end;
  113. tforwarddef = class(tstoreddef)
  114. tosymname : pshortstring;
  115. forwardpos : tfileposinfo;
  116. constructor create(const s:string;const pos : tfileposinfo);
  117. destructor destroy;override;
  118. function GetTypeName:string;override;
  119. end;
  120. tundefineddef = class(tstoreddef)
  121. constructor create;
  122. constructor ppuload(ppufile:tcompilerppufile);
  123. procedure ppuwrite(ppufile:tcompilerppufile);override;
  124. function GetTypeName:string;override;
  125. end;
  126. terrordef = class(tstoreddef)
  127. constructor create;
  128. procedure ppuwrite(ppufile:tcompilerppufile);override;
  129. function GetTypeName:string;override;
  130. function getmangledparaname : string;override;
  131. end;
  132. tabstractpointerdef = class(tstoreddef)
  133. pointeddef : tdef;
  134. pointeddefderef : tderef;
  135. constructor create(dt:tdeftyp;def:tdef);
  136. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. procedure buildderef;override;
  139. procedure deref;override;
  140. end;
  141. tpointerdef = class(tabstractpointerdef)
  142. is_far : boolean;
  143. constructor create(def:tdef);
  144. constructor createfar(def:tdef);
  145. function getcopy : tstoreddef;override;
  146. constructor ppuload(ppufile:tcompilerppufile);
  147. procedure ppuwrite(ppufile:tcompilerppufile);override;
  148. function GetTypeName:string;override;
  149. end;
  150. tprocdef = class;
  151. { tabstractrecorddef }
  152. tabstractrecorddef= class(tstoreddef)
  153. objname,
  154. objrealname: PShortString;
  155. symtable : TSymtable;
  156. cloneddef : tabstractrecorddef;
  157. cloneddefderef : tderef;
  158. objectoptions : tobjectoptions;
  159. constructor create(const n:string; dt:tdeftyp);
  160. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  161. procedure ppuwrite(ppufile:tcompilerppufile);override;
  162. destructor destroy; override;
  163. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  164. function GetSymtable(t:tGetSymtable):TSymtable;override;
  165. function is_packed:boolean;
  166. function RttiName: string;
  167. end;
  168. trecorddef = class(tabstractrecorddef)
  169. public
  170. isunion : boolean;
  171. constructor create(const n:string; p:TSymtable);
  172. constructor ppuload(ppufile:tcompilerppufile);
  173. destructor destroy;override;
  174. function getcopy : tstoreddef;override;
  175. procedure ppuwrite(ppufile:tcompilerppufile);override;
  176. procedure buildderef;override;
  177. procedure deref;override;
  178. function size:aint;override;
  179. function alignment : shortint;override;
  180. function padalignment: shortint;
  181. function GetTypeName:string;override;
  182. { debug }
  183. function needs_inittable : boolean;override;
  184. end;
  185. tobjectdef = class;
  186. { TImplementedInterface }
  187. TImplementedInterface = class
  188. IntfDef : tobjectdef;
  189. IntfDefDeref : tderef;
  190. IType : tinterfaceentrytype;
  191. IOffset : longint;
  192. VtblImplIntf : TImplementedInterface;
  193. NameMappings : TFPHashList;
  194. ProcDefs : TFPObjectList;
  195. ImplementsGetter : tsym;
  196. constructor create(aintf: tobjectdef);
  197. constructor create_deref(d:tderef);
  198. destructor destroy; override;
  199. function getcopy:TImplementedInterface;
  200. procedure buildderef;
  201. procedure deref;
  202. procedure AddMapping(const origname, newname: string);
  203. function GetMapping(const origname: string):string;
  204. procedure AddImplProc(pd:tprocdef);
  205. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  206. end;
  207. { tvmtentry }
  208. tvmtentry = record
  209. procdef : tprocdef;
  210. procdefderef : tderef;
  211. visibility : tvisibility;
  212. end;
  213. pvmtentry = ^tvmtentry;
  214. { tobjectdef }
  215. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  216. pmvcallstaticinfo = ^tmvcallstaticinfo;
  217. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  218. tobjectdef = class(tabstractrecorddef)
  219. private
  220. fcurrent_dispid: longint;
  221. public
  222. dwarf_struct_lab : tasmsymbol;
  223. childof : tobjectdef;
  224. childofderef : tderef;
  225. { for C++ classes: name of the library this class is imported from }
  226. import_lib,
  227. { for Objective-C: protocols and classes can have the same name there }
  228. objextname : pshortstring;
  229. { to be able to have a variable vmt position }
  230. { and no vmt field for objects without virtuals }
  231. vmtentries : TFPList;
  232. vmcallstaticinfo : pmvcallstaticinfo;
  233. vmt_offset : longint;
  234. objecttype : tobjecttyp;
  235. iidguid : pguid;
  236. iidstr : pshortstring;
  237. writing_class_record_dbginfo,
  238. { a class of this type has been created in this module }
  239. created_in_current_module,
  240. { a loadvmtnode for this class has been created in this
  241. module, so if a classrefdef variable of this or a parent
  242. class is used somewhere to instantiate a class, then this
  243. class may be instantiated
  244. }
  245. maybe_created_in_current_module,
  246. { a "class of" this particular class has been created in
  247. this module
  248. }
  249. classref_created_in_current_module : boolean;
  250. { store implemented interfaces defs and name mappings }
  251. ImplementedInterfaces : TFPObjectList;
  252. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);
  253. constructor ppuload(ppufile:tcompilerppufile);
  254. destructor destroy;override;
  255. function getcopy : tstoreddef;override;
  256. procedure ppuwrite(ppufile:tcompilerppufile);override;
  257. function GetTypeName:string;override;
  258. procedure buildderef;override;
  259. procedure deref;override;
  260. procedure buildderefimpl;override;
  261. procedure derefimpl;override;
  262. procedure resetvmtentries;
  263. procedure copyvmtentries(objdef:tobjectdef);
  264. function getparentdef:tdef;override;
  265. function size : aint;override;
  266. function alignment:shortint;override;
  267. function vmtmethodoffset(index:longint):longint;
  268. function members_need_inittable : boolean;
  269. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  270. { this should be called when this class implements an interface }
  271. procedure prepareguid;
  272. function is_publishable : boolean;override;
  273. function is_related(d : tdef) : boolean;override;
  274. function needs_inittable : boolean;override;
  275. function rtti_mangledname(rt:trttitype):string;override;
  276. function vmt_mangledname : string;
  277. procedure check_forwards;
  278. procedure insertvmt;
  279. procedure set_parent(c : tobjectdef);
  280. function find_destructor: tprocdef;
  281. function implements_any_interfaces: boolean;
  282. { dispinterface support }
  283. function get_next_dispid: longint;
  284. { enumerator support }
  285. function search_enumerator_get: tprocdef;
  286. function search_enumerator_move: tprocdef;
  287. function search_enumerator_current: tsym;
  288. { WPO }
  289. procedure register_created_object_type;override;
  290. procedure register_maybe_created_object_type;
  291. procedure register_created_classref_type;
  292. procedure register_vmt_call(index:longint);
  293. { ObjC & C++ }
  294. procedure make_all_methods_external;
  295. { ObjC }
  296. procedure finish_objc_data;
  297. function check_objc_types: boolean;
  298. { C++ }
  299. procedure finish_cpp_data;
  300. end;
  301. tclassrefdef = class(tabstractpointerdef)
  302. constructor create(def:tdef);
  303. constructor ppuload(ppufile:tcompilerppufile);
  304. procedure ppuwrite(ppufile:tcompilerppufile);override;
  305. function GetTypeName:string;override;
  306. function is_publishable : boolean;override;
  307. function rtti_mangledname(rt:trttitype):string;override;
  308. procedure register_created_object_type;override;
  309. end;
  310. tarraydef = class(tstoreddef)
  311. lowrange,
  312. highrange : aint;
  313. rangedef : tdef;
  314. rangedefderef : tderef;
  315. arrayoptions : tarraydefoptions;
  316. protected
  317. _elementdef : tdef;
  318. _elementdefderef : tderef;
  319. procedure setelementdef(def:tdef);
  320. public
  321. function elesize : aint;
  322. function elepackedbitsize : aint;
  323. function elecount : aword;
  324. constructor create_from_pointer(def:tdef);
  325. constructor create(l,h : aint;def:tdef);
  326. constructor ppuload(ppufile:tcompilerppufile);
  327. function getcopy : tstoreddef;override;
  328. procedure ppuwrite(ppufile:tcompilerppufile);override;
  329. function GetTypeName:string;override;
  330. function getmangledparaname : string;override;
  331. procedure buildderef;override;
  332. procedure deref;override;
  333. function size : aint;override;
  334. function alignment : shortint;override;
  335. { returns the label of the range check string }
  336. function needs_inittable : boolean;override;
  337. property elementdef : tdef read _elementdef write setelementdef;
  338. function is_publishable : boolean;override;
  339. end;
  340. torddef = class(tstoreddef)
  341. low,high : TConstExprInt;
  342. ordtype : tordtype;
  343. constructor create(t : tordtype;v,b : TConstExprInt);
  344. constructor ppuload(ppufile:tcompilerppufile);
  345. function getcopy : tstoreddef;override;
  346. procedure ppuwrite(ppufile:tcompilerppufile);override;
  347. function is_publishable : boolean;override;
  348. function GetTypeName:string;override;
  349. function alignment:shortint;override;
  350. procedure setsize;
  351. function packedbitsize: aint; override;
  352. function getvardef : longint;override;
  353. end;
  354. tfloatdef = class(tstoreddef)
  355. floattype : tfloattype;
  356. constructor create(t : tfloattype);
  357. constructor ppuload(ppufile:tcompilerppufile);
  358. function getcopy : tstoreddef;override;
  359. procedure ppuwrite(ppufile:tcompilerppufile);override;
  360. function GetTypeName:string;override;
  361. function is_publishable : boolean;override;
  362. function alignment:shortint;override;
  363. procedure setsize;
  364. function getvardef:longint;override;
  365. end;
  366. { tabstractprocdef }
  367. tabstractprocdef = class(tstoreddef)
  368. { saves a definition to the return type }
  369. returndef : tdef;
  370. returndefderef : tderef;
  371. parast : TSymtable;
  372. paras : tparalist;
  373. proctypeoption : tproctypeoption;
  374. proccalloption : tproccalloption;
  375. procoptions : tprocoptions;
  376. callerargareasize,
  377. calleeargareasize: pint;
  378. { number of user visibile parameters }
  379. maxparacount,
  380. minparacount : byte;
  381. {$ifdef m68k}
  382. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  383. {$endif}
  384. funcretloc : array[tcallercallee] of TCGPara;
  385. has_paraloc_info : tcallercallee; { paraloc info is available }
  386. constructor create(dt:tdeftyp;level:byte);
  387. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  388. destructor destroy;override;
  389. procedure ppuwrite(ppufile:tcompilerppufile);override;
  390. procedure buildderef;override;
  391. procedure deref;override;
  392. procedure calcparas;
  393. function typename_paras(showhidden:boolean): string;
  394. function is_methodpointer:boolean;virtual;
  395. function is_addressonly:boolean;virtual;
  396. function no_self_node:boolean;
  397. procedure check_mark_as_nested;
  398. procedure init_paraloc_info(side: tcallercallee);
  399. function stack_tainting_parameter(side: tcallercallee): boolean;
  400. private
  401. procedure count_para(p:TObject;arg:pointer);
  402. procedure insert_para(p:TObject;arg:pointer);
  403. end;
  404. tprocvardef = class(tabstractprocdef)
  405. constructor create(level:byte);
  406. constructor ppuload(ppufile:tcompilerppufile);
  407. function getcopy : tstoreddef;override;
  408. procedure ppuwrite(ppufile:tcompilerppufile);override;
  409. function GetSymtable(t:tGetSymtable):TSymtable;override;
  410. function size : aint;override;
  411. function GetTypeName:string;override;
  412. function is_publishable : boolean;override;
  413. function is_methodpointer:boolean;override;
  414. function is_addressonly:boolean;override;
  415. function getmangledparaname:string;override;
  416. end;
  417. tmessageinf = record
  418. case integer of
  419. 0 : (str : pshortstring);
  420. 1 : (i : longint);
  421. end;
  422. tinlininginfo = record
  423. { node tree }
  424. code : tnode;
  425. flags : tprocinfoflags;
  426. end;
  427. pinlininginfo = ^tinlininginfo;
  428. {$ifdef oldregvars}
  429. { register variables }
  430. pregvarinfo = ^tregvarinfo;
  431. tregvarinfo = record
  432. regvars : array[1..maxvarregs] of tsym;
  433. regvars_para : array[1..maxvarregs] of boolean;
  434. regvars_refs : array[1..maxvarregs] of longint;
  435. fpuregvars : array[1..maxfpuvarregs] of tsym;
  436. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  437. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  438. end;
  439. {$endif oldregvars}
  440. { tprocdef }
  441. tprocdef = class(tabstractprocdef)
  442. private
  443. _mangledname : pshortstring;
  444. public
  445. messageinf : tmessageinf;
  446. dispid : longint;
  447. {$ifndef EXTDEBUG}
  448. { where is this function defined and what were the symbol
  449. flags, needed here because there
  450. is only one symbol for all overloaded functions
  451. EXTDEBUG has fileinfo in tdef (PFV) }
  452. fileinfo : tfileposinfo;
  453. {$endif}
  454. symoptions : tsymoptions;
  455. deprecatedmsg : pshortstring;
  456. { symbol owning this definition }
  457. procsym : tsym;
  458. procsymderef : tderef;
  459. { alias names }
  460. aliasnames : TCmdStrList;
  461. { symtables }
  462. localst : TSymtable;
  463. funcretsym : tsym;
  464. funcretsymderef : tderef;
  465. struct : tabstractrecorddef;
  466. structderef : tderef;
  467. {$if defined(powerpc) or defined(m68k)}
  468. { library symbol for AmigaOS/MorphOS }
  469. libsym : tsym;
  470. libsymderef : tderef;
  471. {$endif powerpc or m68k}
  472. { name of the result variable to insert in the localsymtable }
  473. resultname : pshortstring;
  474. { import info }
  475. import_dll,
  476. import_name : pshortstring;
  477. { info for inlining the subroutine, if this pointer is nil,
  478. the procedure can't be inlined }
  479. inlininginfo : pinlininginfo;
  480. {$ifdef oldregvars}
  481. regvarinfo: pregvarinfo;
  482. {$endif oldregvars}
  483. { First/last assembler symbol/instruction in aasmoutput list.
  484. Note: initialised after compiling the code for the procdef, but
  485. not saved to/restored from ppu. Used when inserting debug info }
  486. procstarttai,
  487. procendtai : tai;
  488. import_nr : word;
  489. extnumber : word;
  490. {$ifdef i386}
  491. fpu_used : byte;
  492. {$endif i386}
  493. visibility : tvisibility;
  494. { true, if the procedure is only declared
  495. (forward procedure) }
  496. forwarddef,
  497. { true if the procedure is declared in the interface }
  498. interfacedef : boolean;
  499. { true if the procedure has a forward declaration }
  500. hasforward : boolean;
  501. constructor create(level:byte);
  502. constructor ppuload(ppufile:tcompilerppufile);
  503. destructor destroy;override;
  504. procedure ppuwrite(ppufile:tcompilerppufile);override;
  505. procedure buildderef;override;
  506. procedure buildderefimpl;override;
  507. procedure deref;override;
  508. procedure derefimpl;override;
  509. function GetSymtable(t:tGetSymtable):TSymtable;override;
  510. function GetTypeName : string;override;
  511. function mangledname : string;
  512. procedure setmangledname(const s : string);
  513. function fullprocname(showhidden:boolean):string;
  514. function cplusplusmangledname : string;
  515. function objcmangledname : string;
  516. function is_methodpointer:boolean;override;
  517. function is_addressonly:boolean;override;
  518. end;
  519. { single linked list of overloaded procs }
  520. pprocdeflist = ^tprocdeflist;
  521. tprocdeflist = record
  522. def : tprocdef;
  523. defderef : tderef;
  524. next : pprocdeflist;
  525. end;
  526. tstringdef = class(tstoreddef)
  527. stringtype : tstringtype;
  528. len : aint;
  529. constructor createshort(l : byte);
  530. constructor loadshort(ppufile:tcompilerppufile);
  531. constructor createlong(l : aint);
  532. constructor loadlong(ppufile:tcompilerppufile);
  533. constructor createansi;
  534. constructor loadansi(ppufile:tcompilerppufile);
  535. constructor createwide;
  536. constructor loadwide(ppufile:tcompilerppufile);
  537. constructor createunicode;
  538. constructor loadunicode(ppufile:tcompilerppufile);
  539. function getcopy : tstoreddef;override;
  540. function stringtypname:string;
  541. procedure ppuwrite(ppufile:tcompilerppufile);override;
  542. function GetTypeName:string;override;
  543. function getmangledparaname:string;override;
  544. function is_publishable : boolean;override;
  545. function alignment : shortint;override;
  546. function needs_inittable : boolean;override;
  547. function getvardef:longint;override;
  548. end;
  549. { tenumdef }
  550. tenumdef = class(tstoreddef)
  551. minval,
  552. maxval : aint;
  553. has_jumps : boolean;
  554. basedef : tenumdef;
  555. basedefderef : tderef;
  556. symtable : TSymtable;
  557. constructor create;
  558. constructor create_subrange(_basedef:tenumdef;_min,_max:aint);
  559. constructor ppuload(ppufile:tcompilerppufile);
  560. destructor destroy;override;
  561. function getcopy : tstoreddef;override;
  562. procedure ppuwrite(ppufile:tcompilerppufile);override;
  563. procedure buildderef;override;
  564. procedure deref;override;
  565. function GetTypeName:string;override;
  566. function is_publishable : boolean;override;
  567. procedure calcsavesize;
  568. function packedbitsize: aint; override;
  569. procedure setmax(_max:aint);
  570. procedure setmin(_min:aint);
  571. function min:aint;
  572. function max:aint;
  573. function getfirstsym:tsym;
  574. end;
  575. tsetdef = class(tstoreddef)
  576. elementdef : tdef;
  577. elementdefderef : tderef;
  578. setbase,
  579. setmax : aword;
  580. constructor create(def:tdef;low, high : aint);
  581. constructor ppuload(ppufile:tcompilerppufile);
  582. function getcopy : tstoreddef;override;
  583. procedure ppuwrite(ppufile:tcompilerppufile);override;
  584. procedure buildderef;override;
  585. procedure deref;override;
  586. function GetTypeName:string;override;
  587. function is_publishable : boolean;override;
  588. end;
  589. var
  590. current_structdef: tabstractrecorddef;
  591. current_objectdef : tobjectdef absolute current_structdef; { used for private functions check !! }
  592. current_genericdef : tobjectdef; { used to reject declaration of generic class inside generic class }
  593. current_specializedef : tobjectdef; { used to implement usage of generic class in itself }
  594. { default types }
  595. generrordef, { error in definition }
  596. voidpointertype, { pointer for Void-pointeddef }
  597. charpointertype, { pointer for Char-pointeddef }
  598. widecharpointertype, { pointer for WideChar-pointeddef }
  599. voidfarpointertype,
  600. cundefinedtype,
  601. cformaltype, { unique formal definition }
  602. ctypedformaltype, { unique typed formal definition }
  603. voidtype, { Void (procedure) }
  604. cchartype, { Char }
  605. cwidechartype, { WideChar }
  606. booltype, { boolean type }
  607. bool8type,
  608. bool16type,
  609. bool32type,
  610. bool64type, { implement me }
  611. u8inttype, { 8-Bit unsigned integer }
  612. s8inttype, { 8-Bit signed integer }
  613. u16inttype, { 16-Bit unsigned integer }
  614. s16inttype, { 16-Bit signed integer }
  615. u32inttype, { 32-Bit unsigned integer }
  616. s32inttype, { 32-Bit signed integer }
  617. u64inttype, { 64-bit unsigned integer }
  618. s64inttype, { 64-bit signed integer }
  619. s32floattype, { 32 bit floating point number }
  620. s64floattype, { 64 bit floating point number }
  621. s80floattype, { 80 bit floating point number }
  622. sc80floattype, { 80 bit floating point number but stored like in C }
  623. s64currencytype, { pointer to a currency type }
  624. cshortstringtype, { pointer to type of short string const }
  625. clongstringtype, { pointer to type of long string const }
  626. cansistringtype, { pointer to type of ansi string const }
  627. cwidestringtype, { pointer to type of wide string const }
  628. cunicodestringtype,
  629. openshortstringtype, { pointer to type of an open shortstring,
  630. needed for readln() }
  631. openchararraytype, { pointer to type of an open array of char,
  632. needed for readln() }
  633. cfiletype, { get the same definition for all file }
  634. { used for stabs }
  635. methodpointertype, { typecasting of methodpointers to extract self }
  636. hresultdef,
  637. { we use only one variant def for every variant class }
  638. cvarianttype,
  639. colevarianttype,
  640. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  641. sinttype,
  642. uinttype,
  643. { unsigned and signed ord type with the same size as a pointer }
  644. ptruinttype,
  645. ptrsinttype,
  646. { several types to simulate more or less C++ objects for GDB }
  647. vmttype,
  648. vmtarraytype,
  649. pvmttype : tdef; { type of classrefs, used for stabs }
  650. { pointer to the anchestor of all classes }
  651. class_tobject : tobjectdef;
  652. { pointer to the ancestor of all COM interfaces }
  653. interface_iunknown : tobjectdef;
  654. { pointer to the TGUID type
  655. of all interfaces }
  656. rec_tguid : trecorddef;
  657. { pointer to jump buffer }
  658. rec_jmp_buf : trecorddef;
  659. { Objective-C base types }
  660. objc_metaclasstype,
  661. objc_superclasstype,
  662. objc_idtype,
  663. objc_seltype : tpointerdef;
  664. objc_objecttype : trecorddef;
  665. { base type of @protocol(protocolname) Objective-C statements }
  666. objc_protocoltype : tobjectdef;
  667. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  668. objc_fastenumeration : tobjectdef;
  669. objc_fastenumerationstate : trecorddef;
  670. const
  671. {$ifdef i386}
  672. pbestrealtype : ^tdef = @s80floattype;
  673. {$endif}
  674. {$ifdef x86_64}
  675. pbestrealtype : ^tdef = @s80floattype;
  676. {$endif}
  677. {$ifdef m68k}
  678. pbestrealtype : ^tdef = @s64floattype;
  679. {$endif}
  680. {$ifdef alpha}
  681. pbestrealtype : ^tdef = @s64floattype;
  682. {$endif}
  683. {$ifdef powerpc}
  684. pbestrealtype : ^tdef = @s64floattype;
  685. {$endif}
  686. {$ifdef POWERPC64}
  687. pbestrealtype : ^tdef = @s64floattype;
  688. {$endif}
  689. {$ifdef ia64}
  690. pbestrealtype : ^tdef = @s64floattype;
  691. {$endif}
  692. {$ifdef SPARC}
  693. pbestrealtype : ^tdef = @s64floattype;
  694. {$endif SPARC}
  695. {$ifdef vis}
  696. pbestrealtype : ^tdef = @s64floattype;
  697. {$endif vis}
  698. {$ifdef ARM}
  699. pbestrealtype : ^tdef = @s64floattype;
  700. {$endif ARM}
  701. {$ifdef MIPS}
  702. pbestrealtype : ^tdef = @s64floattype;
  703. {$endif MIPS}
  704. {$ifdef AVR}
  705. pbestrealtype : ^tdef = @s64floattype;
  706. {$endif AVR}
  707. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  708. { should be in the types unit, but the types unit uses the node stuff :( }
  709. function is_interfacecom(def: tdef): boolean;
  710. function is_interfacecorba(def: tdef): boolean;
  711. function is_interface(def: tdef): boolean;
  712. function is_dispinterface(def: tdef): boolean;
  713. function is_object(def: tdef): boolean;
  714. function is_class(def: tdef): boolean;
  715. function is_cppclass(def: tdef): boolean;
  716. function is_objcclass(def: tdef): boolean;
  717. function is_objcclassref(def: tdef): boolean;
  718. function is_objcprotocol(def: tdef): boolean;
  719. function is_objccategory(def: tdef): boolean;
  720. function is_objc_class_or_protocol(def: tdef): boolean;
  721. function is_objc_protocol_or_category(def: tdef): boolean;
  722. function is_class_or_interface(def: tdef): boolean;
  723. function is_class_or_interface_or_objc(def: tdef): boolean;
  724. function is_class_or_interface_or_object(def: tdef): boolean;
  725. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  726. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  727. function is_class_or_object(def: tdef): boolean;
  728. function is_record(def: tdef): boolean;
  729. procedure loadobjctypes;
  730. procedure maybeloadcocoatypes;
  731. function use_vectorfpu(def : tdef) : boolean;
  732. implementation
  733. uses
  734. SysUtils,
  735. cutils,
  736. { global }
  737. verbose,
  738. { target }
  739. systems,aasmcpu,paramgr,
  740. { symtable }
  741. symsym,symtable,symutil,defutil,objcdef,
  742. { module }
  743. fmodule,
  744. { other }
  745. gendef,
  746. fpccrc
  747. ;
  748. {****************************************************************************
  749. Helpers
  750. ****************************************************************************}
  751. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  752. var
  753. s,hs,
  754. prefix : string;
  755. oldlen,
  756. newlen,
  757. i : longint;
  758. crc : dword;
  759. hp : tparavarsym;
  760. begin
  761. prefix:='';
  762. if not assigned(st) then
  763. internalerror(200204212);
  764. { sub procedures }
  765. while (st.symtabletype=localsymtable) do
  766. begin
  767. if st.defowner.typ<>procdef then
  768. internalerror(200204173);
  769. { Add the full mangledname of procedure to prevent
  770. conflicts with 2 overloads having both a nested procedure
  771. with the same name, see tb0314 (PFV) }
  772. s:=tprocdef(st.defowner).procsym.name;
  773. oldlen:=length(s);
  774. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  775. begin
  776. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  777. if not(vo_is_hidden_para in hp.varoptions) then
  778. s:=s+'$'+hp.vardef.mangledparaname;
  779. end;
  780. if not is_void(tprocdef(st.defowner).returndef) then
  781. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  782. newlen:=length(s);
  783. { Replace with CRC if the parameter line is very long }
  784. if (newlen-oldlen>12) and
  785. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  786. begin
  787. crc:=0;
  788. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  789. begin
  790. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  791. if not(vo_is_hidden_para in hp.varoptions) then
  792. begin
  793. hs:=hp.vardef.mangledparaname;
  794. crc:=UpdateCrc32(crc,hs[1],length(hs));
  795. end;
  796. end;
  797. hs:=hp.vardef.mangledparaname;
  798. crc:=UpdateCrc32(crc,hs[1],length(hs));
  799. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  800. end;
  801. if prefix<>'' then
  802. prefix:=s+'_'+prefix
  803. else
  804. prefix:=s;
  805. if length(prefix)>100 then
  806. begin
  807. crc:=0;
  808. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  809. prefix:='$CRC'+hexstr(crc,8);
  810. end;
  811. st:=st.defowner.owner;
  812. end;
  813. { object/classes symtable, nested type definitions in classes require the while loop }
  814. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  815. begin
  816. if not (st.defowner.typ in [objectdef,recorddef]) then
  817. internalerror(200204174);
  818. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  819. st:=st.defowner.owner;
  820. end;
  821. { symtable must now be static or global }
  822. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  823. internalerror(200204175);
  824. result:='';
  825. if typeprefix<>'' then
  826. result:=result+typeprefix+'_';
  827. { Add P$ for program, which can have the same name as
  828. a unit }
  829. if (TSymtable(main_module.localsymtable)=st) and
  830. (not main_module.is_unit) then
  831. result:=result+'P$'+st.name^
  832. else
  833. result:=result+st.name^;
  834. if prefix<>'' then
  835. result:=result+'_'+prefix;
  836. if suffix<>'' then
  837. result:=result+'_'+suffix;
  838. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  839. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  840. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  841. { those in the debug map, leading to troubles with dsymutil). So always }
  842. { add an underscore on darwin. }
  843. if (target_info.system in systems_darwin) then
  844. result := '_' + result;
  845. end;
  846. {****************************************************************************
  847. TDEF (base class for definitions)
  848. ****************************************************************************}
  849. constructor tstoreddef.create(dt:tdeftyp);
  850. var
  851. insertstack : psymtablestackitem;
  852. begin
  853. inherited create(dt);
  854. savesize := 0;
  855. {$ifdef EXTDEBUG}
  856. fileinfo := current_filepos;
  857. {$endif}
  858. generictokenbuf:=nil;
  859. genericdef:=nil;
  860. { Don't register forwarddefs, they are disposed at the
  861. end of an type block }
  862. if (dt=forwarddef) then
  863. exit;
  864. { Register in current_module }
  865. if assigned(current_module) then
  866. begin
  867. current_module.deflist.Add(self);
  868. DefId:=current_module.deflist.Count-1;
  869. end;
  870. { Register in symtable stack }
  871. if assigned(symtablestack) then
  872. begin
  873. insertstack:=symtablestack.stack;
  874. while assigned(insertstack) and
  875. (insertstack^.symtable.symtabletype=withsymtable) do
  876. insertstack:=insertstack^.next;
  877. if not assigned(insertstack) then
  878. internalerror(200602044);
  879. insertstack^.symtable.insertdef(self);
  880. end;
  881. end;
  882. destructor tstoreddef.destroy;
  883. begin
  884. { Direct calls are not allowed, use symtable.deletedef() }
  885. if assigned(owner) then
  886. internalerror(200612311);
  887. if assigned(generictokenbuf) then
  888. begin
  889. generictokenbuf.free;
  890. generictokenbuf:=nil;
  891. end;
  892. inherited destroy;
  893. end;
  894. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  895. var
  896. sizeleft,i : longint;
  897. buf : array[0..255] of byte;
  898. begin
  899. inherited create(dt);
  900. DefId:=ppufile.getlongint;
  901. current_module.deflist[DefId]:=self;
  902. {$ifdef EXTDEBUG}
  903. fillchar(fileinfo,sizeof(fileinfo),0);
  904. {$endif}
  905. { load }
  906. ppufile.getderef(typesymderef);
  907. ppufile.getsmallset(defoptions);
  908. ppufile.getsmallset(defstates);
  909. if df_generic in defoptions then
  910. begin
  911. sizeleft:=ppufile.getlongint;
  912. initgeneric;
  913. while sizeleft>0 do
  914. begin
  915. if sizeleft>sizeof(buf) then
  916. i:=sizeof(buf)
  917. else
  918. i:=sizeleft;
  919. ppufile.getdata(buf,i);
  920. generictokenbuf.write(buf,i);
  921. dec(sizeleft,i);
  922. end;
  923. end;
  924. if df_specialization in defoptions then
  925. ppufile.getderef(genericdefderef);
  926. end;
  927. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  928. var
  929. prefix : string[4];
  930. begin
  931. if rt=fullrtti then
  932. begin
  933. prefix:='RTTI';
  934. include(defstates,ds_rtti_table_used);
  935. end
  936. else
  937. begin
  938. prefix:='INIT';
  939. include(defstates,ds_init_table_used);
  940. end;
  941. if assigned(typesym) and
  942. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  943. result:=make_mangledname(prefix,owner,typesym.name)
  944. else
  945. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  946. end;
  947. function tstoreddef.in_currentunit: boolean;
  948. var
  949. st: tsymtable;
  950. begin
  951. st:=owner;
  952. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  953. st:=st.defowner.owner;
  954. result:=st.iscurrentunit;
  955. end;
  956. function tstoreddef.getcopy : tstoreddef;
  957. begin
  958. Message(sym_e_cant_create_unique_type);
  959. getcopy:=terrordef.create;
  960. end;
  961. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  962. var
  963. sizeleft,i : longint;
  964. buf : array[0..255] of byte;
  965. oldintfcrc : boolean;
  966. begin
  967. ppufile.putlongint(DefId);
  968. ppufile.putderef(typesymderef);
  969. ppufile.putsmallset(defoptions);
  970. oldintfcrc:=ppufile.do_crc;
  971. ppufile.do_crc:=false;
  972. ppufile.putsmallset(defstates);
  973. if df_generic in defoptions then
  974. begin
  975. if assigned(generictokenbuf) then
  976. begin
  977. sizeleft:=generictokenbuf.size;
  978. generictokenbuf.seek(0);
  979. end
  980. else
  981. sizeleft:=0;
  982. ppufile.putlongint(sizeleft);
  983. while sizeleft>0 do
  984. begin
  985. if sizeleft>sizeof(buf) then
  986. i:=sizeof(buf)
  987. else
  988. i:=sizeleft;
  989. generictokenbuf.read(buf,i);
  990. ppufile.putdata(buf,i);
  991. dec(sizeleft,i);
  992. end;
  993. end;
  994. ppufile.do_crc:=oldintfcrc;
  995. if df_specialization in defoptions then
  996. ppufile.putderef(genericdefderef);
  997. end;
  998. procedure tstoreddef.buildderef;
  999. begin
  1000. typesymderef.build(typesym);
  1001. genericdefderef.build(genericdef);
  1002. end;
  1003. procedure tstoreddef.buildderefimpl;
  1004. begin
  1005. end;
  1006. procedure tstoreddef.deref;
  1007. begin
  1008. typesym:=ttypesym(typesymderef.resolve);
  1009. if df_specialization in defoptions then
  1010. genericdef:=tstoreddef(genericdefderef.resolve);
  1011. end;
  1012. procedure tstoreddef.derefimpl;
  1013. begin
  1014. end;
  1015. function tstoreddef.size : aint;
  1016. begin
  1017. size:=savesize;
  1018. end;
  1019. function tstoreddef.getvardef:longint;
  1020. begin
  1021. result:=varUndefined;
  1022. end;
  1023. function tstoreddef.alignment : shortint;
  1024. begin
  1025. { natural alignment by default }
  1026. alignment:=size_2_align(savesize);
  1027. { can happen if savesize = 0, e.g. for voiddef or
  1028. an empty record
  1029. }
  1030. if (alignment=0) then
  1031. alignment:=1;
  1032. end;
  1033. { returns true, if the definition can be published }
  1034. function tstoreddef.is_publishable : boolean;
  1035. begin
  1036. is_publishable:=false;
  1037. end;
  1038. { needs an init table }
  1039. function tstoreddef.needs_inittable : boolean;
  1040. begin
  1041. needs_inittable:=false;
  1042. end;
  1043. function tstoreddef.is_intregable : boolean;
  1044. var
  1045. recsize,temp: longint;
  1046. begin
  1047. is_intregable:=false;
  1048. case typ of
  1049. orddef,
  1050. pointerdef,
  1051. enumdef,
  1052. classrefdef:
  1053. is_intregable:=true;
  1054. procvardef :
  1055. is_intregable:=tprocvardef(self).is_addressonly;
  1056. objectdef:
  1057. is_intregable:=(is_class_or_interface_or_dispinterface_or_objc(self)) and not needs_inittable;
  1058. setdef:
  1059. is_intregable:=is_smallset(self);
  1060. recorddef:
  1061. begin
  1062. recsize:=size;
  1063. is_intregable:=
  1064. ispowerof2(recsize,temp) and
  1065. (recsize <= sizeof(aint));
  1066. end;
  1067. end;
  1068. end;
  1069. function tstoreddef.is_fpuregable : boolean;
  1070. begin
  1071. {$ifdef x86}
  1072. result:=use_vectorfpu(self);
  1073. {$else x86}
  1074. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1075. {$endif x86}
  1076. end;
  1077. procedure tstoreddef.initgeneric;
  1078. begin
  1079. if assigned(generictokenbuf) then
  1080. internalerror(200512131);
  1081. generictokenbuf:=tdynamicarray.create(256);
  1082. end;
  1083. {****************************************************************************
  1084. Tstringdef
  1085. ****************************************************************************}
  1086. constructor tstringdef.createshort(l : byte);
  1087. begin
  1088. inherited create(stringdef);
  1089. stringtype:=st_shortstring;
  1090. len:=l;
  1091. savesize:=len+1;
  1092. end;
  1093. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1094. begin
  1095. inherited ppuload(stringdef,ppufile);
  1096. stringtype:=st_shortstring;
  1097. len:=ppufile.getbyte;
  1098. savesize:=len+1;
  1099. end;
  1100. constructor tstringdef.createlong(l : aint);
  1101. begin
  1102. inherited create(stringdef);
  1103. stringtype:=st_longstring;
  1104. len:=l;
  1105. savesize:=sizeof(pint);
  1106. end;
  1107. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1108. begin
  1109. inherited ppuload(stringdef,ppufile);
  1110. stringtype:=st_longstring;
  1111. len:=ppufile.getaint;
  1112. savesize:=sizeof(pint);
  1113. end;
  1114. constructor tstringdef.createansi;
  1115. begin
  1116. inherited create(stringdef);
  1117. stringtype:=st_ansistring;
  1118. len:=-1;
  1119. savesize:=sizeof(pint);
  1120. end;
  1121. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1122. begin
  1123. inherited ppuload(stringdef,ppufile);
  1124. stringtype:=st_ansistring;
  1125. len:=ppufile.getaint;
  1126. savesize:=sizeof(pint);
  1127. end;
  1128. constructor tstringdef.createwide;
  1129. begin
  1130. inherited create(stringdef);
  1131. stringtype:=st_widestring;
  1132. len:=-1;
  1133. savesize:=sizeof(pint);
  1134. end;
  1135. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1136. begin
  1137. inherited ppuload(stringdef,ppufile);
  1138. stringtype:=st_widestring;
  1139. len:=ppufile.getaint;
  1140. savesize:=sizeof(pint);
  1141. end;
  1142. constructor tstringdef.createunicode;
  1143. begin
  1144. inherited create(stringdef);
  1145. stringtype:=st_unicodestring;
  1146. len:=-1;
  1147. savesize:=sizeof(pint);
  1148. end;
  1149. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1150. begin
  1151. inherited ppuload(stringdef,ppufile);
  1152. stringtype:=st_unicodestring;
  1153. len:=ppufile.getaint;
  1154. savesize:=sizeof(pint);
  1155. end;
  1156. function tstringdef.getcopy : tstoreddef;
  1157. begin
  1158. result:=tstringdef.create(typ);
  1159. result.typ:=stringdef;
  1160. tstringdef(result).stringtype:=stringtype;
  1161. tstringdef(result).len:=len;
  1162. tstringdef(result).savesize:=savesize;
  1163. end;
  1164. function tstringdef.stringtypname:string;
  1165. const
  1166. typname:array[tstringtype] of string[10]=(
  1167. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1168. );
  1169. begin
  1170. stringtypname:=typname[stringtype];
  1171. end;
  1172. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1173. begin
  1174. inherited ppuwrite(ppufile);
  1175. if stringtype=st_shortstring then
  1176. begin
  1177. {$ifdef extdebug}
  1178. if len > 255 then internalerror(12122002);
  1179. {$endif}
  1180. ppufile.putbyte(byte(len))
  1181. end
  1182. else
  1183. ppufile.putaint(len);
  1184. case stringtype of
  1185. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1186. st_longstring : ppufile.writeentry(iblongstringdef);
  1187. st_ansistring : ppufile.writeentry(ibansistringdef);
  1188. st_widestring : ppufile.writeentry(ibwidestringdef);
  1189. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1190. end;
  1191. end;
  1192. function tstringdef.needs_inittable : boolean;
  1193. begin
  1194. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1195. end;
  1196. function tstringdef.GetTypeName : string;
  1197. const
  1198. names : array[tstringtype] of string[15] = (
  1199. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1200. begin
  1201. GetTypeName:=names[stringtype];
  1202. end;
  1203. function tstringdef.getvardef : longint;
  1204. const
  1205. vardef : array[tstringtype] of longint = (
  1206. varUndefined,varUndefined,varString,varOleStr,varUString);
  1207. begin
  1208. result:=vardef[stringtype];
  1209. end;
  1210. function tstringdef.alignment : shortint;
  1211. begin
  1212. case stringtype of
  1213. st_unicodestring,
  1214. st_widestring,
  1215. st_ansistring:
  1216. alignment:=size_2_align(savesize);
  1217. st_longstring,
  1218. st_shortstring:
  1219. { char to string accesses byte 0 and 1 with one word access }
  1220. if (tf_requires_proper_alignment in target_info.flags) or
  1221. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1222. (m_mac in current_settings.modeswitches) then
  1223. alignment:=size_2_align(2)
  1224. else
  1225. alignment:=size_2_align(1);
  1226. else
  1227. internalerror(200412301);
  1228. end;
  1229. end;
  1230. function tstringdef.getmangledparaname : string;
  1231. begin
  1232. getmangledparaname:='STRING';
  1233. end;
  1234. function tstringdef.is_publishable : boolean;
  1235. begin
  1236. is_publishable:=true;
  1237. end;
  1238. {****************************************************************************
  1239. TENUMDEF
  1240. ****************************************************************************}
  1241. constructor tenumdef.create;
  1242. begin
  1243. inherited create(enumdef);
  1244. minval:=0;
  1245. maxval:=0;
  1246. calcsavesize;
  1247. has_jumps:=false;
  1248. basedef:=nil;
  1249. symtable:=tenumsymtable.create(self);
  1250. end;
  1251. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:aint);
  1252. begin
  1253. inherited create(enumdef);
  1254. minval:=_min;
  1255. maxval:=_max;
  1256. basedef:=_basedef;
  1257. calcsavesize;
  1258. has_jumps:=false;
  1259. symtable:=basedef.symtable.getcopy;
  1260. include(defoptions, df_copied_def);
  1261. end;
  1262. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1263. begin
  1264. inherited ppuload(enumdef,ppufile);
  1265. minval:=ppufile.getaint;
  1266. maxval:=ppufile.getaint;
  1267. savesize:=ppufile.getaint;
  1268. has_jumps:=false;
  1269. if df_copied_def in defoptions then
  1270. begin
  1271. symtable:=nil;
  1272. ppufile.getderef(basedefderef);
  1273. end
  1274. else
  1275. begin
  1276. // create with nil defowner first to prevent values changes on insert
  1277. symtable:=tenumsymtable.create(nil);
  1278. tenumsymtable(symtable).ppuload(ppufile);
  1279. symtable.defowner:=self;
  1280. end;
  1281. end;
  1282. destructor tenumdef.destroy;
  1283. begin
  1284. symtable.free;
  1285. symtable:=nil;
  1286. inherited destroy;
  1287. end;
  1288. function tenumdef.getcopy : tstoreddef;
  1289. begin
  1290. if assigned(basedef) then
  1291. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1292. else
  1293. begin
  1294. result:=tenumdef.create;
  1295. tenumdef(result).minval:=minval;
  1296. tenumdef(result).maxval:=maxval;
  1297. tenumdef(result).symtable.free;
  1298. tenumdef(result).symtable:=symtable.getcopy;
  1299. tenumdef(result).basedef:=self;
  1300. end;
  1301. tenumdef(result).has_jumps:=has_jumps;
  1302. tenumdef(result).basedefderef:=basedefderef;
  1303. include(tenumdef(result).defoptions,df_copied_def);
  1304. end;
  1305. procedure tenumdef.calcsavesize;
  1306. begin
  1307. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1308. savesize:=8
  1309. else
  1310. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1311. savesize:=4
  1312. else
  1313. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1314. savesize:=2
  1315. else
  1316. savesize:=1;
  1317. end;
  1318. function tenumdef.packedbitsize: aint;
  1319. var
  1320. sizeval: tconstexprint;
  1321. power: longint;
  1322. begin
  1323. result := 0;
  1324. if (minval >= 0) and
  1325. (maxval <= 1) then
  1326. result := 1
  1327. else
  1328. begin
  1329. if (minval>=0) then
  1330. sizeval:=maxval
  1331. else
  1332. { don't count 0 twice }
  1333. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1334. { 256 must become 512 etc. }
  1335. nextpowerof2(sizeval+1,power);
  1336. result := power;
  1337. end;
  1338. end;
  1339. procedure tenumdef.setmax(_max:aint);
  1340. begin
  1341. maxval:=_max;
  1342. calcsavesize;
  1343. end;
  1344. procedure tenumdef.setmin(_min:aint);
  1345. begin
  1346. minval:=_min;
  1347. calcsavesize;
  1348. end;
  1349. function tenumdef.min:aint;
  1350. begin
  1351. min:=minval;
  1352. end;
  1353. function tenumdef.max:aint;
  1354. begin
  1355. max:=maxval;
  1356. end;
  1357. function tenumdef.getfirstsym: tsym;
  1358. var
  1359. i:integer;
  1360. begin
  1361. for i := 0 to symtable.SymList.Count - 1 do
  1362. begin
  1363. result:=tsym(symtable.SymList[i]);
  1364. if tenumsym(result).value=minval then
  1365. exit;
  1366. end;
  1367. result:=nil;
  1368. end;
  1369. procedure tenumdef.buildderef;
  1370. begin
  1371. inherited buildderef;
  1372. if df_copied_def in defoptions then
  1373. basedefderef.build(basedef)
  1374. else
  1375. tenumsymtable(symtable).buildderef;
  1376. end;
  1377. procedure tenumdef.deref;
  1378. begin
  1379. inherited deref;
  1380. if df_copied_def in defoptions then
  1381. begin
  1382. basedef:=tenumdef(basedefderef.resolve);
  1383. symtable:=basedef.symtable.getcopy;
  1384. end
  1385. else
  1386. tenumsymtable(symtable).deref;
  1387. end;
  1388. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1389. begin
  1390. inherited ppuwrite(ppufile);
  1391. ppufile.putaint(min);
  1392. ppufile.putaint(max);
  1393. ppufile.putaint(savesize);
  1394. if df_copied_def in defoptions then
  1395. ppufile.putderef(basedefderef);
  1396. ppufile.writeentry(ibenumdef);
  1397. if not (df_copied_def in defoptions) then
  1398. tenumsymtable(symtable).ppuwrite(ppufile);
  1399. end;
  1400. function tenumdef.is_publishable : boolean;
  1401. begin
  1402. is_publishable:=true;
  1403. end;
  1404. function tenumdef.GetTypeName : string;
  1405. begin
  1406. GetTypeName:='<enumeration type>';
  1407. end;
  1408. {****************************************************************************
  1409. TORDDEF
  1410. ****************************************************************************}
  1411. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1412. begin
  1413. inherited create(orddef);
  1414. low:=v;
  1415. high:=b;
  1416. ordtype:=t;
  1417. setsize;
  1418. end;
  1419. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1420. begin
  1421. inherited ppuload(orddef,ppufile);
  1422. ordtype:=tordtype(ppufile.getbyte);
  1423. low:=ppufile.getexprint;
  1424. high:=ppufile.getexprint;
  1425. setsize;
  1426. end;
  1427. function torddef.getcopy : tstoreddef;
  1428. begin
  1429. result:=torddef.create(ordtype,low,high);
  1430. result.typ:=orddef;
  1431. torddef(result).low:=low;
  1432. torddef(result).high:=high;
  1433. torddef(result).ordtype:=ordtype;
  1434. torddef(result).savesize:=savesize;
  1435. end;
  1436. function torddef.alignment:shortint;
  1437. begin
  1438. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  1439. (ordtype in [s64bit,u64bit]) then
  1440. result := 4
  1441. else
  1442. result := inherited alignment;
  1443. end;
  1444. procedure torddef.setsize;
  1445. const
  1446. sizetbl : array[tordtype] of longint = (
  1447. 0,
  1448. 1,2,4,8,
  1449. 1,2,4,8,
  1450. 1,1,2,4,8,
  1451. 1,2,8
  1452. );
  1453. begin
  1454. savesize:=sizetbl[ordtype];
  1455. end;
  1456. function torddef.packedbitsize: aint;
  1457. var
  1458. sizeval: tconstexprint;
  1459. power: longint;
  1460. begin
  1461. result := 0;
  1462. if ordtype = uvoid then
  1463. exit;
  1464. {$ifndef cpu64bitalu}
  1465. if (ordtype in [s64bit,u64bit]) then
  1466. {$else not cpu64bitalu}
  1467. if (ordtype = u64bit) or
  1468. ((ordtype = s64bit) and
  1469. ((low <= (system.low(int64) div 2)) or
  1470. (high > (system.high(int64) div 2)))) then
  1471. {$endif cpu64bitalu}
  1472. result := 64
  1473. else if (low >= 0) and
  1474. (high <= 1) then
  1475. result := 1
  1476. else
  1477. begin
  1478. if (low>=0) then
  1479. sizeval:=high
  1480. else
  1481. { don't count 0 twice }
  1482. sizeval:=(cutils.max(-low,high)*2)-1;
  1483. { 256 must become 512 etc. }
  1484. nextpowerof2(sizeval+1,power);
  1485. result := power;
  1486. end;
  1487. end;
  1488. function torddef.getvardef : longint;
  1489. const
  1490. basetype2vardef : array[tordtype] of longint = (
  1491. varUndefined,
  1492. varbyte,varword,varlongword,varqword,
  1493. varshortint,varsmallint,varinteger,varint64,
  1494. varboolean,varboolean,varboolean,varUndefined,varUndefined,
  1495. varUndefined,varUndefined,varCurrency);
  1496. begin
  1497. result:=basetype2vardef[ordtype];
  1498. end;
  1499. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1500. begin
  1501. inherited ppuwrite(ppufile);
  1502. ppufile.putbyte(byte(ordtype));
  1503. ppufile.putexprint(low);
  1504. ppufile.putexprint(high);
  1505. ppufile.writeentry(iborddef);
  1506. end;
  1507. function torddef.is_publishable : boolean;
  1508. begin
  1509. is_publishable:=(ordtype<>uvoid);
  1510. end;
  1511. function torddef.GetTypeName : string;
  1512. const
  1513. names : array[tordtype] of string[20] = (
  1514. 'untyped',
  1515. 'Byte','Word','DWord','QWord',
  1516. 'ShortInt','SmallInt','LongInt','Int64',
  1517. 'Boolean','ByteBool','WordBool','LongBool','QWordBool',
  1518. 'Char','WideChar','Currency');
  1519. begin
  1520. GetTypeName:=names[ordtype];
  1521. end;
  1522. {****************************************************************************
  1523. TFLOATDEF
  1524. ****************************************************************************}
  1525. constructor tfloatdef.create(t : tfloattype);
  1526. begin
  1527. inherited create(floatdef);
  1528. floattype:=t;
  1529. setsize;
  1530. end;
  1531. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1532. begin
  1533. inherited ppuload(floatdef,ppufile);
  1534. floattype:=tfloattype(ppufile.getbyte);
  1535. setsize;
  1536. end;
  1537. function tfloatdef.getcopy : tstoreddef;
  1538. begin
  1539. result:=tfloatdef.create(floattype);
  1540. result.typ:=floatdef;
  1541. tfloatdef(result).savesize:=savesize;
  1542. end;
  1543. function tfloatdef.alignment:shortint;
  1544. begin
  1545. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  1546. case floattype of
  1547. s80real: result:=16;
  1548. s64real,
  1549. s64currency,
  1550. s64comp : result:=4;
  1551. else
  1552. result := inherited alignment;
  1553. end
  1554. else
  1555. result := inherited alignment;
  1556. end;
  1557. procedure tfloatdef.setsize;
  1558. begin
  1559. case floattype of
  1560. s32real : savesize:=4;
  1561. s80real : savesize:=10;
  1562. sc80real:
  1563. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  1564. system_x86_64_linux,system_x86_64_freebsd,
  1565. system_x86_64_solaris,system_x86_64_embedded] then
  1566. savesize:=16
  1567. else
  1568. savesize:=12;
  1569. s64real,
  1570. s64currency,
  1571. s64comp : savesize:=8;
  1572. else
  1573. savesize:=0;
  1574. end;
  1575. end;
  1576. function tfloatdef.getvardef : longint;
  1577. const
  1578. floattype2vardef : array[tfloattype] of longint = (
  1579. varSingle,varDouble,varUndefined,varUndefined,
  1580. varUndefined,varCurrency,varUndefined);
  1581. begin
  1582. if (upper(typename)='TDATETIME') and
  1583. assigned(owner) and
  1584. assigned(owner.name) and
  1585. (owner.name^='SYSTEM') then
  1586. result:=varDate
  1587. else
  1588. result:=floattype2vardef[floattype];
  1589. end;
  1590. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1591. begin
  1592. inherited ppuwrite(ppufile);
  1593. ppufile.putbyte(byte(floattype));
  1594. ppufile.writeentry(ibfloatdef);
  1595. end;
  1596. function tfloatdef.is_publishable : boolean;
  1597. begin
  1598. is_publishable:=true;
  1599. end;
  1600. function tfloatdef.GetTypeName : string;
  1601. const
  1602. names : array[tfloattype] of string[20] = (
  1603. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  1604. begin
  1605. GetTypeName:=names[floattype];
  1606. end;
  1607. {****************************************************************************
  1608. TFILEDEF
  1609. ****************************************************************************}
  1610. constructor tfiledef.createtext;
  1611. begin
  1612. inherited create(filedef);
  1613. filetyp:=ft_text;
  1614. typedfiledef:=nil;
  1615. setsize;
  1616. end;
  1617. constructor tfiledef.createuntyped;
  1618. begin
  1619. inherited create(filedef);
  1620. filetyp:=ft_untyped;
  1621. typedfiledef:=nil;
  1622. setsize;
  1623. end;
  1624. constructor tfiledef.createtyped(def:tdef);
  1625. begin
  1626. inherited create(filedef);
  1627. filetyp:=ft_typed;
  1628. typedfiledef:=def;
  1629. setsize;
  1630. end;
  1631. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1632. begin
  1633. inherited ppuload(filedef,ppufile);
  1634. filetyp:=tfiletyp(ppufile.getbyte);
  1635. if filetyp=ft_typed then
  1636. ppufile.getderef(typedfiledefderef)
  1637. else
  1638. typedfiledef:=nil;
  1639. setsize;
  1640. end;
  1641. function tfiledef.getcopy : tstoreddef;
  1642. begin
  1643. case filetyp of
  1644. ft_typed:
  1645. result:=tfiledef.createtyped(typedfiledef);
  1646. ft_untyped:
  1647. result:=tfiledef.createuntyped;
  1648. ft_text:
  1649. result:=tfiledef.createtext;
  1650. else
  1651. internalerror(2004121201);
  1652. end;
  1653. end;
  1654. procedure tfiledef.buildderef;
  1655. begin
  1656. inherited buildderef;
  1657. if filetyp=ft_typed then
  1658. typedfiledefderef.build(typedfiledef);
  1659. end;
  1660. procedure tfiledef.deref;
  1661. begin
  1662. inherited deref;
  1663. if filetyp=ft_typed then
  1664. typedfiledef:=tdef(typedfiledefderef.resolve);
  1665. end;
  1666. procedure tfiledef.setsize;
  1667. begin
  1668. {$ifdef cpu64bitaddr}
  1669. case filetyp of
  1670. ft_text :
  1671. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1672. savesize:=632{+8}
  1673. else
  1674. savesize:=628{+8};
  1675. ft_typed,
  1676. ft_untyped :
  1677. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1678. savesize:=372
  1679. else
  1680. savesize:=368;
  1681. end;
  1682. {$else cpu64bitaddr}
  1683. case filetyp of
  1684. ft_text :
  1685. savesize:=592{+4};
  1686. ft_typed,
  1687. ft_untyped :
  1688. savesize:=332;
  1689. end;
  1690. {$endif cpu64bitaddr}
  1691. end;
  1692. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1693. begin
  1694. inherited ppuwrite(ppufile);
  1695. ppufile.putbyte(byte(filetyp));
  1696. if filetyp=ft_typed then
  1697. ppufile.putderef(typedfiledefderef);
  1698. ppufile.writeentry(ibfiledef);
  1699. end;
  1700. function tfiledef.GetTypeName : string;
  1701. begin
  1702. case filetyp of
  1703. ft_untyped:
  1704. GetTypeName:='File';
  1705. ft_typed:
  1706. GetTypeName:='File Of '+typedfiledef.typename;
  1707. ft_text:
  1708. GetTypeName:='Text'
  1709. end;
  1710. end;
  1711. function tfiledef.getmangledparaname : string;
  1712. begin
  1713. case filetyp of
  1714. ft_untyped:
  1715. getmangledparaname:='FILE';
  1716. ft_typed:
  1717. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1718. ft_text:
  1719. getmangledparaname:='TEXT'
  1720. end;
  1721. end;
  1722. {****************************************************************************
  1723. TVARIANTDEF
  1724. ****************************************************************************}
  1725. constructor tvariantdef.create(v : tvarianttype);
  1726. begin
  1727. inherited create(variantdef);
  1728. varianttype:=v;
  1729. setsize;
  1730. end;
  1731. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1732. begin
  1733. inherited ppuload(variantdef,ppufile);
  1734. varianttype:=tvarianttype(ppufile.getbyte);
  1735. setsize;
  1736. end;
  1737. function tvariantdef.getcopy : tstoreddef;
  1738. begin
  1739. result:=tvariantdef.create(varianttype);
  1740. end;
  1741. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1742. begin
  1743. inherited ppuwrite(ppufile);
  1744. ppufile.putbyte(byte(varianttype));
  1745. ppufile.writeentry(ibvariantdef);
  1746. end;
  1747. function tvariantdef.getvardef : longint;
  1748. begin
  1749. Result:=varVariant;
  1750. end;
  1751. procedure tvariantdef.setsize;
  1752. begin
  1753. {$ifdef cpu64bitaddr}
  1754. savesize:=24;
  1755. {$else cpu64bitaddr}
  1756. savesize:=16;
  1757. {$endif cpu64bitaddr}
  1758. end;
  1759. function tvariantdef.GetTypeName : string;
  1760. begin
  1761. case varianttype of
  1762. vt_normalvariant:
  1763. GetTypeName:='Variant';
  1764. vt_olevariant:
  1765. GetTypeName:='OleVariant';
  1766. end;
  1767. end;
  1768. function tvariantdef.needs_inittable : boolean;
  1769. begin
  1770. needs_inittable:=true;
  1771. end;
  1772. function tvariantdef.is_publishable : boolean;
  1773. begin
  1774. is_publishable:=true;
  1775. end;
  1776. {****************************************************************************
  1777. TABSTRACtpointerdef
  1778. ****************************************************************************}
  1779. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  1780. begin
  1781. inherited create(dt);
  1782. pointeddef:=def;
  1783. savesize:=sizeof(pint);
  1784. end;
  1785. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1786. begin
  1787. inherited ppuload(dt,ppufile);
  1788. ppufile.getderef(pointeddefderef);
  1789. savesize:=sizeof(pint);
  1790. end;
  1791. procedure tabstractpointerdef.buildderef;
  1792. begin
  1793. inherited buildderef;
  1794. pointeddefderef.build(pointeddef);
  1795. end;
  1796. procedure tabstractpointerdef.deref;
  1797. begin
  1798. inherited deref;
  1799. pointeddef:=tdef(pointeddefderef.resolve);
  1800. end;
  1801. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1802. begin
  1803. inherited ppuwrite(ppufile);
  1804. ppufile.putderef(pointeddefderef);
  1805. end;
  1806. {****************************************************************************
  1807. tpointerdef
  1808. ****************************************************************************}
  1809. constructor tpointerdef.create(def:tdef);
  1810. begin
  1811. inherited create(pointerdef,def);
  1812. is_far:=false;
  1813. end;
  1814. constructor tpointerdef.createfar(def:tdef);
  1815. begin
  1816. inherited create(pointerdef,def);
  1817. is_far:=true;
  1818. end;
  1819. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1820. begin
  1821. inherited ppuload(pointerdef,ppufile);
  1822. is_far:=(ppufile.getbyte<>0);
  1823. end;
  1824. function tpointerdef.getcopy : tstoreddef;
  1825. begin
  1826. result:=tpointerdef.create(pointeddef);
  1827. tpointerdef(result).is_far:=is_far;
  1828. tpointerdef(result).savesize:=savesize;
  1829. end;
  1830. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1831. begin
  1832. inherited ppuwrite(ppufile);
  1833. ppufile.putbyte(byte(is_far));
  1834. ppufile.writeentry(ibpointerdef);
  1835. end;
  1836. function tpointerdef.GetTypeName : string;
  1837. begin
  1838. if is_far then
  1839. GetTypeName:='^'+pointeddef.typename+';far'
  1840. else
  1841. GetTypeName:='^'+pointeddef.typename;
  1842. end;
  1843. {****************************************************************************
  1844. TCLASSREFDEF
  1845. ****************************************************************************}
  1846. constructor tclassrefdef.create(def:tdef);
  1847. begin
  1848. inherited create(classrefdef,def);
  1849. end;
  1850. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1851. begin
  1852. inherited ppuload(classrefdef,ppufile);
  1853. end;
  1854. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  1855. begin
  1856. inherited ppuwrite(ppufile);
  1857. ppufile.writeentry(ibclassrefdef);
  1858. end;
  1859. function tclassrefdef.GetTypeName : string;
  1860. begin
  1861. GetTypeName:='Class Of '+pointeddef.typename;
  1862. end;
  1863. function tclassrefdef.is_publishable : boolean;
  1864. begin
  1865. result:=true;
  1866. end;
  1867. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  1868. begin
  1869. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  1870. result:=inherited rtti_mangledname(rt)
  1871. else
  1872. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  1873. end;
  1874. procedure tclassrefdef.register_created_object_type;
  1875. begin
  1876. tobjectdef(pointeddef).register_created_classref_type;
  1877. end;
  1878. {***************************************************************************
  1879. TSETDEF
  1880. ***************************************************************************}
  1881. constructor tsetdef.create(def:tdef;low, high : aint);
  1882. var
  1883. setallocbits: aint;
  1884. packedsavesize: aint;
  1885. begin
  1886. inherited create(setdef);
  1887. elementdef:=def;
  1888. setmax:=high;
  1889. if (current_settings.setalloc=0) then
  1890. begin
  1891. setbase:=0;
  1892. if (high<32) then
  1893. savesize:=Sizeof(longint)
  1894. else if (high<256) then
  1895. savesize:=32
  1896. else
  1897. savesize:=(high+7) div 8
  1898. end
  1899. else
  1900. begin
  1901. setallocbits:=current_settings.setalloc*8;
  1902. setbase:=low and not(setallocbits-1);
  1903. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  1904. savesize:=packedsavesize;
  1905. if savesize=3 then
  1906. savesize:=4;
  1907. end;
  1908. end;
  1909. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  1910. begin
  1911. inherited ppuload(setdef,ppufile);
  1912. ppufile.getderef(elementdefderef);
  1913. savesize:=ppufile.getaint;
  1914. setbase:=ppufile.getaint;
  1915. setmax:=ppufile.getaint;
  1916. end;
  1917. function tsetdef.getcopy : tstoreddef;
  1918. begin
  1919. result:=tsetdef.create(elementdef,setbase,setmax);
  1920. { the copy might have been created with a different setalloc setting }
  1921. tsetdef(result).savesize:=savesize;
  1922. end;
  1923. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  1924. begin
  1925. inherited ppuwrite(ppufile);
  1926. ppufile.putderef(elementdefderef);
  1927. ppufile.putaint(savesize);
  1928. ppufile.putaint(setbase);
  1929. ppufile.putaint(setmax);
  1930. ppufile.writeentry(ibsetdef);
  1931. end;
  1932. procedure tsetdef.buildderef;
  1933. begin
  1934. inherited buildderef;
  1935. elementdefderef.build(elementdef);
  1936. end;
  1937. procedure tsetdef.deref;
  1938. begin
  1939. inherited deref;
  1940. elementdef:=tdef(elementdefderef.resolve);
  1941. end;
  1942. function tsetdef.is_publishable : boolean;
  1943. begin
  1944. is_publishable:=savesize in [1,2,4];
  1945. end;
  1946. function tsetdef.GetTypeName : string;
  1947. begin
  1948. if assigned(elementdef) then
  1949. GetTypeName:='Set Of '+elementdef.typename
  1950. else
  1951. GetTypeName:='Empty Set';
  1952. end;
  1953. {***************************************************************************
  1954. TFORMALDEF
  1955. ***************************************************************************}
  1956. constructor tformaldef.create(Atyped:boolean);
  1957. begin
  1958. inherited create(formaldef);
  1959. typed:=Atyped;
  1960. savesize:=0;
  1961. end;
  1962. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  1963. begin
  1964. inherited ppuload(formaldef,ppufile);
  1965. typed:=boolean(ppufile.getbyte);
  1966. savesize:=0;
  1967. end;
  1968. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  1969. begin
  1970. inherited ppuwrite(ppufile);
  1971. ppufile.putbyte(byte(typed));
  1972. ppufile.writeentry(ibformaldef);
  1973. end;
  1974. function tformaldef.GetTypeName : string;
  1975. begin
  1976. if typed then
  1977. GetTypeName:='<Typed formal type>'
  1978. else
  1979. GetTypeName:='<Formal type>';
  1980. end;
  1981. {***************************************************************************
  1982. TARRAYDEF
  1983. ***************************************************************************}
  1984. constructor tarraydef.create(l,h : aint;def:tdef);
  1985. begin
  1986. inherited create(arraydef);
  1987. lowrange:=l;
  1988. highrange:=h;
  1989. rangedef:=def;
  1990. _elementdef:=nil;
  1991. arrayoptions:=[];
  1992. end;
  1993. constructor tarraydef.create_from_pointer(def:tdef);
  1994. begin
  1995. { use -1 so that the elecount will not overflow }
  1996. self.create(0,high(aint)-1,s32inttype);
  1997. arrayoptions:=[ado_IsConvertedPointer];
  1998. setelementdef(def);
  1999. end;
  2000. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2001. begin
  2002. inherited ppuload(arraydef,ppufile);
  2003. { the addresses are calculated later }
  2004. ppufile.getderef(_elementdefderef);
  2005. ppufile.getderef(rangedefderef);
  2006. lowrange:=ppufile.getaint;
  2007. highrange:=ppufile.getaint;
  2008. ppufile.getsmallset(arrayoptions);
  2009. end;
  2010. function tarraydef.getcopy : tstoreddef;
  2011. begin
  2012. result:=tarraydef.create(lowrange,highrange,rangedef);
  2013. tarraydef(result).arrayoptions:=arrayoptions;
  2014. tarraydef(result)._elementdef:=_elementdef;
  2015. end;
  2016. procedure tarraydef.buildderef;
  2017. begin
  2018. inherited buildderef;
  2019. _elementdefderef.build(_elementdef);
  2020. rangedefderef.build(rangedef);
  2021. end;
  2022. procedure tarraydef.deref;
  2023. begin
  2024. inherited deref;
  2025. _elementdef:=tdef(_elementdefderef.resolve);
  2026. rangedef:=tdef(rangedefderef.resolve);
  2027. end;
  2028. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2029. begin
  2030. inherited ppuwrite(ppufile);
  2031. ppufile.putderef(_elementdefderef);
  2032. ppufile.putderef(rangedefderef);
  2033. ppufile.putaint(lowrange);
  2034. ppufile.putaint(highrange);
  2035. ppufile.putsmallset(arrayoptions);
  2036. ppufile.writeentry(ibarraydef);
  2037. end;
  2038. function tarraydef.elesize : aint;
  2039. begin
  2040. if (ado_IsBitPacked in arrayoptions) then
  2041. internalerror(2006080101);
  2042. if assigned(_elementdef) then
  2043. result:=_elementdef.size
  2044. else
  2045. result:=0;
  2046. end;
  2047. function tarraydef.elepackedbitsize : aint;
  2048. begin
  2049. if not(ado_IsBitPacked in arrayoptions) then
  2050. internalerror(2006080102);
  2051. if assigned(_elementdef) then
  2052. result:=_elementdef.packedbitsize
  2053. else
  2054. result:=0;
  2055. end;
  2056. function tarraydef.elecount : aword;
  2057. var
  2058. qhigh,qlow : qword;
  2059. begin
  2060. if ado_IsDynamicArray in arrayoptions then
  2061. begin
  2062. result:=0;
  2063. exit;
  2064. end;
  2065. if (highrange>0) and (lowrange<0) then
  2066. begin
  2067. qhigh:=highrange;
  2068. qlow:=qword(-lowrange);
  2069. { prevent overflow, return 0 to indicate overflow }
  2070. if qhigh+qlow>qword(high(aint)-1) then
  2071. result:=0
  2072. else
  2073. result:=qhigh+qlow+1;
  2074. end
  2075. else
  2076. result:=int64(highrange)-lowrange+1;
  2077. end;
  2078. function tarraydef.size : aint;
  2079. var
  2080. cachedelecount : aword;
  2081. cachedelesize : aint;
  2082. begin
  2083. if ado_IsDynamicArray in arrayoptions then
  2084. begin
  2085. size:=sizeof(pint);
  2086. exit;
  2087. end;
  2088. { Tarraydef.size may never be called for an open array! }
  2089. if highrange<lowrange then
  2090. internalerror(99080501);
  2091. if not (ado_IsBitPacked in arrayoptions) then
  2092. cachedelesize:=elesize
  2093. else
  2094. cachedelesize := elepackedbitsize;
  2095. cachedelecount:=elecount;
  2096. if (cachedelesize = 0) then
  2097. begin
  2098. size := 0;
  2099. exit;
  2100. end;
  2101. if (cachedelecount = 0) then
  2102. begin
  2103. size := -1;
  2104. exit;
  2105. end;
  2106. { prevent overflow, return -1 to indicate overflow }
  2107. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2108. if (cachedelecount > aword(high(aint))) or
  2109. ((high(aint) div cachedelesize) < aint(cachedelecount)) or
  2110. { also lowrange*elesize must be < high(aint) to prevent overflow when
  2111. accessing the array, see ncgmem (PFV) }
  2112. ((high(aint) div cachedelesize) < abs(lowrange)) then
  2113. begin
  2114. result:=-1;
  2115. exit;
  2116. end;
  2117. result:=cachedelesize*aint(cachedelecount);
  2118. if (ado_IsBitPacked in arrayoptions) then
  2119. { can't just add 7 and divide by 8, because that may overflow }
  2120. result:=result div 8 + ord((result mod 8)<>0);
  2121. end;
  2122. procedure tarraydef.setelementdef(def:tdef);
  2123. begin
  2124. _elementdef:=def;
  2125. if not(
  2126. (ado_IsDynamicArray in arrayoptions) or
  2127. (ado_IsConvertedPointer in arrayoptions) or
  2128. (highrange<lowrange)
  2129. ) and
  2130. (size=-1) then
  2131. Message(sym_e_segment_too_large);
  2132. end;
  2133. function tarraydef.alignment : shortint;
  2134. begin
  2135. { alignment of dyn. arrays doesn't depend on the element size }
  2136. if (ado_IsDynamicArray in arrayoptions) then
  2137. alignment:=size_2_align(sizeof(pint))
  2138. { alignment is the alignment of the elements }
  2139. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2140. ((elementdef.typ=objectdef) and
  2141. is_object(elementdef)) then
  2142. alignment:=elementdef.alignment
  2143. { alignment is the size of the elements }
  2144. else if not (ado_IsBitPacked in arrayoptions) then
  2145. alignment:=size_2_align(elesize)
  2146. else
  2147. alignment:=packedbitsloadsize(elepackedbitsize);
  2148. end;
  2149. function tarraydef.needs_inittable : boolean;
  2150. begin
  2151. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2152. end;
  2153. function tarraydef.GetTypeName : string;
  2154. begin
  2155. if (ado_IsConstString in arrayoptions) then
  2156. result:='Constant String'
  2157. else if (ado_isarrayofconst in arrayoptions) or
  2158. (ado_isConstructor in arrayoptions) then
  2159. begin
  2160. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2161. GetTypeName:='Array Of Const'
  2162. else
  2163. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2164. end
  2165. else if (ado_IsDynamicArray in arrayoptions) then
  2166. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2167. else if ((highrange=-1) and (lowrange=0)) then
  2168. GetTypeName:='Open Array Of '+elementdef.typename
  2169. else
  2170. begin
  2171. result := '';
  2172. if (ado_IsBitPacked in arrayoptions) then
  2173. result:='Packed ';
  2174. if rangedef.typ=enumdef then
  2175. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2176. else
  2177. result:=result+'Array['+tostr(lowrange)+'..'+
  2178. tostr(highrange)+'] Of '+elementdef.typename
  2179. end;
  2180. end;
  2181. function tarraydef.getmangledparaname : string;
  2182. begin
  2183. if ado_isarrayofconst in arrayoptions then
  2184. getmangledparaname:='array_of_const'
  2185. else
  2186. if ((highrange=-1) and (lowrange=0)) then
  2187. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2188. else
  2189. internalerror(200204176);
  2190. end;
  2191. function tarraydef.is_publishable : boolean;
  2192. begin
  2193. Result:=ado_IsDynamicArray in arrayoptions;
  2194. end;
  2195. {***************************************************************************
  2196. tabstractrecorddef
  2197. ***************************************************************************}
  2198. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  2199. begin
  2200. inherited create(dt);
  2201. objname:=stringdup(upper(n));
  2202. objrealname:=stringdup(n);
  2203. objectoptions:=[];
  2204. end;
  2205. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2206. begin
  2207. inherited ppuload(dt,ppufile);
  2208. objrealname:=stringdup(ppufile.getstring);
  2209. objname:=stringdup(upper(objrealname^));
  2210. ppufile.getsmallset(objectoptions);
  2211. end;
  2212. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2213. begin
  2214. inherited ppuwrite(ppufile);
  2215. ppufile.putstring(objrealname^);
  2216. ppufile.putsmallset(objectoptions);
  2217. end;
  2218. destructor tabstractrecorddef.destroy;
  2219. begin
  2220. stringdispose(objname);
  2221. stringdispose(objrealname);
  2222. inherited destroy;
  2223. end;
  2224. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  2225. var
  2226. i: longint;
  2227. sym: tsym;
  2228. begin
  2229. for i:=0 to symtable.SymList.Count-1 do
  2230. begin
  2231. sym:=tsym(symtable.SymList[i]);
  2232. if sym.typ=procsym then
  2233. begin
  2234. result:=tprocsym(sym).find_procdef_bytype(pt);
  2235. if assigned(result) then
  2236. exit;
  2237. end;
  2238. end;
  2239. result:=nil;
  2240. end;
  2241. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2242. begin
  2243. if t=gs_record then
  2244. GetSymtable:=symtable
  2245. else
  2246. GetSymtable:=nil;
  2247. end;
  2248. function tabstractrecorddef.is_packed:boolean;
  2249. begin
  2250. result:=tabstractrecordsymtable(symtable).is_packed;
  2251. end;
  2252. function tabstractrecorddef.RttiName: string;
  2253. var
  2254. tmp: tabstractrecorddef;
  2255. begin
  2256. Result:=objrealname^;
  2257. tmp:=self;
  2258. repeat
  2259. if tmp.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  2260. tmp:=tabstractrecorddef(tmp.owner.defowner)
  2261. else
  2262. break;
  2263. Result:=tmp.objrealname^+'.'+Result;
  2264. until tmp=nil;
  2265. end;
  2266. {***************************************************************************
  2267. trecorddef
  2268. ***************************************************************************}
  2269. constructor trecorddef.create(const n:string; p:TSymtable);
  2270. begin
  2271. inherited create(n,recorddef);
  2272. symtable:=p;
  2273. { we can own the symtable only if nobody else owns a copy so far }
  2274. if symtable.refcount=1 then
  2275. symtable.defowner:=self;
  2276. isunion:=false;
  2277. end;
  2278. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2279. begin
  2280. inherited ppuload(recorddef,ppufile);
  2281. if df_copied_def in defoptions then
  2282. ppufile.getderef(cloneddefderef)
  2283. else
  2284. begin
  2285. symtable:=trecordsymtable.create(objrealname^,0);
  2286. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2287. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2288. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2289. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2290. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2291. trecordsymtable(symtable).ppuload(ppufile);
  2292. { requires usefieldalignment to be set }
  2293. symtable.defowner:=self;
  2294. end;
  2295. isunion:=false;
  2296. end;
  2297. destructor trecorddef.destroy;
  2298. begin
  2299. if assigned(symtable) then
  2300. begin
  2301. symtable.free;
  2302. symtable:=nil;
  2303. end;
  2304. inherited destroy;
  2305. end;
  2306. function trecorddef.getcopy : tstoreddef;
  2307. begin
  2308. result:=trecorddef.create(objrealname^,symtable.getcopy);
  2309. trecorddef(result).isunion:=isunion;
  2310. include(trecorddef(result).defoptions,df_copied_def);
  2311. end;
  2312. function trecorddef.needs_inittable : boolean;
  2313. begin
  2314. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2315. end;
  2316. procedure trecorddef.buildderef;
  2317. begin
  2318. inherited buildderef;
  2319. if df_copied_def in defoptions then
  2320. cloneddefderef.build(symtable.defowner)
  2321. else
  2322. tstoredsymtable(symtable).buildderef;
  2323. end;
  2324. procedure trecorddef.deref;
  2325. begin
  2326. inherited deref;
  2327. { now dereference the definitions }
  2328. if df_copied_def in defoptions then
  2329. begin
  2330. cloneddef:=trecorddef(cloneddefderef.resolve);
  2331. symtable:=cloneddef.symtable.getcopy;
  2332. end
  2333. else
  2334. tstoredsymtable(symtable).deref;
  2335. { assign TGUID? load only from system unit }
  2336. if not(assigned(rec_tguid)) and
  2337. (upper(typename)='TGUID') and
  2338. assigned(owner) and
  2339. assigned(owner.name) and
  2340. (owner.name^='SYSTEM') then
  2341. rec_tguid:=self;
  2342. { assign JMP_BUF? load only from system unit }
  2343. if not(assigned(rec_jmp_buf)) and
  2344. (upper(typename)='JMP_BUF') and
  2345. assigned(owner) and
  2346. assigned(owner.name) and
  2347. (owner.name^='SYSTEM') then
  2348. rec_jmp_buf:=self;
  2349. end;
  2350. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2351. begin
  2352. inherited ppuwrite(ppufile);
  2353. if df_copied_def in defoptions then
  2354. ppufile.putderef(cloneddefderef)
  2355. else
  2356. begin
  2357. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2358. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2359. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2360. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2361. ppufile.putaint(trecordsymtable(symtable).datasize);
  2362. end;
  2363. ppufile.writeentry(ibrecorddef);
  2364. if not(df_copied_def in defoptions) then
  2365. trecordsymtable(symtable).ppuwrite(ppufile);
  2366. end;
  2367. function trecorddef.size:aint;
  2368. begin
  2369. result:=trecordsymtable(symtable).datasize;
  2370. end;
  2371. function trecorddef.alignment:shortint;
  2372. begin
  2373. alignment:=trecordsymtable(symtable).recordalignment;
  2374. end;
  2375. function trecorddef.padalignment:shortint;
  2376. begin
  2377. padalignment := trecordsymtable(symtable).padalignment;
  2378. end;
  2379. function trecorddef.GetTypeName : string;
  2380. begin
  2381. GetTypeName:='<record type>'
  2382. end;
  2383. {***************************************************************************
  2384. TABSTRACTPROCDEF
  2385. ***************************************************************************}
  2386. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2387. begin
  2388. inherited create(dt);
  2389. parast:=tparasymtable.create(self,level);
  2390. paras:=nil;
  2391. minparacount:=0;
  2392. maxparacount:=0;
  2393. proctypeoption:=potype_none;
  2394. proccalloption:=pocall_none;
  2395. procoptions:=[];
  2396. returndef:=voidtype;
  2397. savesize:=sizeof(pint);
  2398. callerargareasize:=0;
  2399. calleeargareasize:=0;
  2400. has_paraloc_info:=callnoside;
  2401. funcretloc[callerside].init;
  2402. funcretloc[calleeside].init;
  2403. check_mark_as_nested;
  2404. end;
  2405. destructor tabstractprocdef.destroy;
  2406. begin
  2407. if assigned(paras) then
  2408. begin
  2409. {$ifdef MEMDEBUG}
  2410. memprocpara.start;
  2411. {$endif MEMDEBUG}
  2412. paras.free;
  2413. paras:=nil;
  2414. {$ifdef MEMDEBUG}
  2415. memprocpara.stop;
  2416. {$endif MEMDEBUG}
  2417. end;
  2418. if assigned(parast) then
  2419. begin
  2420. {$ifdef MEMDEBUG}
  2421. memprocparast.start;
  2422. {$endif MEMDEBUG}
  2423. parast.free;
  2424. parast:=nil;
  2425. {$ifdef MEMDEBUG}
  2426. memprocparast.stop;
  2427. {$endif MEMDEBUG}
  2428. end;
  2429. funcretloc[callerside].done;
  2430. funcretloc[calleeside].done;
  2431. inherited destroy;
  2432. end;
  2433. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2434. begin
  2435. if (tsym(p).typ<>paravarsym) then
  2436. exit;
  2437. inc(plongint(arg)^);
  2438. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2439. begin
  2440. if not assigned(tparavarsym(p).defaultconstsym) then
  2441. inc(minparacount);
  2442. inc(maxparacount);
  2443. end;
  2444. end;
  2445. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2446. begin
  2447. if (tsym(p).typ<>paravarsym) then
  2448. exit;
  2449. paras.add(p);
  2450. end;
  2451. procedure tabstractprocdef.calcparas;
  2452. var
  2453. paracount : longint;
  2454. begin
  2455. { This can already be assigned when
  2456. we need to reresolve this unit (PFV) }
  2457. if assigned(paras) then
  2458. paras.free;
  2459. paras:=tparalist.create(false);
  2460. paracount:=0;
  2461. minparacount:=0;
  2462. maxparacount:=0;
  2463. parast.SymList.ForEachCall(@count_para,@paracount);
  2464. paras.capacity:=paracount;
  2465. { Insert parameters in table }
  2466. parast.SymList.ForEachCall(@insert_para,nil);
  2467. { Order parameters }
  2468. paras.sortparas;
  2469. end;
  2470. procedure tabstractprocdef.buildderef;
  2471. begin
  2472. { released procdef? }
  2473. if not assigned(parast) then
  2474. exit;
  2475. inherited buildderef;
  2476. returndefderef.build(returndef);
  2477. { parast }
  2478. tparasymtable(parast).buildderef;
  2479. end;
  2480. procedure tabstractprocdef.deref;
  2481. begin
  2482. inherited deref;
  2483. returndef:=tdef(returndefderef.resolve);
  2484. { parast }
  2485. tparasymtable(parast).deref;
  2486. { recalculated parameters }
  2487. calcparas;
  2488. end;
  2489. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2490. begin
  2491. inherited ppuload(dt,ppufile);
  2492. parast:=nil;
  2493. Paras:=nil;
  2494. minparacount:=0;
  2495. maxparacount:=0;
  2496. ppufile.getderef(returndefderef);
  2497. { TODO: remove fpu_used loading}
  2498. ppufile.getbyte;
  2499. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2500. proccalloption:=tproccalloption(ppufile.getbyte);
  2501. ppufile.getnormalset(procoptions);
  2502. funcretloc[callerside].init;
  2503. if po_explicitparaloc in procoptions then
  2504. funcretloc[callerside].ppuload(ppufile);
  2505. savesize:=sizeof(pint);
  2506. if (po_explicitparaloc in procoptions) then
  2507. has_paraloc_info:=callerside;
  2508. end;
  2509. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2510. var
  2511. oldintfcrc : boolean;
  2512. begin
  2513. { released procdef? }
  2514. if not assigned(parast) then
  2515. exit;
  2516. inherited ppuwrite(ppufile);
  2517. ppufile.putderef(returndefderef);
  2518. oldintfcrc:=ppufile.do_interface_crc;
  2519. ppufile.do_interface_crc:=false;
  2520. ppufile.putbyte(0);
  2521. ppufile.putbyte(ord(proctypeoption));
  2522. ppufile.putbyte(ord(proccalloption));
  2523. ppufile.putnormalset(procoptions);
  2524. ppufile.do_interface_crc:=oldintfcrc;
  2525. if (po_explicitparaloc in procoptions) then
  2526. funcretloc[callerside].ppuwrite(ppufile);
  2527. end;
  2528. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2529. var
  2530. hs,s : string;
  2531. hp : TParavarsym;
  2532. hpc : tconstsym;
  2533. first : boolean;
  2534. i : integer;
  2535. begin
  2536. s:='';
  2537. first:=true;
  2538. for i:=0 to paras.count-1 do
  2539. begin
  2540. hp:=tparavarsym(paras[i]);
  2541. if not(vo_is_hidden_para in hp.varoptions) or
  2542. (showhidden) then
  2543. begin
  2544. if first then
  2545. begin
  2546. s:=s+'(';
  2547. first:=false;
  2548. end
  2549. else
  2550. s:=s+',';
  2551. if vo_is_hidden_para in hp.varoptions then
  2552. s:=s+'<';
  2553. case hp.varspez of
  2554. vs_var :
  2555. s:=s+'var ';
  2556. vs_const :
  2557. s:=s+'const ';
  2558. vs_out :
  2559. s:=s+'out ';
  2560. vs_constref :
  2561. s:=s+'constref ';
  2562. end;
  2563. if hp.univpara then
  2564. s:=s+'univ ';
  2565. if assigned(hp.vardef.typesym) then
  2566. begin
  2567. hs:=hp.vardef.typesym.realname;
  2568. if hs[1]<>'$' then
  2569. s:=s+hs
  2570. else
  2571. s:=s+hp.vardef.GetTypeName;
  2572. end
  2573. else
  2574. s:=s+hp.vardef.GetTypeName;
  2575. { default value }
  2576. if assigned(hp.defaultconstsym) then
  2577. begin
  2578. hpc:=tconstsym(hp.defaultconstsym);
  2579. hs:='';
  2580. case hpc.consttyp of
  2581. conststring,
  2582. constresourcestring :
  2583. begin
  2584. If hpc.value.len>0 then
  2585. begin
  2586. setLength(hs,hpc.value.len);
  2587. { don't write past the end of hs if the constant
  2588. is > 255 chars }
  2589. move(hpc.value.valueptr^,hs[1],length(hs));
  2590. { make sure that constant strings with newline chars
  2591. don't create a linebreak in the assembler code,
  2592. since comments are line-based. Also remove nulls
  2593. because the comments are written as a pchar. }
  2594. ReplaceCase(hs,#0,'.');
  2595. ReplaceCase(hs,#10,'.');
  2596. ReplaceCase(hs,#13,'.');
  2597. end;
  2598. end;
  2599. constreal :
  2600. str(pbestreal(hpc.value.valueptr)^,hs);
  2601. constpointer :
  2602. hs:=tostr(hpc.value.valueordptr);
  2603. constord :
  2604. begin
  2605. if is_boolean(hpc.constdef) then
  2606. begin
  2607. if hpc.value.valueord<>0 then
  2608. hs:='TRUE'
  2609. else
  2610. hs:='FALSE';
  2611. end
  2612. else
  2613. hs:=tostr(hpc.value.valueord);
  2614. end;
  2615. constnil :
  2616. hs:='nil';
  2617. constset :
  2618. hs:='<set>';
  2619. end;
  2620. if hs<>'' then
  2621. s:=s+'="'+hs+'"';
  2622. end;
  2623. if vo_is_hidden_para in hp.varoptions then
  2624. s:=s+'>';
  2625. end;
  2626. end;
  2627. if not first then
  2628. s:=s+')';
  2629. if (po_varargs in procoptions) then
  2630. s:=s+';VarArgs';
  2631. typename_paras:=s;
  2632. end;
  2633. function tabstractprocdef.is_methodpointer:boolean;
  2634. begin
  2635. result:=false;
  2636. end;
  2637. function tabstractprocdef.is_addressonly:boolean;
  2638. begin
  2639. result:=true;
  2640. end;
  2641. function tabstractprocdef.no_self_node: boolean;
  2642. begin
  2643. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  2644. (proctypeoption in [potype_class_constructor,potype_class_destructor]);
  2645. end;
  2646. procedure tabstractprocdef.check_mark_as_nested;
  2647. begin
  2648. { nested procvars require that nested functions use the Delphi-style
  2649. nested procedure calling convention }
  2650. if (parast.symtablelevel>normal_function_level) and
  2651. (m_nested_procvars in current_settings.modeswitches) then
  2652. include(procoptions,po_delphi_nested_cc);
  2653. end;
  2654. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  2655. begin
  2656. if (side in [callerside,callbothsides]) and
  2657. not(has_paraloc_info in [callerside,callbothsides]) then
  2658. begin
  2659. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  2660. if has_paraloc_info in [calleeside,callbothsides] then
  2661. has_paraloc_info:=callbothsides
  2662. else
  2663. has_paraloc_info:=callerside;
  2664. end;
  2665. if (side in [calleeside,callbothsides]) and
  2666. not(has_paraloc_info in [calleeside,callbothsides]) then
  2667. begin
  2668. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  2669. if has_paraloc_info in [callerside,callbothsides] then
  2670. has_paraloc_info:=callbothsides
  2671. else
  2672. has_paraloc_info:=calleeside;
  2673. end;
  2674. end;
  2675. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  2676. var
  2677. p: tparavarsym;
  2678. ploc: PCGParalocation;
  2679. i: longint;
  2680. begin
  2681. result:=false;
  2682. init_paraloc_info(side);
  2683. for i:=0 to parast.SymList.Count-1 do
  2684. if tsym(parast.SymList[i]).typ=paravarsym then
  2685. begin
  2686. p:=tparavarsym(parast.SymList[i]);
  2687. { check if no parameter is located on the stack }
  2688. if is_open_array(p.vardef) or
  2689. is_array_of_const(p.vardef) then
  2690. begin
  2691. result:=true;
  2692. exit;
  2693. end;
  2694. ploc:=p.paraloc[side].location;
  2695. while assigned(ploc) do
  2696. begin
  2697. if (ploc^.loc=LOC_REFERENCE) then
  2698. begin
  2699. result:=true;
  2700. exit
  2701. end;
  2702. ploc:=ploc^.next;
  2703. end;
  2704. end;
  2705. end;
  2706. {***************************************************************************
  2707. TPROCDEF
  2708. ***************************************************************************}
  2709. constructor tprocdef.create(level:byte);
  2710. begin
  2711. inherited create(procdef,level);
  2712. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2713. _mangledname:=nil;
  2714. fileinfo:=current_filepos;
  2715. extnumber:=$ffff;
  2716. aliasnames:=TCmdStrList.create;
  2717. funcretsym:=nil;
  2718. forwarddef:=true;
  2719. interfacedef:=false;
  2720. hasforward:=false;
  2721. struct := nil;
  2722. import_dll:=nil;
  2723. import_name:=nil;
  2724. import_nr:=0;
  2725. inlininginfo:=nil;
  2726. deprecatedmsg:=nil;
  2727. {$ifdef i386}
  2728. fpu_used:=maxfpuregs;
  2729. {$endif i386}
  2730. end;
  2731. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2732. var
  2733. i,aliasnamescount : longint;
  2734. level : byte;
  2735. begin
  2736. inherited ppuload(procdef,ppufile);
  2737. if po_has_mangledname in procoptions then
  2738. _mangledname:=stringdup(ppufile.getstring)
  2739. else
  2740. _mangledname:=nil;
  2741. extnumber:=ppufile.getword;
  2742. level:=ppufile.getbyte;
  2743. ppufile.getderef(structderef);
  2744. ppufile.getderef(procsymderef);
  2745. ppufile.getposinfo(fileinfo);
  2746. visibility:=tvisibility(ppufile.getbyte);
  2747. ppufile.getsmallset(symoptions);
  2748. if sp_has_deprecated_msg in symoptions then
  2749. deprecatedmsg:=stringdup(ppufile.getstring)
  2750. else
  2751. deprecatedmsg:=nil;
  2752. {$ifdef powerpc}
  2753. { library symbol for AmigaOS/MorphOS }
  2754. ppufile.getderef(libsymderef);
  2755. {$endif powerpc}
  2756. { import stuff }
  2757. if po_has_importdll in procoptions then
  2758. import_dll:=stringdup(ppufile.getstring)
  2759. else
  2760. import_dll:=nil;
  2761. if po_has_importname in procoptions then
  2762. import_name:=stringdup(ppufile.getstring)
  2763. else
  2764. import_name:=nil;
  2765. import_nr:=ppufile.getword;
  2766. if (po_msgint in procoptions) then
  2767. messageinf.i:=ppufile.getlongint;
  2768. if (po_msgstr in procoptions) then
  2769. messageinf.str:=stringdup(ppufile.getstring);
  2770. if (po_dispid in procoptions) then
  2771. dispid:=ppufile.getlongint;
  2772. { inline stuff }
  2773. if (po_has_inlininginfo in procoptions) then
  2774. begin
  2775. ppufile.getderef(funcretsymderef);
  2776. new(inlininginfo);
  2777. ppufile.getsmallset(inlininginfo^.flags);
  2778. end
  2779. else
  2780. begin
  2781. inlininginfo:=nil;
  2782. funcretsym:=nil;
  2783. end;
  2784. aliasnames:=TCmdStrList.create;
  2785. { count alias names }
  2786. aliasnamescount:=ppufile.getbyte;
  2787. for i:=1 to aliasnamescount do
  2788. aliasnames.insert(ppufile.getstring);
  2789. { load para symtable }
  2790. parast:=tparasymtable.create(self,level);
  2791. tparasymtable(parast).ppuload(ppufile);
  2792. { load local symtable }
  2793. if (po_has_inlininginfo in procoptions) then
  2794. begin
  2795. localst:=tlocalsymtable.create(self,level);
  2796. tlocalsymtable(localst).ppuload(ppufile);
  2797. end
  2798. else
  2799. localst:=nil;
  2800. { inline stuff }
  2801. if (po_has_inlininginfo in procoptions) then
  2802. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2803. { default values for no persistent data }
  2804. if (cs_link_deffile in current_settings.globalswitches) and
  2805. (tf_need_export in target_info.flags) and
  2806. (po_exports in procoptions) then
  2807. deffile.AddExport(mangledname);
  2808. forwarddef:=false;
  2809. interfacedef:=false;
  2810. hasforward:=false;
  2811. { Disable po_has_inlining until the derefimpl is done }
  2812. exclude(procoptions,po_has_inlininginfo);
  2813. {$ifdef i386}
  2814. fpu_used:=maxfpuregs;
  2815. {$endif i386}
  2816. end;
  2817. destructor tprocdef.destroy;
  2818. begin
  2819. aliasnames.free;
  2820. aliasnames:=nil;
  2821. if assigned(localst) and
  2822. (localst.symtabletype<>staticsymtable) then
  2823. begin
  2824. {$ifdef MEMDEBUG}
  2825. memproclocalst.start;
  2826. {$endif MEMDEBUG}
  2827. localst.free;
  2828. localst:=nil;
  2829. {$ifdef MEMDEBUG}
  2830. memproclocalst.start;
  2831. {$endif MEMDEBUG}
  2832. end;
  2833. if assigned(inlininginfo) then
  2834. begin
  2835. {$ifdef MEMDEBUG}
  2836. memprocnodetree.start;
  2837. {$endif MEMDEBUG}
  2838. tnode(inlininginfo^.code).free;
  2839. {$ifdef MEMDEBUG}
  2840. memprocnodetree.start;
  2841. {$endif MEMDEBUG}
  2842. dispose(inlininginfo);
  2843. inlininginfo:=nil;
  2844. end;
  2845. stringdispose(resultname);
  2846. stringdispose(import_dll);
  2847. stringdispose(import_name);
  2848. stringdispose(deprecatedmsg);
  2849. if (po_msgstr in procoptions) then
  2850. stringdispose(messageinf.str);
  2851. if assigned(_mangledname) then
  2852. begin
  2853. {$ifdef MEMDEBUG}
  2854. memmanglednames.start;
  2855. {$endif MEMDEBUG}
  2856. stringdispose(_mangledname);
  2857. {$ifdef MEMDEBUG}
  2858. memmanglednames.stop;
  2859. {$endif MEMDEBUG}
  2860. end;
  2861. inherited destroy;
  2862. end;
  2863. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2864. var
  2865. oldintfcrc : boolean;
  2866. aliasnamescount : longint;
  2867. item : TCmdStrListItem;
  2868. begin
  2869. { released procdef? }
  2870. if not assigned(parast) then
  2871. exit;
  2872. inherited ppuwrite(ppufile);
  2873. if po_has_mangledname in procoptions then
  2874. ppufile.putstring(_mangledname^);
  2875. ppufile.putword(extnumber);
  2876. ppufile.putbyte(parast.symtablelevel);
  2877. ppufile.putderef(structderef);
  2878. ppufile.putderef(procsymderef);
  2879. ppufile.putposinfo(fileinfo);
  2880. ppufile.putbyte(byte(visibility));
  2881. ppufile.putsmallset(symoptions);
  2882. if sp_has_deprecated_msg in symoptions then
  2883. ppufile.putstring(deprecatedmsg^);
  2884. {$ifdef powerpc}
  2885. { library symbol for AmigaOS/MorphOS }
  2886. ppufile.putderef(libsymderef);
  2887. {$endif powerpc}
  2888. { import }
  2889. if po_has_importdll in procoptions then
  2890. ppufile.putstring(import_dll^);
  2891. if po_has_importname in procoptions then
  2892. ppufile.putstring(import_name^);
  2893. ppufile.putword(import_nr);
  2894. if (po_msgint in procoptions) then
  2895. ppufile.putlongint(messageinf.i);
  2896. if (po_msgstr in procoptions) then
  2897. ppufile.putstring(messageinf.str^);
  2898. if (po_dispid in procoptions) then
  2899. ppufile.putlongint(dispid);
  2900. { inline stuff }
  2901. oldintfcrc:=ppufile.do_crc;
  2902. ppufile.do_crc:=false;
  2903. if (po_has_inlininginfo in procoptions) then
  2904. begin
  2905. ppufile.putderef(funcretsymderef);
  2906. ppufile.putsmallset(inlininginfo^.flags);
  2907. end;
  2908. { count alias names }
  2909. aliasnamescount:=0;
  2910. item:=TCmdStrListItem(aliasnames.first);
  2911. while assigned(item) do
  2912. begin
  2913. inc(aliasnamescount);
  2914. item:=TCmdStrListItem(item.next);
  2915. end;
  2916. if aliasnamescount>255 then
  2917. internalerror(200711021);
  2918. ppufile.putbyte(aliasnamescount);
  2919. item:=TCmdStrListItem(aliasnames.first);
  2920. while assigned(item) do
  2921. begin
  2922. ppufile.putstring(item.str);
  2923. item:=TCmdStrListItem(item.next);
  2924. end;
  2925. ppufile.do_crc:=oldintfcrc;
  2926. { write this entry }
  2927. ppufile.writeentry(ibprocdef);
  2928. { Save the para symtable, this is taken from the interface }
  2929. tparasymtable(parast).ppuwrite(ppufile);
  2930. { save localsymtable for inline procedures or when local
  2931. browser info is requested, this has no influence on the crc }
  2932. if (po_has_inlininginfo in procoptions) then
  2933. begin
  2934. oldintfcrc:=ppufile.do_crc;
  2935. ppufile.do_crc:=false;
  2936. tlocalsymtable(localst).ppuwrite(ppufile);
  2937. ppufile.do_crc:=oldintfcrc;
  2938. end;
  2939. { node tree for inlining }
  2940. oldintfcrc:=ppufile.do_crc;
  2941. ppufile.do_crc:=false;
  2942. if (po_has_inlininginfo in procoptions) then
  2943. ppuwritenodetree(ppufile,inlininginfo^.code);
  2944. ppufile.do_crc:=oldintfcrc;
  2945. end;
  2946. function tprocdef.fullprocname(showhidden:boolean):string;
  2947. var
  2948. s : string;
  2949. t : ttoken;
  2950. begin
  2951. {$ifdef EXTDEBUG}
  2952. showhidden:=true;
  2953. {$endif EXTDEBUG}
  2954. s:='';
  2955. if assigned(struct) then
  2956. begin
  2957. s:=struct.RttiName+'.';
  2958. if (po_classmethod in procoptions) and
  2959. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2960. s:='class ' + s;
  2961. end;
  2962. if proctypeoption=potype_operator then
  2963. begin
  2964. for t:=NOTOKEN to last_overloaded do
  2965. if procsym.realname='$'+overloaded_names[t] then
  2966. begin
  2967. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2968. break;
  2969. end;
  2970. end
  2971. else
  2972. s:=s+procsym.realname+typename_paras(showhidden);
  2973. case proctypeoption of
  2974. potype_constructor:
  2975. s:='constructor '+s;
  2976. potype_destructor:
  2977. s:='destructor '+s;
  2978. potype_class_constructor:
  2979. s:='class constructor '+s;
  2980. potype_class_destructor:
  2981. s:='class destructor '+s;
  2982. else
  2983. if assigned(returndef) and
  2984. not(is_void(returndef)) then
  2985. s:=s+':'+returndef.GetTypeName;
  2986. end;
  2987. if owner.symtabletype=localsymtable then
  2988. s:=s+' is nested';
  2989. s:=s+';';
  2990. { forced calling convention? }
  2991. if (po_hascallingconvention in procoptions) then
  2992. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2993. if (po_staticmethod in procoptions) and
  2994. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2995. s:=s+' Static;';
  2996. fullprocname:=s;
  2997. end;
  2998. function tprocdef.is_methodpointer:boolean;
  2999. begin
  3000. { don't check assigned(_class), that's also the case for nested
  3001. procedures inside methods }
  3002. result:=owner.symtabletype=ObjectSymtable;
  3003. end;
  3004. function tprocdef.is_addressonly:boolean;
  3005. begin
  3006. result:=assigned(owner) and
  3007. (owner.symtabletype<>ObjectSymtable) and
  3008. (not(m_nested_procvars in current_settings.modeswitches) or
  3009. not is_nested_pd(self));
  3010. end;
  3011. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  3012. begin
  3013. case t of
  3014. gs_local :
  3015. GetSymtable:=localst;
  3016. gs_para :
  3017. GetSymtable:=parast;
  3018. else
  3019. GetSymtable:=nil;
  3020. end;
  3021. end;
  3022. procedure tprocdef.buildderef;
  3023. begin
  3024. inherited buildderef;
  3025. structderef.build(struct);
  3026. { procsym that originaly defined this definition, should be in the
  3027. same symtable }
  3028. procsymderef.build(procsym);
  3029. {$ifdef powerpc}
  3030. { library symbol for AmigaOS/MorphOS }
  3031. libsymderef.build(libsym);
  3032. {$endif powerpc}
  3033. end;
  3034. procedure tprocdef.buildderefimpl;
  3035. begin
  3036. inherited buildderefimpl;
  3037. { Localst is not available for main/unit init }
  3038. if assigned(localst) then
  3039. begin
  3040. tlocalsymtable(localst).buildderef;
  3041. tlocalsymtable(localst).buildderefimpl;
  3042. end;
  3043. { inline tree }
  3044. if (po_has_inlininginfo in procoptions) then
  3045. begin
  3046. funcretsymderef.build(funcretsym);
  3047. inlininginfo^.code.buildderefimpl;
  3048. end;
  3049. end;
  3050. procedure tprocdef.deref;
  3051. begin
  3052. inherited deref;
  3053. struct:=tabstractrecorddef(structderef.resolve);
  3054. { procsym that originaly defined this definition, should be in the
  3055. same symtable }
  3056. procsym:=tprocsym(procsymderef.resolve);
  3057. {$ifdef powerpc}
  3058. { library symbol for AmigaOS/MorphOS }
  3059. libsym:=tsym(libsymderef.resolve);
  3060. {$endif powerpc}
  3061. end;
  3062. procedure tprocdef.derefimpl;
  3063. begin
  3064. { Enable has_inlininginfo when the inlininginfo
  3065. structure is available. The has_inlininginfo was disabled
  3066. after the load, since the data was invalid }
  3067. if assigned(inlininginfo) then
  3068. include(procoptions,po_has_inlininginfo);
  3069. { Locals }
  3070. if assigned(localst) then
  3071. begin
  3072. tlocalsymtable(localst).deref;
  3073. tlocalsymtable(localst).derefimpl;
  3074. end;
  3075. { Inline }
  3076. if (po_has_inlininginfo in procoptions) then
  3077. begin
  3078. inlininginfo^.code.derefimpl;
  3079. { funcretsym, this is always located in the localst }
  3080. funcretsym:=tsym(funcretsymderef.resolve);
  3081. end
  3082. else
  3083. begin
  3084. { safety }
  3085. { Not safe! A unit may be reresolved after its interface has been
  3086. parsed but before its implementation has been parsed, and in that
  3087. case the funcretsym is still required!
  3088. funcretsym:=nil; }
  3089. end;
  3090. end;
  3091. function tprocdef.GetTypeName : string;
  3092. begin
  3093. GetTypeName := FullProcName(false);
  3094. end;
  3095. function tprocdef.mangledname : string;
  3096. var
  3097. hp : TParavarsym;
  3098. hs : string;
  3099. crc : dword;
  3100. newlen,
  3101. oldlen,
  3102. i : integer;
  3103. begin
  3104. if assigned(_mangledname) then
  3105. begin
  3106. {$ifdef compress}
  3107. mangledname:=minilzw_decode(_mangledname^);
  3108. {$else}
  3109. mangledname:=_mangledname^;
  3110. {$endif}
  3111. exit;
  3112. end;
  3113. { we need to use the symtable where the procsym is inserted,
  3114. because that is visible to the world }
  3115. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3116. oldlen:=length(mangledname);
  3117. { add parameter types }
  3118. for i:=0 to paras.count-1 do
  3119. begin
  3120. hp:=tparavarsym(paras[i]);
  3121. if not(vo_is_hidden_para in hp.varoptions) then
  3122. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  3123. end;
  3124. { add resultdef, add $$ as separator to make it unique from a
  3125. parameter separator }
  3126. if not is_void(returndef) then
  3127. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  3128. newlen:=length(mangledname);
  3129. { Replace with CRC if the parameter line is very long }
  3130. if (newlen-oldlen>12) and
  3131. ((newlen>100) or (newlen-oldlen>64)) then
  3132. begin
  3133. crc:=0;
  3134. for i:=0 to paras.count-1 do
  3135. begin
  3136. hp:=tparavarsym(paras[i]);
  3137. if not(vo_is_hidden_para in hp.varoptions) then
  3138. begin
  3139. hs:=hp.vardef.mangledparaname;
  3140. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3141. end;
  3142. end;
  3143. hs:=hp.vardef.mangledparaname;
  3144. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3145. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3146. end;
  3147. {$ifdef compress}
  3148. _mangledname:=stringdup(minilzw_encode(mangledname));
  3149. {$else}
  3150. _mangledname:=stringdup(mangledname);
  3151. {$endif}
  3152. end;
  3153. function tprocdef.cplusplusmangledname : string;
  3154. function getcppparaname(p : tdef) : string;
  3155. const
  3156. {$ifdef NAMEMANGLING_GCC2}
  3157. ordtype2str : array[tordtype] of string[2] = (
  3158. '',
  3159. 'Uc','Us','Ui','Us',
  3160. 'Sc','s','i','x',
  3161. 'b','b','b','b','b',
  3162. 'c','w','x');
  3163. {$else NAMEMANGLING_GCC2}
  3164. ordtype2str : array[tordtype] of string[1] = (
  3165. 'v',
  3166. 'h','t','j','y',
  3167. 'a','s','i','x',
  3168. 'b','b','b','b','b',
  3169. 'c','w','x');
  3170. floattype2str : array[tfloattype] of string[1] = (
  3171. 'f','d','e','e',
  3172. 'd','d','g');
  3173. {$endif NAMEMANGLING_GCC2}
  3174. var
  3175. s : string;
  3176. begin
  3177. case p.typ of
  3178. orddef:
  3179. s:=ordtype2str[torddef(p).ordtype];
  3180. pointerdef:
  3181. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3182. {$ifndef NAMEMANGLING_GCC2}
  3183. floatdef:
  3184. s:=floattype2str[tfloatdef(p).floattype];
  3185. {$endif NAMEMANGLING_GCC2}
  3186. else
  3187. internalerror(2103001);
  3188. end;
  3189. getcppparaname:=s;
  3190. end;
  3191. var
  3192. s,s2 : string;
  3193. hp : TParavarsym;
  3194. i : integer;
  3195. begin
  3196. {$ifdef NAMEMANGLING_GCC2}
  3197. { outdated gcc 2.x name mangling scheme }
  3198. s := procsym.realname;
  3199. if procsym.owner.symtabletype=ObjectSymtable then
  3200. begin
  3201. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3202. case proctypeoption of
  3203. potype_destructor:
  3204. s:='_$_'+tostr(length(s2))+s2;
  3205. potype_constructor:
  3206. s:='___'+tostr(length(s2))+s2;
  3207. else
  3208. s:='_'+s+'__'+tostr(length(s2))+s2;
  3209. end;
  3210. end
  3211. else s:=s+'__';
  3212. s:=s+'F';
  3213. { concat modifiers }
  3214. { !!!!! }
  3215. { now we handle the parameters }
  3216. if maxparacount>0 then
  3217. begin
  3218. for i:=0 to paras.count-1 do
  3219. begin
  3220. hp:=tparavarsym(paras[i]);
  3221. { no hidden parameters form part of a C++ mangled name:
  3222. a) self is not included
  3223. b) there are no "high" or other hidden parameters
  3224. }
  3225. if vo_is_hidden_para in hp.varoptions then
  3226. continue;
  3227. s2:=getcppparaname(hp.vardef);
  3228. if hp.varspez in [vs_var,vs_out] then
  3229. s2:='R'+s2;
  3230. s:=s+s2;
  3231. end;
  3232. end
  3233. else
  3234. s:=s+'v';
  3235. cplusplusmangledname:=s;
  3236. {$else NAMEMANGLING_GCC2}
  3237. { gcc 3.x and 4.x name mangling scheme }
  3238. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3239. if procsym.owner.symtabletype=ObjectSymtable then
  3240. begin
  3241. s:='_ZN';
  3242. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3243. s:=s+tostr(length(s2))+s2;
  3244. case proctypeoption of
  3245. potype_constructor:
  3246. s:=s+'C1';
  3247. potype_destructor:
  3248. s:=s+'D1';
  3249. else
  3250. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3251. end;
  3252. s:=s+'E';
  3253. end
  3254. else
  3255. s:=procsym.realname;
  3256. { now we handle the parameters }
  3257. if maxparacount>0 then
  3258. begin
  3259. for i:=0 to paras.count-1 do
  3260. begin
  3261. hp:=tparavarsym(paras[i]);
  3262. { no hidden parameters form part of a C++ mangled name:
  3263. a) self is not included
  3264. b) there are no "high" or other hidden parameters
  3265. }
  3266. if vo_is_hidden_para in hp.varoptions then
  3267. continue;
  3268. s2:=getcppparaname(hp.vardef);
  3269. if hp.varspez in [vs_var,vs_out] then
  3270. s2:='R'+s2;
  3271. s:=s+s2;
  3272. end;
  3273. end
  3274. else
  3275. s:=s+'v';
  3276. cplusplusmangledname:=s;
  3277. {$endif NAMEMANGLING_GCC2}
  3278. end;
  3279. function tprocdef.objcmangledname : string;
  3280. var
  3281. manglednamelen: longint;
  3282. iscatmethod : boolean;
  3283. begin
  3284. if not (po_msgstr in procoptions) then
  3285. internalerror(2009030901);
  3286. { we may very well need longer strings to handle these... }
  3287. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3288. length('+"[ ]"')+length(messageinf.str^);
  3289. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3290. if (iscatmethod) then
  3291. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3292. if manglednamelen>255 then
  3293. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3294. if not(po_classmethod in procoptions) then
  3295. result:='"-['
  3296. else
  3297. result:='"+[';
  3298. { quotes are necessary because the +/- otherwise confuse the assembler
  3299. into expecting a number
  3300. }
  3301. if iscatmethod then
  3302. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3303. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3304. if iscatmethod then
  3305. result:=result+')';
  3306. result:=result+' '+messageinf.str^+']"';
  3307. end;
  3308. procedure tprocdef.setmangledname(const s : string);
  3309. begin
  3310. { This is not allowed anymore, the forward declaration
  3311. already needs to create the correct mangledname, no changes
  3312. afterwards are allowed (PFV) }
  3313. { Exception: interface definitions in mode macpas, since in that }
  3314. { case no reference to the old name can exist yet (JM) }
  3315. if assigned(_mangledname) then
  3316. if ((m_mac in current_settings.modeswitches) and
  3317. (interfacedef)) then
  3318. stringdispose(_mangledname)
  3319. else
  3320. internalerror(200411171);
  3321. {$ifdef compress}
  3322. _mangledname:=stringdup(minilzw_encode(s));
  3323. {$else}
  3324. _mangledname:=stringdup(s);
  3325. {$endif}
  3326. include(procoptions,po_has_mangledname);
  3327. end;
  3328. {***************************************************************************
  3329. TPROCVARDEF
  3330. ***************************************************************************}
  3331. constructor tprocvardef.create(level:byte);
  3332. begin
  3333. inherited create(procvardef,level);
  3334. end;
  3335. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3336. begin
  3337. inherited ppuload(procvardef,ppufile);
  3338. { load para symtable }
  3339. parast:=tparasymtable.create(self,ppufile.getbyte);
  3340. tparasymtable(parast).ppuload(ppufile);
  3341. end;
  3342. function tprocvardef.getcopy : tstoreddef;
  3343. var
  3344. i : tcallercallee;
  3345. j : longint;
  3346. begin
  3347. result:=tprocvardef.create(parast.symtablelevel);
  3348. tprocvardef(result).returndef:=returndef;
  3349. tprocvardef(result).returndefderef:=returndefderef;
  3350. tprocvardef(result).parast:=parast.getcopy;
  3351. tprocvardef(result).savesize:=savesize;
  3352. { create paralist copy }
  3353. tprocvardef(result).paras:=tparalist.create(false);
  3354. tprocvardef(result).paras.count:=paras.count;
  3355. for j:=0 to paras.count-1 do
  3356. tprocvardef(result).paras[j]:=paras[j];
  3357. tprocvardef(result).proctypeoption:=proctypeoption;
  3358. tprocvardef(result).proccalloption:=proccalloption;
  3359. tprocvardef(result).procoptions:=procoptions;
  3360. tprocvardef(result).callerargareasize:=callerargareasize;
  3361. tprocvardef(result).calleeargareasize:=calleeargareasize;
  3362. tprocvardef(result).maxparacount:=maxparacount;
  3363. tprocvardef(result).minparacount:=minparacount;
  3364. for i:=low(tcallercallee) to high(tcallercallee) do
  3365. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  3366. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3367. {$ifdef m68k}
  3368. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3369. {$endif}
  3370. end;
  3371. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3372. begin
  3373. inherited ppuwrite(ppufile);
  3374. { Save the para symtable level (necessary to distinguish nested
  3375. procvars) }
  3376. ppufile.putbyte(parast.symtablelevel);
  3377. { Write this entry }
  3378. ppufile.writeentry(ibprocvardef);
  3379. { Save the para symtable, this is taken from the interface }
  3380. tparasymtable(parast).ppuwrite(ppufile);
  3381. end;
  3382. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3383. begin
  3384. case t of
  3385. gs_para :
  3386. GetSymtable:=parast;
  3387. else
  3388. GetSymtable:=nil;
  3389. end;
  3390. end;
  3391. function tprocvardef.size : aint;
  3392. begin
  3393. if ((po_methodpointer in procoptions) or
  3394. is_nested_pd(self)) and
  3395. not(po_addressonly in procoptions) then
  3396. size:=2*sizeof(pint)
  3397. else
  3398. size:=sizeof(pint);
  3399. end;
  3400. function tprocvardef.is_methodpointer:boolean;
  3401. begin
  3402. result:=(po_methodpointer in procoptions);
  3403. end;
  3404. function tprocvardef.is_addressonly:boolean;
  3405. begin
  3406. result:=(not(po_methodpointer in procoptions) and
  3407. not is_nested_pd(self)) or
  3408. (po_addressonly in procoptions);
  3409. end;
  3410. function tprocvardef.getmangledparaname:string;
  3411. begin
  3412. if not(po_methodpointer in procoptions) then
  3413. if not is_nested_pd(self) then
  3414. result:='procvar'
  3415. else
  3416. result:='nestedprovar'
  3417. else
  3418. result:='procvarofobj'
  3419. end;
  3420. function tprocvardef.is_publishable : boolean;
  3421. begin
  3422. is_publishable:=(po_methodpointer in procoptions);
  3423. end;
  3424. function tprocvardef.GetTypeName : string;
  3425. var
  3426. s: string;
  3427. showhidden : boolean;
  3428. begin
  3429. {$ifdef EXTDEBUG}
  3430. showhidden:=true;
  3431. {$else EXTDEBUG}
  3432. showhidden:=false;
  3433. {$endif EXTDEBUG}
  3434. s:='<';
  3435. if po_classmethod in procoptions then
  3436. s := s+'class method type of'
  3437. else
  3438. if po_addressonly in procoptions then
  3439. s := s+'address of'
  3440. else
  3441. s := s+'procedure variable type of';
  3442. if assigned(returndef) and
  3443. (returndef<>voidtype) then
  3444. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3445. else
  3446. s:=s+' procedure'+typename_paras(showhidden);
  3447. if po_methodpointer in procoptions then
  3448. s := s+' of object';
  3449. if is_nested_pd(self) then
  3450. s := s+' is nested';
  3451. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3452. end;
  3453. {***************************************************************************
  3454. TOBJECTDEF
  3455. ***************************************************************************}
  3456. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  3457. begin
  3458. inherited create(n,objectdef);
  3459. fcurrent_dispid:=0;
  3460. objecttype:=ot;
  3461. childof:=nil;
  3462. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3463. { create space for vmt !! }
  3464. vmtentries:=TFPList.Create;
  3465. vmt_offset:=0;
  3466. set_parent(c);
  3467. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3468. prepareguid;
  3469. { setup implemented interfaces }
  3470. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3471. ImplementedInterfaces:=TFPObjectList.Create(true)
  3472. else
  3473. ImplementedInterfaces:=nil;
  3474. writing_class_record_dbginfo:=false;
  3475. end;
  3476. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3477. var
  3478. i,
  3479. implintfcount : longint;
  3480. d : tderef;
  3481. ImplIntf : TImplementedInterface;
  3482. vmtentry : pvmtentry;
  3483. begin
  3484. inherited ppuload(objectdef,ppufile);
  3485. objecttype:=tobjecttyp(ppufile.getbyte);
  3486. objextname:=stringdup(ppufile.getstring);
  3487. { only used for external Objective-C classes/protocols }
  3488. if (objextname^='') then
  3489. stringdispose(objextname);
  3490. import_lib:=stringdup(ppufile.getstring);
  3491. { only used for external C++ classes }
  3492. if (import_lib^='') then
  3493. stringdispose(import_lib);
  3494. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3495. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3496. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3497. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3498. vmt_offset:=ppufile.getlongint;
  3499. ppufile.getderef(childofderef);
  3500. { load guid }
  3501. iidstr:=nil;
  3502. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3503. begin
  3504. new(iidguid);
  3505. ppufile.getguid(iidguid^);
  3506. iidstr:=stringdup(ppufile.getstring);
  3507. end;
  3508. vmtentries:=TFPList.Create;
  3509. vmtentries.count:=ppufile.getlongint;
  3510. for i:=0 to vmtentries.count-1 do
  3511. begin
  3512. ppufile.getderef(d);
  3513. new(vmtentry);
  3514. vmtentry^.procdef:=nil;
  3515. vmtentry^.procdefderef:=d;
  3516. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3517. vmtentries[i]:=vmtentry;
  3518. end;
  3519. { load implemented interfaces }
  3520. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3521. begin
  3522. ImplementedInterfaces:=TFPObjectList.Create(true);
  3523. implintfcount:=ppufile.getlongint;
  3524. for i:=0 to implintfcount-1 do
  3525. begin
  3526. ppufile.getderef(d);
  3527. ImplIntf:=TImplementedInterface.Create_deref(d);
  3528. ImplIntf.IOffset:=ppufile.getlongint;
  3529. ImplementedInterfaces.Add(ImplIntf);
  3530. end;
  3531. end
  3532. else
  3533. ImplementedInterfaces:=nil;
  3534. if df_copied_def in defoptions then
  3535. ppufile.getderef(cloneddefderef)
  3536. else
  3537. tObjectSymtable(symtable).ppuload(ppufile);
  3538. { handles the predefined class tobject }
  3539. { the last TOBJECT which is loaded gets }
  3540. { it ! }
  3541. if (childof=nil) and
  3542. (objecttype=odt_class) and
  3543. (objname^='TOBJECT') then
  3544. class_tobject:=self;
  3545. if (childof=nil) and
  3546. (objecttype=odt_interfacecom) and
  3547. (objname^='IUNKNOWN') then
  3548. interface_iunknown:=self;
  3549. if (childof=nil) and
  3550. (objecttype=odt_objcclass) and
  3551. (objname^='PROTOCOL') then
  3552. objc_protocoltype:=self;
  3553. writing_class_record_dbginfo:=false;
  3554. end;
  3555. destructor tobjectdef.destroy;
  3556. begin
  3557. if assigned(symtable) then
  3558. begin
  3559. symtable.free;
  3560. symtable:=nil;
  3561. end;
  3562. stringdispose(objextname);
  3563. stringdispose(import_lib);
  3564. stringdispose(iidstr);
  3565. if assigned(ImplementedInterfaces) then
  3566. begin
  3567. ImplementedInterfaces.free;
  3568. ImplementedInterfaces:=nil;
  3569. end;
  3570. if assigned(iidguid) then
  3571. begin
  3572. dispose(iidguid);
  3573. iidguid:=nil;
  3574. end;
  3575. if assigned(vmtentries) then
  3576. begin
  3577. resetvmtentries;
  3578. vmtentries.free;
  3579. vmtentries:=nil;
  3580. end;
  3581. if assigned(vmcallstaticinfo) then
  3582. begin
  3583. freemem(vmcallstaticinfo);
  3584. vmcallstaticinfo:=nil;
  3585. end;
  3586. inherited destroy;
  3587. end;
  3588. function tobjectdef.getcopy : tstoreddef;
  3589. var
  3590. i : longint;
  3591. begin
  3592. result:=tobjectdef.create(objecttype,objrealname^,childof);
  3593. { the constructor allocates a symtable which we release to avoid memory leaks }
  3594. tobjectdef(result).symtable.free;
  3595. tobjectdef(result).symtable:=symtable.getcopy;
  3596. if assigned(objextname) then
  3597. tobjectdef(result).objextname:=stringdup(objextname^);
  3598. if assigned(import_lib) then
  3599. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3600. tobjectdef(result).objectoptions:=objectoptions;
  3601. include(tobjectdef(result).defoptions,df_copied_def);
  3602. tobjectdef(result).vmt_offset:=vmt_offset;
  3603. if assigned(iidguid) then
  3604. begin
  3605. new(tobjectdef(result).iidguid);
  3606. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3607. end;
  3608. if assigned(iidstr) then
  3609. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3610. if assigned(ImplementedInterfaces) then
  3611. begin
  3612. for i:=0 to ImplementedInterfaces.count-1 do
  3613. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3614. end;
  3615. if assigned(vmtentries) then
  3616. begin
  3617. tobjectdef(result).vmtentries:=TFPList.Create;
  3618. tobjectdef(result).copyvmtentries(self);
  3619. end;
  3620. end;
  3621. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3622. var
  3623. i : longint;
  3624. vmtentry : pvmtentry;
  3625. ImplIntf : TImplementedInterface;
  3626. old_do_indirect_crc: boolean;
  3627. begin
  3628. { if class1 in unit A changes, and class2 in unit B inherits from it
  3629. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3630. However, if there is also a class3 in unit C that only depends on
  3631. unit B, then unit C will not be recompiled because nothing changed
  3632. to the interface of unit B. Nevertheless, unit C can indirectly
  3633. depend on unit A via derefs, and these must be updated -> the
  3634. indirect crc keeps track of such changes. }
  3635. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3636. ppufile.do_indirect_crc:=true;
  3637. inherited ppuwrite(ppufile);
  3638. ppufile.putbyte(byte(objecttype));
  3639. if assigned(objextname) then
  3640. ppufile.putstring(objextname^)
  3641. else
  3642. ppufile.putstring('');
  3643. if assigned(import_lib) then
  3644. ppufile.putstring(import_lib^)
  3645. else
  3646. ppufile.putstring('');
  3647. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3648. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3649. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3650. ppufile.putlongint(vmt_offset);
  3651. ppufile.putderef(childofderef);
  3652. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3653. begin
  3654. ppufile.putguid(iidguid^);
  3655. ppufile.putstring(iidstr^);
  3656. end;
  3657. ppufile.putlongint(vmtentries.count);
  3658. for i:=0 to vmtentries.count-1 do
  3659. begin
  3660. vmtentry:=pvmtentry(vmtentries[i]);
  3661. ppufile.putderef(vmtentry^.procdefderef);
  3662. ppufile.putbyte(byte(vmtentry^.visibility));
  3663. end;
  3664. if assigned(ImplementedInterfaces) then
  3665. begin
  3666. ppufile.putlongint(ImplementedInterfaces.Count);
  3667. for i:=0 to ImplementedInterfaces.Count-1 do
  3668. begin
  3669. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3670. ppufile.putderef(ImplIntf.intfdefderef);
  3671. ppufile.putlongint(ImplIntf.Ioffset);
  3672. end;
  3673. end;
  3674. if df_copied_def in defoptions then
  3675. ppufile.putderef(cloneddefderef);
  3676. ppufile.writeentry(ibobjectdef);
  3677. if not(df_copied_def in defoptions) then
  3678. tObjectSymtable(symtable).ppuwrite(ppufile);
  3679. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3680. end;
  3681. function tobjectdef.GetTypeName:string;
  3682. begin
  3683. { in this case we will go in endless recursion, because then }
  3684. { there is no tsym associated yet with the def. It can occur }
  3685. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3686. { instead of the actual type name }
  3687. if not assigned(typesym) then
  3688. result:='<Currently Parsed Class>'
  3689. else
  3690. result:=typename;
  3691. end;
  3692. procedure tobjectdef.buildderef;
  3693. var
  3694. i : longint;
  3695. vmtentry : pvmtentry;
  3696. begin
  3697. inherited buildderef;
  3698. childofderef.build(childof);
  3699. if df_copied_def in defoptions then
  3700. cloneddefderef.build(symtable.defowner)
  3701. else
  3702. tstoredsymtable(symtable).buildderef;
  3703. for i:=0 to vmtentries.count-1 do
  3704. begin
  3705. vmtentry:=pvmtentry(vmtentries[i]);
  3706. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3707. end;
  3708. if assigned(ImplementedInterfaces) then
  3709. begin
  3710. for i:=0 to ImplementedInterfaces.count-1 do
  3711. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3712. end;
  3713. end;
  3714. procedure tobjectdef.deref;
  3715. var
  3716. i : longint;
  3717. vmtentry : pvmtentry;
  3718. begin
  3719. inherited deref;
  3720. childof:=tobjectdef(childofderef.resolve);
  3721. if df_copied_def in defoptions then
  3722. begin
  3723. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3724. symtable:=cloneddef.symtable.getcopy;
  3725. end
  3726. else
  3727. tstoredsymtable(symtable).deref;
  3728. for i:=0 to vmtentries.count-1 do
  3729. begin
  3730. vmtentry:=pvmtentry(vmtentries[i]);
  3731. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3732. end;
  3733. if assigned(ImplementedInterfaces) then
  3734. begin
  3735. for i:=0 to ImplementedInterfaces.count-1 do
  3736. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3737. end;
  3738. end;
  3739. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  3740. var
  3741. pd: tprocdef absolute def;
  3742. st: tsymtable;
  3743. psym: tsym;
  3744. nname: TIDString;
  3745. begin
  3746. if (tdef(def).typ<>procdef) then
  3747. exit;
  3748. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  3749. owner = symtable in which objcclassdef is defined
  3750. }
  3751. st:=pd.owner.defowner.owner;
  3752. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  3753. { check for an existing procsym with our special name }
  3754. psym:=tsym(st.find(nname));
  3755. if not assigned(psym) then
  3756. begin
  3757. psym:=tprocsym.create(nname);
  3758. { avoid warning about this symbol being unused }
  3759. psym.IncRefCount;
  3760. st.insert(psym,true);
  3761. end
  3762. else if (psym.typ<>procsym) then
  3763. internalerror(2009111501);
  3764. { add ourselves to this special procsym }
  3765. tprocsym(psym).procdeflist.add(def);
  3766. end;
  3767. procedure tobjectdef.buildderefimpl;
  3768. begin
  3769. inherited buildderefimpl;
  3770. if not (df_copied_def in defoptions) then
  3771. tstoredsymtable(symtable).buildderefimpl;
  3772. end;
  3773. procedure tobjectdef.derefimpl;
  3774. begin
  3775. inherited derefimpl;
  3776. if not (df_copied_def in defoptions) then
  3777. tstoredsymtable(symtable).derefimpl;
  3778. { the procdefs are not owned by the class helper procsyms, so they
  3779. are not stored/restored either -> re-add them here }
  3780. if (objecttype=odt_objcclass) or
  3781. (oo_is_classhelper in objectoptions) then
  3782. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  3783. end;
  3784. procedure tobjectdef.resetvmtentries;
  3785. var
  3786. i : longint;
  3787. begin
  3788. for i:=0 to vmtentries.Count-1 do
  3789. Dispose(pvmtentry(vmtentries[i]));
  3790. vmtentries.clear;
  3791. end;
  3792. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3793. var
  3794. i : longint;
  3795. vmtentry : pvmtentry;
  3796. begin
  3797. resetvmtentries;
  3798. vmtentries.count:=objdef.vmtentries.count;
  3799. for i:=0 to objdef.vmtentries.count-1 do
  3800. begin
  3801. new(vmtentry);
  3802. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3803. vmtentries[i]:=vmtentry;
  3804. end;
  3805. end;
  3806. function tobjectdef.getparentdef:tdef;
  3807. begin
  3808. { TODO: Remove getparentdef hack}
  3809. { With 2 forward declared classes with the child class before the
  3810. parent class the child class is written earlier to the ppu. Leaving it
  3811. possible to have a reference to the parent class for property overriding,
  3812. but the parent class still has the childof not resolved yet (PFV) }
  3813. if childof=nil then
  3814. childof:=tobjectdef(childofderef.resolve);
  3815. result:=childof;
  3816. end;
  3817. procedure tobjectdef.prepareguid;
  3818. begin
  3819. { set up guid }
  3820. if not assigned(iidguid) then
  3821. begin
  3822. new(iidguid);
  3823. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3824. end;
  3825. { setup iidstring }
  3826. if not assigned(iidstr) then
  3827. iidstr:=stringdup(''); { default is empty string }
  3828. end;
  3829. procedure tobjectdef.set_parent( c : tobjectdef);
  3830. begin
  3831. if assigned(childof) then
  3832. exit;
  3833. childof:=c;
  3834. if not assigned(c) then
  3835. exit;
  3836. { inherit options and status }
  3837. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3838. { add the data of the anchestor class/object }
  3839. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  3840. begin
  3841. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3842. { inherit recordalignment }
  3843. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3844. if (oo_has_vmt in objectoptions) and
  3845. (oo_has_vmt in c.objectoptions) then
  3846. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3847. { if parent has a vmt field then the offset is the same for the child PM }
  3848. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3849. begin
  3850. vmt_offset:=c.vmt_offset;
  3851. include(objectoptions,oo_has_vmt);
  3852. end;
  3853. end;
  3854. end;
  3855. procedure tobjectdef.insertvmt;
  3856. var
  3857. vs: tfieldvarsym;
  3858. begin
  3859. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3860. exit;
  3861. if (oo_has_vmt in objectoptions) then
  3862. internalerror(12345)
  3863. else
  3864. begin
  3865. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3866. tObjectSymtable(symtable).fieldalignment);
  3867. if (tf_requires_proper_alignment in target_info.flags) then
  3868. begin
  3869. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3870. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3871. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3872. end;
  3873. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3874. hidesym(vs);
  3875. tObjectSymtable(symtable).insert(vs);
  3876. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3877. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  3878. vmt_offset:=vs.fieldoffset
  3879. else
  3880. vmt_offset:=vs.fieldoffset div 8;
  3881. include(objectoptions,oo_has_vmt);
  3882. end;
  3883. end;
  3884. procedure tobjectdef.check_forwards;
  3885. begin
  3886. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  3887. tstoredsymtable(symtable).check_forwards;
  3888. if (oo_is_forward in objectoptions) then
  3889. begin
  3890. { ok, in future, the forward can be resolved }
  3891. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3892. exclude(objectoptions,oo_is_forward);
  3893. end;
  3894. end;
  3895. { true if prot implements d (or if they are equal) }
  3896. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  3897. var
  3898. i : longint;
  3899. begin
  3900. { objcprotocols have multiple inheritance, all protocols from which
  3901. the current protocol inherits are stored in implementedinterfaces }
  3902. result:=prot=d;
  3903. if result then
  3904. exit;
  3905. for i:=0 to prot.ImplementedInterfaces.count-1 do
  3906. begin
  3907. result:=is_related_protocol(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  3908. if result then
  3909. exit;
  3910. end;
  3911. end;
  3912. { true, if self inherits from d (or if they are equal) }
  3913. function tobjectdef.is_related(d : tdef) : boolean;
  3914. var
  3915. hp : tobjectdef;
  3916. begin
  3917. if self=d then
  3918. begin
  3919. is_related:=true;
  3920. exit;
  3921. end;
  3922. if (d.typ<>objectdef) then
  3923. begin
  3924. is_related:=false;
  3925. exit;
  3926. end;
  3927. { Objective-C protocols can use multiple inheritance }
  3928. if (objecttype=odt_objcprotocol) then
  3929. begin
  3930. is_related:=is_related_protocol(self,d);
  3931. exit
  3932. end;
  3933. { formally declared Objective-C classes match Objective-C classes with
  3934. the same name }
  3935. if (objecttype=odt_objcclass) and
  3936. (tobjectdef(d).objecttype=odt_objcclass) and
  3937. ((oo_is_formal in objectoptions) or
  3938. (oo_is_formal in tobjectdef(d).objectoptions)) and
  3939. (objrealname^=tobjectdef(d).objrealname^) then
  3940. begin
  3941. is_related:=true;
  3942. exit;
  3943. end;
  3944. hp:=childof;
  3945. while assigned(hp) do
  3946. begin
  3947. if hp=d then
  3948. begin
  3949. is_related:=true;
  3950. exit;
  3951. end;
  3952. hp:=hp.childof;
  3953. end;
  3954. is_related:=false;
  3955. end;
  3956. function tobjectdef.find_destructor: tprocdef;
  3957. var
  3958. objdef: tobjectdef;
  3959. begin
  3960. objdef:=self;
  3961. while assigned(objdef) do
  3962. begin
  3963. result:=objdef.find_procdef_bytype(potype_destructor);
  3964. if assigned(result) then
  3965. exit;
  3966. objdef:=objdef.childof;
  3967. end;
  3968. result:=nil;
  3969. end;
  3970. function tobjectdef.implements_any_interfaces: boolean;
  3971. begin
  3972. result := (ImplementedInterfaces.Count > 0) or
  3973. (assigned(childof) and childof.implements_any_interfaces);
  3974. end;
  3975. function tobjectdef.size : aint;
  3976. begin
  3977. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3978. result:=sizeof(pint)
  3979. else
  3980. result:=tObjectSymtable(symtable).datasize;
  3981. end;
  3982. function tobjectdef.alignment:shortint;
  3983. begin
  3984. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3985. alignment:=sizeof(pint)
  3986. else
  3987. alignment:=tObjectSymtable(symtable).recordalignment;
  3988. end;
  3989. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3990. begin
  3991. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3992. case objecttype of
  3993. odt_class:
  3994. { the +2*sizeof(pint) is size and -size }
  3995. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3996. odt_objcclass,
  3997. odt_objcprotocol:
  3998. vmtmethodoffset:=0;
  3999. odt_interfacecom,odt_interfacecorba:
  4000. vmtmethodoffset:=index*sizeof(pint);
  4001. else
  4002. {$ifdef WITHDMT}
  4003. vmtmethodoffset:=(index+4)*sizeof(pint);
  4004. {$else WITHDMT}
  4005. vmtmethodoffset:=(index+3)*sizeof(pint);
  4006. {$endif WITHDMT}
  4007. end;
  4008. end;
  4009. function tobjectdef.vmt_mangledname : string;
  4010. begin
  4011. if not(oo_has_vmt in objectoptions) then
  4012. Message1(parser_n_object_has_no_vmt,objrealname^);
  4013. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  4014. end;
  4015. function tobjectdef.needs_inittable : boolean;
  4016. begin
  4017. case objecttype of
  4018. odt_dispinterface,
  4019. odt_class :
  4020. needs_inittable:=false;
  4021. odt_interfacecom:
  4022. needs_inittable:=true;
  4023. odt_interfacecorba:
  4024. needs_inittable:=is_related(interface_iunknown);
  4025. odt_object:
  4026. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  4027. odt_cppclass,
  4028. odt_objcclass,
  4029. odt_objcprotocol:
  4030. needs_inittable:=false;
  4031. else
  4032. internalerror(200108267);
  4033. end;
  4034. end;
  4035. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  4036. begin
  4037. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  4038. result:=inherited rtti_mangledname(rt)
  4039. else
  4040. begin
  4041. { necessary in case of a dynamic array of nsobject, or
  4042. if an nsobject field appears in a record that needs
  4043. init/finalisation }
  4044. if rt=initrtti then
  4045. begin
  4046. result:=voidpointertype.rtti_mangledname(rt);
  4047. exit;
  4048. end;
  4049. if not(target_info.system in systems_objc_nfabi) then
  4050. begin
  4051. result:=target_asm.labelprefix;
  4052. case objecttype of
  4053. odt_objcclass:
  4054. begin
  4055. case rt of
  4056. objcclassrtti:
  4057. if not(oo_is_classhelper in objectoptions) then
  4058. result:=result+'_OBJC_CLASS_'
  4059. else
  4060. result:=result+'_OBJC_CATEGORY_';
  4061. objcmetartti:
  4062. if not(oo_is_classhelper in objectoptions) then
  4063. result:=result+'_OBJC_METACLASS_'
  4064. else
  4065. internalerror(2009111511);
  4066. else
  4067. internalerror(2009092302);
  4068. end;
  4069. end;
  4070. odt_objcprotocol:
  4071. result:=result+'_OBJC_PROTOCOL_';
  4072. end;
  4073. end
  4074. else
  4075. begin
  4076. case objecttype of
  4077. odt_objcclass:
  4078. begin
  4079. if (oo_is_classhelper in objectoptions) and
  4080. (rt<>objcclassrtti) then
  4081. internalerror(2009111512);
  4082. case rt of
  4083. objcclassrtti:
  4084. if not(oo_is_classhelper in objectoptions) then
  4085. result:='_OBJC_CLASS_$_'
  4086. else
  4087. result:='_OBJC_$_CATEGORY_';
  4088. objcmetartti:
  4089. result:='_OBJC_METACLASS_$_';
  4090. objcclassrortti:
  4091. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  4092. objcmetarortti:
  4093. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  4094. else
  4095. internalerror(2009092303);
  4096. end;
  4097. end;
  4098. odt_objcprotocol:
  4099. begin
  4100. result:=lower(target_asm.labelprefix);
  4101. case rt of
  4102. objcclassrtti:
  4103. result:=result+'_OBJC_PROTOCOL_$_';
  4104. objcmetartti:
  4105. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  4106. else
  4107. internalerror(2009092501);
  4108. end;
  4109. end;
  4110. end;
  4111. end;
  4112. result:=result+objextname^;
  4113. end;
  4114. end;
  4115. function tobjectdef.members_need_inittable : boolean;
  4116. begin
  4117. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  4118. end;
  4119. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  4120. var
  4121. ImplIntf : TImplementedInterface;
  4122. i : longint;
  4123. begin
  4124. result:=nil;
  4125. if not assigned(ImplementedInterfaces) then
  4126. exit;
  4127. for i:=0 to ImplementedInterfaces.Count-1 do
  4128. begin
  4129. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4130. if ImplIntf.intfdef=aintfdef then
  4131. begin
  4132. result:=ImplIntf;
  4133. exit;
  4134. end;
  4135. end;
  4136. end;
  4137. function tobjectdef.is_publishable : boolean;
  4138. begin
  4139. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  4140. end;
  4141. function tobjectdef.get_next_dispid: longint;
  4142. begin
  4143. inc(fcurrent_dispid);
  4144. result:=fcurrent_dispid;
  4145. end;
  4146. function tobjectdef.search_enumerator_get: tprocdef;
  4147. var
  4148. objdef : tobjectdef;
  4149. sym : tsym;
  4150. i : integer;
  4151. pd : tprocdef;
  4152. hashedid : THashedIDString;
  4153. begin
  4154. result:=nil;
  4155. objdef:=self;
  4156. hashedid.id:='GETENUMERATOR';
  4157. while assigned(objdef) do
  4158. begin
  4159. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4160. if assigned(sym) and (sym.typ=procsym) then
  4161. begin
  4162. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4163. begin
  4164. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4165. if (pd.proctypeoption = potype_function) and
  4166. is_class_or_interface_or_object(pd.returndef) and
  4167. (pd.visibility >= vis_public) then
  4168. begin
  4169. result:=pd;
  4170. exit;
  4171. end;
  4172. end;
  4173. end;
  4174. objdef:=objdef.childof;
  4175. end;
  4176. end;
  4177. function tobjectdef.search_enumerator_move: tprocdef;
  4178. var
  4179. objdef : tobjectdef;
  4180. sym : tsym;
  4181. i : integer;
  4182. pd : tprocdef;
  4183. hashedid : THashedIDString;
  4184. begin
  4185. result:=nil;
  4186. objdef:=self;
  4187. // first search for po_enumerator_movenext method modifier
  4188. // then search for public function MoveNext: Boolean
  4189. hashedid.id:='MOVENEXT';
  4190. while assigned(objdef) do
  4191. begin
  4192. for i:=0 to objdef.symtable.SymList.Count-1 do
  4193. begin
  4194. sym:=TSym(objdef.symtable.SymList[i]);
  4195. if (sym.typ=procsym) then
  4196. begin
  4197. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4198. if assigned(pd) then
  4199. begin
  4200. result:=pd;
  4201. exit;
  4202. end;
  4203. end;
  4204. end;
  4205. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4206. if assigned(sym) and (sym.typ=procsym) then
  4207. begin
  4208. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4209. begin
  4210. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4211. if (pd.proctypeoption = potype_function) and
  4212. is_boolean(pd.returndef) and
  4213. (pd.minparacount = 0) and
  4214. (pd.visibility >= vis_public) then
  4215. begin
  4216. result:=pd;
  4217. exit;
  4218. end;
  4219. end;
  4220. end;
  4221. objdef:=objdef.childof;
  4222. end;
  4223. end;
  4224. function tobjectdef.search_enumerator_current: tsym;
  4225. var
  4226. objdef : tobjectdef;
  4227. sym: tsym;
  4228. i: integer;
  4229. hashedid : THashedIDString;
  4230. begin
  4231. result:=nil;
  4232. objdef:=self;
  4233. hashedid.id:='CURRENT';
  4234. // first search for ppo_enumerator_current property modifier
  4235. // then search for public property Current
  4236. while assigned(objdef) do
  4237. begin
  4238. for i:=0 to objdef.symtable.SymList.Count-1 do
  4239. begin
  4240. sym:=TSym(objdef.symtable.SymList[i]);
  4241. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4242. begin
  4243. result:=sym;
  4244. exit;
  4245. end;
  4246. end;
  4247. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4248. if assigned(sym) and (sym.typ=propertysym) and
  4249. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4250. begin
  4251. result:=sym;
  4252. exit;
  4253. end;
  4254. objdef:=objdef.childof;
  4255. end;
  4256. end;
  4257. procedure tobjectdef.register_created_classref_type;
  4258. begin
  4259. if not classref_created_in_current_module then
  4260. begin
  4261. classref_created_in_current_module:=true;
  4262. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4263. end;
  4264. end;
  4265. procedure tobjectdef.register_created_object_type;
  4266. begin
  4267. if not created_in_current_module then
  4268. begin
  4269. created_in_current_module:=true;
  4270. current_module.wpoinfo.addcreatedobjtype(self);
  4271. end;
  4272. end;
  4273. procedure tobjectdef.register_maybe_created_object_type;
  4274. begin
  4275. { if we know it has been created for sure, no need
  4276. to also record that it maybe can be created in
  4277. this module
  4278. }
  4279. if not (created_in_current_module) and
  4280. not (maybe_created_in_current_module) then
  4281. begin
  4282. maybe_created_in_current_module:=true;
  4283. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4284. end;
  4285. end;
  4286. procedure tobjectdef.register_vmt_call(index: longint);
  4287. begin
  4288. if (is_object(self) or is_class(self)) then
  4289. current_module.wpoinfo.addcalledvmtentry(self,index);
  4290. end;
  4291. procedure make_procdef_external(data: tobject; arg: pointer);
  4292. var
  4293. def: tdef absolute data;
  4294. begin
  4295. if (def.typ = procdef) then
  4296. begin
  4297. include(tprocdef(def).procoptions,po_external);
  4298. tprocdef(def).forwarddef:=false;
  4299. end;
  4300. end;
  4301. procedure tobjectdef.make_all_methods_external;
  4302. begin
  4303. self.symtable.deflist.foreachcall(@make_procdef_external,nil);
  4304. end;
  4305. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4306. var
  4307. def: tdef absolute data;
  4308. pd: tprocdef absolute data;
  4309. i,
  4310. paracount: longint;
  4311. begin
  4312. if (def.typ=procdef) then
  4313. begin
  4314. { add all messages also under a dummy name to the symtable in
  4315. which the objcclass/protocol/category is declared, so they can
  4316. be called via id.<name>
  4317. }
  4318. create_class_helper_for_procdef(pd,nil);
  4319. { we have to wait until now to set the mangled name because it
  4320. depends on the (possibly external) class name, which is defined
  4321. at the very end. }
  4322. if not(po_msgstr in pd.procoptions) then
  4323. begin
  4324. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4325. { recover to avoid internalerror later on }
  4326. include(pd.procoptions,po_msgstr);
  4327. pd.messageinf.str:=stringdup('MissingDeclaration');
  4328. end;
  4329. { Mangled name is already set in case this is a copy of
  4330. another type. }
  4331. if not(po_has_mangledname in pd.procoptions) then
  4332. begin
  4333. { check whether the number of formal parameters is correct,
  4334. and whether they have valid Objective-C types }
  4335. paracount:=0;
  4336. for i:=1 to length(pd.messageinf.str^) do
  4337. if pd.messageinf.str^[i]=':' then
  4338. inc(paracount);
  4339. for i:=0 to pd.paras.count-1 do
  4340. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4341. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4342. dec(paracount);
  4343. if (paracount<>0) then
  4344. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4345. pd.setmangledname(pd.objcmangledname);
  4346. end
  4347. else
  4348. { all checks already done }
  4349. exit;
  4350. if not(oo_is_external in pd.struct.objectoptions) then
  4351. begin
  4352. if (po_varargs in pd.procoptions) then
  4353. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4354. else
  4355. begin
  4356. { check for "array of const" parameters }
  4357. for i:=0 to pd.parast.symlist.count-1 do
  4358. begin
  4359. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4360. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4361. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4362. end;
  4363. end;
  4364. end;
  4365. end;
  4366. end;
  4367. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4368. var
  4369. sym: tsym absolute data;
  4370. begin
  4371. if (sym.typ=fieldvarsym) and
  4372. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4373. sym.IncRefCount;
  4374. end;
  4375. procedure tobjectdef.finish_objc_data;
  4376. begin
  4377. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4378. if (oo_is_external in objectoptions) then
  4379. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4380. end;
  4381. procedure verify_objc_vardef(data: tobject; arg: pointer);
  4382. var
  4383. sym: tabstractvarsym absolute data;
  4384. res: pboolean absolute arg;
  4385. founderrordef: tdef;
  4386. begin
  4387. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  4388. exit;
  4389. if (sym.typ=paravarsym) and
  4390. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  4391. is_array_of_const(tparavarsym(sym).vardef)) then
  4392. exit;
  4393. if not objcchecktype(sym.vardef,founderrordef) then
  4394. begin
  4395. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4396. res^:=false;
  4397. end;
  4398. end;
  4399. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  4400. var
  4401. def: tdef absolute data;
  4402. res: pboolean absolute arg;
  4403. founderrordef: tdef;
  4404. begin
  4405. if (def.typ<>procdef) then
  4406. exit;
  4407. { check parameter types for validity }
  4408. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  4409. { check the result type for validity }
  4410. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  4411. begin
  4412. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4413. res^:=false;
  4414. end;
  4415. end;
  4416. function tobjectdef.check_objc_types: boolean;
  4417. begin
  4418. { done in separate step from finish_objc_data, because when
  4419. finish_objc_data is called, not all forwarddefs have been resolved
  4420. yet and we need to know all types here }
  4421. result:=true;
  4422. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  4423. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  4424. end;
  4425. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4426. var
  4427. def: tdef absolute data;
  4428. pd: tprocdef absolute data;
  4429. begin
  4430. if (def.typ=procdef) then
  4431. begin
  4432. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4433. if (oo_is_external in pd.struct.objectoptions) then
  4434. begin
  4435. { copied from psub.read_proc }
  4436. if assigned(tobjectdef(pd.struct).import_lib) then
  4437. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,0,false,false)
  4438. else
  4439. begin
  4440. { add import name to external list for DLL scanning }
  4441. if tf_has_dllscanner in target_info.flags then
  4442. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4443. end;
  4444. end;
  4445. end;
  4446. end;
  4447. procedure tobjectdef.finish_cpp_data;
  4448. begin
  4449. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4450. end;
  4451. {****************************************************************************
  4452. TImplementedInterface
  4453. ****************************************************************************}
  4454. constructor TImplementedInterface.create(aintf: tobjectdef);
  4455. begin
  4456. inherited create;
  4457. intfdef:=aintf;
  4458. IOffset:=-1;
  4459. IType:=etStandard;
  4460. NameMappings:=nil;
  4461. procdefs:=nil;
  4462. end;
  4463. constructor TImplementedInterface.create_deref(d:tderef);
  4464. begin
  4465. inherited create;
  4466. intfdef:=nil;
  4467. intfdefderef:=d;
  4468. IOffset:=-1;
  4469. IType:=etStandard;
  4470. NameMappings:=nil;
  4471. procdefs:=nil;
  4472. end;
  4473. destructor TImplementedInterface.destroy;
  4474. var
  4475. i : longint;
  4476. mappedname : pshortstring;
  4477. begin
  4478. if assigned(NameMappings) then
  4479. begin
  4480. for i:=0 to NameMappings.Count-1 do
  4481. begin
  4482. mappedname:=pshortstring(NameMappings[i]);
  4483. stringdispose(mappedname);
  4484. end;
  4485. NameMappings.free;
  4486. NameMappings:=nil;
  4487. end;
  4488. if assigned(procdefs) then
  4489. begin
  4490. procdefs.free;
  4491. procdefs:=nil;
  4492. end;
  4493. inherited destroy;
  4494. end;
  4495. procedure TImplementedInterface.buildderef;
  4496. begin
  4497. intfdefderef.build(intfdef);
  4498. end;
  4499. procedure TImplementedInterface.deref;
  4500. begin
  4501. intfdef:=tobjectdef(intfdefderef.resolve);
  4502. end;
  4503. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4504. begin
  4505. if not assigned(NameMappings) then
  4506. NameMappings:=TFPHashList.Create;
  4507. NameMappings.Add(origname,stringdup(newname));
  4508. end;
  4509. function TImplementedInterface.GetMapping(const origname: string):string;
  4510. var
  4511. mappedname : pshortstring;
  4512. begin
  4513. result:='';
  4514. if not assigned(NameMappings) then
  4515. exit;
  4516. mappedname:=PShortstring(NameMappings.Find(origname));
  4517. if assigned(mappedname) then
  4518. result:=mappedname^;
  4519. end;
  4520. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4521. begin
  4522. if not assigned(procdefs) then
  4523. procdefs:=TFPObjectList.Create(false);
  4524. { duplicate entries must be stored, because multiple }
  4525. { interfaces can declare methods with the same name }
  4526. { and all of these get their own VMT entry }
  4527. procdefs.Add(pd);
  4528. end;
  4529. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4530. var
  4531. i : longint;
  4532. begin
  4533. result:=false;
  4534. { interfaces being implemented through delegation are not mergable (FK) }
  4535. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  4536. exit;
  4537. weight:=0;
  4538. { empty interface is mergeable }
  4539. if ProcDefs.Count=0 then
  4540. begin
  4541. result:=true;
  4542. exit;
  4543. end;
  4544. { The interface to merge must at least the number of
  4545. procedures of this interface }
  4546. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4547. exit;
  4548. for i:=0 to ProcDefs.Count-1 do
  4549. begin
  4550. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4551. exit;
  4552. end;
  4553. weight:=ProcDefs.Count;
  4554. result:=true;
  4555. end;
  4556. function TImplementedInterface.getcopy:TImplementedInterface;
  4557. begin
  4558. Result:=TImplementedInterface.Create(nil);
  4559. {$warning: this is completely wrong on so many levels...}
  4560. { 1) the procdefs list will be freed once for each copy
  4561. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4562. 3) idem for the name mappings
  4563. }
  4564. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4565. end;
  4566. {****************************************************************************
  4567. TFORWARDDEF
  4568. ****************************************************************************}
  4569. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4570. begin
  4571. inherited create(forwarddef);
  4572. tosymname:=stringdup(s);
  4573. forwardpos:=pos;
  4574. end;
  4575. function tforwarddef.GetTypeName:string;
  4576. begin
  4577. GetTypeName:='unresolved forward to '+tosymname^;
  4578. end;
  4579. destructor tforwarddef.destroy;
  4580. begin
  4581. stringdispose(tosymname);
  4582. inherited destroy;
  4583. end;
  4584. {****************************************************************************
  4585. TUNDEFINEDDEF
  4586. ****************************************************************************}
  4587. constructor tundefineddef.create;
  4588. begin
  4589. inherited create(undefineddef);
  4590. end;
  4591. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4592. begin
  4593. inherited ppuload(undefineddef,ppufile);
  4594. end;
  4595. function tundefineddef.GetTypeName:string;
  4596. begin
  4597. GetTypeName:='<undefined type>';
  4598. end;
  4599. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4600. begin
  4601. inherited ppuwrite(ppufile);
  4602. ppufile.writeentry(ibundefineddef);
  4603. end;
  4604. {****************************************************************************
  4605. TERRORDEF
  4606. ****************************************************************************}
  4607. constructor terrordef.create;
  4608. begin
  4609. inherited create(errordef);
  4610. { prevent consecutive faults }
  4611. savesize:=1;
  4612. end;
  4613. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4614. begin
  4615. { Can't write errordefs to ppu }
  4616. internalerror(200411063);
  4617. end;
  4618. function terrordef.GetTypeName:string;
  4619. begin
  4620. GetTypeName:='<erroneous type>';
  4621. end;
  4622. function terrordef.getmangledparaname:string;
  4623. begin
  4624. getmangledparaname:='error';
  4625. end;
  4626. {****************************************************************************
  4627. Definition Helpers
  4628. ****************************************************************************}
  4629. function is_interfacecom(def: tdef): boolean;
  4630. begin
  4631. is_interfacecom:=
  4632. assigned(def) and
  4633. (def.typ=objectdef) and
  4634. (tobjectdef(def).objecttype=odt_interfacecom);
  4635. end;
  4636. function is_interfacecorba(def: tdef): boolean;
  4637. begin
  4638. is_interfacecorba:=
  4639. assigned(def) and
  4640. (def.typ=objectdef) and
  4641. (tobjectdef(def).objecttype=odt_interfacecorba);
  4642. end;
  4643. function is_interface(def: tdef): boolean;
  4644. begin
  4645. is_interface:=
  4646. assigned(def) and
  4647. (def.typ=objectdef) and
  4648. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4649. end;
  4650. function is_dispinterface(def: tdef): boolean;
  4651. begin
  4652. result:=
  4653. assigned(def) and
  4654. (def.typ=objectdef) and
  4655. (tobjectdef(def).objecttype=odt_dispinterface);
  4656. end;
  4657. function is_class(def: tdef): boolean;
  4658. begin
  4659. is_class:=
  4660. assigned(def) and
  4661. (def.typ=objectdef) and
  4662. (tobjectdef(def).objecttype=odt_class);
  4663. end;
  4664. function is_object(def: tdef): boolean;
  4665. begin
  4666. is_object:=
  4667. assigned(def) and
  4668. (def.typ=objectdef) and
  4669. (tobjectdef(def).objecttype=odt_object);
  4670. end;
  4671. function is_cppclass(def: tdef): boolean;
  4672. begin
  4673. is_cppclass:=
  4674. assigned(def) and
  4675. (def.typ=objectdef) and
  4676. (tobjectdef(def).objecttype=odt_cppclass);
  4677. end;
  4678. function is_objcclass(def: tdef): boolean;
  4679. begin
  4680. is_objcclass:=
  4681. assigned(def) and
  4682. (def.typ=objectdef) and
  4683. (tobjectdef(def).objecttype=odt_objcclass);
  4684. end;
  4685. function is_objcclassref(def: tdef): boolean;
  4686. begin
  4687. is_objcclassref:=
  4688. assigned(def) and
  4689. (def.typ=classrefdef) and
  4690. is_objcclass(tclassrefdef(def).pointeddef);
  4691. end;
  4692. function is_objcprotocol(def: tdef): boolean;
  4693. begin
  4694. result:=
  4695. assigned(def) and
  4696. (def.typ=objectdef) and
  4697. (tobjectdef(def).objecttype=odt_objcprotocol);
  4698. end;
  4699. function is_objccategory(def: tdef): boolean;
  4700. begin
  4701. result:=
  4702. assigned(def) and
  4703. (def.typ=objectdef) and
  4704. { if used as a forward type }
  4705. ((tobjectdef(def).objecttype=odt_objccategory) or
  4706. { if used as after it has been resolved }
  4707. ((tobjectdef(def).objecttype=odt_objcclass) and
  4708. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4709. end;
  4710. function is_objc_class_or_protocol(def: tdef): boolean;
  4711. begin
  4712. result:=
  4713. assigned(def) and
  4714. (def.typ=objectdef) and
  4715. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4716. end;
  4717. function is_objc_protocol_or_category(def: tdef): boolean;
  4718. begin
  4719. result:=
  4720. assigned(def) and
  4721. (def.typ=objectdef) and
  4722. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4723. ((tobjectdef(def).objecttype = odt_objcclass) and
  4724. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4725. end;
  4726. function is_class_or_interface(def: tdef): boolean;
  4727. begin
  4728. result:=
  4729. assigned(def) and
  4730. (def.typ=objectdef) and
  4731. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4732. end;
  4733. function is_class_or_interface_or_objc(def: tdef): boolean;
  4734. begin
  4735. result:=
  4736. assigned(def) and
  4737. (def.typ=objectdef) and
  4738. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4739. end;
  4740. function is_class_or_interface_or_object(def: tdef): boolean;
  4741. begin
  4742. result:=
  4743. assigned(def) and
  4744. (def.typ=objectdef) and
  4745. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4746. end;
  4747. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4748. begin
  4749. result:=
  4750. assigned(def) and
  4751. (def.typ=objectdef) and
  4752. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4753. end;
  4754. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  4755. begin
  4756. result:=
  4757. assigned(def) and
  4758. (def.typ=objectdef) and
  4759. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4760. end;
  4761. function is_class_or_object(def: tdef): boolean;
  4762. begin
  4763. result:=
  4764. assigned(def) and
  4765. (def.typ=objectdef) and
  4766. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  4767. end;
  4768. function is_record(def: tdef): boolean;
  4769. begin
  4770. result:=
  4771. assigned(def) and
  4772. (def.typ=recorddef);
  4773. end;
  4774. procedure loadobjctypes;
  4775. begin
  4776. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  4777. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  4778. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  4779. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  4780. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  4781. end;
  4782. procedure maybeloadcocoatypes;
  4783. var
  4784. tsym: ttypesym;
  4785. begin
  4786. if assigned(objc_fastenumeration) then
  4787. exit;
  4788. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONPROTOCOL',false);
  4789. if assigned(tsym) then
  4790. objc_fastenumeration:=tobjectdef(tsym.typedef)
  4791. else
  4792. objc_fastenumeration:=nil;
  4793. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONSTATE',false);
  4794. if assigned(tsym) then
  4795. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  4796. else
  4797. objc_fastenumerationstate:=nil;
  4798. end;
  4799. function use_vectorfpu(def : tdef) : boolean;
  4800. begin
  4801. {$ifdef x86}
  4802. {$define use_vectorfpuimplemented}
  4803. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  4804. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  4805. {$endif x86}
  4806. {$ifdef arm}
  4807. {$define use_vectorfpuimplemented}
  4808. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  4809. {$endif arm}
  4810. {$ifndef use_vectorfpuimplemented}
  4811. use_vectorfpu:=false;
  4812. {$endif}
  4813. end;
  4814. end.