symdef.pas 202 KB

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