symdef.pas 170 KB

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