symdef.pas 173 KB

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