symdef.pas 235 KB

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