symdef.pas 180 KB

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