symdef.pas 234 KB

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