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. { tabstractrecorddef }
  151. tabstractrecorddef= class(tstoreddef)
  152. objname,
  153. objrealname: PShortString;
  154. symtable : TSymtable;
  155. cloneddef : tabstractrecorddef;
  156. cloneddefderef : tderef;
  157. constructor create(const n:string; dt:tdeftyp);
  158. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  159. procedure ppuwrite(ppufile:tcompilerppufile);override;
  160. destructor destroy; override;
  161. function GetSymtable(t:tGetSymtable):TSymtable;override;
  162. function is_packed:boolean;
  163. function RttiName: string;
  164. end;
  165. trecorddef = class(tabstractrecorddef)
  166. public
  167. isunion : boolean;
  168. constructor create(const n:string; p:TSymtable);
  169. constructor ppuload(ppufile:tcompilerppufile);
  170. destructor destroy;override;
  171. function getcopy : tstoreddef;override;
  172. procedure ppuwrite(ppufile:tcompilerppufile);override;
  173. procedure buildderef;override;
  174. procedure deref;override;
  175. function size:aint;override;
  176. function alignment : shortint;override;
  177. function padalignment: shortint;
  178. function GetTypeName:string;override;
  179. { debug }
  180. function needs_inittable : boolean;override;
  181. end;
  182. tprocdef = class;
  183. tobjectdef = class;
  184. { TImplementedInterface }
  185. TImplementedInterface = class
  186. IntfDef : tobjectdef;
  187. IntfDefDeref : tderef;
  188. IType : tinterfaceentrytype;
  189. IOffset : longint;
  190. VtblImplIntf : TImplementedInterface;
  191. NameMappings : TFPHashList;
  192. ProcDefs : TFPObjectList;
  193. ImplementsGetter : tsym;
  194. constructor create(aintf: tobjectdef);
  195. constructor create_deref(d:tderef);
  196. destructor destroy; override;
  197. function getcopy:TImplementedInterface;
  198. procedure buildderef;
  199. procedure deref;
  200. procedure AddMapping(const origname, newname: string);
  201. function GetMapping(const origname: string):string;
  202. procedure AddImplProc(pd:tprocdef);
  203. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  204. end;
  205. { tvmtentry }
  206. tvmtentry = record
  207. procdef : tprocdef;
  208. procdefderef : tderef;
  209. visibility : tvisibility;
  210. end;
  211. pvmtentry = ^tvmtentry;
  212. { tobjectdef }
  213. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  214. pmvcallstaticinfo = ^tmvcallstaticinfo;
  215. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  216. tobjectdef = class(tabstractrecorddef)
  217. private
  218. fcurrent_dispid: longint;
  219. public
  220. dwarf_struct_lab : tasmsymbol;
  221. childof : tobjectdef;
  222. childofderef : tderef;
  223. { for C++ classes: name of the library this class is imported from }
  224. import_lib,
  225. { for Objective-C: protocols and classes can have the same name there }
  226. objextname : pshortstring;
  227. objectoptions : tobjectoptions;
  228. { to be able to have a variable vmt position }
  229. { and no vmt field for objects without virtuals }
  230. vmtentries : TFPList;
  231. vmcallstaticinfo : pmvcallstaticinfo;
  232. vmt_offset : longint;
  233. objecttype : tobjecttyp;
  234. iidguid : pguid;
  235. iidstr : pshortstring;
  236. writing_class_record_dbginfo,
  237. { a class of this type has been created in this module }
  238. created_in_current_module,
  239. { a loadvmtnode for this class has been created in this
  240. module, so if a classrefdef variable of this or a parent
  241. class is used somewhere to instantiate a class, then this
  242. class may be instantiated
  243. }
  244. maybe_created_in_current_module,
  245. { a "class of" this particular class has been created in
  246. this module
  247. }
  248. classref_created_in_current_module : boolean;
  249. { store implemented interfaces defs and name mappings }
  250. ImplementedInterfaces : TFPObjectList;
  251. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);
  252. constructor ppuload(ppufile:tcompilerppufile);
  253. destructor destroy;override;
  254. function getcopy : tstoreddef;override;
  255. procedure ppuwrite(ppufile:tcompilerppufile);override;
  256. function GetTypeName:string;override;
  257. procedure buildderef;override;
  258. procedure deref;override;
  259. procedure buildderefimpl;override;
  260. procedure derefimpl;override;
  261. procedure resetvmtentries;
  262. procedure copyvmtentries(objdef:tobjectdef);
  263. function getparentdef:tdef;override;
  264. function size : aint;override;
  265. function alignment:shortint;override;
  266. function vmtmethodoffset(index:longint):longint;
  267. function members_need_inittable : boolean;
  268. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  269. { this should be called when this class implements an interface }
  270. procedure prepareguid;
  271. function is_publishable : boolean;override;
  272. function is_related(d : tdef) : boolean;override;
  273. function needs_inittable : boolean;override;
  274. function rtti_mangledname(rt:trttitype):string;override;
  275. function vmt_mangledname : string;
  276. procedure check_forwards;
  277. procedure insertvmt;
  278. procedure set_parent(c : tobjectdef);
  279. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  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. end;
  2204. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2205. begin
  2206. inherited ppuload(dt,ppufile);
  2207. objrealname:=stringdup(ppufile.getstring);
  2208. objname:=stringdup(upper(objrealname^));
  2209. end;
  2210. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2211. begin
  2212. inherited ppuwrite(ppufile);
  2213. ppufile.putstring(objrealname^);
  2214. end;
  2215. destructor tabstractrecorddef.destroy;
  2216. begin
  2217. stringdispose(objname);
  2218. stringdispose(objrealname);
  2219. inherited destroy;
  2220. end;
  2221. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2222. begin
  2223. if t=gs_record then
  2224. GetSymtable:=symtable
  2225. else
  2226. GetSymtable:=nil;
  2227. end;
  2228. function tabstractrecorddef.is_packed:boolean;
  2229. begin
  2230. result:=tabstractrecordsymtable(symtable).is_packed;
  2231. end;
  2232. function tabstractrecorddef.RttiName: string;
  2233. var
  2234. tmp: tabstractrecorddef;
  2235. begin
  2236. Result:=objrealname^;
  2237. tmp:=self;
  2238. repeat
  2239. if tmp.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  2240. tmp:=tabstractrecorddef(tmp.owner.defowner)
  2241. else
  2242. break;
  2243. Result:=tmp.objrealname^+'.'+Result;
  2244. until tmp=nil;
  2245. end;
  2246. {***************************************************************************
  2247. trecorddef
  2248. ***************************************************************************}
  2249. constructor trecorddef.create(const n:string; p:TSymtable);
  2250. begin
  2251. inherited create(n,recorddef);
  2252. symtable:=p;
  2253. { we can own the symtable only if nobody else owns a copy so far }
  2254. if symtable.refcount=1 then
  2255. symtable.defowner:=self;
  2256. isunion:=false;
  2257. end;
  2258. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2259. begin
  2260. inherited ppuload(recorddef,ppufile);
  2261. if df_copied_def in defoptions then
  2262. ppufile.getderef(cloneddefderef)
  2263. else
  2264. begin
  2265. symtable:=trecordsymtable.create(objrealname^,0);
  2266. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2267. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2268. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2269. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2270. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2271. trecordsymtable(symtable).ppuload(ppufile);
  2272. { requires usefieldalignment to be set }
  2273. symtable.defowner:=self;
  2274. end;
  2275. isunion:=false;
  2276. end;
  2277. destructor trecorddef.destroy;
  2278. begin
  2279. if assigned(symtable) then
  2280. begin
  2281. symtable.free;
  2282. symtable:=nil;
  2283. end;
  2284. inherited destroy;
  2285. end;
  2286. function trecorddef.getcopy : tstoreddef;
  2287. begin
  2288. result:=trecorddef.create(objrealname^,symtable.getcopy);
  2289. trecorddef(result).isunion:=isunion;
  2290. include(trecorddef(result).defoptions,df_copied_def);
  2291. end;
  2292. function trecorddef.needs_inittable : boolean;
  2293. begin
  2294. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2295. end;
  2296. procedure trecorddef.buildderef;
  2297. begin
  2298. inherited buildderef;
  2299. if df_copied_def in defoptions then
  2300. cloneddefderef.build(symtable.defowner)
  2301. else
  2302. tstoredsymtable(symtable).buildderef;
  2303. end;
  2304. procedure trecorddef.deref;
  2305. begin
  2306. inherited deref;
  2307. { now dereference the definitions }
  2308. if df_copied_def in defoptions then
  2309. begin
  2310. cloneddef:=trecorddef(cloneddefderef.resolve);
  2311. symtable:=cloneddef.symtable.getcopy;
  2312. end
  2313. else
  2314. tstoredsymtable(symtable).deref;
  2315. { assign TGUID? load only from system unit }
  2316. if not(assigned(rec_tguid)) and
  2317. (upper(typename)='TGUID') and
  2318. assigned(owner) and
  2319. assigned(owner.name) and
  2320. (owner.name^='SYSTEM') then
  2321. rec_tguid:=self;
  2322. { assign JMP_BUF? load only from system unit }
  2323. if not(assigned(rec_jmp_buf)) and
  2324. (upper(typename)='JMP_BUF') and
  2325. assigned(owner) and
  2326. assigned(owner.name) and
  2327. (owner.name^='SYSTEM') then
  2328. rec_jmp_buf:=self;
  2329. end;
  2330. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2331. begin
  2332. inherited ppuwrite(ppufile);
  2333. if df_copied_def in defoptions then
  2334. ppufile.putderef(cloneddefderef)
  2335. else
  2336. begin
  2337. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2338. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2339. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2340. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2341. ppufile.putaint(trecordsymtable(symtable).datasize);
  2342. end;
  2343. ppufile.writeentry(ibrecorddef);
  2344. if not(df_copied_def in defoptions) then
  2345. trecordsymtable(symtable).ppuwrite(ppufile);
  2346. end;
  2347. function trecorddef.size:aint;
  2348. begin
  2349. result:=trecordsymtable(symtable).datasize;
  2350. end;
  2351. function trecorddef.alignment:shortint;
  2352. begin
  2353. alignment:=trecordsymtable(symtable).recordalignment;
  2354. end;
  2355. function trecorddef.padalignment:shortint;
  2356. begin
  2357. padalignment := trecordsymtable(symtable).padalignment;
  2358. end;
  2359. function trecorddef.GetTypeName : string;
  2360. begin
  2361. GetTypeName:='<record type>'
  2362. end;
  2363. {***************************************************************************
  2364. TABSTRACTPROCDEF
  2365. ***************************************************************************}
  2366. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2367. begin
  2368. inherited create(dt);
  2369. parast:=tparasymtable.create(self,level);
  2370. paras:=nil;
  2371. minparacount:=0;
  2372. maxparacount:=0;
  2373. proctypeoption:=potype_none;
  2374. proccalloption:=pocall_none;
  2375. procoptions:=[];
  2376. returndef:=voidtype;
  2377. savesize:=sizeof(pint);
  2378. callerargareasize:=0;
  2379. calleeargareasize:=0;
  2380. has_paraloc_info:=callnoside;
  2381. funcretloc[callerside].init;
  2382. funcretloc[calleeside].init;
  2383. check_mark_as_nested;
  2384. end;
  2385. destructor tabstractprocdef.destroy;
  2386. begin
  2387. if assigned(paras) then
  2388. begin
  2389. {$ifdef MEMDEBUG}
  2390. memprocpara.start;
  2391. {$endif MEMDEBUG}
  2392. paras.free;
  2393. paras:=nil;
  2394. {$ifdef MEMDEBUG}
  2395. memprocpara.stop;
  2396. {$endif MEMDEBUG}
  2397. end;
  2398. if assigned(parast) then
  2399. begin
  2400. {$ifdef MEMDEBUG}
  2401. memprocparast.start;
  2402. {$endif MEMDEBUG}
  2403. parast.free;
  2404. parast:=nil;
  2405. {$ifdef MEMDEBUG}
  2406. memprocparast.stop;
  2407. {$endif MEMDEBUG}
  2408. end;
  2409. funcretloc[callerside].done;
  2410. funcretloc[calleeside].done;
  2411. inherited destroy;
  2412. end;
  2413. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2414. begin
  2415. if (tsym(p).typ<>paravarsym) then
  2416. exit;
  2417. inc(plongint(arg)^);
  2418. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2419. begin
  2420. if not assigned(tparavarsym(p).defaultconstsym) then
  2421. inc(minparacount);
  2422. inc(maxparacount);
  2423. end;
  2424. end;
  2425. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2426. begin
  2427. if (tsym(p).typ<>paravarsym) then
  2428. exit;
  2429. paras.add(p);
  2430. end;
  2431. procedure tabstractprocdef.calcparas;
  2432. var
  2433. paracount : longint;
  2434. begin
  2435. { This can already be assigned when
  2436. we need to reresolve this unit (PFV) }
  2437. if assigned(paras) then
  2438. paras.free;
  2439. paras:=tparalist.create(false);
  2440. paracount:=0;
  2441. minparacount:=0;
  2442. maxparacount:=0;
  2443. parast.SymList.ForEachCall(@count_para,@paracount);
  2444. paras.capacity:=paracount;
  2445. { Insert parameters in table }
  2446. parast.SymList.ForEachCall(@insert_para,nil);
  2447. { Order parameters }
  2448. paras.sortparas;
  2449. end;
  2450. procedure tabstractprocdef.buildderef;
  2451. begin
  2452. { released procdef? }
  2453. if not assigned(parast) then
  2454. exit;
  2455. inherited buildderef;
  2456. returndefderef.build(returndef);
  2457. { parast }
  2458. tparasymtable(parast).buildderef;
  2459. end;
  2460. procedure tabstractprocdef.deref;
  2461. begin
  2462. inherited deref;
  2463. returndef:=tdef(returndefderef.resolve);
  2464. { parast }
  2465. tparasymtable(parast).deref;
  2466. { recalculated parameters }
  2467. calcparas;
  2468. end;
  2469. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2470. begin
  2471. inherited ppuload(dt,ppufile);
  2472. parast:=nil;
  2473. Paras:=nil;
  2474. minparacount:=0;
  2475. maxparacount:=0;
  2476. ppufile.getderef(returndefderef);
  2477. { TODO: remove fpu_used loading}
  2478. ppufile.getbyte;
  2479. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2480. proccalloption:=tproccalloption(ppufile.getbyte);
  2481. ppufile.getnormalset(procoptions);
  2482. funcretloc[callerside].init;
  2483. if po_explicitparaloc in procoptions then
  2484. funcretloc[callerside].ppuload(ppufile);
  2485. savesize:=sizeof(pint);
  2486. if (po_explicitparaloc in procoptions) then
  2487. has_paraloc_info:=callerside;
  2488. end;
  2489. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2490. var
  2491. oldintfcrc : boolean;
  2492. begin
  2493. { released procdef? }
  2494. if not assigned(parast) then
  2495. exit;
  2496. inherited ppuwrite(ppufile);
  2497. ppufile.putderef(returndefderef);
  2498. oldintfcrc:=ppufile.do_interface_crc;
  2499. ppufile.do_interface_crc:=false;
  2500. ppufile.putbyte(0);
  2501. ppufile.putbyte(ord(proctypeoption));
  2502. ppufile.putbyte(ord(proccalloption));
  2503. ppufile.putnormalset(procoptions);
  2504. ppufile.do_interface_crc:=oldintfcrc;
  2505. if (po_explicitparaloc in procoptions) then
  2506. funcretloc[callerside].ppuwrite(ppufile);
  2507. end;
  2508. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2509. var
  2510. hs,s : string;
  2511. hp : TParavarsym;
  2512. hpc : tconstsym;
  2513. first : boolean;
  2514. i : integer;
  2515. begin
  2516. s:='';
  2517. first:=true;
  2518. for i:=0 to paras.count-1 do
  2519. begin
  2520. hp:=tparavarsym(paras[i]);
  2521. if not(vo_is_hidden_para in hp.varoptions) or
  2522. (showhidden) then
  2523. begin
  2524. if first then
  2525. begin
  2526. s:=s+'(';
  2527. first:=false;
  2528. end
  2529. else
  2530. s:=s+',';
  2531. if vo_is_hidden_para in hp.varoptions then
  2532. s:=s+'<';
  2533. case hp.varspez of
  2534. vs_var :
  2535. s:=s+'var ';
  2536. vs_const :
  2537. s:=s+'const ';
  2538. vs_out :
  2539. s:=s+'out ';
  2540. vs_constref :
  2541. s:=s+'constref ';
  2542. end;
  2543. if hp.univpara then
  2544. s:=s+'univ ';
  2545. if assigned(hp.vardef.typesym) then
  2546. begin
  2547. hs:=hp.vardef.typesym.realname;
  2548. if hs[1]<>'$' then
  2549. s:=s+hs
  2550. else
  2551. s:=s+hp.vardef.GetTypeName;
  2552. end
  2553. else
  2554. s:=s+hp.vardef.GetTypeName;
  2555. { default value }
  2556. if assigned(hp.defaultconstsym) then
  2557. begin
  2558. hpc:=tconstsym(hp.defaultconstsym);
  2559. hs:='';
  2560. case hpc.consttyp of
  2561. conststring,
  2562. constresourcestring :
  2563. begin
  2564. If hpc.value.len>0 then
  2565. begin
  2566. setLength(hs,hpc.value.len);
  2567. { don't write past the end of hs if the constant
  2568. is > 255 chars }
  2569. move(hpc.value.valueptr^,hs[1],length(hs));
  2570. { make sure that constant strings with newline chars
  2571. don't create a linebreak in the assembler code,
  2572. since comments are line-based. Also remove nulls
  2573. because the comments are written as a pchar. }
  2574. ReplaceCase(hs,#0,'.');
  2575. ReplaceCase(hs,#10,'.');
  2576. ReplaceCase(hs,#13,'.');
  2577. end;
  2578. end;
  2579. constreal :
  2580. str(pbestreal(hpc.value.valueptr)^,hs);
  2581. constpointer :
  2582. hs:=tostr(hpc.value.valueordptr);
  2583. constord :
  2584. begin
  2585. if is_boolean(hpc.constdef) then
  2586. begin
  2587. if hpc.value.valueord<>0 then
  2588. hs:='TRUE'
  2589. else
  2590. hs:='FALSE';
  2591. end
  2592. else
  2593. hs:=tostr(hpc.value.valueord);
  2594. end;
  2595. constnil :
  2596. hs:='nil';
  2597. constset :
  2598. hs:='<set>';
  2599. end;
  2600. if hs<>'' then
  2601. s:=s+'="'+hs+'"';
  2602. end;
  2603. if vo_is_hidden_para in hp.varoptions then
  2604. s:=s+'>';
  2605. end;
  2606. end;
  2607. if not first then
  2608. s:=s+')';
  2609. if (po_varargs in procoptions) then
  2610. s:=s+';VarArgs';
  2611. typename_paras:=s;
  2612. end;
  2613. function tabstractprocdef.is_methodpointer:boolean;
  2614. begin
  2615. result:=false;
  2616. end;
  2617. function tabstractprocdef.is_addressonly:boolean;
  2618. begin
  2619. result:=true;
  2620. end;
  2621. function tabstractprocdef.no_self_node: boolean;
  2622. begin
  2623. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  2624. (proctypeoption in [potype_class_constructor,potype_class_destructor]);
  2625. end;
  2626. procedure tabstractprocdef.check_mark_as_nested;
  2627. begin
  2628. { nested procvars require that nested functions use the Delphi-style
  2629. nested procedure calling convention }
  2630. if (parast.symtablelevel>normal_function_level) and
  2631. (m_nested_procvars in current_settings.modeswitches) then
  2632. include(procoptions,po_delphi_nested_cc);
  2633. end;
  2634. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  2635. begin
  2636. if (side in [callerside,callbothsides]) and
  2637. not(has_paraloc_info in [callerside,callbothsides]) then
  2638. begin
  2639. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  2640. if has_paraloc_info in [calleeside,callbothsides] then
  2641. has_paraloc_info:=callbothsides
  2642. else
  2643. has_paraloc_info:=callerside;
  2644. end;
  2645. if (side in [calleeside,callbothsides]) and
  2646. not(has_paraloc_info in [calleeside,callbothsides]) then
  2647. begin
  2648. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  2649. if has_paraloc_info in [callerside,callbothsides] then
  2650. has_paraloc_info:=callbothsides
  2651. else
  2652. has_paraloc_info:=calleeside;
  2653. end;
  2654. end;
  2655. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  2656. var
  2657. p: tparavarsym;
  2658. ploc: PCGParalocation;
  2659. i: longint;
  2660. begin
  2661. result:=false;
  2662. init_paraloc_info(side);
  2663. for i:=0 to parast.SymList.Count-1 do
  2664. if tsym(parast.SymList[i]).typ=paravarsym then
  2665. begin
  2666. p:=tparavarsym(parast.SymList[i]);
  2667. { check if no parameter is located on the stack }
  2668. if is_open_array(p.vardef) or
  2669. is_array_of_const(p.vardef) then
  2670. begin
  2671. result:=true;
  2672. exit;
  2673. end;
  2674. ploc:=p.paraloc[side].location;
  2675. while assigned(ploc) do
  2676. begin
  2677. if (ploc^.loc=LOC_REFERENCE) then
  2678. begin
  2679. result:=true;
  2680. exit
  2681. end;
  2682. ploc:=ploc^.next;
  2683. end;
  2684. end;
  2685. end;
  2686. {***************************************************************************
  2687. TPROCDEF
  2688. ***************************************************************************}
  2689. constructor tprocdef.create(level:byte);
  2690. begin
  2691. inherited create(procdef,level);
  2692. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2693. _mangledname:=nil;
  2694. fileinfo:=current_filepos;
  2695. extnumber:=$ffff;
  2696. aliasnames:=TCmdStrList.create;
  2697. funcretsym:=nil;
  2698. forwarddef:=true;
  2699. interfacedef:=false;
  2700. hasforward:=false;
  2701. struct := nil;
  2702. import_dll:=nil;
  2703. import_name:=nil;
  2704. import_nr:=0;
  2705. inlininginfo:=nil;
  2706. deprecatedmsg:=nil;
  2707. {$ifdef i386}
  2708. fpu_used:=maxfpuregs;
  2709. {$endif i386}
  2710. end;
  2711. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2712. var
  2713. i,aliasnamescount : longint;
  2714. level : byte;
  2715. begin
  2716. inherited ppuload(procdef,ppufile);
  2717. if po_has_mangledname in procoptions then
  2718. _mangledname:=stringdup(ppufile.getstring)
  2719. else
  2720. _mangledname:=nil;
  2721. extnumber:=ppufile.getword;
  2722. level:=ppufile.getbyte;
  2723. ppufile.getderef(structderef);
  2724. ppufile.getderef(procsymderef);
  2725. ppufile.getposinfo(fileinfo);
  2726. visibility:=tvisibility(ppufile.getbyte);
  2727. ppufile.getsmallset(symoptions);
  2728. if sp_has_deprecated_msg in symoptions then
  2729. deprecatedmsg:=stringdup(ppufile.getstring)
  2730. else
  2731. deprecatedmsg:=nil;
  2732. {$ifdef powerpc}
  2733. { library symbol for AmigaOS/MorphOS }
  2734. ppufile.getderef(libsymderef);
  2735. {$endif powerpc}
  2736. { import stuff }
  2737. if po_has_importdll in procoptions then
  2738. import_dll:=stringdup(ppufile.getstring)
  2739. else
  2740. import_dll:=nil;
  2741. if po_has_importname in procoptions then
  2742. import_name:=stringdup(ppufile.getstring)
  2743. else
  2744. import_name:=nil;
  2745. import_nr:=ppufile.getword;
  2746. if (po_msgint in procoptions) then
  2747. messageinf.i:=ppufile.getlongint;
  2748. if (po_msgstr in procoptions) then
  2749. messageinf.str:=stringdup(ppufile.getstring);
  2750. if (po_dispid in procoptions) then
  2751. dispid:=ppufile.getlongint;
  2752. { inline stuff }
  2753. if (po_has_inlininginfo in procoptions) then
  2754. begin
  2755. ppufile.getderef(funcretsymderef);
  2756. new(inlininginfo);
  2757. ppufile.getsmallset(inlininginfo^.flags);
  2758. end
  2759. else
  2760. begin
  2761. inlininginfo:=nil;
  2762. funcretsym:=nil;
  2763. end;
  2764. aliasnames:=TCmdStrList.create;
  2765. { count alias names }
  2766. aliasnamescount:=ppufile.getbyte;
  2767. for i:=1 to aliasnamescount do
  2768. aliasnames.insert(ppufile.getstring);
  2769. { load para symtable }
  2770. parast:=tparasymtable.create(self,level);
  2771. tparasymtable(parast).ppuload(ppufile);
  2772. { load local symtable }
  2773. if (po_has_inlininginfo in procoptions) then
  2774. begin
  2775. localst:=tlocalsymtable.create(self,level);
  2776. tlocalsymtable(localst).ppuload(ppufile);
  2777. end
  2778. else
  2779. localst:=nil;
  2780. { inline stuff }
  2781. if (po_has_inlininginfo in procoptions) then
  2782. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2783. { default values for no persistent data }
  2784. if (cs_link_deffile in current_settings.globalswitches) and
  2785. (tf_need_export in target_info.flags) and
  2786. (po_exports in procoptions) then
  2787. deffile.AddExport(mangledname);
  2788. forwarddef:=false;
  2789. interfacedef:=false;
  2790. hasforward:=false;
  2791. { Disable po_has_inlining until the derefimpl is done }
  2792. exclude(procoptions,po_has_inlininginfo);
  2793. {$ifdef i386}
  2794. fpu_used:=maxfpuregs;
  2795. {$endif i386}
  2796. end;
  2797. destructor tprocdef.destroy;
  2798. begin
  2799. aliasnames.free;
  2800. aliasnames:=nil;
  2801. if assigned(localst) and
  2802. (localst.symtabletype<>staticsymtable) then
  2803. begin
  2804. {$ifdef MEMDEBUG}
  2805. memproclocalst.start;
  2806. {$endif MEMDEBUG}
  2807. localst.free;
  2808. localst:=nil;
  2809. {$ifdef MEMDEBUG}
  2810. memproclocalst.start;
  2811. {$endif MEMDEBUG}
  2812. end;
  2813. if assigned(inlininginfo) then
  2814. begin
  2815. {$ifdef MEMDEBUG}
  2816. memprocnodetree.start;
  2817. {$endif MEMDEBUG}
  2818. tnode(inlininginfo^.code).free;
  2819. {$ifdef MEMDEBUG}
  2820. memprocnodetree.start;
  2821. {$endif MEMDEBUG}
  2822. dispose(inlininginfo);
  2823. inlininginfo:=nil;
  2824. end;
  2825. stringdispose(resultname);
  2826. stringdispose(import_dll);
  2827. stringdispose(import_name);
  2828. stringdispose(deprecatedmsg);
  2829. if (po_msgstr in procoptions) then
  2830. stringdispose(messageinf.str);
  2831. if assigned(_mangledname) then
  2832. begin
  2833. {$ifdef MEMDEBUG}
  2834. memmanglednames.start;
  2835. {$endif MEMDEBUG}
  2836. stringdispose(_mangledname);
  2837. {$ifdef MEMDEBUG}
  2838. memmanglednames.stop;
  2839. {$endif MEMDEBUG}
  2840. end;
  2841. inherited destroy;
  2842. end;
  2843. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2844. var
  2845. oldintfcrc : boolean;
  2846. aliasnamescount : longint;
  2847. item : TCmdStrListItem;
  2848. begin
  2849. { released procdef? }
  2850. if not assigned(parast) then
  2851. exit;
  2852. inherited ppuwrite(ppufile);
  2853. if po_has_mangledname in procoptions then
  2854. ppufile.putstring(_mangledname^);
  2855. ppufile.putword(extnumber);
  2856. ppufile.putbyte(parast.symtablelevel);
  2857. ppufile.putderef(structderef);
  2858. ppufile.putderef(procsymderef);
  2859. ppufile.putposinfo(fileinfo);
  2860. ppufile.putbyte(byte(visibility));
  2861. ppufile.putsmallset(symoptions);
  2862. if sp_has_deprecated_msg in symoptions then
  2863. ppufile.putstring(deprecatedmsg^);
  2864. {$ifdef powerpc}
  2865. { library symbol for AmigaOS/MorphOS }
  2866. ppufile.putderef(libsymderef);
  2867. {$endif powerpc}
  2868. { import }
  2869. if po_has_importdll in procoptions then
  2870. ppufile.putstring(import_dll^);
  2871. if po_has_importname in procoptions then
  2872. ppufile.putstring(import_name^);
  2873. ppufile.putword(import_nr);
  2874. if (po_msgint in procoptions) then
  2875. ppufile.putlongint(messageinf.i);
  2876. if (po_msgstr in procoptions) then
  2877. ppufile.putstring(messageinf.str^);
  2878. if (po_dispid in procoptions) then
  2879. ppufile.putlongint(dispid);
  2880. { inline stuff }
  2881. oldintfcrc:=ppufile.do_crc;
  2882. ppufile.do_crc:=false;
  2883. if (po_has_inlininginfo in procoptions) then
  2884. begin
  2885. ppufile.putderef(funcretsymderef);
  2886. ppufile.putsmallset(inlininginfo^.flags);
  2887. end;
  2888. { count alias names }
  2889. aliasnamescount:=0;
  2890. item:=TCmdStrListItem(aliasnames.first);
  2891. while assigned(item) do
  2892. begin
  2893. inc(aliasnamescount);
  2894. item:=TCmdStrListItem(item.next);
  2895. end;
  2896. if aliasnamescount>255 then
  2897. internalerror(200711021);
  2898. ppufile.putbyte(aliasnamescount);
  2899. item:=TCmdStrListItem(aliasnames.first);
  2900. while assigned(item) do
  2901. begin
  2902. ppufile.putstring(item.str);
  2903. item:=TCmdStrListItem(item.next);
  2904. end;
  2905. ppufile.do_crc:=oldintfcrc;
  2906. { write this entry }
  2907. ppufile.writeentry(ibprocdef);
  2908. { Save the para symtable, this is taken from the interface }
  2909. tparasymtable(parast).ppuwrite(ppufile);
  2910. { save localsymtable for inline procedures or when local
  2911. browser info is requested, this has no influence on the crc }
  2912. if (po_has_inlininginfo in procoptions) then
  2913. begin
  2914. oldintfcrc:=ppufile.do_crc;
  2915. ppufile.do_crc:=false;
  2916. tlocalsymtable(localst).ppuwrite(ppufile);
  2917. ppufile.do_crc:=oldintfcrc;
  2918. end;
  2919. { node tree for inlining }
  2920. oldintfcrc:=ppufile.do_crc;
  2921. ppufile.do_crc:=false;
  2922. if (po_has_inlininginfo in procoptions) then
  2923. ppuwritenodetree(ppufile,inlininginfo^.code);
  2924. ppufile.do_crc:=oldintfcrc;
  2925. end;
  2926. function tprocdef.fullprocname(showhidden:boolean):string;
  2927. var
  2928. s : string;
  2929. t : ttoken;
  2930. begin
  2931. {$ifdef EXTDEBUG}
  2932. showhidden:=true;
  2933. {$endif EXTDEBUG}
  2934. s:='';
  2935. if assigned(struct) then
  2936. begin
  2937. s:=struct.RttiName+'.';
  2938. if (po_classmethod in procoptions) and
  2939. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2940. s:='class ' + s;
  2941. end;
  2942. if proctypeoption=potype_operator then
  2943. begin
  2944. for t:=NOTOKEN to last_overloaded do
  2945. if procsym.realname='$'+overloaded_names[t] then
  2946. begin
  2947. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2948. break;
  2949. end;
  2950. end
  2951. else
  2952. s:=s+procsym.realname+typename_paras(showhidden);
  2953. case proctypeoption of
  2954. potype_constructor:
  2955. s:='constructor '+s;
  2956. potype_destructor:
  2957. s:='destructor '+s;
  2958. potype_class_constructor:
  2959. s:='class constructor '+s;
  2960. potype_class_destructor:
  2961. s:='class destructor '+s;
  2962. else
  2963. if assigned(returndef) and
  2964. not(is_void(returndef)) then
  2965. s:=s+':'+returndef.GetTypeName;
  2966. end;
  2967. if owner.symtabletype=localsymtable then
  2968. s:=s+' is nested';
  2969. s:=s+';';
  2970. { forced calling convention? }
  2971. if (po_hascallingconvention in procoptions) then
  2972. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2973. if (po_staticmethod in procoptions) and
  2974. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2975. s:=s+' Static;';
  2976. fullprocname:=s;
  2977. end;
  2978. function tprocdef.is_methodpointer:boolean;
  2979. begin
  2980. { don't check assigned(_class), that's also the case for nested
  2981. procedures inside methods }
  2982. result:=owner.symtabletype=ObjectSymtable;
  2983. end;
  2984. function tprocdef.is_addressonly:boolean;
  2985. begin
  2986. result:=assigned(owner) and
  2987. (owner.symtabletype<>ObjectSymtable) and
  2988. (not(m_nested_procvars in current_settings.modeswitches) or
  2989. not is_nested_pd(self));
  2990. end;
  2991. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2992. begin
  2993. case t of
  2994. gs_local :
  2995. GetSymtable:=localst;
  2996. gs_para :
  2997. GetSymtable:=parast;
  2998. else
  2999. GetSymtable:=nil;
  3000. end;
  3001. end;
  3002. procedure tprocdef.buildderef;
  3003. begin
  3004. inherited buildderef;
  3005. structderef.build(struct);
  3006. { procsym that originaly defined this definition, should be in the
  3007. same symtable }
  3008. procsymderef.build(procsym);
  3009. {$ifdef powerpc}
  3010. { library symbol for AmigaOS/MorphOS }
  3011. libsymderef.build(libsym);
  3012. {$endif powerpc}
  3013. end;
  3014. procedure tprocdef.buildderefimpl;
  3015. begin
  3016. inherited buildderefimpl;
  3017. { Localst is not available for main/unit init }
  3018. if assigned(localst) then
  3019. begin
  3020. tlocalsymtable(localst).buildderef;
  3021. tlocalsymtable(localst).buildderefimpl;
  3022. end;
  3023. { inline tree }
  3024. if (po_has_inlininginfo in procoptions) then
  3025. begin
  3026. funcretsymderef.build(funcretsym);
  3027. inlininginfo^.code.buildderefimpl;
  3028. end;
  3029. end;
  3030. procedure tprocdef.deref;
  3031. begin
  3032. inherited deref;
  3033. struct:=tabstractrecorddef(structderef.resolve);
  3034. { procsym that originaly defined this definition, should be in the
  3035. same symtable }
  3036. procsym:=tprocsym(procsymderef.resolve);
  3037. {$ifdef powerpc}
  3038. { library symbol for AmigaOS/MorphOS }
  3039. libsym:=tsym(libsymderef.resolve);
  3040. {$endif powerpc}
  3041. end;
  3042. procedure tprocdef.derefimpl;
  3043. begin
  3044. { Enable has_inlininginfo when the inlininginfo
  3045. structure is available. The has_inlininginfo was disabled
  3046. after the load, since the data was invalid }
  3047. if assigned(inlininginfo) then
  3048. include(procoptions,po_has_inlininginfo);
  3049. { Locals }
  3050. if assigned(localst) then
  3051. begin
  3052. tlocalsymtable(localst).deref;
  3053. tlocalsymtable(localst).derefimpl;
  3054. end;
  3055. { Inline }
  3056. if (po_has_inlininginfo in procoptions) then
  3057. begin
  3058. inlininginfo^.code.derefimpl;
  3059. { funcretsym, this is always located in the localst }
  3060. funcretsym:=tsym(funcretsymderef.resolve);
  3061. end
  3062. else
  3063. begin
  3064. { safety }
  3065. { Not safe! A unit may be reresolved after its interface has been
  3066. parsed but before its implementation has been parsed, and in that
  3067. case the funcretsym is still required!
  3068. funcretsym:=nil; }
  3069. end;
  3070. end;
  3071. function tprocdef.GetTypeName : string;
  3072. begin
  3073. GetTypeName := FullProcName(false);
  3074. end;
  3075. function tprocdef.mangledname : string;
  3076. var
  3077. hp : TParavarsym;
  3078. hs : string;
  3079. crc : dword;
  3080. newlen,
  3081. oldlen,
  3082. i : integer;
  3083. begin
  3084. if assigned(_mangledname) then
  3085. begin
  3086. {$ifdef compress}
  3087. mangledname:=minilzw_decode(_mangledname^);
  3088. {$else}
  3089. mangledname:=_mangledname^;
  3090. {$endif}
  3091. exit;
  3092. end;
  3093. { we need to use the symtable where the procsym is inserted,
  3094. because that is visible to the world }
  3095. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3096. oldlen:=length(mangledname);
  3097. { add parameter types }
  3098. for i:=0 to paras.count-1 do
  3099. begin
  3100. hp:=tparavarsym(paras[i]);
  3101. if not(vo_is_hidden_para in hp.varoptions) then
  3102. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  3103. end;
  3104. { add resultdef, add $$ as separator to make it unique from a
  3105. parameter separator }
  3106. if not is_void(returndef) then
  3107. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  3108. newlen:=length(mangledname);
  3109. { Replace with CRC if the parameter line is very long }
  3110. if (newlen-oldlen>12) and
  3111. ((newlen>100) or (newlen-oldlen>64)) then
  3112. begin
  3113. crc:=0;
  3114. for i:=0 to paras.count-1 do
  3115. begin
  3116. hp:=tparavarsym(paras[i]);
  3117. if not(vo_is_hidden_para in hp.varoptions) then
  3118. begin
  3119. hs:=hp.vardef.mangledparaname;
  3120. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3121. end;
  3122. end;
  3123. hs:=hp.vardef.mangledparaname;
  3124. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3125. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3126. end;
  3127. {$ifdef compress}
  3128. _mangledname:=stringdup(minilzw_encode(mangledname));
  3129. {$else}
  3130. _mangledname:=stringdup(mangledname);
  3131. {$endif}
  3132. end;
  3133. function tprocdef.cplusplusmangledname : string;
  3134. function getcppparaname(p : tdef) : string;
  3135. const
  3136. {$ifdef NAMEMANGLING_GCC2}
  3137. ordtype2str : array[tordtype] of string[2] = (
  3138. '',
  3139. 'Uc','Us','Ui','Us',
  3140. 'Sc','s','i','x',
  3141. 'b','b','b','b','b',
  3142. 'c','w','x');
  3143. {$else NAMEMANGLING_GCC2}
  3144. ordtype2str : array[tordtype] of string[1] = (
  3145. 'v',
  3146. 'h','t','j','y',
  3147. 'a','s','i','x',
  3148. 'b','b','b','b','b',
  3149. 'c','w','x');
  3150. floattype2str : array[tfloattype] of string[1] = (
  3151. 'f','d','e','e',
  3152. 'd','d','g');
  3153. {$endif NAMEMANGLING_GCC2}
  3154. var
  3155. s : string;
  3156. begin
  3157. case p.typ of
  3158. orddef:
  3159. s:=ordtype2str[torddef(p).ordtype];
  3160. pointerdef:
  3161. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3162. {$ifndef NAMEMANGLING_GCC2}
  3163. floatdef:
  3164. s:=floattype2str[tfloatdef(p).floattype];
  3165. {$endif NAMEMANGLING_GCC2}
  3166. else
  3167. internalerror(2103001);
  3168. end;
  3169. getcppparaname:=s;
  3170. end;
  3171. var
  3172. s,s2 : string;
  3173. hp : TParavarsym;
  3174. i : integer;
  3175. begin
  3176. {$ifdef NAMEMANGLING_GCC2}
  3177. { outdated gcc 2.x name mangling scheme }
  3178. s := procsym.realname;
  3179. if procsym.owner.symtabletype=ObjectSymtable then
  3180. begin
  3181. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3182. case proctypeoption of
  3183. potype_destructor:
  3184. s:='_$_'+tostr(length(s2))+s2;
  3185. potype_constructor:
  3186. s:='___'+tostr(length(s2))+s2;
  3187. else
  3188. s:='_'+s+'__'+tostr(length(s2))+s2;
  3189. end;
  3190. end
  3191. else s:=s+'__';
  3192. s:=s+'F';
  3193. { concat modifiers }
  3194. { !!!!! }
  3195. { now we handle the parameters }
  3196. if maxparacount>0 then
  3197. begin
  3198. for i:=0 to paras.count-1 do
  3199. begin
  3200. hp:=tparavarsym(paras[i]);
  3201. { no hidden parameters form part of a C++ mangled name:
  3202. a) self is not included
  3203. b) there are no "high" or other hidden parameters
  3204. }
  3205. if vo_is_hidden_para in hp.varoptions then
  3206. continue;
  3207. s2:=getcppparaname(hp.vardef);
  3208. if hp.varspez in [vs_var,vs_out] then
  3209. s2:='R'+s2;
  3210. s:=s+s2;
  3211. end;
  3212. end
  3213. else
  3214. s:=s+'v';
  3215. cplusplusmangledname:=s;
  3216. {$else NAMEMANGLING_GCC2}
  3217. { gcc 3.x and 4.x name mangling scheme }
  3218. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3219. if procsym.owner.symtabletype=ObjectSymtable then
  3220. begin
  3221. s:='_ZN';
  3222. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3223. s:=s+tostr(length(s2))+s2;
  3224. case proctypeoption of
  3225. potype_constructor:
  3226. s:=s+'C1';
  3227. potype_destructor:
  3228. s:=s+'D1';
  3229. else
  3230. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3231. end;
  3232. s:=s+'E';
  3233. end
  3234. else
  3235. s:=procsym.realname;
  3236. { now we handle the parameters }
  3237. if maxparacount>0 then
  3238. begin
  3239. for i:=0 to paras.count-1 do
  3240. begin
  3241. hp:=tparavarsym(paras[i]);
  3242. { no hidden parameters form part of a C++ mangled name:
  3243. a) self is not included
  3244. b) there are no "high" or other hidden parameters
  3245. }
  3246. if vo_is_hidden_para in hp.varoptions then
  3247. continue;
  3248. s2:=getcppparaname(hp.vardef);
  3249. if hp.varspez in [vs_var,vs_out] then
  3250. s2:='R'+s2;
  3251. s:=s+s2;
  3252. end;
  3253. end
  3254. else
  3255. s:=s+'v';
  3256. cplusplusmangledname:=s;
  3257. {$endif NAMEMANGLING_GCC2}
  3258. end;
  3259. function tprocdef.objcmangledname : string;
  3260. var
  3261. manglednamelen: longint;
  3262. iscatmethod : boolean;
  3263. begin
  3264. if not (po_msgstr in procoptions) then
  3265. internalerror(2009030901);
  3266. { we may very well need longer strings to handle these... }
  3267. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3268. length('+"[ ]"')+length(messageinf.str^);
  3269. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3270. if (iscatmethod) then
  3271. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3272. if manglednamelen>255 then
  3273. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3274. if not(po_classmethod in procoptions) then
  3275. result:='"-['
  3276. else
  3277. result:='"+[';
  3278. { quotes are necessary because the +/- otherwise confuse the assembler
  3279. into expecting a number
  3280. }
  3281. if iscatmethod then
  3282. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3283. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3284. if iscatmethod then
  3285. result:=result+')';
  3286. result:=result+' '+messageinf.str^+']"';
  3287. end;
  3288. procedure tprocdef.setmangledname(const s : string);
  3289. begin
  3290. { This is not allowed anymore, the forward declaration
  3291. already needs to create the correct mangledname, no changes
  3292. afterwards are allowed (PFV) }
  3293. { Exception: interface definitions in mode macpas, since in that }
  3294. { case no reference to the old name can exist yet (JM) }
  3295. if assigned(_mangledname) then
  3296. if ((m_mac in current_settings.modeswitches) and
  3297. (interfacedef)) then
  3298. stringdispose(_mangledname)
  3299. else
  3300. internalerror(200411171);
  3301. {$ifdef compress}
  3302. _mangledname:=stringdup(minilzw_encode(s));
  3303. {$else}
  3304. _mangledname:=stringdup(s);
  3305. {$endif}
  3306. include(procoptions,po_has_mangledname);
  3307. end;
  3308. {***************************************************************************
  3309. TPROCVARDEF
  3310. ***************************************************************************}
  3311. constructor tprocvardef.create(level:byte);
  3312. begin
  3313. inherited create(procvardef,level);
  3314. end;
  3315. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3316. begin
  3317. inherited ppuload(procvardef,ppufile);
  3318. { load para symtable }
  3319. parast:=tparasymtable.create(self,ppufile.getbyte);
  3320. tparasymtable(parast).ppuload(ppufile);
  3321. end;
  3322. function tprocvardef.getcopy : tstoreddef;
  3323. var
  3324. i : tcallercallee;
  3325. j : longint;
  3326. begin
  3327. result:=tprocvardef.create(parast.symtablelevel);
  3328. tprocvardef(result).returndef:=returndef;
  3329. tprocvardef(result).returndefderef:=returndefderef;
  3330. tprocvardef(result).parast:=parast.getcopy;
  3331. tprocvardef(result).savesize:=savesize;
  3332. { create paralist copy }
  3333. tprocvardef(result).paras:=tparalist.create(false);
  3334. tprocvardef(result).paras.count:=paras.count;
  3335. for j:=0 to paras.count-1 do
  3336. tprocvardef(result).paras[j]:=paras[j];
  3337. tprocvardef(result).proctypeoption:=proctypeoption;
  3338. tprocvardef(result).proccalloption:=proccalloption;
  3339. tprocvardef(result).procoptions:=procoptions;
  3340. tprocvardef(result).callerargareasize:=callerargareasize;
  3341. tprocvardef(result).calleeargareasize:=calleeargareasize;
  3342. tprocvardef(result).maxparacount:=maxparacount;
  3343. tprocvardef(result).minparacount:=minparacount;
  3344. for i:=low(tcallercallee) to high(tcallercallee) do
  3345. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  3346. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3347. {$ifdef m68k}
  3348. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3349. {$endif}
  3350. end;
  3351. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3352. begin
  3353. inherited ppuwrite(ppufile);
  3354. { Save the para symtable level (necessary to distinguish nested
  3355. procvars) }
  3356. ppufile.putbyte(parast.symtablelevel);
  3357. { Write this entry }
  3358. ppufile.writeentry(ibprocvardef);
  3359. { Save the para symtable, this is taken from the interface }
  3360. tparasymtable(parast).ppuwrite(ppufile);
  3361. end;
  3362. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3363. begin
  3364. case t of
  3365. gs_para :
  3366. GetSymtable:=parast;
  3367. else
  3368. GetSymtable:=nil;
  3369. end;
  3370. end;
  3371. function tprocvardef.size : aint;
  3372. begin
  3373. if ((po_methodpointer in procoptions) or
  3374. is_nested_pd(self)) and
  3375. not(po_addressonly in procoptions) then
  3376. size:=2*sizeof(pint)
  3377. else
  3378. size:=sizeof(pint);
  3379. end;
  3380. function tprocvardef.is_methodpointer:boolean;
  3381. begin
  3382. result:=(po_methodpointer in procoptions);
  3383. end;
  3384. function tprocvardef.is_addressonly:boolean;
  3385. begin
  3386. result:=(not(po_methodpointer in procoptions) and
  3387. not is_nested_pd(self)) or
  3388. (po_addressonly in procoptions);
  3389. end;
  3390. function tprocvardef.getmangledparaname:string;
  3391. begin
  3392. if not(po_methodpointer in procoptions) then
  3393. if not is_nested_pd(self) then
  3394. result:='procvar'
  3395. else
  3396. result:='nestedprovar'
  3397. else
  3398. result:='procvarofobj'
  3399. end;
  3400. function tprocvardef.is_publishable : boolean;
  3401. begin
  3402. is_publishable:=(po_methodpointer in procoptions);
  3403. end;
  3404. function tprocvardef.GetTypeName : string;
  3405. var
  3406. s: string;
  3407. showhidden : boolean;
  3408. begin
  3409. {$ifdef EXTDEBUG}
  3410. showhidden:=true;
  3411. {$else EXTDEBUG}
  3412. showhidden:=false;
  3413. {$endif EXTDEBUG}
  3414. s:='<';
  3415. if po_classmethod in procoptions then
  3416. s := s+'class method type of'
  3417. else
  3418. if po_addressonly in procoptions then
  3419. s := s+'address of'
  3420. else
  3421. s := s+'procedure variable type of';
  3422. if assigned(returndef) and
  3423. (returndef<>voidtype) then
  3424. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3425. else
  3426. s:=s+' procedure'+typename_paras(showhidden);
  3427. if po_methodpointer in procoptions then
  3428. s := s+' of object';
  3429. if is_nested_pd(self) then
  3430. s := s+' is nested';
  3431. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3432. end;
  3433. {***************************************************************************
  3434. TOBJECTDEF
  3435. ***************************************************************************}
  3436. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  3437. begin
  3438. inherited create(n,objectdef);
  3439. fcurrent_dispid:=0;
  3440. objecttype:=ot;
  3441. objectoptions:=[];
  3442. childof:=nil;
  3443. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3444. { create space for vmt !! }
  3445. vmtentries:=TFPList.Create;
  3446. vmt_offset:=0;
  3447. set_parent(c);
  3448. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3449. prepareguid;
  3450. { setup implemented interfaces }
  3451. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3452. ImplementedInterfaces:=TFPObjectList.Create(true)
  3453. else
  3454. ImplementedInterfaces:=nil;
  3455. writing_class_record_dbginfo:=false;
  3456. end;
  3457. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3458. var
  3459. i,
  3460. implintfcount : longint;
  3461. d : tderef;
  3462. ImplIntf : TImplementedInterface;
  3463. vmtentry : pvmtentry;
  3464. begin
  3465. inherited ppuload(objectdef,ppufile);
  3466. objecttype:=tobjecttyp(ppufile.getbyte);
  3467. objextname:=stringdup(ppufile.getstring);
  3468. { only used for external Objective-C classes/protocols }
  3469. if (objextname^='') then
  3470. stringdispose(objextname);
  3471. import_lib:=stringdup(ppufile.getstring);
  3472. { only used for external C++ classes }
  3473. if (import_lib^='') then
  3474. stringdispose(import_lib);
  3475. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3476. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3477. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3478. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3479. vmt_offset:=ppufile.getlongint;
  3480. ppufile.getderef(childofderef);
  3481. ppufile.getsmallset(objectoptions);
  3482. { load guid }
  3483. iidstr:=nil;
  3484. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3485. begin
  3486. new(iidguid);
  3487. ppufile.getguid(iidguid^);
  3488. iidstr:=stringdup(ppufile.getstring);
  3489. end;
  3490. vmtentries:=TFPList.Create;
  3491. vmtentries.count:=ppufile.getlongint;
  3492. for i:=0 to vmtentries.count-1 do
  3493. begin
  3494. ppufile.getderef(d);
  3495. new(vmtentry);
  3496. vmtentry^.procdef:=nil;
  3497. vmtentry^.procdefderef:=d;
  3498. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3499. vmtentries[i]:=vmtentry;
  3500. end;
  3501. { load implemented interfaces }
  3502. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3503. begin
  3504. ImplementedInterfaces:=TFPObjectList.Create(true);
  3505. implintfcount:=ppufile.getlongint;
  3506. for i:=0 to implintfcount-1 do
  3507. begin
  3508. ppufile.getderef(d);
  3509. ImplIntf:=TImplementedInterface.Create_deref(d);
  3510. ImplIntf.IOffset:=ppufile.getlongint;
  3511. ImplementedInterfaces.Add(ImplIntf);
  3512. end;
  3513. end
  3514. else
  3515. ImplementedInterfaces:=nil;
  3516. if df_copied_def in defoptions then
  3517. ppufile.getderef(cloneddefderef)
  3518. else
  3519. tObjectSymtable(symtable).ppuload(ppufile);
  3520. { handles the predefined class tobject }
  3521. { the last TOBJECT which is loaded gets }
  3522. { it ! }
  3523. if (childof=nil) and
  3524. (objecttype=odt_class) and
  3525. (objname^='TOBJECT') then
  3526. class_tobject:=self;
  3527. if (childof=nil) and
  3528. (objecttype=odt_interfacecom) and
  3529. (objname^='IUNKNOWN') then
  3530. interface_iunknown:=self;
  3531. if (childof=nil) and
  3532. (objecttype=odt_objcclass) and
  3533. (objname^='PROTOCOL') then
  3534. objc_protocoltype:=self;
  3535. writing_class_record_dbginfo:=false;
  3536. end;
  3537. destructor tobjectdef.destroy;
  3538. begin
  3539. if assigned(symtable) then
  3540. begin
  3541. symtable.free;
  3542. symtable:=nil;
  3543. end;
  3544. stringdispose(objextname);
  3545. stringdispose(import_lib);
  3546. stringdispose(iidstr);
  3547. if assigned(ImplementedInterfaces) then
  3548. begin
  3549. ImplementedInterfaces.free;
  3550. ImplementedInterfaces:=nil;
  3551. end;
  3552. if assigned(iidguid) then
  3553. begin
  3554. dispose(iidguid);
  3555. iidguid:=nil;
  3556. end;
  3557. if assigned(vmtentries) then
  3558. begin
  3559. resetvmtentries;
  3560. vmtentries.free;
  3561. vmtentries:=nil;
  3562. end;
  3563. if assigned(vmcallstaticinfo) then
  3564. begin
  3565. freemem(vmcallstaticinfo);
  3566. vmcallstaticinfo:=nil;
  3567. end;
  3568. inherited destroy;
  3569. end;
  3570. function tobjectdef.getcopy : tstoreddef;
  3571. var
  3572. i : longint;
  3573. begin
  3574. result:=tobjectdef.create(objecttype,objrealname^,childof);
  3575. { the constructor allocates a symtable which we release to avoid memory leaks }
  3576. tobjectdef(result).symtable.free;
  3577. tobjectdef(result).symtable:=symtable.getcopy;
  3578. if assigned(objextname) then
  3579. tobjectdef(result).objextname:=stringdup(objextname^);
  3580. if assigned(import_lib) then
  3581. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3582. tobjectdef(result).objectoptions:=objectoptions;
  3583. include(tobjectdef(result).defoptions,df_copied_def);
  3584. tobjectdef(result).vmt_offset:=vmt_offset;
  3585. if assigned(iidguid) then
  3586. begin
  3587. new(tobjectdef(result).iidguid);
  3588. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3589. end;
  3590. if assigned(iidstr) then
  3591. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3592. if assigned(ImplementedInterfaces) then
  3593. begin
  3594. for i:=0 to ImplementedInterfaces.count-1 do
  3595. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3596. end;
  3597. if assigned(vmtentries) then
  3598. begin
  3599. tobjectdef(result).vmtentries:=TFPList.Create;
  3600. tobjectdef(result).copyvmtentries(self);
  3601. end;
  3602. end;
  3603. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3604. var
  3605. i : longint;
  3606. vmtentry : pvmtentry;
  3607. ImplIntf : TImplementedInterface;
  3608. old_do_indirect_crc: boolean;
  3609. begin
  3610. { if class1 in unit A changes, and class2 in unit B inherits from it
  3611. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3612. However, if there is also a class3 in unit C that only depends on
  3613. unit B, then unit C will not be recompiled because nothing changed
  3614. to the interface of unit B. Nevertheless, unit C can indirectly
  3615. depend on unit A via derefs, and these must be updated -> the
  3616. indirect crc keeps track of such changes. }
  3617. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3618. ppufile.do_indirect_crc:=true;
  3619. inherited ppuwrite(ppufile);
  3620. ppufile.putbyte(byte(objecttype));
  3621. if assigned(objextname) then
  3622. ppufile.putstring(objextname^)
  3623. else
  3624. ppufile.putstring('');
  3625. if assigned(import_lib) then
  3626. ppufile.putstring(import_lib^)
  3627. else
  3628. ppufile.putstring('');
  3629. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3630. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3631. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3632. ppufile.putlongint(vmt_offset);
  3633. ppufile.putderef(childofderef);
  3634. ppufile.putsmallset(objectoptions);
  3635. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3636. begin
  3637. ppufile.putguid(iidguid^);
  3638. ppufile.putstring(iidstr^);
  3639. end;
  3640. ppufile.putlongint(vmtentries.count);
  3641. for i:=0 to vmtentries.count-1 do
  3642. begin
  3643. vmtentry:=pvmtentry(vmtentries[i]);
  3644. ppufile.putderef(vmtentry^.procdefderef);
  3645. ppufile.putbyte(byte(vmtentry^.visibility));
  3646. end;
  3647. if assigned(ImplementedInterfaces) then
  3648. begin
  3649. ppufile.putlongint(ImplementedInterfaces.Count);
  3650. for i:=0 to ImplementedInterfaces.Count-1 do
  3651. begin
  3652. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3653. ppufile.putderef(ImplIntf.intfdefderef);
  3654. ppufile.putlongint(ImplIntf.Ioffset);
  3655. end;
  3656. end;
  3657. if df_copied_def in defoptions then
  3658. ppufile.putderef(cloneddefderef);
  3659. ppufile.writeentry(ibobjectdef);
  3660. if not(df_copied_def in defoptions) then
  3661. tObjectSymtable(symtable).ppuwrite(ppufile);
  3662. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3663. end;
  3664. function tobjectdef.GetTypeName:string;
  3665. begin
  3666. { in this case we will go in endless recursion, because then }
  3667. { there is no tsym associated yet with the def. It can occur }
  3668. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3669. { instead of the actual type name }
  3670. if not assigned(typesym) then
  3671. result:='<Currently Parsed Class>'
  3672. else
  3673. result:=typename;
  3674. end;
  3675. procedure tobjectdef.buildderef;
  3676. var
  3677. i : longint;
  3678. vmtentry : pvmtentry;
  3679. begin
  3680. inherited buildderef;
  3681. childofderef.build(childof);
  3682. if df_copied_def in defoptions then
  3683. cloneddefderef.build(symtable.defowner)
  3684. else
  3685. tstoredsymtable(symtable).buildderef;
  3686. for i:=0 to vmtentries.count-1 do
  3687. begin
  3688. vmtentry:=pvmtentry(vmtentries[i]);
  3689. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3690. end;
  3691. if assigned(ImplementedInterfaces) then
  3692. begin
  3693. for i:=0 to ImplementedInterfaces.count-1 do
  3694. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3695. end;
  3696. end;
  3697. procedure tobjectdef.deref;
  3698. var
  3699. i : longint;
  3700. vmtentry : pvmtentry;
  3701. begin
  3702. inherited deref;
  3703. childof:=tobjectdef(childofderef.resolve);
  3704. if df_copied_def in defoptions then
  3705. begin
  3706. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3707. symtable:=cloneddef.symtable.getcopy;
  3708. end
  3709. else
  3710. tstoredsymtable(symtable).deref;
  3711. for i:=0 to vmtentries.count-1 do
  3712. begin
  3713. vmtentry:=pvmtentry(vmtentries[i]);
  3714. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3715. end;
  3716. if assigned(ImplementedInterfaces) then
  3717. begin
  3718. for i:=0 to ImplementedInterfaces.count-1 do
  3719. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3720. end;
  3721. end;
  3722. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  3723. var
  3724. pd: tprocdef absolute def;
  3725. st: tsymtable;
  3726. psym: tsym;
  3727. nname: TIDString;
  3728. begin
  3729. if (tdef(def).typ<>procdef) then
  3730. exit;
  3731. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  3732. owner = symtable in which objcclassdef is defined
  3733. }
  3734. st:=pd.owner.defowner.owner;
  3735. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  3736. { check for an existing procsym with our special name }
  3737. psym:=tsym(st.find(nname));
  3738. if not assigned(psym) then
  3739. begin
  3740. psym:=tprocsym.create(nname);
  3741. { avoid warning about this symbol being unused }
  3742. psym.IncRefCount;
  3743. st.insert(psym,true);
  3744. end
  3745. else if (psym.typ<>procsym) then
  3746. internalerror(2009111501);
  3747. { add ourselves to this special procsym }
  3748. tprocsym(psym).procdeflist.add(def);
  3749. end;
  3750. procedure tobjectdef.buildderefimpl;
  3751. begin
  3752. inherited buildderefimpl;
  3753. if not (df_copied_def in defoptions) then
  3754. tstoredsymtable(symtable).buildderefimpl;
  3755. end;
  3756. procedure tobjectdef.derefimpl;
  3757. begin
  3758. inherited derefimpl;
  3759. if not (df_copied_def in defoptions) then
  3760. tstoredsymtable(symtable).derefimpl;
  3761. { the procdefs are not owned by the class helper procsyms, so they
  3762. are not stored/restored either -> re-add them here }
  3763. if (objecttype=odt_objcclass) or
  3764. (oo_is_classhelper in objectoptions) then
  3765. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  3766. end;
  3767. procedure tobjectdef.resetvmtentries;
  3768. var
  3769. i : longint;
  3770. begin
  3771. for i:=0 to vmtentries.Count-1 do
  3772. Dispose(pvmtentry(vmtentries[i]));
  3773. vmtentries.clear;
  3774. end;
  3775. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3776. var
  3777. i : longint;
  3778. vmtentry : pvmtentry;
  3779. begin
  3780. resetvmtentries;
  3781. vmtentries.count:=objdef.vmtentries.count;
  3782. for i:=0 to objdef.vmtentries.count-1 do
  3783. begin
  3784. new(vmtentry);
  3785. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3786. vmtentries[i]:=vmtentry;
  3787. end;
  3788. end;
  3789. function tobjectdef.getparentdef:tdef;
  3790. begin
  3791. { TODO: Remove getparentdef hack}
  3792. { With 2 forward declared classes with the child class before the
  3793. parent class the child class is written earlier to the ppu. Leaving it
  3794. possible to have a reference to the parent class for property overriding,
  3795. but the parent class still has the childof not resolved yet (PFV) }
  3796. if childof=nil then
  3797. childof:=tobjectdef(childofderef.resolve);
  3798. result:=childof;
  3799. end;
  3800. procedure tobjectdef.prepareguid;
  3801. begin
  3802. { set up guid }
  3803. if not assigned(iidguid) then
  3804. begin
  3805. new(iidguid);
  3806. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3807. end;
  3808. { setup iidstring }
  3809. if not assigned(iidstr) then
  3810. iidstr:=stringdup(''); { default is empty string }
  3811. end;
  3812. procedure tobjectdef.set_parent( c : tobjectdef);
  3813. begin
  3814. if assigned(childof) then
  3815. exit;
  3816. childof:=c;
  3817. if not assigned(c) then
  3818. exit;
  3819. { inherit options and status }
  3820. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3821. { add the data of the anchestor class/object }
  3822. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  3823. begin
  3824. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3825. { inherit recordalignment }
  3826. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3827. if (oo_has_vmt in objectoptions) and
  3828. (oo_has_vmt in c.objectoptions) then
  3829. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3830. { if parent has a vmt field then the offset is the same for the child PM }
  3831. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3832. begin
  3833. vmt_offset:=c.vmt_offset;
  3834. include(objectoptions,oo_has_vmt);
  3835. end;
  3836. end;
  3837. end;
  3838. procedure tobjectdef.insertvmt;
  3839. var
  3840. vs: tfieldvarsym;
  3841. begin
  3842. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3843. exit;
  3844. if (oo_has_vmt in objectoptions) then
  3845. internalerror(12345)
  3846. else
  3847. begin
  3848. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3849. tObjectSymtable(symtable).fieldalignment);
  3850. if (tf_requires_proper_alignment in target_info.flags) then
  3851. begin
  3852. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3853. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3854. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3855. end;
  3856. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3857. hidesym(vs);
  3858. tObjectSymtable(symtable).insert(vs);
  3859. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3860. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  3861. vmt_offset:=vs.fieldoffset
  3862. else
  3863. vmt_offset:=vs.fieldoffset div 8;
  3864. include(objectoptions,oo_has_vmt);
  3865. end;
  3866. end;
  3867. procedure tobjectdef.check_forwards;
  3868. begin
  3869. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  3870. tstoredsymtable(symtable).check_forwards;
  3871. if (oo_is_forward in objectoptions) then
  3872. begin
  3873. { ok, in future, the forward can be resolved }
  3874. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3875. exclude(objectoptions,oo_is_forward);
  3876. end;
  3877. end;
  3878. { true if prot implements d (or if they are equal) }
  3879. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  3880. var
  3881. i : longint;
  3882. begin
  3883. { objcprotocols have multiple inheritance, all protocols from which
  3884. the current protocol inherits are stored in implementedinterfaces }
  3885. result:=prot=d;
  3886. if result then
  3887. exit;
  3888. for i:=0 to prot.ImplementedInterfaces.count-1 do
  3889. begin
  3890. result:=is_related_protocol(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  3891. if result then
  3892. exit;
  3893. end;
  3894. end;
  3895. { true, if self inherits from d (or if they are equal) }
  3896. function tobjectdef.is_related(d : tdef) : boolean;
  3897. var
  3898. hp : tobjectdef;
  3899. begin
  3900. if self=d then
  3901. begin
  3902. is_related:=true;
  3903. exit;
  3904. end;
  3905. if (d.typ<>objectdef) then
  3906. begin
  3907. is_related:=false;
  3908. exit;
  3909. end;
  3910. { Objective-C protocols can use multiple inheritance }
  3911. if (objecttype=odt_objcprotocol) then
  3912. begin
  3913. is_related:=is_related_protocol(self,d);
  3914. exit
  3915. end;
  3916. { formally declared Objective-C classes match Objective-C classes with
  3917. the same name }
  3918. if (objecttype=odt_objcclass) and
  3919. (tobjectdef(d).objecttype=odt_objcclass) and
  3920. ((oo_is_formal in objectoptions) or
  3921. (oo_is_formal in tobjectdef(d).objectoptions)) and
  3922. (objrealname^=tobjectdef(d).objrealname^) then
  3923. begin
  3924. is_related:=true;
  3925. exit;
  3926. end;
  3927. hp:=childof;
  3928. while assigned(hp) do
  3929. begin
  3930. if hp=d then
  3931. begin
  3932. is_related:=true;
  3933. exit;
  3934. end;
  3935. hp:=hp.childof;
  3936. end;
  3937. is_related:=false;
  3938. end;
  3939. function tobjectdef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3940. var
  3941. i: longint;
  3942. sym: tsym;
  3943. begin
  3944. for i:=0 to symtable.SymList.Count-1 do
  3945. begin
  3946. sym:=tsym(symtable.SymList[i]);
  3947. if sym.typ=procsym then
  3948. begin
  3949. result:=tprocsym(sym).find_procdef_bytype(pt);
  3950. if assigned(result) then
  3951. exit;
  3952. end;
  3953. end;
  3954. result:=nil;
  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((pd.struct.typ=objectdef)and(oo_is_external in tobjectdef(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 (pd.struct.typ=objectdef) and (oo_is_external in tobjectdef(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.