symdef.pas 184 KB

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