symdef.pas 173 KB

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