symdef.pas 203 KB

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