symdef.pas 168 KB

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