symdef.pas 224 KB

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