symdef.pas 174 KB

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