symdef.pas 196 KB

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