symdef.pas 171 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. objectoptions : tobjectoptions;
  158. constructor create(const n:string; dt:tdeftyp);
  159. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  160. procedure ppuwrite(ppufile:tcompilerppufile);override;
  161. destructor destroy; override;
  162. function GetSymtable(t:tGetSymtable):TSymtable;override;
  163. function is_packed:boolean;
  164. function RttiName: string;
  165. end;
  166. trecorddef = class(tabstractrecorddef)
  167. public
  168. isunion : boolean;
  169. constructor create(const n:string; p:TSymtable);
  170. constructor ppuload(ppufile:tcompilerppufile);
  171. destructor destroy;override;
  172. function getcopy : tstoreddef;override;
  173. procedure ppuwrite(ppufile:tcompilerppufile);override;
  174. procedure buildderef;override;
  175. procedure deref;override;
  176. function size:aint;override;
  177. function alignment : shortint;override;
  178. function padalignment: shortint;
  179. function GetTypeName:string;override;
  180. { debug }
  181. function needs_inittable : boolean;override;
  182. end;
  183. tprocdef = class;
  184. tobjectdef = class;
  185. { TImplementedInterface }
  186. TImplementedInterface = class
  187. IntfDef : tobjectdef;
  188. IntfDefDeref : tderef;
  189. IType : tinterfaceentrytype;
  190. IOffset : longint;
  191. VtblImplIntf : TImplementedInterface;
  192. NameMappings : TFPHashList;
  193. ProcDefs : TFPObjectList;
  194. ImplementsGetter : tsym;
  195. constructor create(aintf: tobjectdef);
  196. constructor create_deref(d:tderef);
  197. destructor destroy; override;
  198. function getcopy:TImplementedInterface;
  199. procedure buildderef;
  200. procedure deref;
  201. procedure AddMapping(const origname, newname: string);
  202. function GetMapping(const origname: string):string;
  203. procedure AddImplProc(pd:tprocdef);
  204. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  205. end;
  206. { tvmtentry }
  207. tvmtentry = record
  208. procdef : tprocdef;
  209. procdefderef : tderef;
  210. visibility : tvisibility;
  211. end;
  212. pvmtentry = ^tvmtentry;
  213. { tobjectdef }
  214. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  215. pmvcallstaticinfo = ^tmvcallstaticinfo;
  216. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  217. tobjectdef = class(tabstractrecorddef)
  218. private
  219. fcurrent_dispid: longint;
  220. public
  221. dwarf_struct_lab : tasmsymbol;
  222. childof : tobjectdef;
  223. childofderef : tderef;
  224. { for C++ classes: name of the library this class is imported from }
  225. import_lib,
  226. { for Objective-C: protocols and classes can have the same name there }
  227. objextname : pshortstring;
  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. objectoptions:=[];
  2204. end;
  2205. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2206. begin
  2207. inherited ppuload(dt,ppufile);
  2208. objrealname:=stringdup(ppufile.getstring);
  2209. objname:=stringdup(upper(objrealname^));
  2210. ppufile.getsmallset(objectoptions);
  2211. end;
  2212. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2213. begin
  2214. inherited ppuwrite(ppufile);
  2215. ppufile.putstring(objrealname^);
  2216. ppufile.putsmallset(objectoptions);
  2217. end;
  2218. destructor tabstractrecorddef.destroy;
  2219. begin
  2220. stringdispose(objname);
  2221. stringdispose(objrealname);
  2222. inherited destroy;
  2223. end;
  2224. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2225. begin
  2226. if t=gs_record then
  2227. GetSymtable:=symtable
  2228. else
  2229. GetSymtable:=nil;
  2230. end;
  2231. function tabstractrecorddef.is_packed:boolean;
  2232. begin
  2233. result:=tabstractrecordsymtable(symtable).is_packed;
  2234. end;
  2235. function tabstractrecorddef.RttiName: string;
  2236. var
  2237. tmp: tabstractrecorddef;
  2238. begin
  2239. Result:=objrealname^;
  2240. tmp:=self;
  2241. repeat
  2242. if tmp.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  2243. tmp:=tabstractrecorddef(tmp.owner.defowner)
  2244. else
  2245. break;
  2246. Result:=tmp.objrealname^+'.'+Result;
  2247. until tmp=nil;
  2248. end;
  2249. {***************************************************************************
  2250. trecorddef
  2251. ***************************************************************************}
  2252. constructor trecorddef.create(const n:string; p:TSymtable);
  2253. begin
  2254. inherited create(n,recorddef);
  2255. symtable:=p;
  2256. { we can own the symtable only if nobody else owns a copy so far }
  2257. if symtable.refcount=1 then
  2258. symtable.defowner:=self;
  2259. isunion:=false;
  2260. end;
  2261. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2262. begin
  2263. inherited ppuload(recorddef,ppufile);
  2264. if df_copied_def in defoptions then
  2265. ppufile.getderef(cloneddefderef)
  2266. else
  2267. begin
  2268. symtable:=trecordsymtable.create(objrealname^,0);
  2269. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2270. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2271. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2272. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2273. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2274. trecordsymtable(symtable).ppuload(ppufile);
  2275. { requires usefieldalignment to be set }
  2276. symtable.defowner:=self;
  2277. end;
  2278. isunion:=false;
  2279. end;
  2280. destructor trecorddef.destroy;
  2281. begin
  2282. if assigned(symtable) then
  2283. begin
  2284. symtable.free;
  2285. symtable:=nil;
  2286. end;
  2287. inherited destroy;
  2288. end;
  2289. function trecorddef.getcopy : tstoreddef;
  2290. begin
  2291. result:=trecorddef.create(objrealname^,symtable.getcopy);
  2292. trecorddef(result).isunion:=isunion;
  2293. include(trecorddef(result).defoptions,df_copied_def);
  2294. end;
  2295. function trecorddef.needs_inittable : boolean;
  2296. begin
  2297. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2298. end;
  2299. procedure trecorddef.buildderef;
  2300. begin
  2301. inherited buildderef;
  2302. if df_copied_def in defoptions then
  2303. cloneddefderef.build(symtable.defowner)
  2304. else
  2305. tstoredsymtable(symtable).buildderef;
  2306. end;
  2307. procedure trecorddef.deref;
  2308. begin
  2309. inherited deref;
  2310. { now dereference the definitions }
  2311. if df_copied_def in defoptions then
  2312. begin
  2313. cloneddef:=trecorddef(cloneddefderef.resolve);
  2314. symtable:=cloneddef.symtable.getcopy;
  2315. end
  2316. else
  2317. tstoredsymtable(symtable).deref;
  2318. { assign TGUID? load only from system unit }
  2319. if not(assigned(rec_tguid)) and
  2320. (upper(typename)='TGUID') and
  2321. assigned(owner) and
  2322. assigned(owner.name) and
  2323. (owner.name^='SYSTEM') then
  2324. rec_tguid:=self;
  2325. { assign JMP_BUF? load only from system unit }
  2326. if not(assigned(rec_jmp_buf)) and
  2327. (upper(typename)='JMP_BUF') and
  2328. assigned(owner) and
  2329. assigned(owner.name) and
  2330. (owner.name^='SYSTEM') then
  2331. rec_jmp_buf:=self;
  2332. end;
  2333. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2334. begin
  2335. inherited ppuwrite(ppufile);
  2336. if df_copied_def in defoptions then
  2337. ppufile.putderef(cloneddefderef)
  2338. else
  2339. begin
  2340. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2341. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2342. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2343. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2344. ppufile.putaint(trecordsymtable(symtable).datasize);
  2345. end;
  2346. ppufile.writeentry(ibrecorddef);
  2347. if not(df_copied_def in defoptions) then
  2348. trecordsymtable(symtable).ppuwrite(ppufile);
  2349. end;
  2350. function trecorddef.size:aint;
  2351. begin
  2352. result:=trecordsymtable(symtable).datasize;
  2353. end;
  2354. function trecorddef.alignment:shortint;
  2355. begin
  2356. alignment:=trecordsymtable(symtable).recordalignment;
  2357. end;
  2358. function trecorddef.padalignment:shortint;
  2359. begin
  2360. padalignment := trecordsymtable(symtable).padalignment;
  2361. end;
  2362. function trecorddef.GetTypeName : string;
  2363. begin
  2364. GetTypeName:='<record type>'
  2365. end;
  2366. {***************************************************************************
  2367. TABSTRACTPROCDEF
  2368. ***************************************************************************}
  2369. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2370. begin
  2371. inherited create(dt);
  2372. parast:=tparasymtable.create(self,level);
  2373. paras:=nil;
  2374. minparacount:=0;
  2375. maxparacount:=0;
  2376. proctypeoption:=potype_none;
  2377. proccalloption:=pocall_none;
  2378. procoptions:=[];
  2379. returndef:=voidtype;
  2380. savesize:=sizeof(pint);
  2381. callerargareasize:=0;
  2382. calleeargareasize:=0;
  2383. has_paraloc_info:=callnoside;
  2384. funcretloc[callerside].init;
  2385. funcretloc[calleeside].init;
  2386. check_mark_as_nested;
  2387. end;
  2388. destructor tabstractprocdef.destroy;
  2389. begin
  2390. if assigned(paras) then
  2391. begin
  2392. {$ifdef MEMDEBUG}
  2393. memprocpara.start;
  2394. {$endif MEMDEBUG}
  2395. paras.free;
  2396. paras:=nil;
  2397. {$ifdef MEMDEBUG}
  2398. memprocpara.stop;
  2399. {$endif MEMDEBUG}
  2400. end;
  2401. if assigned(parast) then
  2402. begin
  2403. {$ifdef MEMDEBUG}
  2404. memprocparast.start;
  2405. {$endif MEMDEBUG}
  2406. parast.free;
  2407. parast:=nil;
  2408. {$ifdef MEMDEBUG}
  2409. memprocparast.stop;
  2410. {$endif MEMDEBUG}
  2411. end;
  2412. funcretloc[callerside].done;
  2413. funcretloc[calleeside].done;
  2414. inherited destroy;
  2415. end;
  2416. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2417. begin
  2418. if (tsym(p).typ<>paravarsym) then
  2419. exit;
  2420. inc(plongint(arg)^);
  2421. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2422. begin
  2423. if not assigned(tparavarsym(p).defaultconstsym) then
  2424. inc(minparacount);
  2425. inc(maxparacount);
  2426. end;
  2427. end;
  2428. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2429. begin
  2430. if (tsym(p).typ<>paravarsym) then
  2431. exit;
  2432. paras.add(p);
  2433. end;
  2434. procedure tabstractprocdef.calcparas;
  2435. var
  2436. paracount : longint;
  2437. begin
  2438. { This can already be assigned when
  2439. we need to reresolve this unit (PFV) }
  2440. if assigned(paras) then
  2441. paras.free;
  2442. paras:=tparalist.create(false);
  2443. paracount:=0;
  2444. minparacount:=0;
  2445. maxparacount:=0;
  2446. parast.SymList.ForEachCall(@count_para,@paracount);
  2447. paras.capacity:=paracount;
  2448. { Insert parameters in table }
  2449. parast.SymList.ForEachCall(@insert_para,nil);
  2450. { Order parameters }
  2451. paras.sortparas;
  2452. end;
  2453. procedure tabstractprocdef.buildderef;
  2454. begin
  2455. { released procdef? }
  2456. if not assigned(parast) then
  2457. exit;
  2458. inherited buildderef;
  2459. returndefderef.build(returndef);
  2460. { parast }
  2461. tparasymtable(parast).buildderef;
  2462. end;
  2463. procedure tabstractprocdef.deref;
  2464. begin
  2465. inherited deref;
  2466. returndef:=tdef(returndefderef.resolve);
  2467. { parast }
  2468. tparasymtable(parast).deref;
  2469. { recalculated parameters }
  2470. calcparas;
  2471. end;
  2472. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2473. begin
  2474. inherited ppuload(dt,ppufile);
  2475. parast:=nil;
  2476. Paras:=nil;
  2477. minparacount:=0;
  2478. maxparacount:=0;
  2479. ppufile.getderef(returndefderef);
  2480. { TODO: remove fpu_used loading}
  2481. ppufile.getbyte;
  2482. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2483. proccalloption:=tproccalloption(ppufile.getbyte);
  2484. ppufile.getnormalset(procoptions);
  2485. funcretloc[callerside].init;
  2486. if po_explicitparaloc in procoptions then
  2487. funcretloc[callerside].ppuload(ppufile);
  2488. savesize:=sizeof(pint);
  2489. if (po_explicitparaloc in procoptions) then
  2490. has_paraloc_info:=callerside;
  2491. end;
  2492. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2493. var
  2494. oldintfcrc : boolean;
  2495. begin
  2496. { released procdef? }
  2497. if not assigned(parast) then
  2498. exit;
  2499. inherited ppuwrite(ppufile);
  2500. ppufile.putderef(returndefderef);
  2501. oldintfcrc:=ppufile.do_interface_crc;
  2502. ppufile.do_interface_crc:=false;
  2503. ppufile.putbyte(0);
  2504. ppufile.putbyte(ord(proctypeoption));
  2505. ppufile.putbyte(ord(proccalloption));
  2506. ppufile.putnormalset(procoptions);
  2507. ppufile.do_interface_crc:=oldintfcrc;
  2508. if (po_explicitparaloc in procoptions) then
  2509. funcretloc[callerside].ppuwrite(ppufile);
  2510. end;
  2511. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2512. var
  2513. hs,s : string;
  2514. hp : TParavarsym;
  2515. hpc : tconstsym;
  2516. first : boolean;
  2517. i : integer;
  2518. begin
  2519. s:='';
  2520. first:=true;
  2521. for i:=0 to paras.count-1 do
  2522. begin
  2523. hp:=tparavarsym(paras[i]);
  2524. if not(vo_is_hidden_para in hp.varoptions) or
  2525. (showhidden) then
  2526. begin
  2527. if first then
  2528. begin
  2529. s:=s+'(';
  2530. first:=false;
  2531. end
  2532. else
  2533. s:=s+',';
  2534. if vo_is_hidden_para in hp.varoptions then
  2535. s:=s+'<';
  2536. case hp.varspez of
  2537. vs_var :
  2538. s:=s+'var ';
  2539. vs_const :
  2540. s:=s+'const ';
  2541. vs_out :
  2542. s:=s+'out ';
  2543. vs_constref :
  2544. s:=s+'constref ';
  2545. end;
  2546. if hp.univpara then
  2547. s:=s+'univ ';
  2548. if assigned(hp.vardef.typesym) then
  2549. begin
  2550. hs:=hp.vardef.typesym.realname;
  2551. if hs[1]<>'$' then
  2552. s:=s+hs
  2553. else
  2554. s:=s+hp.vardef.GetTypeName;
  2555. end
  2556. else
  2557. s:=s+hp.vardef.GetTypeName;
  2558. { default value }
  2559. if assigned(hp.defaultconstsym) then
  2560. begin
  2561. hpc:=tconstsym(hp.defaultconstsym);
  2562. hs:='';
  2563. case hpc.consttyp of
  2564. conststring,
  2565. constresourcestring :
  2566. begin
  2567. If hpc.value.len>0 then
  2568. begin
  2569. setLength(hs,hpc.value.len);
  2570. { don't write past the end of hs if the constant
  2571. is > 255 chars }
  2572. move(hpc.value.valueptr^,hs[1],length(hs));
  2573. { make sure that constant strings with newline chars
  2574. don't create a linebreak in the assembler code,
  2575. since comments are line-based. Also remove nulls
  2576. because the comments are written as a pchar. }
  2577. ReplaceCase(hs,#0,'.');
  2578. ReplaceCase(hs,#10,'.');
  2579. ReplaceCase(hs,#13,'.');
  2580. end;
  2581. end;
  2582. constreal :
  2583. str(pbestreal(hpc.value.valueptr)^,hs);
  2584. constpointer :
  2585. hs:=tostr(hpc.value.valueordptr);
  2586. constord :
  2587. begin
  2588. if is_boolean(hpc.constdef) then
  2589. begin
  2590. if hpc.value.valueord<>0 then
  2591. hs:='TRUE'
  2592. else
  2593. hs:='FALSE';
  2594. end
  2595. else
  2596. hs:=tostr(hpc.value.valueord);
  2597. end;
  2598. constnil :
  2599. hs:='nil';
  2600. constset :
  2601. hs:='<set>';
  2602. end;
  2603. if hs<>'' then
  2604. s:=s+'="'+hs+'"';
  2605. end;
  2606. if vo_is_hidden_para in hp.varoptions then
  2607. s:=s+'>';
  2608. end;
  2609. end;
  2610. if not first then
  2611. s:=s+')';
  2612. if (po_varargs in procoptions) then
  2613. s:=s+';VarArgs';
  2614. typename_paras:=s;
  2615. end;
  2616. function tabstractprocdef.is_methodpointer:boolean;
  2617. begin
  2618. result:=false;
  2619. end;
  2620. function tabstractprocdef.is_addressonly:boolean;
  2621. begin
  2622. result:=true;
  2623. end;
  2624. function tabstractprocdef.no_self_node: boolean;
  2625. begin
  2626. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  2627. (proctypeoption in [potype_class_constructor,potype_class_destructor]);
  2628. end;
  2629. procedure tabstractprocdef.check_mark_as_nested;
  2630. begin
  2631. { nested procvars require that nested functions use the Delphi-style
  2632. nested procedure calling convention }
  2633. if (parast.symtablelevel>normal_function_level) and
  2634. (m_nested_procvars in current_settings.modeswitches) then
  2635. include(procoptions,po_delphi_nested_cc);
  2636. end;
  2637. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  2638. begin
  2639. if (side in [callerside,callbothsides]) and
  2640. not(has_paraloc_info in [callerside,callbothsides]) then
  2641. begin
  2642. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  2643. if has_paraloc_info in [calleeside,callbothsides] then
  2644. has_paraloc_info:=callbothsides
  2645. else
  2646. has_paraloc_info:=callerside;
  2647. end;
  2648. if (side in [calleeside,callbothsides]) and
  2649. not(has_paraloc_info in [calleeside,callbothsides]) then
  2650. begin
  2651. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  2652. if has_paraloc_info in [callerside,callbothsides] then
  2653. has_paraloc_info:=callbothsides
  2654. else
  2655. has_paraloc_info:=calleeside;
  2656. end;
  2657. end;
  2658. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  2659. var
  2660. p: tparavarsym;
  2661. ploc: PCGParalocation;
  2662. i: longint;
  2663. begin
  2664. result:=false;
  2665. init_paraloc_info(side);
  2666. for i:=0 to parast.SymList.Count-1 do
  2667. if tsym(parast.SymList[i]).typ=paravarsym then
  2668. begin
  2669. p:=tparavarsym(parast.SymList[i]);
  2670. { check if no parameter is located on the stack }
  2671. if is_open_array(p.vardef) or
  2672. is_array_of_const(p.vardef) then
  2673. begin
  2674. result:=true;
  2675. exit;
  2676. end;
  2677. ploc:=p.paraloc[side].location;
  2678. while assigned(ploc) do
  2679. begin
  2680. if (ploc^.loc=LOC_REFERENCE) then
  2681. begin
  2682. result:=true;
  2683. exit
  2684. end;
  2685. ploc:=ploc^.next;
  2686. end;
  2687. end;
  2688. end;
  2689. {***************************************************************************
  2690. TPROCDEF
  2691. ***************************************************************************}
  2692. constructor tprocdef.create(level:byte);
  2693. begin
  2694. inherited create(procdef,level);
  2695. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2696. _mangledname:=nil;
  2697. fileinfo:=current_filepos;
  2698. extnumber:=$ffff;
  2699. aliasnames:=TCmdStrList.create;
  2700. funcretsym:=nil;
  2701. forwarddef:=true;
  2702. interfacedef:=false;
  2703. hasforward:=false;
  2704. struct := nil;
  2705. import_dll:=nil;
  2706. import_name:=nil;
  2707. import_nr:=0;
  2708. inlininginfo:=nil;
  2709. deprecatedmsg:=nil;
  2710. {$ifdef i386}
  2711. fpu_used:=maxfpuregs;
  2712. {$endif i386}
  2713. end;
  2714. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2715. var
  2716. i,aliasnamescount : longint;
  2717. level : byte;
  2718. begin
  2719. inherited ppuload(procdef,ppufile);
  2720. if po_has_mangledname in procoptions then
  2721. _mangledname:=stringdup(ppufile.getstring)
  2722. else
  2723. _mangledname:=nil;
  2724. extnumber:=ppufile.getword;
  2725. level:=ppufile.getbyte;
  2726. ppufile.getderef(structderef);
  2727. ppufile.getderef(procsymderef);
  2728. ppufile.getposinfo(fileinfo);
  2729. visibility:=tvisibility(ppufile.getbyte);
  2730. ppufile.getsmallset(symoptions);
  2731. if sp_has_deprecated_msg in symoptions then
  2732. deprecatedmsg:=stringdup(ppufile.getstring)
  2733. else
  2734. deprecatedmsg:=nil;
  2735. {$ifdef powerpc}
  2736. { library symbol for AmigaOS/MorphOS }
  2737. ppufile.getderef(libsymderef);
  2738. {$endif powerpc}
  2739. { import stuff }
  2740. if po_has_importdll in procoptions then
  2741. import_dll:=stringdup(ppufile.getstring)
  2742. else
  2743. import_dll:=nil;
  2744. if po_has_importname in procoptions then
  2745. import_name:=stringdup(ppufile.getstring)
  2746. else
  2747. import_name:=nil;
  2748. import_nr:=ppufile.getword;
  2749. if (po_msgint in procoptions) then
  2750. messageinf.i:=ppufile.getlongint;
  2751. if (po_msgstr in procoptions) then
  2752. messageinf.str:=stringdup(ppufile.getstring);
  2753. if (po_dispid in procoptions) then
  2754. dispid:=ppufile.getlongint;
  2755. { inline stuff }
  2756. if (po_has_inlininginfo in procoptions) then
  2757. begin
  2758. ppufile.getderef(funcretsymderef);
  2759. new(inlininginfo);
  2760. ppufile.getsmallset(inlininginfo^.flags);
  2761. end
  2762. else
  2763. begin
  2764. inlininginfo:=nil;
  2765. funcretsym:=nil;
  2766. end;
  2767. aliasnames:=TCmdStrList.create;
  2768. { count alias names }
  2769. aliasnamescount:=ppufile.getbyte;
  2770. for i:=1 to aliasnamescount do
  2771. aliasnames.insert(ppufile.getstring);
  2772. { load para symtable }
  2773. parast:=tparasymtable.create(self,level);
  2774. tparasymtable(parast).ppuload(ppufile);
  2775. { load local symtable }
  2776. if (po_has_inlininginfo in procoptions) then
  2777. begin
  2778. localst:=tlocalsymtable.create(self,level);
  2779. tlocalsymtable(localst).ppuload(ppufile);
  2780. end
  2781. else
  2782. localst:=nil;
  2783. { inline stuff }
  2784. if (po_has_inlininginfo in procoptions) then
  2785. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2786. { default values for no persistent data }
  2787. if (cs_link_deffile in current_settings.globalswitches) and
  2788. (tf_need_export in target_info.flags) and
  2789. (po_exports in procoptions) then
  2790. deffile.AddExport(mangledname);
  2791. forwarddef:=false;
  2792. interfacedef:=false;
  2793. hasforward:=false;
  2794. { Disable po_has_inlining until the derefimpl is done }
  2795. exclude(procoptions,po_has_inlininginfo);
  2796. {$ifdef i386}
  2797. fpu_used:=maxfpuregs;
  2798. {$endif i386}
  2799. end;
  2800. destructor tprocdef.destroy;
  2801. begin
  2802. aliasnames.free;
  2803. aliasnames:=nil;
  2804. if assigned(localst) and
  2805. (localst.symtabletype<>staticsymtable) then
  2806. begin
  2807. {$ifdef MEMDEBUG}
  2808. memproclocalst.start;
  2809. {$endif MEMDEBUG}
  2810. localst.free;
  2811. localst:=nil;
  2812. {$ifdef MEMDEBUG}
  2813. memproclocalst.start;
  2814. {$endif MEMDEBUG}
  2815. end;
  2816. if assigned(inlininginfo) then
  2817. begin
  2818. {$ifdef MEMDEBUG}
  2819. memprocnodetree.start;
  2820. {$endif MEMDEBUG}
  2821. tnode(inlininginfo^.code).free;
  2822. {$ifdef MEMDEBUG}
  2823. memprocnodetree.start;
  2824. {$endif MEMDEBUG}
  2825. dispose(inlininginfo);
  2826. inlininginfo:=nil;
  2827. end;
  2828. stringdispose(resultname);
  2829. stringdispose(import_dll);
  2830. stringdispose(import_name);
  2831. stringdispose(deprecatedmsg);
  2832. if (po_msgstr in procoptions) then
  2833. stringdispose(messageinf.str);
  2834. if assigned(_mangledname) then
  2835. begin
  2836. {$ifdef MEMDEBUG}
  2837. memmanglednames.start;
  2838. {$endif MEMDEBUG}
  2839. stringdispose(_mangledname);
  2840. {$ifdef MEMDEBUG}
  2841. memmanglednames.stop;
  2842. {$endif MEMDEBUG}
  2843. end;
  2844. inherited destroy;
  2845. end;
  2846. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2847. var
  2848. oldintfcrc : boolean;
  2849. aliasnamescount : longint;
  2850. item : TCmdStrListItem;
  2851. begin
  2852. { released procdef? }
  2853. if not assigned(parast) then
  2854. exit;
  2855. inherited ppuwrite(ppufile);
  2856. if po_has_mangledname in procoptions then
  2857. ppufile.putstring(_mangledname^);
  2858. ppufile.putword(extnumber);
  2859. ppufile.putbyte(parast.symtablelevel);
  2860. ppufile.putderef(structderef);
  2861. ppufile.putderef(procsymderef);
  2862. ppufile.putposinfo(fileinfo);
  2863. ppufile.putbyte(byte(visibility));
  2864. ppufile.putsmallset(symoptions);
  2865. if sp_has_deprecated_msg in symoptions then
  2866. ppufile.putstring(deprecatedmsg^);
  2867. {$ifdef powerpc}
  2868. { library symbol for AmigaOS/MorphOS }
  2869. ppufile.putderef(libsymderef);
  2870. {$endif powerpc}
  2871. { import }
  2872. if po_has_importdll in procoptions then
  2873. ppufile.putstring(import_dll^);
  2874. if po_has_importname in procoptions then
  2875. ppufile.putstring(import_name^);
  2876. ppufile.putword(import_nr);
  2877. if (po_msgint in procoptions) then
  2878. ppufile.putlongint(messageinf.i);
  2879. if (po_msgstr in procoptions) then
  2880. ppufile.putstring(messageinf.str^);
  2881. if (po_dispid in procoptions) then
  2882. ppufile.putlongint(dispid);
  2883. { inline stuff }
  2884. oldintfcrc:=ppufile.do_crc;
  2885. ppufile.do_crc:=false;
  2886. if (po_has_inlininginfo in procoptions) then
  2887. begin
  2888. ppufile.putderef(funcretsymderef);
  2889. ppufile.putsmallset(inlininginfo^.flags);
  2890. end;
  2891. { count alias names }
  2892. aliasnamescount:=0;
  2893. item:=TCmdStrListItem(aliasnames.first);
  2894. while assigned(item) do
  2895. begin
  2896. inc(aliasnamescount);
  2897. item:=TCmdStrListItem(item.next);
  2898. end;
  2899. if aliasnamescount>255 then
  2900. internalerror(200711021);
  2901. ppufile.putbyte(aliasnamescount);
  2902. item:=TCmdStrListItem(aliasnames.first);
  2903. while assigned(item) do
  2904. begin
  2905. ppufile.putstring(item.str);
  2906. item:=TCmdStrListItem(item.next);
  2907. end;
  2908. ppufile.do_crc:=oldintfcrc;
  2909. { write this entry }
  2910. ppufile.writeentry(ibprocdef);
  2911. { Save the para symtable, this is taken from the interface }
  2912. tparasymtable(parast).ppuwrite(ppufile);
  2913. { save localsymtable for inline procedures or when local
  2914. browser info is requested, this has no influence on the crc }
  2915. if (po_has_inlininginfo in procoptions) then
  2916. begin
  2917. oldintfcrc:=ppufile.do_crc;
  2918. ppufile.do_crc:=false;
  2919. tlocalsymtable(localst).ppuwrite(ppufile);
  2920. ppufile.do_crc:=oldintfcrc;
  2921. end;
  2922. { node tree for inlining }
  2923. oldintfcrc:=ppufile.do_crc;
  2924. ppufile.do_crc:=false;
  2925. if (po_has_inlininginfo in procoptions) then
  2926. ppuwritenodetree(ppufile,inlininginfo^.code);
  2927. ppufile.do_crc:=oldintfcrc;
  2928. end;
  2929. function tprocdef.fullprocname(showhidden:boolean):string;
  2930. var
  2931. s : string;
  2932. t : ttoken;
  2933. begin
  2934. {$ifdef EXTDEBUG}
  2935. showhidden:=true;
  2936. {$endif EXTDEBUG}
  2937. s:='';
  2938. if assigned(struct) then
  2939. begin
  2940. s:=struct.RttiName+'.';
  2941. if (po_classmethod in procoptions) and
  2942. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2943. s:='class ' + s;
  2944. end;
  2945. if proctypeoption=potype_operator then
  2946. begin
  2947. for t:=NOTOKEN to last_overloaded do
  2948. if procsym.realname='$'+overloaded_names[t] then
  2949. begin
  2950. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2951. break;
  2952. end;
  2953. end
  2954. else
  2955. s:=s+procsym.realname+typename_paras(showhidden);
  2956. case proctypeoption of
  2957. potype_constructor:
  2958. s:='constructor '+s;
  2959. potype_destructor:
  2960. s:='destructor '+s;
  2961. potype_class_constructor:
  2962. s:='class constructor '+s;
  2963. potype_class_destructor:
  2964. s:='class destructor '+s;
  2965. else
  2966. if assigned(returndef) and
  2967. not(is_void(returndef)) then
  2968. s:=s+':'+returndef.GetTypeName;
  2969. end;
  2970. if owner.symtabletype=localsymtable then
  2971. s:=s+' is nested';
  2972. s:=s+';';
  2973. { forced calling convention? }
  2974. if (po_hascallingconvention in procoptions) then
  2975. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2976. if (po_staticmethod in procoptions) and
  2977. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2978. s:=s+' Static;';
  2979. fullprocname:=s;
  2980. end;
  2981. function tprocdef.is_methodpointer:boolean;
  2982. begin
  2983. { don't check assigned(_class), that's also the case for nested
  2984. procedures inside methods }
  2985. result:=owner.symtabletype=ObjectSymtable;
  2986. end;
  2987. function tprocdef.is_addressonly:boolean;
  2988. begin
  2989. result:=assigned(owner) and
  2990. (owner.symtabletype<>ObjectSymtable) and
  2991. (not(m_nested_procvars in current_settings.modeswitches) or
  2992. not is_nested_pd(self));
  2993. end;
  2994. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2995. begin
  2996. case t of
  2997. gs_local :
  2998. GetSymtable:=localst;
  2999. gs_para :
  3000. GetSymtable:=parast;
  3001. else
  3002. GetSymtable:=nil;
  3003. end;
  3004. end;
  3005. procedure tprocdef.buildderef;
  3006. begin
  3007. inherited buildderef;
  3008. structderef.build(struct);
  3009. { procsym that originaly defined this definition, should be in the
  3010. same symtable }
  3011. procsymderef.build(procsym);
  3012. {$ifdef powerpc}
  3013. { library symbol for AmigaOS/MorphOS }
  3014. libsymderef.build(libsym);
  3015. {$endif powerpc}
  3016. end;
  3017. procedure tprocdef.buildderefimpl;
  3018. begin
  3019. inherited buildderefimpl;
  3020. { Localst is not available for main/unit init }
  3021. if assigned(localst) then
  3022. begin
  3023. tlocalsymtable(localst).buildderef;
  3024. tlocalsymtable(localst).buildderefimpl;
  3025. end;
  3026. { inline tree }
  3027. if (po_has_inlininginfo in procoptions) then
  3028. begin
  3029. funcretsymderef.build(funcretsym);
  3030. inlininginfo^.code.buildderefimpl;
  3031. end;
  3032. end;
  3033. procedure tprocdef.deref;
  3034. begin
  3035. inherited deref;
  3036. struct:=tabstractrecorddef(structderef.resolve);
  3037. { procsym that originaly defined this definition, should be in the
  3038. same symtable }
  3039. procsym:=tprocsym(procsymderef.resolve);
  3040. {$ifdef powerpc}
  3041. { library symbol for AmigaOS/MorphOS }
  3042. libsym:=tsym(libsymderef.resolve);
  3043. {$endif powerpc}
  3044. end;
  3045. procedure tprocdef.derefimpl;
  3046. begin
  3047. { Enable has_inlininginfo when the inlininginfo
  3048. structure is available. The has_inlininginfo was disabled
  3049. after the load, since the data was invalid }
  3050. if assigned(inlininginfo) then
  3051. include(procoptions,po_has_inlininginfo);
  3052. { Locals }
  3053. if assigned(localst) then
  3054. begin
  3055. tlocalsymtable(localst).deref;
  3056. tlocalsymtable(localst).derefimpl;
  3057. end;
  3058. { Inline }
  3059. if (po_has_inlininginfo in procoptions) then
  3060. begin
  3061. inlininginfo^.code.derefimpl;
  3062. { funcretsym, this is always located in the localst }
  3063. funcretsym:=tsym(funcretsymderef.resolve);
  3064. end
  3065. else
  3066. begin
  3067. { safety }
  3068. { Not safe! A unit may be reresolved after its interface has been
  3069. parsed but before its implementation has been parsed, and in that
  3070. case the funcretsym is still required!
  3071. funcretsym:=nil; }
  3072. end;
  3073. end;
  3074. function tprocdef.GetTypeName : string;
  3075. begin
  3076. GetTypeName := FullProcName(false);
  3077. end;
  3078. function tprocdef.mangledname : string;
  3079. var
  3080. hp : TParavarsym;
  3081. hs : string;
  3082. crc : dword;
  3083. newlen,
  3084. oldlen,
  3085. i : integer;
  3086. begin
  3087. if assigned(_mangledname) then
  3088. begin
  3089. {$ifdef compress}
  3090. mangledname:=minilzw_decode(_mangledname^);
  3091. {$else}
  3092. mangledname:=_mangledname^;
  3093. {$endif}
  3094. exit;
  3095. end;
  3096. { we need to use the symtable where the procsym is inserted,
  3097. because that is visible to the world }
  3098. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3099. oldlen:=length(mangledname);
  3100. { add parameter types }
  3101. for i:=0 to paras.count-1 do
  3102. begin
  3103. hp:=tparavarsym(paras[i]);
  3104. if not(vo_is_hidden_para in hp.varoptions) then
  3105. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  3106. end;
  3107. { add resultdef, add $$ as separator to make it unique from a
  3108. parameter separator }
  3109. if not is_void(returndef) then
  3110. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  3111. newlen:=length(mangledname);
  3112. { Replace with CRC if the parameter line is very long }
  3113. if (newlen-oldlen>12) and
  3114. ((newlen>100) or (newlen-oldlen>64)) then
  3115. begin
  3116. crc:=0;
  3117. for i:=0 to paras.count-1 do
  3118. begin
  3119. hp:=tparavarsym(paras[i]);
  3120. if not(vo_is_hidden_para in hp.varoptions) then
  3121. begin
  3122. hs:=hp.vardef.mangledparaname;
  3123. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3124. end;
  3125. end;
  3126. hs:=hp.vardef.mangledparaname;
  3127. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3128. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3129. end;
  3130. {$ifdef compress}
  3131. _mangledname:=stringdup(minilzw_encode(mangledname));
  3132. {$else}
  3133. _mangledname:=stringdup(mangledname);
  3134. {$endif}
  3135. end;
  3136. function tprocdef.cplusplusmangledname : string;
  3137. function getcppparaname(p : tdef) : string;
  3138. const
  3139. {$ifdef NAMEMANGLING_GCC2}
  3140. ordtype2str : array[tordtype] of string[2] = (
  3141. '',
  3142. 'Uc','Us','Ui','Us',
  3143. 'Sc','s','i','x',
  3144. 'b','b','b','b','b',
  3145. 'c','w','x');
  3146. {$else NAMEMANGLING_GCC2}
  3147. ordtype2str : array[tordtype] of string[1] = (
  3148. 'v',
  3149. 'h','t','j','y',
  3150. 'a','s','i','x',
  3151. 'b','b','b','b','b',
  3152. 'c','w','x');
  3153. floattype2str : array[tfloattype] of string[1] = (
  3154. 'f','d','e','e',
  3155. 'd','d','g');
  3156. {$endif NAMEMANGLING_GCC2}
  3157. var
  3158. s : string;
  3159. begin
  3160. case p.typ of
  3161. orddef:
  3162. s:=ordtype2str[torddef(p).ordtype];
  3163. pointerdef:
  3164. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3165. {$ifndef NAMEMANGLING_GCC2}
  3166. floatdef:
  3167. s:=floattype2str[tfloatdef(p).floattype];
  3168. {$endif NAMEMANGLING_GCC2}
  3169. else
  3170. internalerror(2103001);
  3171. end;
  3172. getcppparaname:=s;
  3173. end;
  3174. var
  3175. s,s2 : string;
  3176. hp : TParavarsym;
  3177. i : integer;
  3178. begin
  3179. {$ifdef NAMEMANGLING_GCC2}
  3180. { outdated gcc 2.x name mangling scheme }
  3181. s := procsym.realname;
  3182. if procsym.owner.symtabletype=ObjectSymtable then
  3183. begin
  3184. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3185. case proctypeoption of
  3186. potype_destructor:
  3187. s:='_$_'+tostr(length(s2))+s2;
  3188. potype_constructor:
  3189. s:='___'+tostr(length(s2))+s2;
  3190. else
  3191. s:='_'+s+'__'+tostr(length(s2))+s2;
  3192. end;
  3193. end
  3194. else s:=s+'__';
  3195. s:=s+'F';
  3196. { concat modifiers }
  3197. { !!!!! }
  3198. { now we handle the parameters }
  3199. if maxparacount>0 then
  3200. begin
  3201. for i:=0 to paras.count-1 do
  3202. begin
  3203. hp:=tparavarsym(paras[i]);
  3204. { no hidden parameters form part of a C++ mangled name:
  3205. a) self is not included
  3206. b) there are no "high" or other hidden parameters
  3207. }
  3208. if vo_is_hidden_para in hp.varoptions then
  3209. continue;
  3210. s2:=getcppparaname(hp.vardef);
  3211. if hp.varspez in [vs_var,vs_out] then
  3212. s2:='R'+s2;
  3213. s:=s+s2;
  3214. end;
  3215. end
  3216. else
  3217. s:=s+'v';
  3218. cplusplusmangledname:=s;
  3219. {$else NAMEMANGLING_GCC2}
  3220. { gcc 3.x and 4.x name mangling scheme }
  3221. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3222. if procsym.owner.symtabletype=ObjectSymtable then
  3223. begin
  3224. s:='_ZN';
  3225. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3226. s:=s+tostr(length(s2))+s2;
  3227. case proctypeoption of
  3228. potype_constructor:
  3229. s:=s+'C1';
  3230. potype_destructor:
  3231. s:=s+'D1';
  3232. else
  3233. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3234. end;
  3235. s:=s+'E';
  3236. end
  3237. else
  3238. s:=procsym.realname;
  3239. { now we handle the parameters }
  3240. if maxparacount>0 then
  3241. begin
  3242. for i:=0 to paras.count-1 do
  3243. begin
  3244. hp:=tparavarsym(paras[i]);
  3245. { no hidden parameters form part of a C++ mangled name:
  3246. a) self is not included
  3247. b) there are no "high" or other hidden parameters
  3248. }
  3249. if vo_is_hidden_para in hp.varoptions then
  3250. continue;
  3251. s2:=getcppparaname(hp.vardef);
  3252. if hp.varspez in [vs_var,vs_out] then
  3253. s2:='R'+s2;
  3254. s:=s+s2;
  3255. end;
  3256. end
  3257. else
  3258. s:=s+'v';
  3259. cplusplusmangledname:=s;
  3260. {$endif NAMEMANGLING_GCC2}
  3261. end;
  3262. function tprocdef.objcmangledname : string;
  3263. var
  3264. manglednamelen: longint;
  3265. iscatmethod : boolean;
  3266. begin
  3267. if not (po_msgstr in procoptions) then
  3268. internalerror(2009030901);
  3269. { we may very well need longer strings to handle these... }
  3270. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3271. length('+"[ ]"')+length(messageinf.str^);
  3272. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3273. if (iscatmethod) then
  3274. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3275. if manglednamelen>255 then
  3276. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3277. if not(po_classmethod in procoptions) then
  3278. result:='"-['
  3279. else
  3280. result:='"+[';
  3281. { quotes are necessary because the +/- otherwise confuse the assembler
  3282. into expecting a number
  3283. }
  3284. if iscatmethod then
  3285. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3286. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3287. if iscatmethod then
  3288. result:=result+')';
  3289. result:=result+' '+messageinf.str^+']"';
  3290. end;
  3291. procedure tprocdef.setmangledname(const s : string);
  3292. begin
  3293. { This is not allowed anymore, the forward declaration
  3294. already needs to create the correct mangledname, no changes
  3295. afterwards are allowed (PFV) }
  3296. { Exception: interface definitions in mode macpas, since in that }
  3297. { case no reference to the old name can exist yet (JM) }
  3298. if assigned(_mangledname) then
  3299. if ((m_mac in current_settings.modeswitches) and
  3300. (interfacedef)) then
  3301. stringdispose(_mangledname)
  3302. else
  3303. internalerror(200411171);
  3304. {$ifdef compress}
  3305. _mangledname:=stringdup(minilzw_encode(s));
  3306. {$else}
  3307. _mangledname:=stringdup(s);
  3308. {$endif}
  3309. include(procoptions,po_has_mangledname);
  3310. end;
  3311. {***************************************************************************
  3312. TPROCVARDEF
  3313. ***************************************************************************}
  3314. constructor tprocvardef.create(level:byte);
  3315. begin
  3316. inherited create(procvardef,level);
  3317. end;
  3318. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3319. begin
  3320. inherited ppuload(procvardef,ppufile);
  3321. { load para symtable }
  3322. parast:=tparasymtable.create(self,ppufile.getbyte);
  3323. tparasymtable(parast).ppuload(ppufile);
  3324. end;
  3325. function tprocvardef.getcopy : tstoreddef;
  3326. var
  3327. i : tcallercallee;
  3328. j : longint;
  3329. begin
  3330. result:=tprocvardef.create(parast.symtablelevel);
  3331. tprocvardef(result).returndef:=returndef;
  3332. tprocvardef(result).returndefderef:=returndefderef;
  3333. tprocvardef(result).parast:=parast.getcopy;
  3334. tprocvardef(result).savesize:=savesize;
  3335. { create paralist copy }
  3336. tprocvardef(result).paras:=tparalist.create(false);
  3337. tprocvardef(result).paras.count:=paras.count;
  3338. for j:=0 to paras.count-1 do
  3339. tprocvardef(result).paras[j]:=paras[j];
  3340. tprocvardef(result).proctypeoption:=proctypeoption;
  3341. tprocvardef(result).proccalloption:=proccalloption;
  3342. tprocvardef(result).procoptions:=procoptions;
  3343. tprocvardef(result).callerargareasize:=callerargareasize;
  3344. tprocvardef(result).calleeargareasize:=calleeargareasize;
  3345. tprocvardef(result).maxparacount:=maxparacount;
  3346. tprocvardef(result).minparacount:=minparacount;
  3347. for i:=low(tcallercallee) to high(tcallercallee) do
  3348. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  3349. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3350. {$ifdef m68k}
  3351. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3352. {$endif}
  3353. end;
  3354. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3355. begin
  3356. inherited ppuwrite(ppufile);
  3357. { Save the para symtable level (necessary to distinguish nested
  3358. procvars) }
  3359. ppufile.putbyte(parast.symtablelevel);
  3360. { Write this entry }
  3361. ppufile.writeentry(ibprocvardef);
  3362. { Save the para symtable, this is taken from the interface }
  3363. tparasymtable(parast).ppuwrite(ppufile);
  3364. end;
  3365. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3366. begin
  3367. case t of
  3368. gs_para :
  3369. GetSymtable:=parast;
  3370. else
  3371. GetSymtable:=nil;
  3372. end;
  3373. end;
  3374. function tprocvardef.size : aint;
  3375. begin
  3376. if ((po_methodpointer in procoptions) or
  3377. is_nested_pd(self)) and
  3378. not(po_addressonly in procoptions) then
  3379. size:=2*sizeof(pint)
  3380. else
  3381. size:=sizeof(pint);
  3382. end;
  3383. function tprocvardef.is_methodpointer:boolean;
  3384. begin
  3385. result:=(po_methodpointer in procoptions);
  3386. end;
  3387. function tprocvardef.is_addressonly:boolean;
  3388. begin
  3389. result:=(not(po_methodpointer in procoptions) and
  3390. not is_nested_pd(self)) or
  3391. (po_addressonly in procoptions);
  3392. end;
  3393. function tprocvardef.getmangledparaname:string;
  3394. begin
  3395. if not(po_methodpointer in procoptions) then
  3396. if not is_nested_pd(self) then
  3397. result:='procvar'
  3398. else
  3399. result:='nestedprovar'
  3400. else
  3401. result:='procvarofobj'
  3402. end;
  3403. function tprocvardef.is_publishable : boolean;
  3404. begin
  3405. is_publishable:=(po_methodpointer in procoptions);
  3406. end;
  3407. function tprocvardef.GetTypeName : string;
  3408. var
  3409. s: string;
  3410. showhidden : boolean;
  3411. begin
  3412. {$ifdef EXTDEBUG}
  3413. showhidden:=true;
  3414. {$else EXTDEBUG}
  3415. showhidden:=false;
  3416. {$endif EXTDEBUG}
  3417. s:='<';
  3418. if po_classmethod in procoptions then
  3419. s := s+'class method type of'
  3420. else
  3421. if po_addressonly in procoptions then
  3422. s := s+'address of'
  3423. else
  3424. s := s+'procedure variable type of';
  3425. if assigned(returndef) and
  3426. (returndef<>voidtype) then
  3427. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3428. else
  3429. s:=s+' procedure'+typename_paras(showhidden);
  3430. if po_methodpointer in procoptions then
  3431. s := s+' of object';
  3432. if is_nested_pd(self) then
  3433. s := s+' is nested';
  3434. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3435. end;
  3436. {***************************************************************************
  3437. TOBJECTDEF
  3438. ***************************************************************************}
  3439. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  3440. begin
  3441. inherited create(n,objectdef);
  3442. fcurrent_dispid:=0;
  3443. objecttype:=ot;
  3444. childof:=nil;
  3445. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3446. { create space for vmt !! }
  3447. vmtentries:=TFPList.Create;
  3448. vmt_offset:=0;
  3449. set_parent(c);
  3450. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3451. prepareguid;
  3452. { setup implemented interfaces }
  3453. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3454. ImplementedInterfaces:=TFPObjectList.Create(true)
  3455. else
  3456. ImplementedInterfaces:=nil;
  3457. writing_class_record_dbginfo:=false;
  3458. end;
  3459. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3460. var
  3461. i,
  3462. implintfcount : longint;
  3463. d : tderef;
  3464. ImplIntf : TImplementedInterface;
  3465. vmtentry : pvmtentry;
  3466. begin
  3467. inherited ppuload(objectdef,ppufile);
  3468. objecttype:=tobjecttyp(ppufile.getbyte);
  3469. objextname:=stringdup(ppufile.getstring);
  3470. { only used for external Objective-C classes/protocols }
  3471. if (objextname^='') then
  3472. stringdispose(objextname);
  3473. import_lib:=stringdup(ppufile.getstring);
  3474. { only used for external C++ classes }
  3475. if (import_lib^='') then
  3476. stringdispose(import_lib);
  3477. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3478. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3479. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3480. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3481. vmt_offset:=ppufile.getlongint;
  3482. ppufile.getderef(childofderef);
  3483. { load guid }
  3484. iidstr:=nil;
  3485. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3486. begin
  3487. new(iidguid);
  3488. ppufile.getguid(iidguid^);
  3489. iidstr:=stringdup(ppufile.getstring);
  3490. end;
  3491. vmtentries:=TFPList.Create;
  3492. vmtentries.count:=ppufile.getlongint;
  3493. for i:=0 to vmtentries.count-1 do
  3494. begin
  3495. ppufile.getderef(d);
  3496. new(vmtentry);
  3497. vmtentry^.procdef:=nil;
  3498. vmtentry^.procdefderef:=d;
  3499. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3500. vmtentries[i]:=vmtentry;
  3501. end;
  3502. { load implemented interfaces }
  3503. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3504. begin
  3505. ImplementedInterfaces:=TFPObjectList.Create(true);
  3506. implintfcount:=ppufile.getlongint;
  3507. for i:=0 to implintfcount-1 do
  3508. begin
  3509. ppufile.getderef(d);
  3510. ImplIntf:=TImplementedInterface.Create_deref(d);
  3511. ImplIntf.IOffset:=ppufile.getlongint;
  3512. ImplementedInterfaces.Add(ImplIntf);
  3513. end;
  3514. end
  3515. else
  3516. ImplementedInterfaces:=nil;
  3517. if df_copied_def in defoptions then
  3518. ppufile.getderef(cloneddefderef)
  3519. else
  3520. tObjectSymtable(symtable).ppuload(ppufile);
  3521. { handles the predefined class tobject }
  3522. { the last TOBJECT which is loaded gets }
  3523. { it ! }
  3524. if (childof=nil) and
  3525. (objecttype=odt_class) and
  3526. (objname^='TOBJECT') then
  3527. class_tobject:=self;
  3528. if (childof=nil) and
  3529. (objecttype=odt_interfacecom) and
  3530. (objname^='IUNKNOWN') then
  3531. interface_iunknown:=self;
  3532. if (childof=nil) and
  3533. (objecttype=odt_objcclass) and
  3534. (objname^='PROTOCOL') then
  3535. objc_protocoltype:=self;
  3536. writing_class_record_dbginfo:=false;
  3537. end;
  3538. destructor tobjectdef.destroy;
  3539. begin
  3540. if assigned(symtable) then
  3541. begin
  3542. symtable.free;
  3543. symtable:=nil;
  3544. end;
  3545. stringdispose(objextname);
  3546. stringdispose(import_lib);
  3547. stringdispose(iidstr);
  3548. if assigned(ImplementedInterfaces) then
  3549. begin
  3550. ImplementedInterfaces.free;
  3551. ImplementedInterfaces:=nil;
  3552. end;
  3553. if assigned(iidguid) then
  3554. begin
  3555. dispose(iidguid);
  3556. iidguid:=nil;
  3557. end;
  3558. if assigned(vmtentries) then
  3559. begin
  3560. resetvmtentries;
  3561. vmtentries.free;
  3562. vmtentries:=nil;
  3563. end;
  3564. if assigned(vmcallstaticinfo) then
  3565. begin
  3566. freemem(vmcallstaticinfo);
  3567. vmcallstaticinfo:=nil;
  3568. end;
  3569. inherited destroy;
  3570. end;
  3571. function tobjectdef.getcopy : tstoreddef;
  3572. var
  3573. i : longint;
  3574. begin
  3575. result:=tobjectdef.create(objecttype,objrealname^,childof);
  3576. { the constructor allocates a symtable which we release to avoid memory leaks }
  3577. tobjectdef(result).symtable.free;
  3578. tobjectdef(result).symtable:=symtable.getcopy;
  3579. if assigned(objextname) then
  3580. tobjectdef(result).objextname:=stringdup(objextname^);
  3581. if assigned(import_lib) then
  3582. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3583. tobjectdef(result).objectoptions:=objectoptions;
  3584. include(tobjectdef(result).defoptions,df_copied_def);
  3585. tobjectdef(result).vmt_offset:=vmt_offset;
  3586. if assigned(iidguid) then
  3587. begin
  3588. new(tobjectdef(result).iidguid);
  3589. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3590. end;
  3591. if assigned(iidstr) then
  3592. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3593. if assigned(ImplementedInterfaces) then
  3594. begin
  3595. for i:=0 to ImplementedInterfaces.count-1 do
  3596. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3597. end;
  3598. if assigned(vmtentries) then
  3599. begin
  3600. tobjectdef(result).vmtentries:=TFPList.Create;
  3601. tobjectdef(result).copyvmtentries(self);
  3602. end;
  3603. end;
  3604. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3605. var
  3606. i : longint;
  3607. vmtentry : pvmtentry;
  3608. ImplIntf : TImplementedInterface;
  3609. old_do_indirect_crc: boolean;
  3610. begin
  3611. { if class1 in unit A changes, and class2 in unit B inherits from it
  3612. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3613. However, if there is also a class3 in unit C that only depends on
  3614. unit B, then unit C will not be recompiled because nothing changed
  3615. to the interface of unit B. Nevertheless, unit C can indirectly
  3616. depend on unit A via derefs, and these must be updated -> the
  3617. indirect crc keeps track of such changes. }
  3618. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3619. ppufile.do_indirect_crc:=true;
  3620. inherited ppuwrite(ppufile);
  3621. ppufile.putbyte(byte(objecttype));
  3622. if assigned(objextname) then
  3623. ppufile.putstring(objextname^)
  3624. else
  3625. ppufile.putstring('');
  3626. if assigned(import_lib) then
  3627. ppufile.putstring(import_lib^)
  3628. else
  3629. ppufile.putstring('');
  3630. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3631. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3632. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3633. ppufile.putlongint(vmt_offset);
  3634. ppufile.putderef(childofderef);
  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(oo_is_external in pd.struct.objectoptions) then
  4351. begin
  4352. if (po_varargs in pd.procoptions) then
  4353. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4354. else
  4355. begin
  4356. { check for "array of const" parameters }
  4357. for i:=0 to pd.parast.symlist.count-1 do
  4358. begin
  4359. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4360. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4361. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4362. end;
  4363. end;
  4364. end;
  4365. end;
  4366. end;
  4367. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4368. var
  4369. sym: tsym absolute data;
  4370. begin
  4371. if (sym.typ=fieldvarsym) and
  4372. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4373. sym.IncRefCount;
  4374. end;
  4375. procedure tobjectdef.finish_objc_data;
  4376. begin
  4377. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4378. if (oo_is_external in objectoptions) then
  4379. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4380. end;
  4381. procedure verify_objc_vardef(data: tobject; arg: pointer);
  4382. var
  4383. sym: tabstractvarsym absolute data;
  4384. res: pboolean absolute arg;
  4385. founderrordef: tdef;
  4386. begin
  4387. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  4388. exit;
  4389. if (sym.typ=paravarsym) and
  4390. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  4391. is_array_of_const(tparavarsym(sym).vardef)) then
  4392. exit;
  4393. if not objcchecktype(sym.vardef,founderrordef) then
  4394. begin
  4395. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4396. res^:=false;
  4397. end;
  4398. end;
  4399. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  4400. var
  4401. def: tdef absolute data;
  4402. res: pboolean absolute arg;
  4403. founderrordef: tdef;
  4404. begin
  4405. if (def.typ<>procdef) then
  4406. exit;
  4407. { check parameter types for validity }
  4408. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  4409. { check the result type for validity }
  4410. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  4411. begin
  4412. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4413. res^:=false;
  4414. end;
  4415. end;
  4416. function tobjectdef.check_objc_types: boolean;
  4417. begin
  4418. { done in separate step from finish_objc_data, because when
  4419. finish_objc_data is called, not all forwarddefs have been resolved
  4420. yet and we need to know all types here }
  4421. result:=true;
  4422. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  4423. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  4424. end;
  4425. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4426. var
  4427. def: tdef absolute data;
  4428. pd: tprocdef absolute data;
  4429. begin
  4430. if (def.typ=procdef) then
  4431. begin
  4432. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4433. if (oo_is_external in pd.struct.objectoptions) then
  4434. begin
  4435. { copied from psub.read_proc }
  4436. if assigned(tobjectdef(pd.struct).import_lib) then
  4437. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,0,false,false)
  4438. else
  4439. begin
  4440. { add import name to external list for DLL scanning }
  4441. if tf_has_dllscanner in target_info.flags then
  4442. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4443. end;
  4444. end;
  4445. end;
  4446. end;
  4447. procedure tobjectdef.finish_cpp_data;
  4448. begin
  4449. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4450. end;
  4451. {****************************************************************************
  4452. TImplementedInterface
  4453. ****************************************************************************}
  4454. constructor TImplementedInterface.create(aintf: tobjectdef);
  4455. begin
  4456. inherited create;
  4457. intfdef:=aintf;
  4458. IOffset:=-1;
  4459. IType:=etStandard;
  4460. NameMappings:=nil;
  4461. procdefs:=nil;
  4462. end;
  4463. constructor TImplementedInterface.create_deref(d:tderef);
  4464. begin
  4465. inherited create;
  4466. intfdef:=nil;
  4467. intfdefderef:=d;
  4468. IOffset:=-1;
  4469. IType:=etStandard;
  4470. NameMappings:=nil;
  4471. procdefs:=nil;
  4472. end;
  4473. destructor TImplementedInterface.destroy;
  4474. var
  4475. i : longint;
  4476. mappedname : pshortstring;
  4477. begin
  4478. if assigned(NameMappings) then
  4479. begin
  4480. for i:=0 to NameMappings.Count-1 do
  4481. begin
  4482. mappedname:=pshortstring(NameMappings[i]);
  4483. stringdispose(mappedname);
  4484. end;
  4485. NameMappings.free;
  4486. NameMappings:=nil;
  4487. end;
  4488. if assigned(procdefs) then
  4489. begin
  4490. procdefs.free;
  4491. procdefs:=nil;
  4492. end;
  4493. inherited destroy;
  4494. end;
  4495. procedure TImplementedInterface.buildderef;
  4496. begin
  4497. intfdefderef.build(intfdef);
  4498. end;
  4499. procedure TImplementedInterface.deref;
  4500. begin
  4501. intfdef:=tobjectdef(intfdefderef.resolve);
  4502. end;
  4503. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4504. begin
  4505. if not assigned(NameMappings) then
  4506. NameMappings:=TFPHashList.Create;
  4507. NameMappings.Add(origname,stringdup(newname));
  4508. end;
  4509. function TImplementedInterface.GetMapping(const origname: string):string;
  4510. var
  4511. mappedname : pshortstring;
  4512. begin
  4513. result:='';
  4514. if not assigned(NameMappings) then
  4515. exit;
  4516. mappedname:=PShortstring(NameMappings.Find(origname));
  4517. if assigned(mappedname) then
  4518. result:=mappedname^;
  4519. end;
  4520. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4521. begin
  4522. if not assigned(procdefs) then
  4523. procdefs:=TFPObjectList.Create(false);
  4524. { duplicate entries must be stored, because multiple }
  4525. { interfaces can declare methods with the same name }
  4526. { and all of these get their own VMT entry }
  4527. procdefs.Add(pd);
  4528. end;
  4529. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4530. var
  4531. i : longint;
  4532. begin
  4533. result:=false;
  4534. { interfaces being implemented through delegation are not mergable (FK) }
  4535. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  4536. exit;
  4537. weight:=0;
  4538. { empty interface is mergeable }
  4539. if ProcDefs.Count=0 then
  4540. begin
  4541. result:=true;
  4542. exit;
  4543. end;
  4544. { The interface to merge must at least the number of
  4545. procedures of this interface }
  4546. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4547. exit;
  4548. for i:=0 to ProcDefs.Count-1 do
  4549. begin
  4550. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4551. exit;
  4552. end;
  4553. weight:=ProcDefs.Count;
  4554. result:=true;
  4555. end;
  4556. function TImplementedInterface.getcopy:TImplementedInterface;
  4557. begin
  4558. Result:=TImplementedInterface.Create(nil);
  4559. {$warning: this is completely wrong on so many levels...}
  4560. { 1) the procdefs list will be freed once for each copy
  4561. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4562. 3) idem for the name mappings
  4563. }
  4564. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4565. end;
  4566. {****************************************************************************
  4567. TFORWARDDEF
  4568. ****************************************************************************}
  4569. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4570. begin
  4571. inherited create(forwarddef);
  4572. tosymname:=stringdup(s);
  4573. forwardpos:=pos;
  4574. end;
  4575. function tforwarddef.GetTypeName:string;
  4576. begin
  4577. GetTypeName:='unresolved forward to '+tosymname^;
  4578. end;
  4579. destructor tforwarddef.destroy;
  4580. begin
  4581. stringdispose(tosymname);
  4582. inherited destroy;
  4583. end;
  4584. {****************************************************************************
  4585. TUNDEFINEDDEF
  4586. ****************************************************************************}
  4587. constructor tundefineddef.create;
  4588. begin
  4589. inherited create(undefineddef);
  4590. end;
  4591. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4592. begin
  4593. inherited ppuload(undefineddef,ppufile);
  4594. end;
  4595. function tundefineddef.GetTypeName:string;
  4596. begin
  4597. GetTypeName:='<undefined type>';
  4598. end;
  4599. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4600. begin
  4601. inherited ppuwrite(ppufile);
  4602. ppufile.writeentry(ibundefineddef);
  4603. end;
  4604. {****************************************************************************
  4605. TERRORDEF
  4606. ****************************************************************************}
  4607. constructor terrordef.create;
  4608. begin
  4609. inherited create(errordef);
  4610. { prevent consecutive faults }
  4611. savesize:=1;
  4612. end;
  4613. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4614. begin
  4615. { Can't write errordefs to ppu }
  4616. internalerror(200411063);
  4617. end;
  4618. function terrordef.GetTypeName:string;
  4619. begin
  4620. GetTypeName:='<erroneous type>';
  4621. end;
  4622. function terrordef.getmangledparaname:string;
  4623. begin
  4624. getmangledparaname:='error';
  4625. end;
  4626. {****************************************************************************
  4627. Definition Helpers
  4628. ****************************************************************************}
  4629. function is_interfacecom(def: tdef): boolean;
  4630. begin
  4631. is_interfacecom:=
  4632. assigned(def) and
  4633. (def.typ=objectdef) and
  4634. (tobjectdef(def).objecttype=odt_interfacecom);
  4635. end;
  4636. function is_interfacecorba(def: tdef): boolean;
  4637. begin
  4638. is_interfacecorba:=
  4639. assigned(def) and
  4640. (def.typ=objectdef) and
  4641. (tobjectdef(def).objecttype=odt_interfacecorba);
  4642. end;
  4643. function is_interface(def: tdef): boolean;
  4644. begin
  4645. is_interface:=
  4646. assigned(def) and
  4647. (def.typ=objectdef) and
  4648. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4649. end;
  4650. function is_dispinterface(def: tdef): boolean;
  4651. begin
  4652. result:=
  4653. assigned(def) and
  4654. (def.typ=objectdef) and
  4655. (tobjectdef(def).objecttype=odt_dispinterface);
  4656. end;
  4657. function is_class(def: tdef): boolean;
  4658. begin
  4659. is_class:=
  4660. assigned(def) and
  4661. (def.typ=objectdef) and
  4662. (tobjectdef(def).objecttype=odt_class);
  4663. end;
  4664. function is_object(def: tdef): boolean;
  4665. begin
  4666. is_object:=
  4667. assigned(def) and
  4668. (def.typ=objectdef) and
  4669. (tobjectdef(def).objecttype=odt_object);
  4670. end;
  4671. function is_cppclass(def: tdef): boolean;
  4672. begin
  4673. is_cppclass:=
  4674. assigned(def) and
  4675. (def.typ=objectdef) and
  4676. (tobjectdef(def).objecttype=odt_cppclass);
  4677. end;
  4678. function is_objcclass(def: tdef): boolean;
  4679. begin
  4680. is_objcclass:=
  4681. assigned(def) and
  4682. (def.typ=objectdef) and
  4683. (tobjectdef(def).objecttype=odt_objcclass);
  4684. end;
  4685. function is_objcclassref(def: tdef): boolean;
  4686. begin
  4687. is_objcclassref:=
  4688. assigned(def) and
  4689. (def.typ=classrefdef) and
  4690. is_objcclass(tclassrefdef(def).pointeddef);
  4691. end;
  4692. function is_objcprotocol(def: tdef): boolean;
  4693. begin
  4694. result:=
  4695. assigned(def) and
  4696. (def.typ=objectdef) and
  4697. (tobjectdef(def).objecttype=odt_objcprotocol);
  4698. end;
  4699. function is_objccategory(def: tdef): boolean;
  4700. begin
  4701. result:=
  4702. assigned(def) and
  4703. (def.typ=objectdef) and
  4704. { if used as a forward type }
  4705. ((tobjectdef(def).objecttype=odt_objccategory) or
  4706. { if used as after it has been resolved }
  4707. ((tobjectdef(def).objecttype=odt_objcclass) and
  4708. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4709. end;
  4710. function is_objc_class_or_protocol(def: tdef): boolean;
  4711. begin
  4712. result:=
  4713. assigned(def) and
  4714. (def.typ=objectdef) and
  4715. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4716. end;
  4717. function is_objc_protocol_or_category(def: tdef): boolean;
  4718. begin
  4719. result:=
  4720. assigned(def) and
  4721. (def.typ=objectdef) and
  4722. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4723. ((tobjectdef(def).objecttype = odt_objcclass) and
  4724. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4725. end;
  4726. function is_class_or_interface(def: tdef): boolean;
  4727. begin
  4728. result:=
  4729. assigned(def) and
  4730. (def.typ=objectdef) and
  4731. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4732. end;
  4733. function is_class_or_interface_or_objc(def: tdef): boolean;
  4734. begin
  4735. result:=
  4736. assigned(def) and
  4737. (def.typ=objectdef) and
  4738. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4739. end;
  4740. function is_class_or_interface_or_object(def: tdef): boolean;
  4741. begin
  4742. result:=
  4743. assigned(def) and
  4744. (def.typ=objectdef) and
  4745. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4746. end;
  4747. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4748. begin
  4749. result:=
  4750. assigned(def) and
  4751. (def.typ=objectdef) and
  4752. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4753. end;
  4754. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  4755. begin
  4756. result:=
  4757. assigned(def) and
  4758. (def.typ=objectdef) and
  4759. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4760. end;
  4761. function is_class_or_object(def: tdef): boolean;
  4762. begin
  4763. result:=
  4764. assigned(def) and
  4765. (def.typ=objectdef) and
  4766. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  4767. end;
  4768. function is_record(def: tdef): boolean;
  4769. begin
  4770. result:=
  4771. assigned(def) and
  4772. (def.typ=recorddef);
  4773. end;
  4774. procedure loadobjctypes;
  4775. begin
  4776. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  4777. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  4778. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  4779. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  4780. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  4781. end;
  4782. procedure maybeloadcocoatypes;
  4783. var
  4784. tsym: ttypesym;
  4785. begin
  4786. if assigned(objc_fastenumeration) then
  4787. exit;
  4788. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONPROTOCOL',false);
  4789. if assigned(tsym) then
  4790. objc_fastenumeration:=tobjectdef(tsym.typedef)
  4791. else
  4792. objc_fastenumeration:=nil;
  4793. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONSTATE',false);
  4794. if assigned(tsym) then
  4795. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  4796. else
  4797. objc_fastenumerationstate:=nil;
  4798. end;
  4799. function use_vectorfpu(def : tdef) : boolean;
  4800. begin
  4801. {$ifdef x86}
  4802. {$define use_vectorfpuimplemented}
  4803. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  4804. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  4805. {$endif x86}
  4806. {$ifdef arm}
  4807. {$define use_vectorfpuimplemented}
  4808. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  4809. {$endif arm}
  4810. {$ifndef use_vectorfpuimplemented}
  4811. use_vectorfpu:=false;
  4812. {$endif}
  4813. end;
  4814. end.