symdef.pas 184 KB

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