symdef.pas 233 KB

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