symdef.pas 223 KB

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