symdef.pas 168 KB

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