symdef.pas 193 KB

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