symdef.pas 222 KB

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