symdef.pas 173 KB

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