symdef.pas 247 KB

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