symdef.pas 235 KB

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