symdef.pas 178 KB

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