symdef.pas 224 KB

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