symdef.pas 234 KB

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