symdef.pas 234 KB

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