symdef.pas 180 KB

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