symdef.pas 223 KB

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