symdef.pas 247 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539
  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)
  1881. { records cannot go into registers on 16 bit targets for now }
  1882. and (sizeof(asizeint)>2)
  1883. and not trecorddef(self).contains_float_field) or
  1884. (recsize <= sizeof(asizeint)))
  1885. and 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_linux,system_x86_64_freebsd,
  2524. system_x86_64_openbsd,system_x86_64_netbsd,
  2525. system_x86_64_solaris,system_x86_64_embedded,
  2526. system_x86_64_dragonfly] then
  2527. savesize:=16
  2528. else
  2529. savesize:=12;
  2530. s64real,
  2531. s64currency,
  2532. s64comp : savesize:=8;
  2533. else
  2534. savesize:=0;
  2535. end;
  2536. end;
  2537. function tfloatdef.getvardef : longint;
  2538. const
  2539. floattype2vardef : array[tfloattype] of longint = (
  2540. varSingle,varDouble,varUndefined,varUndefined,
  2541. varUndefined,varCurrency,varUndefined);
  2542. begin
  2543. if (upper(typename)='TDATETIME') and
  2544. assigned(owner) and
  2545. assigned(owner.name) and
  2546. (owner.name^='SYSTEM') then
  2547. result:=varDate
  2548. else
  2549. result:=floattype2vardef[floattype];
  2550. end;
  2551. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  2552. begin
  2553. inherited ppuwrite(ppufile);
  2554. ppufile.putbyte(byte(floattype));
  2555. writeentry(ppufile,ibfloatdef);
  2556. end;
  2557. function tfloatdef.is_publishable : boolean;
  2558. begin
  2559. is_publishable:=true;
  2560. end;
  2561. function tfloatdef.GetTypeName : string;
  2562. const
  2563. names : array[tfloattype] of string[20] = (
  2564. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  2565. begin
  2566. GetTypeName:=names[floattype];
  2567. end;
  2568. {****************************************************************************
  2569. TFILEDEF
  2570. ****************************************************************************}
  2571. constructor tfiledef.createtext;
  2572. begin
  2573. inherited create(filedef);
  2574. filetyp:=ft_text;
  2575. typedfiledef:=nil;
  2576. end;
  2577. constructor tfiledef.createuntyped;
  2578. begin
  2579. inherited create(filedef);
  2580. filetyp:=ft_untyped;
  2581. typedfiledef:=nil;
  2582. end;
  2583. constructor tfiledef.createtyped(def:tdef);
  2584. begin
  2585. inherited create(filedef);
  2586. filetyp:=ft_typed;
  2587. typedfiledef:=def;
  2588. end;
  2589. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2590. begin
  2591. inherited ppuload(filedef,ppufile);
  2592. filetyp:=tfiletyp(ppufile.getbyte);
  2593. if filetyp=ft_typed then
  2594. ppufile.getderef(typedfiledefderef)
  2595. else
  2596. typedfiledef:=nil;
  2597. ppuload_platform(ppufile);
  2598. end;
  2599. function tfiledef.getcopy : tstoreddef;
  2600. begin
  2601. case filetyp of
  2602. ft_typed:
  2603. result:=cfiledef.createtyped(typedfiledef);
  2604. ft_untyped:
  2605. result:=cfiledef.createuntyped;
  2606. ft_text:
  2607. result:=cfiledef.createtext;
  2608. else
  2609. internalerror(2004121201);
  2610. end;
  2611. end;
  2612. procedure tfiledef.buildderef;
  2613. begin
  2614. inherited buildderef;
  2615. if filetyp=ft_typed then
  2616. typedfiledefderef.build(typedfiledef);
  2617. end;
  2618. procedure tfiledef.deref;
  2619. begin
  2620. inherited deref;
  2621. if filetyp=ft_typed then
  2622. typedfiledef:=tdef(typedfiledefderef.resolve);
  2623. end;
  2624. function tfiledef.size:asizeint;
  2625. begin
  2626. if savesize=0 then
  2627. setsize;
  2628. size:=savesize;
  2629. end;
  2630. procedure tfiledef.setsize;
  2631. begin
  2632. case filetyp of
  2633. ft_text:
  2634. savesize:=search_system_type('TEXTREC').typedef.size;
  2635. ft_typed:
  2636. begin
  2637. savesize:=search_system_type('FILEREC').typedef.size;
  2638. { allocate put/get buffer in iso mode }
  2639. if m_iso in current_settings.modeswitches then
  2640. inc(savesize,typedfiledef.size);
  2641. end;
  2642. ft_untyped:
  2643. savesize:=search_system_type('FILEREC').typedef.size;
  2644. else
  2645. internalerror(2013113001);
  2646. end;
  2647. end;
  2648. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2649. begin
  2650. inherited ppuwrite(ppufile);
  2651. ppufile.putbyte(byte(filetyp));
  2652. if filetyp=ft_typed then
  2653. ppufile.putderef(typedfiledefderef);
  2654. writeentry(ppufile,ibfiledef);
  2655. end;
  2656. function tfiledef.GetTypeName : string;
  2657. begin
  2658. case filetyp of
  2659. ft_untyped:
  2660. GetTypeName:='File';
  2661. ft_typed:
  2662. GetTypeName:='File Of '+typedfiledef.typename;
  2663. ft_text:
  2664. GetTypeName:='Text'
  2665. else
  2666. internalerror(2013113002);
  2667. end;
  2668. end;
  2669. function tfiledef.getmangledparaname : TSymStr;
  2670. begin
  2671. case filetyp of
  2672. ft_untyped:
  2673. getmangledparaname:='FILE';
  2674. ft_typed:
  2675. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2676. ft_text:
  2677. getmangledparaname:='TEXT'
  2678. else
  2679. internalerror(2013113003);
  2680. end;
  2681. end;
  2682. {****************************************************************************
  2683. TVARIANTDEF
  2684. ****************************************************************************}
  2685. constructor tvariantdef.create(v : tvarianttype);
  2686. begin
  2687. inherited create(variantdef);
  2688. varianttype:=v;
  2689. setsize;
  2690. end;
  2691. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2692. begin
  2693. inherited ppuload(variantdef,ppufile);
  2694. varianttype:=tvarianttype(ppufile.getbyte);
  2695. setsize;
  2696. ppuload_platform(ppufile);
  2697. end;
  2698. function tvariantdef.getcopy : tstoreddef;
  2699. begin
  2700. result:=cvariantdef.create(varianttype);
  2701. end;
  2702. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2703. begin
  2704. inherited ppuwrite(ppufile);
  2705. ppufile.putbyte(byte(varianttype));
  2706. writeentry(ppufile,ibvariantdef);
  2707. end;
  2708. function tvariantdef.getvardef : longint;
  2709. begin
  2710. Result:=varVariant;
  2711. end;
  2712. procedure tvariantdef.setsize;
  2713. begin
  2714. {$ifdef cpu64bitaddr}
  2715. savesize:=24;
  2716. {$else cpu64bitaddr}
  2717. savesize:=16;
  2718. {$endif cpu64bitaddr}
  2719. end;
  2720. function tvariantdef.GetTypeName : string;
  2721. begin
  2722. case varianttype of
  2723. vt_normalvariant:
  2724. GetTypeName:='Variant';
  2725. vt_olevariant:
  2726. GetTypeName:='OleVariant';
  2727. else
  2728. internalerror(2013113004);
  2729. end;
  2730. end;
  2731. function tvariantdef.needs_inittable : boolean;
  2732. begin
  2733. needs_inittable:=true;
  2734. end;
  2735. function tvariantdef.is_publishable : boolean;
  2736. begin
  2737. is_publishable:=true;
  2738. end;
  2739. {****************************************************************************
  2740. TABSTRACtpointerdef
  2741. ****************************************************************************}
  2742. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2743. begin
  2744. inherited create(dt);
  2745. pointeddef:=def;
  2746. end;
  2747. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2748. begin
  2749. inherited ppuload(dt,ppufile);
  2750. ppufile.getderef(pointeddefderef);
  2751. end;
  2752. procedure tabstractpointerdef.buildderef;
  2753. begin
  2754. inherited buildderef;
  2755. pointeddefderef.build(pointeddef);
  2756. end;
  2757. procedure tabstractpointerdef.deref;
  2758. begin
  2759. inherited deref;
  2760. pointeddef:=tdef(pointeddefderef.resolve);
  2761. end;
  2762. function tabstractpointerdef.size: asizeint;
  2763. begin
  2764. Result:=voidpointertype.size;
  2765. end;
  2766. function tabstractpointerdef.alignment: shortint;
  2767. begin
  2768. alignment:=size_2_align(voidpointertype.size);
  2769. end;
  2770. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2771. begin
  2772. inherited ppuwrite(ppufile);
  2773. ppufile.putderef(pointeddefderef);
  2774. end;
  2775. {****************************************************************************
  2776. tpointerdef
  2777. ****************************************************************************}
  2778. constructor tpointerdef.create(def:tdef);
  2779. begin
  2780. inherited create(pointerdef,def);
  2781. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2782. // Dump_Stack(Output,get_frame);
  2783. end;
  2784. function tpointerdef.size: asizeint;
  2785. begin
  2786. result:=sizeof(pint);
  2787. end;
  2788. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2789. begin
  2790. inherited ppuload(pointerdef,ppufile);
  2791. has_pointer_math:=(ppufile.getbyte<>0);
  2792. ppuload_platform(ppufile);
  2793. end;
  2794. function tpointerdef.getcopy : tstoreddef;
  2795. begin
  2796. { don't use direct pointeddef if it is a forwarddef because in other case
  2797. one of them will be destroyed on forward type resolve and the second will
  2798. point to garbage }
  2799. if pointeddef.typ=forwarddef then
  2800. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  2801. else
  2802. result:=cpointerdef.create(pointeddef);
  2803. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2804. tpointerdef(result).savesize:=savesize;
  2805. end;
  2806. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2807. begin
  2808. inherited ppuwrite(ppufile);
  2809. ppufile.putbyte(byte(has_pointer_math));
  2810. writeentry(ppufile,ibpointerdef);
  2811. end;
  2812. function tpointerdef.GetTypeName : string;
  2813. begin
  2814. { parameter types and the resultdef of a procvardef can contain a
  2815. pointer to this procvardef itself, resulting in endless recursion ->
  2816. use the typesym's name instead if it exists (if it doesn't, such as
  2817. for anynonymous procedure types in macpas/iso mode, then there cannot
  2818. be any recursive references to it either) }
  2819. if (pointeddef.typ<>procvardef) or
  2820. not assigned(pointeddef.typesym) then
  2821. GetTypeName:='^'+pointeddef.typename
  2822. else
  2823. GetTypeName:='^'+pointeddef.typesym.realname;
  2824. end;
  2825. function tpointerdef.pointer_arithmetic_int_type:tdef;
  2826. begin
  2827. result:=ptrsinttype;
  2828. end;
  2829. function tpointerdef.pointer_subtraction_result_type:tdef;
  2830. begin
  2831. result:=ptrsinttype;
  2832. end;
  2833. {****************************************************************************
  2834. TCLASSREFDEF
  2835. ****************************************************************************}
  2836. constructor tclassrefdef.create(def:tdef);
  2837. begin
  2838. inherited create(classrefdef,def);
  2839. end;
  2840. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2841. begin
  2842. inherited ppuload(classrefdef,ppufile);
  2843. ppuload_platform(ppufile);
  2844. end;
  2845. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2846. begin
  2847. inherited ppuwrite(ppufile);
  2848. writeentry(ppufile,ibclassrefdef);
  2849. end;
  2850. function tclassrefdef.getcopy:tstoreddef;
  2851. begin
  2852. if pointeddef.typ=forwarddef then
  2853. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2854. else
  2855. result:=cclassrefdef.create(pointeddef);
  2856. end;
  2857. function tclassrefdef.GetTypeName : string;
  2858. begin
  2859. GetTypeName:='Class Of '+pointeddef.typename;
  2860. end;
  2861. function tclassrefdef.is_publishable : boolean;
  2862. begin
  2863. result:=true;
  2864. end;
  2865. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2866. begin
  2867. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2868. result:=inherited rtti_mangledname(rt)
  2869. else
  2870. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2871. end;
  2872. procedure tclassrefdef.register_created_object_type;
  2873. begin
  2874. tobjectdef(pointeddef).register_created_classref_type;
  2875. end;
  2876. {***************************************************************************
  2877. TSETDEF
  2878. ***************************************************************************}
  2879. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2880. var
  2881. setallocbits: aint;
  2882. packedsavesize: aint;
  2883. actual_setalloc: ShortInt;
  2884. begin
  2885. inherited create(setdef);
  2886. elementdef:=def;
  2887. setmax:=high;
  2888. actual_setalloc:=current_settings.setalloc;
  2889. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  2890. if actual_setalloc=0 then
  2891. actual_setalloc:=1;
  2892. {$endif}
  2893. if (actual_setalloc=0) then
  2894. begin
  2895. setbase:=0;
  2896. if (high<32) then
  2897. savesize:=Sizeof(longint)
  2898. else if (high<256) then
  2899. savesize:=32
  2900. else
  2901. savesize:=(high+7) div 8
  2902. end
  2903. else
  2904. begin
  2905. setallocbits:=actual_setalloc*8;
  2906. setbase:=low and not(setallocbits-1);
  2907. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2908. savesize:=packedsavesize;
  2909. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  2910. if savesize=3 then
  2911. savesize:=4;
  2912. {$endif}
  2913. end;
  2914. end;
  2915. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2916. begin
  2917. inherited ppuload(setdef,ppufile);
  2918. ppufile.getderef(elementdefderef);
  2919. savesize:=ppufile.getaint;
  2920. setbase:=ppufile.getaint;
  2921. setmax:=ppufile.getaint;
  2922. ppuload_platform(ppufile);
  2923. end;
  2924. function tsetdef.getcopy : tstoreddef;
  2925. begin
  2926. result:=csetdef.create(elementdef,setbase,setmax);
  2927. { the copy might have been created with a different setalloc setting }
  2928. tsetdef(result).savesize:=savesize;
  2929. end;
  2930. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2931. begin
  2932. inherited ppuwrite(ppufile);
  2933. ppufile.putderef(elementdefderef);
  2934. ppufile.putaint(savesize);
  2935. ppufile.putaint(setbase);
  2936. ppufile.putaint(setmax);
  2937. writeentry(ppufile,ibsetdef);
  2938. end;
  2939. procedure tsetdef.buildderef;
  2940. begin
  2941. inherited buildderef;
  2942. elementdefderef.build(elementdef);
  2943. end;
  2944. procedure tsetdef.deref;
  2945. begin
  2946. inherited deref;
  2947. elementdef:=tdef(elementdefderef.resolve);
  2948. end;
  2949. function tsetdef.is_publishable : boolean;
  2950. begin
  2951. is_publishable:=savesize in [1,2,4];
  2952. end;
  2953. function tsetdef.GetTypeName : string;
  2954. begin
  2955. if assigned(elementdef) then
  2956. GetTypeName:='Set Of '+elementdef.typename
  2957. else
  2958. GetTypeName:='Empty Set';
  2959. end;
  2960. {***************************************************************************
  2961. TFORMALDEF
  2962. ***************************************************************************}
  2963. constructor tformaldef.create(Atyped:boolean);
  2964. begin
  2965. inherited create(formaldef);
  2966. typed:=Atyped;
  2967. savesize:=0;
  2968. end;
  2969. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2970. begin
  2971. inherited ppuload(formaldef,ppufile);
  2972. typed:=boolean(ppufile.getbyte);
  2973. savesize:=0;
  2974. ppuload_platform(ppufile);
  2975. end;
  2976. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2977. begin
  2978. inherited ppuwrite(ppufile);
  2979. ppufile.putbyte(byte(typed));
  2980. writeentry(ppufile,ibformaldef);
  2981. end;
  2982. function tformaldef.GetTypeName : string;
  2983. begin
  2984. if typed then
  2985. GetTypeName:='<Typed formal type>'
  2986. else
  2987. GetTypeName:='<Formal type>';
  2988. end;
  2989. {***************************************************************************
  2990. TARRAYDEF
  2991. ***************************************************************************}
  2992. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2993. begin
  2994. inherited create(arraydef);
  2995. lowrange:=l;
  2996. highrange:=h;
  2997. rangedef:=def;
  2998. _elementdef:=nil;
  2999. arrayoptions:=[];
  3000. symtable:=tarraysymtable.create(self);
  3001. end;
  3002. destructor tarraydef.destroy;
  3003. begin
  3004. symtable.free;
  3005. symtable:=nil;
  3006. inherited;
  3007. end;
  3008. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3009. begin
  3010. { divide by the element size and do -1 so the array will have a valid size,
  3011. further, the element size might be 0 e.g. for empty records, so use max(...,1)
  3012. to avoid a division by zero }
  3013. self.create(0,(high(asizeint) div max(def.pointeddef.size,1))-1,ptrsinttype);
  3014. arrayoptions:=[ado_IsConvertedPointer];
  3015. setelementdef(def.pointeddef);
  3016. end;
  3017. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3018. begin
  3019. inherited ppuload(arraydef,ppufile);
  3020. { the addresses are calculated later }
  3021. ppufile.getderef(_elementdefderef);
  3022. ppufile.getderef(rangedefderef);
  3023. lowrange:=ppufile.getasizeint;
  3024. highrange:=ppufile.getasizeint;
  3025. ppufile.getsmallset(arrayoptions);
  3026. ppuload_platform(ppufile);
  3027. symtable:=tarraysymtable.create(self);
  3028. tarraysymtable(symtable).ppuload(ppufile)
  3029. end;
  3030. function tarraydef.getcopy : tstoreddef;
  3031. begin
  3032. result:=carraydef.create(lowrange,highrange,rangedef);
  3033. tarraydef(result).arrayoptions:=arrayoptions;
  3034. tarraydef(result)._elementdef:=_elementdef;
  3035. end;
  3036. procedure tarraydef.buildderef;
  3037. begin
  3038. inherited buildderef;
  3039. tarraysymtable(symtable).buildderef;
  3040. _elementdefderef.build(_elementdef);
  3041. rangedefderef.build(rangedef);
  3042. end;
  3043. procedure tarraydef.deref;
  3044. begin
  3045. inherited deref;
  3046. tarraysymtable(symtable).deref;
  3047. _elementdef:=tdef(_elementdefderef.resolve);
  3048. rangedef:=tdef(rangedefderef.resolve);
  3049. end;
  3050. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3051. begin
  3052. inherited ppuwrite(ppufile);
  3053. ppufile.putderef(_elementdefderef);
  3054. ppufile.putderef(rangedefderef);
  3055. ppufile.putasizeint(lowrange);
  3056. ppufile.putasizeint(highrange);
  3057. ppufile.putsmallset(arrayoptions);
  3058. writeentry(ppufile,ibarraydef);
  3059. tarraysymtable(symtable).ppuwrite(ppufile);
  3060. end;
  3061. function tarraydef.elesize : asizeint;
  3062. begin
  3063. if (ado_IsBitPacked in arrayoptions) then
  3064. internalerror(2006080101);
  3065. if assigned(_elementdef) then
  3066. result:=_elementdef.size
  3067. else
  3068. result:=0;
  3069. end;
  3070. function tarraydef.elepackedbitsize : asizeint;
  3071. begin
  3072. if not(ado_IsBitPacked in arrayoptions) then
  3073. internalerror(2006080102);
  3074. if assigned(_elementdef) then
  3075. result:=_elementdef.packedbitsize
  3076. else
  3077. result:=0;
  3078. end;
  3079. function tarraydef.elecount : asizeuint;
  3080. var
  3081. qhigh,qlow : qword;
  3082. begin
  3083. if ado_IsDynamicArray in arrayoptions then
  3084. begin
  3085. result:=0;
  3086. exit;
  3087. end;
  3088. if (highrange>0) and (lowrange<0) then
  3089. begin
  3090. qhigh:=highrange;
  3091. qlow:=qword(-lowrange);
  3092. { prevent overflow, return 0 to indicate overflow }
  3093. if qhigh+qlow>qword(high(asizeint)-1) then
  3094. result:=0
  3095. else
  3096. result:=qhigh+qlow+1;
  3097. end
  3098. else
  3099. result:=int64(highrange)-lowrange+1;
  3100. end;
  3101. function tarraydef.size : asizeint;
  3102. var
  3103. cachedelecount : asizeuint;
  3104. cachedelesize : asizeint;
  3105. begin
  3106. if ado_IsDynamicArray in arrayoptions then
  3107. begin
  3108. size:=voidpointertype.size;
  3109. exit;
  3110. end;
  3111. { Tarraydef.size may never be called for an open array! }
  3112. if highrange<lowrange then
  3113. internalerror(99080501);
  3114. if not (ado_IsBitPacked in arrayoptions) then
  3115. cachedelesize:=elesize
  3116. else
  3117. cachedelesize := elepackedbitsize;
  3118. cachedelecount:=elecount;
  3119. if (cachedelesize = 0) then
  3120. begin
  3121. size := 0;
  3122. exit;
  3123. end;
  3124. if (cachedelecount = 0) then
  3125. begin
  3126. size := -1;
  3127. exit;
  3128. end;
  3129. { prevent overflow, return -1 to indicate overflow }
  3130. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3131. if (cachedelecount > asizeuint(high(asizeint))) or
  3132. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3133. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3134. accessing the array, see ncgmem (PFV) }
  3135. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3136. begin
  3137. result:=-1;
  3138. exit;
  3139. end;
  3140. result:=cachedelesize*asizeint(cachedelecount);
  3141. if (ado_IsBitPacked in arrayoptions) then
  3142. { can't just add 7 and divide by 8, because that may overflow }
  3143. result:=result div 8 + ord((result mod 8)<>0);
  3144. end;
  3145. procedure tarraydef.setelementdef(def:tdef);
  3146. begin
  3147. _elementdef:=def;
  3148. if not(
  3149. (ado_IsDynamicArray in arrayoptions) or
  3150. (ado_IsConvertedPointer in arrayoptions) or
  3151. (highrange<lowrange)
  3152. ) and
  3153. (size=-1) then
  3154. Message(sym_e_segment_too_large);
  3155. end;
  3156. function tarraydef.alignment : shortint;
  3157. begin
  3158. { alignment of dyn. arrays doesn't depend on the element size }
  3159. if (ado_IsDynamicArray in arrayoptions) then
  3160. alignment:=size_2_align(voidpointertype.size)
  3161. { alignment is the target alignment for the used load size }
  3162. else if (ado_IsBitPacked in arrayoptions) and
  3163. (elementdef.typ in [enumdef,orddef]) then
  3164. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3165. { alignment is the alignment of the elements }
  3166. else
  3167. alignment:=elementdef.alignment
  3168. end;
  3169. function tarraydef.needs_inittable : boolean;
  3170. begin
  3171. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3172. end;
  3173. function tarraydef.needs_separate_initrtti : boolean;
  3174. begin
  3175. if ado_IsBitPacked in arrayoptions then
  3176. result:=false
  3177. else
  3178. result:=elementdef.needs_separate_initrtti;
  3179. end;
  3180. function tarraydef.GetTypeName : string;
  3181. begin
  3182. if (ado_IsConstString in arrayoptions) then
  3183. result:='Constant String'
  3184. else if (ado_isarrayofconst in arrayoptions) or
  3185. (ado_isConstructor in arrayoptions) then
  3186. begin
  3187. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3188. GetTypeName:='Array Of Const'
  3189. else
  3190. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3191. end
  3192. else if (ado_IsDynamicArray in arrayoptions) then
  3193. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3194. else if ((highrange=-1) and (lowrange=0)) then
  3195. GetTypeName:='{Open} Array Of '+elementdef.typename
  3196. else
  3197. begin
  3198. result := '';
  3199. if (ado_IsBitPacked in arrayoptions) then
  3200. result:='BitPacked ';
  3201. if rangedef.typ=enumdef then
  3202. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3203. else
  3204. result:=result+'Array['+tostr(lowrange)+'..'+
  3205. tostr(highrange)+'] Of '+elementdef.typename
  3206. end;
  3207. end;
  3208. function tarraydef.getmangledparaname : TSymStr;
  3209. begin
  3210. if ado_isarrayofconst in arrayoptions then
  3211. getmangledparaname:='array_of_const'
  3212. else
  3213. if ((highrange=-1) and (lowrange=0)) then
  3214. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3215. else
  3216. internalerror(200204176);
  3217. end;
  3218. function tarraydef.is_publishable : boolean;
  3219. begin
  3220. Result:=ado_IsDynamicArray in arrayoptions;
  3221. end;
  3222. {***************************************************************************
  3223. tabstractrecorddef
  3224. ***************************************************************************}
  3225. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  3226. begin
  3227. inherited create(dt);
  3228. objname:=stringdup(upper(n));
  3229. objrealname:=stringdup(n);
  3230. objectoptions:=[];
  3231. if assigned(current_module.namespace) then
  3232. begin
  3233. import_lib:=stringdup(current_module.namespace^);
  3234. replace(import_lib^,'.','/');
  3235. end;
  3236. end;
  3237. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3238. begin
  3239. inherited ppuload(dt,ppufile);
  3240. objrealname:=stringdup(ppufile.getstring);
  3241. objname:=stringdup(upper(objrealname^));
  3242. import_lib:=stringdup(ppufile.getstring);
  3243. { only used for external C++ classes and Java classes/records }
  3244. if (import_lib^='') then
  3245. stringdispose(import_lib);
  3246. ppufile.getsmallset(objectoptions);
  3247. end;
  3248. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  3249. begin
  3250. inherited ppuwrite(ppufile);
  3251. ppufile.putstring(objrealname^);
  3252. if assigned(import_lib) then
  3253. ppufile.putstring(import_lib^)
  3254. else
  3255. ppufile.putstring('');
  3256. ppufile.putsmallset(objectoptions);
  3257. end;
  3258. destructor tabstractrecorddef.destroy;
  3259. begin
  3260. stringdispose(objname);
  3261. stringdispose(objrealname);
  3262. stringdispose(import_lib);
  3263. tcinitcode.free;
  3264. inherited destroy;
  3265. end;
  3266. procedure tabstractrecorddef.buildderefimpl;
  3267. begin
  3268. inherited buildderefimpl;
  3269. if not (df_copied_def in defoptions) then
  3270. tstoredsymtable(symtable).buildderefimpl;
  3271. end;
  3272. procedure tabstractrecorddef.derefimpl;
  3273. begin
  3274. inherited derefimpl;
  3275. if not (df_copied_def in defoptions) then
  3276. tstoredsymtable(symtable).derefimpl;
  3277. end;
  3278. procedure tabstractrecorddef.check_forwards;
  3279. begin
  3280. { the defs of a copied def are defined for the original type only }
  3281. if not(df_copied_def in defoptions) then
  3282. tstoredsymtable(symtable).check_forwards;
  3283. end;
  3284. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3285. var
  3286. i: longint;
  3287. sym: tsym;
  3288. begin
  3289. for i:=0 to symtable.SymList.Count-1 do
  3290. begin
  3291. sym:=tsym(symtable.SymList[i]);
  3292. if sym.typ=procsym then
  3293. begin
  3294. result:=tprocsym(sym).find_procdef_bytype(pt);
  3295. if assigned(result) then
  3296. exit;
  3297. end;
  3298. end;
  3299. result:=nil;
  3300. end;
  3301. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  3302. begin
  3303. if t=gs_record then
  3304. GetSymtable:=symtable
  3305. else
  3306. GetSymtable:=nil;
  3307. end;
  3308. function tabstractrecorddef.is_packed:boolean;
  3309. begin
  3310. result:=tabstractrecordsymtable(symtable).is_packed;
  3311. end;
  3312. function tabstractrecorddef.RttiName: string;
  3313. function generate_full_paramname(maxlength:longint):string;
  3314. const
  3315. commacount : array[boolean] of longint = (0,1);
  3316. var
  3317. fullparas,
  3318. paramname : ansistring;
  3319. module : tmodule;
  3320. sym : ttypesym;
  3321. i : longint;
  3322. begin
  3323. { we want at least enough space for an ellipsis }
  3324. if maxlength<3 then
  3325. internalerror(2014121203);
  3326. fullparas:='';
  3327. for i:=0 to genericparas.count-1 do
  3328. begin
  3329. sym:=ttypesym(genericparas[i]);
  3330. module:=find_module_from_symtable(sym.owner);
  3331. if not assigned(module) then
  3332. internalerror(2014121202);
  3333. paramname:=module.realmodulename^;
  3334. if sym.typedef.typ in [objectdef,recorddef] then
  3335. paramname:=paramname+'.'+tabstractrecorddef(sym.typedef).rttiname
  3336. else
  3337. paramname:=paramname+'.'+sym.typedef.typename;
  3338. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  3339. begin
  3340. if i>0 then
  3341. fullparas:=fullparas+',...'
  3342. else
  3343. fullparas:=fullparas+'...';
  3344. break;
  3345. end;
  3346. { could we fit an ellipsis after this parameter if it should be too long? }
  3347. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  3348. begin
  3349. { then omit already this parameter }
  3350. if i>0 then
  3351. fullparas:=fullparas+',...'
  3352. else
  3353. fullparas:=fullparas+'...';
  3354. break;
  3355. end;
  3356. if i>0 then
  3357. fullparas:=fullparas+',';
  3358. fullparas:=fullparas+paramname;
  3359. end;
  3360. result:=fullparas;
  3361. end;
  3362. var
  3363. nongeneric,
  3364. basename : string;
  3365. i,
  3366. remlength,
  3367. paramcount,
  3368. crcidx : longint;
  3369. begin
  3370. if rttistring='' then
  3371. begin
  3372. if is_specialization then
  3373. begin
  3374. rttistring:=OwnerHierarchyName;
  3375. { there should be two $ characters, one before the CRC and one before the count }
  3376. crcidx:=-1;
  3377. for i:=length(objrealname^) downto 1 do
  3378. if objrealname^[i]='$' then
  3379. begin
  3380. crcidx:=i;
  3381. break;
  3382. end;
  3383. if crcidx<0 then
  3384. internalerror(2014121201);
  3385. basename:=copy(objrealname^,1,crcidx-1);
  3386. split_generic_name(basename,nongeneric,paramcount);
  3387. rttistring:=rttistring+nongeneric+'<';
  3388. remlength:=255-length(rttistring)-1;
  3389. if remlength<4 then
  3390. rttistring:=rttistring+'>'
  3391. else
  3392. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  3393. end
  3394. else
  3395. if is_generic then
  3396. begin
  3397. rttistring:=OwnerHierarchyName;
  3398. split_generic_name(objrealname^,nongeneric,paramcount);
  3399. rttistring:=rttistring+nongeneric+'<';
  3400. { we don't want any ',' if there is only one parameter }
  3401. for i:=0 to paramcount-0 do
  3402. rttistring:=rttistring+',';
  3403. rttistring:=rttistring+'>';
  3404. end
  3405. else
  3406. rttistring:=OwnerHierarchyName+objrealname^;
  3407. end;
  3408. result:=rttistring;
  3409. end;
  3410. function tabstractrecorddef.search_enumerator_get: tprocdef;
  3411. var
  3412. sym : tsym;
  3413. i : integer;
  3414. pd : tprocdef;
  3415. hashedid : THashedIDString;
  3416. begin
  3417. result:=nil;
  3418. hashedid.id:='GETENUMERATOR';
  3419. sym:=tsym(symtable.FindWithHash(hashedid));
  3420. if assigned(sym) and (sym.typ=procsym) then
  3421. begin
  3422. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3423. begin
  3424. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3425. if (pd.proctypeoption = potype_function) and
  3426. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  3427. (pd.visibility >= vis_public) then
  3428. begin
  3429. result:=pd;
  3430. exit;
  3431. end;
  3432. end;
  3433. end;
  3434. end;
  3435. function tabstractrecorddef.search_enumerator_move: tprocdef;
  3436. var
  3437. sym : tsym;
  3438. i : integer;
  3439. pd : tprocdef;
  3440. hashedid : THashedIDString;
  3441. begin
  3442. result:=nil;
  3443. // first search for po_enumerator_movenext method modifier
  3444. // then search for public function MoveNext: Boolean
  3445. for i:=0 to symtable.SymList.Count-1 do
  3446. begin
  3447. sym:=TSym(symtable.SymList[i]);
  3448. if (sym.typ=procsym) then
  3449. begin
  3450. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3451. if assigned(pd) then
  3452. begin
  3453. result:=pd;
  3454. exit;
  3455. end;
  3456. end;
  3457. end;
  3458. hashedid.id:='MOVENEXT';
  3459. sym:=tsym(symtable.FindWithHash(hashedid));
  3460. if assigned(sym) and (sym.typ=procsym) then
  3461. begin
  3462. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  3463. begin
  3464. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3465. if (pd.proctypeoption = potype_function) and
  3466. is_boolean(pd.returndef) and
  3467. (pd.minparacount = 0) and
  3468. (pd.visibility >= vis_public) then
  3469. begin
  3470. result:=pd;
  3471. exit;
  3472. end;
  3473. end;
  3474. end;
  3475. end;
  3476. function tabstractrecorddef.search_enumerator_current: tsym;
  3477. var
  3478. sym: tsym;
  3479. i: integer;
  3480. hashedid : THashedIDString;
  3481. begin
  3482. result:=nil;
  3483. // first search for ppo_enumerator_current property modifier
  3484. // then search for public property Current
  3485. for i:=0 to symtable.SymList.Count-1 do
  3486. begin
  3487. sym:=TSym(symtable.SymList[i]);
  3488. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  3489. begin
  3490. result:=sym;
  3491. exit;
  3492. end;
  3493. end;
  3494. hashedid.id:='CURRENT';
  3495. sym:=tsym(symtable.FindWithHash(hashedid));
  3496. if assigned(sym) and (sym.typ=propertysym) and
  3497. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  3498. begin
  3499. result:=sym;
  3500. exit;
  3501. end;
  3502. end;
  3503. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  3504. var
  3505. st: tsymtable;
  3506. enclosingdef: tdef;
  3507. begin
  3508. if typ=objectdef then
  3509. result:=tobjectdef(self).objextname^
  3510. else if assigned(typesym) then
  3511. result:=typesym.realname
  3512. { have to generate anonymous nested type in current unit/class/record }
  3513. else
  3514. internalerror(2011032601);
  3515. { in case of specializations, add some extras to prevent name conflicts
  3516. with nested classes }
  3517. if df_specialization in defoptions then
  3518. result:='$'+result+'$specialization$';
  3519. st:=owner;
  3520. while assigned(st) and
  3521. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  3522. begin
  3523. { nested classes are named as "OuterClass$InnerClass" }
  3524. enclosingdef:=tdef(st.defowner);
  3525. if enclosingdef.typ=procdef then
  3526. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  3527. else if enclosingdef.typ=objectdef then
  3528. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  3529. else if assigned(enclosingdef.typesym) then
  3530. result:=enclosingdef.typesym.realname+'$'+result
  3531. else
  3532. internalerror(2011060305);
  3533. st:=enclosingdef.owner;
  3534. end;
  3535. if with_package_name and
  3536. assigned(import_lib) then
  3537. result:=import_lib^+'/'+result;
  3538. end;
  3539. function tabstractrecorddef.contains_float_field: boolean;
  3540. var
  3541. i : longint;
  3542. begin
  3543. result:=true;
  3544. for i:=0 to symtable.symlist.count-1 do
  3545. begin
  3546. if tsym(symtable.symlist[i]).typ<>fieldvarsym then
  3547. continue;
  3548. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) and
  3549. tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  3550. exit;
  3551. end;
  3552. result:=false;
  3553. end;
  3554. {***************************************************************************
  3555. trecorddef
  3556. ***************************************************************************}
  3557. constructor trecorddef.create(const n:string; p:TSymtable);
  3558. begin
  3559. inherited create(n,recorddef);
  3560. symtable:=p;
  3561. { we can own the symtable only if nobody else owns a copy so far }
  3562. if symtable.refcount=1 then
  3563. symtable.defowner:=self;
  3564. isunion:=false;
  3565. end;
  3566. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  3567. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  3568. var
  3569. i,j : longint;
  3570. begin
  3571. if ppufile.getbyte=1 then
  3572. begin
  3573. new(variantrecdesc);
  3574. ppufile.getderef(variantrecdesc^.variantselectorderef);
  3575. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  3576. for i:=0 to high(variantrecdesc^.branches) do
  3577. begin
  3578. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  3579. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3580. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  3581. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3582. end;
  3583. end
  3584. else
  3585. variantrecdesc:=nil;
  3586. end;
  3587. begin
  3588. inherited ppuload(recorddef,ppufile);
  3589. if df_copied_def in defoptions then
  3590. begin
  3591. ppufile.getderef(cloneddefderef);
  3592. ppuload_platform(ppufile);
  3593. end
  3594. else
  3595. begin
  3596. ppuload_platform(ppufile);
  3597. symtable:=trecordsymtable.create(objrealname^,0);
  3598. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  3599. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  3600. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  3601. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  3602. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  3603. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  3604. trecordsymtable(symtable).ppuload(ppufile);
  3605. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3606. but because iso mode supports no units, there is no need to store the variantrecdesc
  3607. in the ppu
  3608. }
  3609. // readvariantrecdesc(variantrecdesc);
  3610. { requires usefieldalignment to be set }
  3611. symtable.defowner:=self;
  3612. end;
  3613. isunion:=false;
  3614. end;
  3615. destructor trecorddef.destroy;
  3616. begin
  3617. if assigned(symtable) then
  3618. begin
  3619. symtable.free;
  3620. symtable:=nil;
  3621. end;
  3622. inherited destroy;
  3623. end;
  3624. function trecorddef.getcopy : tstoreddef;
  3625. begin
  3626. result:=crecorddef.create(objrealname^,symtable.getcopy);
  3627. trecorddef(result).isunion:=isunion;
  3628. include(trecorddef(result).defoptions,df_copied_def);
  3629. if assigned(tcinitcode) then
  3630. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  3631. if assigned(import_lib) then
  3632. trecorddef(result).import_lib:=stringdup(import_lib^);
  3633. end;
  3634. function trecorddef.needs_inittable : boolean;
  3635. begin
  3636. needs_inittable:=trecordsymtable(symtable).needs_init_final
  3637. end;
  3638. function trecorddef.needs_separate_initrtti : boolean;
  3639. begin
  3640. result:=true;
  3641. end;
  3642. procedure trecorddef.buildderef;
  3643. begin
  3644. inherited buildderef;
  3645. if df_copied_def in defoptions then
  3646. cloneddefderef.build(symtable.defowner)
  3647. else
  3648. tstoredsymtable(symtable).buildderef;
  3649. end;
  3650. procedure trecorddef.deref;
  3651. begin
  3652. inherited deref;
  3653. { now dereference the definitions }
  3654. if df_copied_def in defoptions then
  3655. begin
  3656. cloneddef:=trecorddef(cloneddefderef.resolve);
  3657. symtable:=cloneddef.symtable.getcopy;
  3658. end
  3659. else
  3660. tstoredsymtable(symtable).deref;
  3661. { assign TGUID? load only from system unit }
  3662. if not(assigned(rec_tguid)) and
  3663. (upper(typename)='TGUID') and
  3664. assigned(owner) and
  3665. assigned(owner.name) and
  3666. (owner.name^='SYSTEM') then
  3667. rec_tguid:=self;
  3668. { assign JMP_BUF? load only from system unit }
  3669. if not(assigned(rec_jmp_buf)) and
  3670. (upper(typename)='JMP_BUF') and
  3671. assigned(owner) and
  3672. assigned(owner.name) and
  3673. (owner.name^='SYSTEM') then
  3674. rec_jmp_buf:=self;
  3675. end;
  3676. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  3677. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  3678. var
  3679. i,j : longint;
  3680. begin
  3681. if assigned(variantrecdesc) then
  3682. begin
  3683. ppufile.putbyte(1);
  3684. ppufile.putderef(variantrecdesc^.variantselectorderef);
  3685. ppufile.putasizeint(length(variantrecdesc^.branches));
  3686. for i:=0 to high(variantrecdesc^.branches) do
  3687. begin
  3688. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  3689. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3690. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  3691. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3692. end;
  3693. end
  3694. else
  3695. ppufile.putbyte(0);
  3696. end;
  3697. begin
  3698. inherited ppuwrite(ppufile);
  3699. if df_copied_def in defoptions then
  3700. ppufile.putderef(cloneddefderef)
  3701. else
  3702. begin
  3703. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  3704. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  3705. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  3706. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  3707. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  3708. ppufile.putword(trecordsymtable(symtable).paddingsize);
  3709. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3710. but because iso mode supports no units, there is no need to store the variantrecdesc
  3711. in the ppu
  3712. }
  3713. // writevariantrecdesc(variantrecdesc);
  3714. end;
  3715. writeentry(ppufile,ibrecorddef);
  3716. if not(df_copied_def in defoptions) then
  3717. trecordsymtable(symtable).ppuwrite(ppufile);
  3718. end;
  3719. function trecorddef.size:asizeint;
  3720. begin
  3721. result:=trecordsymtable(symtable).datasize;
  3722. end;
  3723. function trecorddef.alignment:shortint;
  3724. begin
  3725. alignment:=trecordsymtable(symtable).recordalignment;
  3726. end;
  3727. function trecorddef.padalignment:shortint;
  3728. begin
  3729. padalignment := trecordsymtable(symtable).padalignment;
  3730. end;
  3731. function trecorddef.GetTypeName : string;
  3732. begin
  3733. GetTypeName:='<record type>'
  3734. end;
  3735. {***************************************************************************
  3736. TABSTRACTPROCDEF
  3737. ***************************************************************************}
  3738. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  3739. begin
  3740. inherited create(dt);
  3741. parast:=tparasymtable.create(self,level);
  3742. paras:=nil;
  3743. minparacount:=0;
  3744. maxparacount:=0;
  3745. proctypeoption:=potype_none;
  3746. proccalloption:=pocall_none;
  3747. procoptions:=[];
  3748. returndef:=voidtype;
  3749. savesize:=sizeof(pint);
  3750. callerargareasize:=0;
  3751. calleeargareasize:=0;
  3752. has_paraloc_info:=callnoside;
  3753. funcretloc[callerside].init;
  3754. funcretloc[calleeside].init;
  3755. check_mark_as_nested;
  3756. end;
  3757. destructor tabstractprocdef.destroy;
  3758. begin
  3759. if assigned(paras) then
  3760. begin
  3761. {$ifdef MEMDEBUG}
  3762. memprocpara.start;
  3763. {$endif MEMDEBUG}
  3764. paras.free;
  3765. paras:=nil;
  3766. {$ifdef MEMDEBUG}
  3767. memprocpara.stop;
  3768. {$endif MEMDEBUG}
  3769. end;
  3770. if assigned(parast) then
  3771. begin
  3772. {$ifdef MEMDEBUG}
  3773. memprocparast.start;
  3774. {$endif MEMDEBUG}
  3775. parast.free;
  3776. parast:=nil;
  3777. {$ifdef MEMDEBUG}
  3778. memprocparast.stop;
  3779. {$endif MEMDEBUG}
  3780. end;
  3781. funcretloc[callerside].done;
  3782. funcretloc[calleeside].done;
  3783. inherited destroy;
  3784. end;
  3785. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  3786. begin
  3787. if (tsym(p).typ<>paravarsym) then
  3788. exit;
  3789. inc(plongint(arg)^);
  3790. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  3791. begin
  3792. if not assigned(tparavarsym(p).defaultconstsym) then
  3793. inc(minparacount);
  3794. inc(maxparacount);
  3795. end;
  3796. end;
  3797. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  3798. begin
  3799. if (tsym(p).typ<>paravarsym) then
  3800. exit;
  3801. paras.add(p);
  3802. end;
  3803. procedure tabstractprocdef.calcparas;
  3804. var
  3805. paracount : longint;
  3806. begin
  3807. { This can already be assigned when
  3808. we need to reresolve this unit (PFV) }
  3809. if assigned(paras) then
  3810. paras.free;
  3811. paras:=tparalist.create(false);
  3812. paracount:=0;
  3813. minparacount:=0;
  3814. maxparacount:=0;
  3815. parast.SymList.ForEachCall(@count_para,@paracount);
  3816. paras.capacity:=paracount;
  3817. { Insert parameters in table }
  3818. parast.SymList.ForEachCall(@insert_para,nil);
  3819. { Order parameters }
  3820. paras.sortparas;
  3821. end;
  3822. procedure tabstractprocdef.buildderef;
  3823. begin
  3824. { released procdef? }
  3825. if not assigned(parast) then
  3826. exit;
  3827. inherited buildderef;
  3828. returndefderef.build(returndef);
  3829. if po_explicitparaloc in procoptions then
  3830. funcretloc[callerside].buildderef;
  3831. { parast }
  3832. tparasymtable(parast).buildderef;
  3833. end;
  3834. procedure tabstractprocdef.deref;
  3835. begin
  3836. inherited deref;
  3837. returndef:=tdef(returndefderef.resolve);
  3838. if po_explicitparaloc in procoptions then
  3839. begin
  3840. funcretloc[callerside].deref;
  3841. has_paraloc_info:=callerside;
  3842. end
  3843. else
  3844. begin
  3845. { deref is called after loading from a ppu, but also after another
  3846. unit has been reloaded/recompiled and all references must be
  3847. re-resolved. Since the funcretloc contains a reference to a tdef,
  3848. reset it so that we won't try to access the stale def }
  3849. funcretloc[callerside].init;
  3850. has_paraloc_info:=callnoside;
  3851. end;
  3852. { parast }
  3853. tparasymtable(parast).deref;
  3854. { recalculated parameters }
  3855. calcparas;
  3856. end;
  3857. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3858. begin
  3859. inherited ppuload(dt,ppufile);
  3860. parast:=nil;
  3861. Paras:=nil;
  3862. minparacount:=0;
  3863. maxparacount:=0;
  3864. ppufile.getderef(returndefderef);
  3865. proctypeoption:=tproctypeoption(ppufile.getbyte);
  3866. proccalloption:=tproccalloption(ppufile.getbyte);
  3867. ppufile.getnormalset(procoptions);
  3868. funcretloc[callerside].init;
  3869. if po_explicitparaloc in procoptions then
  3870. funcretloc[callerside].ppuload(ppufile);
  3871. savesize:=sizeof(pint);
  3872. if (po_explicitparaloc in procoptions) then
  3873. has_paraloc_info:=callerside;
  3874. end;
  3875. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  3876. var
  3877. oldintfcrc : boolean;
  3878. begin
  3879. { released procdef? }
  3880. if not assigned(parast) then
  3881. exit;
  3882. inherited ppuwrite(ppufile);
  3883. ppufile.putderef(returndefderef);
  3884. oldintfcrc:=ppufile.do_interface_crc;
  3885. ppufile.do_interface_crc:=false;
  3886. ppufile.putbyte(ord(proctypeoption));
  3887. ppufile.putbyte(ord(proccalloption));
  3888. ppufile.putnormalset(procoptions);
  3889. ppufile.do_interface_crc:=oldintfcrc;
  3890. if (po_explicitparaloc in procoptions) then
  3891. funcretloc[callerside].ppuwrite(ppufile);
  3892. end;
  3893. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  3894. var
  3895. hs,s : ansistring;
  3896. hp : TParavarsym;
  3897. hpc : tconstsym;
  3898. first : boolean;
  3899. i : integer;
  3900. begin
  3901. s:='';
  3902. first:=true;
  3903. for i:=0 to paras.count-1 do
  3904. begin
  3905. hp:=tparavarsym(paras[i]);
  3906. if not(vo_is_hidden_para in hp.varoptions) or
  3907. (pno_showhidden in pno) then
  3908. begin
  3909. if first then
  3910. begin
  3911. s:=s+'(';
  3912. first:=false;
  3913. end
  3914. else
  3915. s:=s+';';
  3916. if vo_is_hidden_para in hp.varoptions then
  3917. s:=s+'<';
  3918. case hp.varspez of
  3919. vs_var :
  3920. s:=s+'var ';
  3921. vs_const :
  3922. s:=s+'const ';
  3923. vs_out :
  3924. s:=s+'out ';
  3925. vs_constref :
  3926. s:=s+'constref ';
  3927. end;
  3928. if (pno_paranames in pno) then
  3929. s:=s+hp.realname+':';
  3930. if hp.univpara then
  3931. s:=s+'univ ';
  3932. if assigned(hp.vardef.typesym) then
  3933. begin
  3934. hs:=hp.vardef.typesym.realname;
  3935. if hs[1]<>'$' then
  3936. s:=s+hp.vardef.OwnerHierarchyName+hs
  3937. else
  3938. s:=s+hp.vardef.GetTypeName;
  3939. end
  3940. else
  3941. s:=s+hp.vardef.GetTypeName;
  3942. { default value }
  3943. if assigned(hp.defaultconstsym) then
  3944. begin
  3945. hpc:=tconstsym(hp.defaultconstsym);
  3946. hs:='';
  3947. case hpc.consttyp of
  3948. conststring,
  3949. constresourcestring :
  3950. begin
  3951. If hpc.value.len>0 then
  3952. begin
  3953. setLength(hs,hpc.value.len);
  3954. { don't write past the end of hs if the constant
  3955. is > 255 chars }
  3956. move(hpc.value.valueptr^,hs[1],length(hs));
  3957. { make sure that constant strings with newline chars
  3958. don't create a linebreak in the assembler code,
  3959. since comments are line-based. Also remove nulls
  3960. because the comments are written as a pchar. }
  3961. ReplaceCase(hs,#0,'.');
  3962. ReplaceCase(hs,#10,'.');
  3963. ReplaceCase(hs,#13,'.');
  3964. end;
  3965. end;
  3966. constreal :
  3967. str(pbestreal(hpc.value.valueptr)^,hs);
  3968. constpointer :
  3969. hs:=tostr(hpc.value.valueordptr);
  3970. constord :
  3971. begin
  3972. if is_boolean(hpc.constdef) then
  3973. begin
  3974. if hpc.value.valueord<>0 then
  3975. hs:='TRUE'
  3976. else
  3977. hs:='FALSE';
  3978. end
  3979. else
  3980. hs:=tostr(hpc.value.valueord);
  3981. end;
  3982. constnil :
  3983. hs:='nil';
  3984. constset :
  3985. hs:='<set>';
  3986. end;
  3987. if hs<>'' then
  3988. s:=s+'="'+hs+'"';
  3989. end;
  3990. if vo_is_hidden_para in hp.varoptions then
  3991. s:=s+'>';
  3992. end;
  3993. end;
  3994. if not first then
  3995. s:=s+')';
  3996. if (po_varargs in procoptions) then
  3997. s:=s+';VarArgs';
  3998. typename_paras:=s;
  3999. end;
  4000. function tabstractprocdef.is_methodpointer:boolean;
  4001. begin
  4002. result:=false;
  4003. end;
  4004. function tabstractprocdef.is_addressonly:boolean;
  4005. begin
  4006. result:=true;
  4007. end;
  4008. function tabstractprocdef.no_self_node: boolean;
  4009. begin
  4010. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  4011. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  4012. end;
  4013. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  4014. var
  4015. j, nestinglevel: longint;
  4016. pvs, npvs: tparavarsym;
  4017. begin
  4018. nestinglevel:=parast.symtablelevel;
  4019. if newtyp=procdef then
  4020. begin
  4021. if (copytyp<>pc_bareproc) then
  4022. result:=cprocdef.create(nestinglevel)
  4023. else
  4024. result:=cprocdef.create(normal_function_level);
  4025. tprocdef(result).visibility:=vis_public;
  4026. end
  4027. else
  4028. begin
  4029. result:=cprocvardef.create(nestinglevel);
  4030. end;
  4031. tabstractprocdef(result).returndef:=returndef;
  4032. tabstractprocdef(result).returndefderef:=returndefderef;
  4033. pvs:=nil;
  4034. npvs:=nil;
  4035. for j:=0 to parast.symlist.count-1 do
  4036. begin
  4037. case tsym(parast.symlist[j]).typ of
  4038. paravarsym:
  4039. begin
  4040. pvs:=tparavarsym(parast.symlist[j]);
  4041. { in case of bare proc, don't copy self, vmt or framepointer
  4042. parameters }
  4043. if (copytyp=pc_bareproc) and
  4044. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  4045. continue;
  4046. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  4047. pvs.vardef,pvs.varoptions);
  4048. npvs.defaultconstsym:=pvs.defaultconstsym;
  4049. tabstractprocdef(result).parast.insert(npvs);
  4050. end;
  4051. constsym:
  4052. begin
  4053. // ignore, reuse original constym. Should also be duplicated
  4054. // be safe though
  4055. end;
  4056. symconst.typesym:
  4057. begin
  4058. // reuse original, part of generic declaration
  4059. end
  4060. else
  4061. internalerror(201160604);
  4062. end;
  4063. end;
  4064. tabstractprocdef(result).savesize:=savesize;
  4065. tabstractprocdef(result).proctypeoption:=proctypeoption;
  4066. tabstractprocdef(result).proccalloption:=proccalloption;
  4067. tabstractprocdef(result).procoptions:=procoptions;
  4068. if (copytyp=pc_bareproc) then
  4069. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  4070. if newtyp=procvardef then
  4071. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  4072. tabstractprocdef(result).callerargareasize:=callerargareasize;
  4073. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  4074. tabstractprocdef(result).maxparacount:=maxparacount;
  4075. tabstractprocdef(result).minparacount:=minparacount;
  4076. if po_explicitparaloc in procoptions then
  4077. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  4078. { recalculate parameter info }
  4079. tabstractprocdef(result).has_paraloc_info:=callnoside;
  4080. {$ifdef m68k}
  4081. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  4082. {$endif}
  4083. if (typ=procdef) and
  4084. (newtyp=procvardef) and
  4085. (owner.symtabletype=ObjectSymtable) then
  4086. include(tprocvardef(result).procoptions,po_methodpointer);
  4087. end;
  4088. procedure tabstractprocdef.check_mark_as_nested;
  4089. begin
  4090. { nested procvars require that nested functions use the Delphi-style
  4091. nested procedure calling convention }
  4092. if (parast.symtablelevel>normal_function_level) and
  4093. (m_nested_procvars in current_settings.modeswitches) then
  4094. include(procoptions,po_delphi_nested_cc);
  4095. end;
  4096. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  4097. begin
  4098. if (side in [callerside,callbothsides]) and
  4099. not(has_paraloc_info in [callerside,callbothsides]) then
  4100. begin
  4101. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  4102. if has_paraloc_info in [calleeside,callbothsides] then
  4103. has_paraloc_info:=callbothsides
  4104. else
  4105. has_paraloc_info:=callerside;
  4106. end;
  4107. if (side in [calleeside,callbothsides]) and
  4108. not(has_paraloc_info in [calleeside,callbothsides]) then
  4109. begin
  4110. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  4111. if has_paraloc_info in [callerside,callbothsides] then
  4112. has_paraloc_info:=callbothsides
  4113. else
  4114. has_paraloc_info:=calleeside;
  4115. end;
  4116. end;
  4117. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  4118. var
  4119. p: tparavarsym;
  4120. ploc: PCGParalocation;
  4121. i: longint;
  4122. begin
  4123. result:=false;
  4124. init_paraloc_info(side);
  4125. for i:=0 to parast.SymList.Count-1 do
  4126. if tsym(parast.SymList[i]).typ=paravarsym then
  4127. begin
  4128. p:=tparavarsym(parast.SymList[i]);
  4129. { check if no parameter is located on the stack }
  4130. if is_open_array(p.vardef) or
  4131. is_array_of_const(p.vardef) then
  4132. begin
  4133. result:=true;
  4134. exit;
  4135. end;
  4136. ploc:=p.paraloc[side].location;
  4137. while assigned(ploc) do
  4138. begin
  4139. if (ploc^.loc=LOC_REFERENCE) then
  4140. begin
  4141. result:=true;
  4142. exit
  4143. end;
  4144. ploc:=ploc^.next;
  4145. end;
  4146. end;
  4147. end;
  4148. function tabstractprocdef.is_pushleftright: boolean;
  4149. begin
  4150. result:=false;
  4151. end;
  4152. function tabstractprocdef.address_type: tdef;
  4153. begin
  4154. result:=voidcodepointertype;
  4155. end;
  4156. procedure tabstractprocdef.declared_far;
  4157. begin
  4158. Message1(parser_w_proc_directive_ignored,'FAR');
  4159. end;
  4160. procedure tabstractprocdef.declared_near;
  4161. begin
  4162. Message1(parser_w_proc_directive_ignored,'NEAR');
  4163. end;
  4164. {***************************************************************************
  4165. TPROCDEF
  4166. ***************************************************************************}
  4167. function tprocdef.GetResultName: PShortString;
  4168. begin
  4169. if not assigned(implprocdefinfo) then
  4170. internalerror(2014010301);
  4171. result:=implprocdefinfo^.resultname;
  4172. end;
  4173. procedure tprocdef.SetResultName(AValue: PShortString);
  4174. begin
  4175. if not assigned(implprocdefinfo) then
  4176. internalerror(2014010302);
  4177. implprocdefinfo^.resultname:=AValue;
  4178. end;
  4179. function tprocdef.GetParentFPInitBlock: tnode;
  4180. begin
  4181. if not assigned(implprocdefinfo) then
  4182. internalerror(2014010303);
  4183. result:=implprocdefinfo^.parentfpinitblock;
  4184. end;
  4185. function tprocdef.GetParentFPStruct: tsym;
  4186. begin
  4187. if not assigned(implprocdefinfo) then
  4188. internalerror(2014010304);
  4189. result:=implprocdefinfo^.parentfpstruct;
  4190. end;
  4191. function tprocdef.GetParentFPStructPtrType: tdef;
  4192. begin
  4193. if not assigned(implprocdefinfo) then
  4194. internalerror(2014010305);
  4195. result:=implprocdefinfo^.parentfpstructptrtype;
  4196. end;
  4197. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  4198. begin
  4199. if not assigned(implprocdefinfo) then
  4200. internalerror(2014010306);
  4201. implprocdefinfo^.parentfpinitblock:=AValue;
  4202. end;
  4203. function tprocdef.Getprocendtai: tai;
  4204. begin
  4205. if not assigned(implprocdefinfo) then
  4206. internalerror(2014010307);
  4207. result:=implprocdefinfo^.procendtai;
  4208. end;
  4209. function tprocdef.Getprocstarttai: tai;
  4210. begin
  4211. if not assigned(implprocdefinfo) then
  4212. internalerror(2014010308);
  4213. result:=implprocdefinfo^.procstarttai;
  4214. end;
  4215. procedure tprocdef.Setprocendtai(AValue: tai);
  4216. begin
  4217. if not assigned(implprocdefinfo) then
  4218. internalerror(2014010309);
  4219. implprocdefinfo^.procendtai:=AValue;
  4220. end;
  4221. function tprocdef.Getskpara: pointer;
  4222. begin
  4223. if not assigned(implprocdefinfo) then
  4224. internalerror(2014010310);
  4225. result:=implprocdefinfo^.skpara;
  4226. end;
  4227. procedure tprocdef.Setskpara(AValue: pointer);
  4228. begin
  4229. if not assigned(implprocdefinfo) then
  4230. internalerror(2014010311);
  4231. implprocdefinfo^.skpara:=AValue;
  4232. end;
  4233. function tprocdef.Getforwarddef: boolean;
  4234. begin
  4235. if not assigned(implprocdefinfo) then
  4236. internalerror(2014010312);
  4237. result:=implprocdefinfo^.forwarddef;
  4238. end;
  4239. function tprocdef.Gethasforward: boolean;
  4240. begin
  4241. if not assigned(implprocdefinfo) then
  4242. internalerror(2014010313);
  4243. result:=implprocdefinfo^.hasforward;
  4244. end;
  4245. function tprocdef.Getinterfacedef: boolean;
  4246. begin
  4247. if not assigned(implprocdefinfo) then
  4248. internalerror(2014010314);
  4249. result:=implprocdefinfo^.interfacedef;
  4250. end;
  4251. procedure tprocdef.Setforwarddef(AValue: boolean);
  4252. begin
  4253. if not assigned(implprocdefinfo) then
  4254. internalerror(2014010315);
  4255. implprocdefinfo^.forwarddef:=AValue;
  4256. end;
  4257. procedure tprocdef.Sethasforward(AValue: boolean);
  4258. begin
  4259. if not assigned(implprocdefinfo) then
  4260. internalerror(2014010316);
  4261. implprocdefinfo^.hasforward:=AValue;
  4262. end;
  4263. function tprocdef.GetIsEmpty: boolean;
  4264. begin
  4265. result:=pio_empty in implprocoptions;
  4266. end;
  4267. procedure tprocdef.SetIsEmpty(AValue: boolean);
  4268. begin
  4269. if AValue then
  4270. include(implprocoptions,pio_empty)
  4271. else
  4272. include(implprocoptions,pio_empty);
  4273. end;
  4274. function tprocdef.GetHasInliningInfo: boolean;
  4275. begin
  4276. result:=pio_has_inlininginfo in implprocoptions;
  4277. end;
  4278. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  4279. begin
  4280. if AValue then
  4281. include(implprocoptions,pio_has_inlininginfo)
  4282. else
  4283. exclude(implprocoptions,pio_has_inlininginfo);
  4284. end;
  4285. procedure tprocdef.Setinterfacedef(AValue: boolean);
  4286. begin
  4287. if not assigned(implprocdefinfo) then
  4288. internalerror(2014010317);
  4289. implprocdefinfo^.interfacedef:=AValue;
  4290. end;
  4291. procedure tprocdef.Setprocstarttai(AValue: tai);
  4292. begin
  4293. if not assigned(implprocdefinfo) then
  4294. internalerror(2014010318);
  4295. implprocdefinfo^.procstarttai:=AValue;
  4296. end;
  4297. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  4298. begin
  4299. if not assigned(implprocdefinfo) then
  4300. internalerror(2014010319);
  4301. implprocdefinfo^.parentfpstruct:=AValue;
  4302. end;
  4303. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  4304. begin
  4305. if not assigned(implprocdefinfo) then
  4306. internalerror(2014010320);
  4307. implprocdefinfo^.parentfpstructptrtype:=AValue;
  4308. end;
  4309. constructor tprocdef.create(level:byte);
  4310. begin
  4311. inherited create(procdef,level);
  4312. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  4313. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  4314. {$ifdef symansistr}
  4315. _mangledname:='';
  4316. {$else symansistr}
  4317. _mangledname:=nil;
  4318. {$endif symansistr}
  4319. fileinfo:=current_filepos;
  4320. extnumber:=$ffff;
  4321. aliasnames:=TCmdStrList.create;
  4322. funcretsym:=nil;
  4323. forwarddef:=true;
  4324. interfacedef:=false;
  4325. hasforward:=false;
  4326. struct := nil;
  4327. import_dll:=nil;
  4328. import_name:=nil;
  4329. import_nr:=0;
  4330. inlininginfo:=nil;
  4331. deprecatedmsg:=nil;
  4332. end;
  4333. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  4334. var
  4335. i,aliasnamescount : longint;
  4336. level : byte;
  4337. begin
  4338. inherited ppuload(procdef,ppufile);
  4339. {$ifdef symansistr}
  4340. if po_has_mangledname in procoptions then
  4341. _mangledname:=ppufile.getansistring
  4342. else
  4343. _mangledname:='';
  4344. {$else symansistr}
  4345. if po_has_mangledname in procoptions then
  4346. _mangledname:=stringdup(ppufile.getstring)
  4347. else
  4348. _mangledname:=nil;
  4349. {$endif symansistr}
  4350. extnumber:=ppufile.getword;
  4351. level:=ppufile.getbyte;
  4352. ppufile.getderef(structderef);
  4353. ppufile.getderef(procsymderef);
  4354. ppufile.getposinfo(fileinfo);
  4355. visibility:=tvisibility(ppufile.getbyte);
  4356. ppufile.getsmallset(symoptions);
  4357. if sp_has_deprecated_msg in symoptions then
  4358. deprecatedmsg:=stringdup(ppufile.getstring)
  4359. else
  4360. deprecatedmsg:=nil;
  4361. synthetickind:=tsynthetickind(ppufile.getbyte);
  4362. { import stuff }
  4363. if po_has_importdll in procoptions then
  4364. import_dll:=stringdup(ppufile.getstring)
  4365. else
  4366. import_dll:=nil;
  4367. if po_has_importname in procoptions then
  4368. import_name:=stringdup(ppufile.getstring)
  4369. else
  4370. import_name:=nil;
  4371. import_nr:=ppufile.getword;
  4372. if (po_msgint in procoptions) then
  4373. messageinf.i:=ppufile.getlongint;
  4374. if (po_msgstr in procoptions) then
  4375. messageinf.str:=stringdup(ppufile.getstring);
  4376. if (po_dispid in procoptions) then
  4377. dispid:=ppufile.getlongint;
  4378. { inline stuff }
  4379. ppufile.getsmallset(implprocoptions);
  4380. if has_inlininginfo then
  4381. begin
  4382. ppufile.getderef(funcretsymderef);
  4383. new(inlininginfo);
  4384. ppufile.getsmallset(inlininginfo^.flags);
  4385. end
  4386. else
  4387. begin
  4388. inlininginfo:=nil;
  4389. funcretsym:=nil;
  4390. end;
  4391. aliasnames:=TCmdStrList.create;
  4392. { count alias names }
  4393. aliasnamescount:=ppufile.getbyte;
  4394. for i:=1 to aliasnamescount do
  4395. aliasnames.insert(ppufile.getstring);
  4396. ppuload_platform(ppufile);
  4397. { load para symtable }
  4398. parast:=tparasymtable.create(self,level);
  4399. tparasymtable(parast).ppuload(ppufile);
  4400. { load local symtable }
  4401. if has_inlininginfo then
  4402. begin
  4403. localst:=tlocalsymtable.create(self,level);
  4404. tlocalsymtable(localst).ppuload(ppufile);
  4405. end
  4406. else
  4407. localst:=nil;
  4408. { inline stuff }
  4409. if has_inlininginfo then
  4410. inlininginfo^.code:=ppuloadnodetree(ppufile);
  4411. { default values for no persistent data }
  4412. if (cs_link_deffile in current_settings.globalswitches) and
  4413. (tf_need_export in target_info.flags) and
  4414. (po_exports in procoptions) then
  4415. deffile.AddExport(mangledname);
  4416. { Disable po_has_inlining until the derefimpl is done }
  4417. has_inlininginfo:=false;
  4418. end;
  4419. destructor tprocdef.destroy;
  4420. begin
  4421. aliasnames.free;
  4422. aliasnames:=nil;
  4423. if assigned(localst) and
  4424. (localst.symtabletype<>staticsymtable) then
  4425. begin
  4426. {$ifdef MEMDEBUG}
  4427. memproclocalst.start;
  4428. {$endif MEMDEBUG}
  4429. localst.free;
  4430. localst:=nil;
  4431. {$ifdef MEMDEBUG}
  4432. memproclocalst.start;
  4433. {$endif MEMDEBUG}
  4434. end;
  4435. if assigned(inlininginfo) then
  4436. begin
  4437. {$ifdef MEMDEBUG}
  4438. memprocnodetree.start;
  4439. {$endif MEMDEBUG}
  4440. tnode(inlininginfo^.code).free;
  4441. {$ifdef MEMDEBUG}
  4442. memprocnodetree.start;
  4443. {$endif MEMDEBUG}
  4444. dispose(inlininginfo);
  4445. inlininginfo:=nil;
  4446. end;
  4447. if assigned(implprocdefinfo) then
  4448. begin
  4449. stringdispose(implprocdefinfo^.resultname);
  4450. freemem(implprocdefinfo);
  4451. implprocdefinfo:=nil;
  4452. end;
  4453. stringdispose(import_dll);
  4454. stringdispose(import_name);
  4455. stringdispose(deprecatedmsg);
  4456. if (po_msgstr in procoptions) then
  4457. stringdispose(messageinf.str);
  4458. {$ifndef symansistr}
  4459. if assigned(_mangledname) then
  4460. begin
  4461. {$ifdef MEMDEBUG}
  4462. memmanglednames.start;
  4463. {$endif MEMDEBUG}
  4464. stringdispose(_mangledname);
  4465. {$ifdef MEMDEBUG}
  4466. memmanglednames.stop;
  4467. {$endif MEMDEBUG}
  4468. end;
  4469. {$endif symansistr}
  4470. inherited destroy;
  4471. end;
  4472. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  4473. var
  4474. oldintfcrc : boolean;
  4475. aliasnamescount : longint;
  4476. item : TCmdStrListItem;
  4477. begin
  4478. { released procdef? }
  4479. if not assigned(parast) then
  4480. exit;
  4481. inherited ppuwrite(ppufile);
  4482. {$ifdef symansistr}
  4483. if po_has_mangledname in procoptions then
  4484. ppufile.putansistring(_mangledname);
  4485. {$else symansistr}
  4486. if po_has_mangledname in procoptions then
  4487. ppufile.putstring(_mangledname^);
  4488. {$endif symansistr}
  4489. ppufile.putword(extnumber);
  4490. ppufile.putbyte(parast.symtablelevel);
  4491. ppufile.putderef(structderef);
  4492. ppufile.putderef(procsymderef);
  4493. ppufile.putposinfo(fileinfo);
  4494. ppufile.putbyte(byte(visibility));
  4495. ppufile.putsmallset(symoptions);
  4496. if sp_has_deprecated_msg in symoptions then
  4497. ppufile.putstring(deprecatedmsg^);
  4498. ppufile.putbyte(byte(synthetickind));
  4499. { import }
  4500. if po_has_importdll in procoptions then
  4501. ppufile.putstring(import_dll^);
  4502. if po_has_importname in procoptions then
  4503. ppufile.putstring(import_name^);
  4504. ppufile.putword(import_nr);
  4505. if (po_msgint in procoptions) then
  4506. ppufile.putlongint(messageinf.i);
  4507. if (po_msgstr in procoptions) then
  4508. ppufile.putstring(messageinf.str^);
  4509. if (po_dispid in procoptions) then
  4510. ppufile.putlongint(dispid);
  4511. { inline stuff }
  4512. oldintfcrc:=ppufile.do_crc;
  4513. ppufile.do_crc:=false;
  4514. ppufile.putsmallset(implprocoptions);
  4515. if has_inlininginfo then
  4516. begin
  4517. ppufile.putderef(funcretsymderef);
  4518. ppufile.putsmallset(inlininginfo^.flags);
  4519. end;
  4520. { count alias names }
  4521. aliasnamescount:=0;
  4522. item:=TCmdStrListItem(aliasnames.first);
  4523. while assigned(item) do
  4524. begin
  4525. inc(aliasnamescount);
  4526. item:=TCmdStrListItem(item.next);
  4527. end;
  4528. if aliasnamescount>255 then
  4529. internalerror(200711021);
  4530. ppufile.putbyte(aliasnamescount);
  4531. item:=TCmdStrListItem(aliasnames.first);
  4532. while assigned(item) do
  4533. begin
  4534. ppufile.putstring(item.str);
  4535. item:=TCmdStrListItem(item.next);
  4536. end;
  4537. ppufile.do_crc:=oldintfcrc;
  4538. { write this entry }
  4539. writeentry(ppufile,ibprocdef);
  4540. { Save the para symtable, this is taken from the interface }
  4541. tparasymtable(parast).ppuwrite(ppufile);
  4542. { save localsymtable for inline procedures or when local
  4543. browser info is requested, this has no influence on the crc }
  4544. if has_inlininginfo then
  4545. begin
  4546. oldintfcrc:=ppufile.do_crc;
  4547. ppufile.do_crc:=false;
  4548. tlocalsymtable(localst).ppuwrite(ppufile);
  4549. ppufile.do_crc:=oldintfcrc;
  4550. end;
  4551. { node tree for inlining }
  4552. oldintfcrc:=ppufile.do_crc;
  4553. ppufile.do_crc:=false;
  4554. if has_inlininginfo then
  4555. ppuwritenodetree(ppufile,inlininginfo^.code);
  4556. ppufile.do_crc:=oldintfcrc;
  4557. end;
  4558. function tprocdef.fullprocname(showhidden:boolean):string;
  4559. var
  4560. pno: tprocnameoptions;
  4561. begin
  4562. pno:=[];
  4563. if showhidden then
  4564. include(pno,pno_showhidden);
  4565. result:=customprocname(pno);
  4566. end;
  4567. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  4568. var
  4569. s, rn : ansistring;
  4570. t : ttoken;
  4571. begin
  4572. {$ifdef EXTDEBUG}
  4573. include(pno,pno_showhidden);
  4574. {$endif EXTDEBUG}
  4575. s:='';
  4576. if proctypeoption=potype_operator then
  4577. begin
  4578. for t:=NOTOKEN to last_overloaded do
  4579. if procsym.realname='$'+overloaded_names[t] then
  4580. begin
  4581. s:='operator ';
  4582. if (pno_ownername in pno) and
  4583. assigned(struct) then
  4584. s:=s+struct.RttiName+'.';
  4585. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  4586. break;
  4587. end;
  4588. end
  4589. else
  4590. begin
  4591. if (po_classmethod in procoptions) and
  4592. not(pno_noclassmarker in pno) then
  4593. s:='class ';
  4594. case proctypeoption of
  4595. potype_constructor,
  4596. potype_class_constructor:
  4597. s:=s+'constructor ';
  4598. potype_class_destructor,
  4599. potype_destructor:
  4600. s:=s+'destructor ';
  4601. else
  4602. if (pno_proctypeoption in pno) then
  4603. begin
  4604. if assigned(returndef) and
  4605. not(is_void(returndef)) then
  4606. s:=s+'function '
  4607. else
  4608. s:=s+'procedure ';
  4609. end;
  4610. end;
  4611. if (pno_ownername in pno) and
  4612. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  4613. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  4614. rn:=procsym.realname;
  4615. if (pno_noleadingdollar in pno) and
  4616. (rn[1]='$') then
  4617. delete(rn,1,1);
  4618. s:=s+rn+typename_paras(pno);
  4619. end;
  4620. if not(proctypeoption in [potype_constructor,potype_destructor,
  4621. potype_class_constructor,potype_class_destructor]) and
  4622. assigned(returndef) and
  4623. not(is_void(returndef)) then
  4624. s:=s+':'+returndef.GetTypeName;
  4625. if owner.symtabletype=localsymtable then
  4626. s:=s+' is nested';
  4627. s:=s+';';
  4628. { forced calling convention? }
  4629. if (po_hascallingconvention in procoptions) then
  4630. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  4631. if (po_staticmethod in procoptions) and
  4632. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  4633. s:=s+' Static;';
  4634. if pno_mangledname in pno then
  4635. s:=s+' -- mangled name: '+mangledname;
  4636. customprocname:=s;
  4637. end;
  4638. function tprocdef.is_methodpointer:boolean;
  4639. begin
  4640. { don't check assigned(_class), that's also the case for nested
  4641. procedures inside methods }
  4642. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  4643. end;
  4644. function tprocdef.is_addressonly:boolean;
  4645. begin
  4646. result:=assigned(owner) and
  4647. not is_methodpointer and
  4648. (not(m_nested_procvars in current_settings.modeswitches) or
  4649. not is_nested_pd(self));
  4650. end;
  4651. procedure tprocdef.make_external;
  4652. begin
  4653. include(procoptions,po_external);
  4654. forwarddef:=false;
  4655. end;
  4656. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  4657. begin
  4658. case t of
  4659. gs_local :
  4660. GetSymtable:=localst;
  4661. gs_para :
  4662. GetSymtable:=parast;
  4663. else
  4664. GetSymtable:=nil;
  4665. end;
  4666. end;
  4667. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  4668. var
  4669. j : longint;
  4670. begin
  4671. result:=inherited getcopyas(newtyp,copytyp);
  4672. if newtyp=procvardef then
  4673. exit;
  4674. { don't copy mangled name, can be different }
  4675. tprocdef(result).messageinf:=messageinf;
  4676. tprocdef(result).dispid:=dispid;
  4677. if po_msgstr in procoptions then
  4678. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  4679. tprocdef(result).symoptions:=symoptions;
  4680. if assigned(deprecatedmsg) then
  4681. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  4682. { will have to be associated with appropriate procsym }
  4683. tprocdef(result).procsym:=nil;
  4684. { don't create aliases for bare copies, nor copy the funcretsym as
  4685. the function result parameter will be inserted again if necessary
  4686. (e.g. if the calling convention is changed) }
  4687. if copytyp<>pc_bareproc then
  4688. begin
  4689. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  4690. if assigned(funcretsym) then
  4691. begin
  4692. if funcretsym.owner=parast then
  4693. begin
  4694. j:=parast.symlist.indexof(funcretsym);
  4695. if j<0 then
  4696. internalerror(2011040606);
  4697. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  4698. end
  4699. else if funcretsym.owner=localst then
  4700. begin
  4701. { nothing to do, will be inserted for the new procdef while
  4702. parsing its body (by pdecsub.insert_funcret_local) }
  4703. end
  4704. else
  4705. internalerror(2011040605);
  4706. end;
  4707. end;
  4708. { will have to be associated with a new struct }
  4709. tprocdef(result).struct:=nil;
  4710. if assigned(implprocdefinfo) then
  4711. begin
  4712. if assigned(resultname) then
  4713. tprocdef(result).resultname:=stringdup(resultname^);
  4714. end;
  4715. if assigned(import_dll) then
  4716. tprocdef(result).import_dll:=stringdup(import_dll^);
  4717. if assigned(import_name) then
  4718. tprocdef(result).import_name:=stringdup(import_name^);
  4719. tprocdef(result).import_nr:=import_nr;
  4720. tprocdef(result).extnumber:=$ffff;
  4721. tprocdef(result).visibility:=visibility;
  4722. tprocdef(result).synthetickind:=synthetickind;
  4723. { we need a separate implementation for the copied def }
  4724. tprocdef(result).forwarddef:=true;
  4725. tprocdef(result).interfacedef:=true;
  4726. { create new paralist }
  4727. tprocdef(result).calcparas;
  4728. end;
  4729. function tprocdef.getcopy: tstoreddef;
  4730. begin
  4731. result:=getcopyas(procdef,pc_normal);
  4732. end;
  4733. procedure tprocdef.buildderef;
  4734. begin
  4735. inherited buildderef;
  4736. structderef.build(struct);
  4737. { procsym that originaly defined this definition, should be in the
  4738. same symtable }
  4739. procsymderef.build(procsym);
  4740. end;
  4741. procedure tprocdef.buildderefimpl;
  4742. begin
  4743. inherited buildderefimpl;
  4744. { inline tree }
  4745. if has_inlininginfo then
  4746. begin
  4747. { Localst is not available for main/unit init }
  4748. if assigned(localst) then
  4749. begin
  4750. tlocalsymtable(localst).buildderef;
  4751. tlocalsymtable(localst).buildderefimpl;
  4752. end;
  4753. funcretsymderef.build(funcretsym);
  4754. inlininginfo^.code.buildderefimpl;
  4755. end;
  4756. end;
  4757. procedure tprocdef.deref;
  4758. begin
  4759. inherited deref;
  4760. struct:=tabstractrecorddef(structderef.resolve);
  4761. { procsym that originaly defined this definition, should be in the
  4762. same symtable }
  4763. procsym:=tprocsym(procsymderef.resolve);
  4764. end;
  4765. procedure tprocdef.derefimpl;
  4766. begin
  4767. { Enable has_inlininginfo when the inlininginfo
  4768. structure is available. The has_inlininginfo was disabled
  4769. after the load, since the data was invalid }
  4770. if assigned(inlininginfo) then
  4771. has_inlininginfo:=true;
  4772. { Inline }
  4773. if has_inlininginfo then
  4774. begin
  4775. { Locals }
  4776. if assigned(localst) then
  4777. begin
  4778. tlocalsymtable(localst).deref;
  4779. tlocalsymtable(localst).derefimpl;
  4780. end;
  4781. inlininginfo^.code.derefimpl;
  4782. { funcretsym, this is always located in the localst }
  4783. funcretsym:=tsym(funcretsymderef.resolve);
  4784. end
  4785. else
  4786. begin
  4787. { safety }
  4788. { Not safe! A unit may be reresolved after its interface has been
  4789. parsed but before its implementation has been parsed, and in that
  4790. case the funcretsym is still required!
  4791. funcretsym:=nil; }
  4792. end;
  4793. end;
  4794. function tprocdef.GetTypeName : string;
  4795. begin
  4796. GetTypeName := FullProcName(false);
  4797. end;
  4798. function tprocdef.mangledname : TSymStr;
  4799. begin
  4800. {$ifdef symansistr}
  4801. if _mangledname='' then
  4802. begin
  4803. result:=defaultmangledname;
  4804. _mangledname:=result;
  4805. end
  4806. else
  4807. result:=_mangledname;
  4808. {$else symansistr}
  4809. if not assigned(_mangledname) then
  4810. begin
  4811. result:=defaultmangledname;
  4812. _mangledname:=stringdup(mangledname);
  4813. end
  4814. else
  4815. result:=_mangledname^;
  4816. {$endif symansistr}
  4817. end;
  4818. function tprocdef.defaultmangledname: TSymStr;
  4819. var
  4820. hp : TParavarsym;
  4821. hs : TSymStr;
  4822. crc : dword;
  4823. newlen,
  4824. oldlen,
  4825. i : integer;
  4826. begin
  4827. hp:=nil;
  4828. { we need to use the symtable where the procsym is inserted,
  4829. because that is visible to the world }
  4830. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  4831. oldlen:=length(defaultmangledname);
  4832. { add parameter types }
  4833. for i:=0 to paras.count-1 do
  4834. begin
  4835. hp:=tparavarsym(paras[i]);
  4836. if not(vo_is_hidden_para in hp.varoptions) then
  4837. defaultmangledname:=defaultmangledname+'$'+hp.vardef.mangledparaname;
  4838. end;
  4839. { add resultdef, add $$ as separator to make it unique from a
  4840. parameter separator }
  4841. if not is_void(returndef) then
  4842. defaultmangledname:=defaultmangledname+'$$'+returndef.mangledparaname;
  4843. newlen:=length(defaultmangledname);
  4844. { Replace with CRC if the parameter line is very long }
  4845. if (newlen-oldlen>12) and
  4846. ((newlen>100) or (newlen-oldlen>64)) then
  4847. begin
  4848. crc:=0;
  4849. for i:=0 to paras.count-1 do
  4850. begin
  4851. hp:=tparavarsym(paras[i]);
  4852. if not(vo_is_hidden_para in hp.varoptions) then
  4853. begin
  4854. hs:=hp.vardef.mangledparaname;
  4855. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4856. end;
  4857. end;
  4858. if not is_void(returndef) then
  4859. begin
  4860. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  4861. hs:='$$'+returndef.mangledparaname;
  4862. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4863. end;
  4864. defaultmangledname:=Copy(defaultmangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  4865. end;
  4866. end;
  4867. function tprocdef.cplusplusmangledname : TSymStr;
  4868. function getcppparaname(p : tdef) : TSymStr;
  4869. const
  4870. {$ifdef NAMEMANGLING_GCC2}
  4871. ordtype2str : array[tordtype] of string[2] = (
  4872. '',
  4873. 'Uc','Us','Ui','Us',
  4874. 'Sc','s','i','x',
  4875. 'b','b','b','b','b',
  4876. 'c','w','x');
  4877. {$else NAMEMANGLING_GCC2}
  4878. ordtype2str : array[tordtype] of string[1] = (
  4879. 'v',
  4880. 'h','t','j','y',
  4881. 'a','s','i','x',
  4882. 'b','b','b','b',
  4883. 'b','b','b','b',
  4884. 'c','w','x');
  4885. floattype2str : array[tfloattype] of string[1] = (
  4886. 'f','d','e','e',
  4887. 'd','d','g');
  4888. {$endif NAMEMANGLING_GCC2}
  4889. var
  4890. s : TSymStr;
  4891. begin
  4892. case p.typ of
  4893. orddef:
  4894. s:=ordtype2str[torddef(p).ordtype];
  4895. pointerdef:
  4896. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  4897. {$ifndef NAMEMANGLING_GCC2}
  4898. floatdef:
  4899. s:=floattype2str[tfloatdef(p).floattype];
  4900. {$endif NAMEMANGLING_GCC2}
  4901. else
  4902. internalerror(2103001);
  4903. end;
  4904. getcppparaname:=s;
  4905. end;
  4906. var
  4907. s,s2 : TSymStr;
  4908. hp : TParavarsym;
  4909. i : integer;
  4910. begin
  4911. {$ifdef NAMEMANGLING_GCC2}
  4912. { outdated gcc 2.x name mangling scheme }
  4913. s := procsym.realname;
  4914. if procsym.owner.symtabletype=ObjectSymtable then
  4915. begin
  4916. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  4917. case proctypeoption of
  4918. potype_destructor:
  4919. s:='_$_'+tostr(length(s2))+s2;
  4920. potype_constructor:
  4921. s:='___'+tostr(length(s2))+s2;
  4922. else
  4923. s:='_'+s+'__'+tostr(length(s2))+s2;
  4924. end;
  4925. end
  4926. else s:=s+'__';
  4927. s:=s+'F';
  4928. { concat modifiers }
  4929. { !!!!! }
  4930. { now we handle the parameters }
  4931. if maxparacount>0 then
  4932. begin
  4933. for i:=0 to paras.count-1 do
  4934. begin
  4935. hp:=tparavarsym(paras[i]);
  4936. { no hidden parameters form part of a C++ mangled name:
  4937. a) self is not included
  4938. b) there are no "high" or other hidden parameters
  4939. }
  4940. if vo_is_hidden_para in hp.varoptions then
  4941. continue;
  4942. s2:=getcppparaname(hp.vardef);
  4943. if hp.varspez in [vs_var,vs_out] then
  4944. s2:='R'+s2;
  4945. s:=s+s2;
  4946. end;
  4947. end
  4948. else
  4949. s:=s+'v';
  4950. cplusplusmangledname:=s;
  4951. {$else NAMEMANGLING_GCC2}
  4952. { gcc 3.x and 4.x name mangling scheme }
  4953. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  4954. if procsym.owner.symtabletype=ObjectSymtable then
  4955. begin
  4956. s:='_ZN';
  4957. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  4958. s:=s+tostr(length(s2))+s2;
  4959. case proctypeoption of
  4960. potype_constructor:
  4961. s:=s+'C1';
  4962. potype_destructor:
  4963. s:=s+'D1';
  4964. else
  4965. s:=s+tostr(length(procsym.realname))+procsym.realname;
  4966. end;
  4967. s:=s+'E';
  4968. end
  4969. else
  4970. s:=procsym.realname;
  4971. { now we handle the parameters }
  4972. if maxparacount>0 then
  4973. begin
  4974. for i:=0 to paras.count-1 do
  4975. begin
  4976. hp:=tparavarsym(paras[i]);
  4977. { no hidden parameters form part of a C++ mangled name:
  4978. a) self is not included
  4979. b) there are no "high" or other hidden parameters
  4980. }
  4981. if vo_is_hidden_para in hp.varoptions then
  4982. continue;
  4983. s2:=getcppparaname(hp.vardef);
  4984. if hp.varspez in [vs_var,vs_out] then
  4985. s2:='R'+s2;
  4986. s:=s+s2;
  4987. end;
  4988. end
  4989. else
  4990. s:=s+'v';
  4991. cplusplusmangledname:=s;
  4992. {$endif NAMEMANGLING_GCC2}
  4993. end;
  4994. function tprocdef.objcmangledname : TSymStr;
  4995. var
  4996. manglednamelen: longint;
  4997. iscatmethod : boolean;
  4998. begin
  4999. if not (po_msgstr in procoptions) then
  5000. internalerror(2009030901);
  5001. { we may very well need longer strings to handle these... }
  5002. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  5003. length('+"[ ]"')+length(messageinf.str^);
  5004. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  5005. if (iscatmethod) then
  5006. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  5007. if manglednamelen>255 then
  5008. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  5009. if not(po_classmethod in procoptions) then
  5010. result:='"-['
  5011. else
  5012. result:='"+[';
  5013. { quotes are necessary because the +/- otherwise confuse the assembler
  5014. into expecting a number
  5015. }
  5016. if iscatmethod then
  5017. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  5018. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  5019. if iscatmethod then
  5020. result:=result+')';
  5021. result:=result+' '+messageinf.str^+']"';
  5022. end;
  5023. procedure tprocdef.setmangledname(const s : TSymStr);
  5024. begin
  5025. { This is not allowed anymore, the forward declaration
  5026. already needs to create the correct mangledname, no changes
  5027. afterwards are allowed (PFV) }
  5028. { Exception: interface definitions in mode macpas, since in that }
  5029. { case no reference to the old name can exist yet (JM) }
  5030. {$ifdef symansistr}
  5031. if _mangledname<>'' then
  5032. if ((m_mac in current_settings.modeswitches) and
  5033. (interfacedef)) then
  5034. _mangledname:=''
  5035. else
  5036. internalerror(200411171);
  5037. {$else symansistr}
  5038. if assigned(_mangledname) then
  5039. if ((m_mac in current_settings.modeswitches) and
  5040. (interfacedef)) then
  5041. stringdispose(_mangledname)
  5042. else
  5043. internalerror(200411171);
  5044. {$endif symansistr}
  5045. {$ifdef jvm}
  5046. { this routine can be called for compilerproces. can't set mangled
  5047. name since it must be calculated, but it uses import_name when set
  5048. -> set that one }
  5049. import_name:=stringdup(s);
  5050. include(procoptions,po_has_importname);
  5051. include(procoptions,po_has_mangledname);
  5052. {$else}
  5053. {$ifdef symansistr}
  5054. _mangledname:=s;
  5055. {$else symansistr}
  5056. _mangledname:=stringdup(s);
  5057. {$endif symansistr}
  5058. {$endif jvm}
  5059. include(procoptions,po_has_mangledname);
  5060. end;
  5061. {***************************************************************************
  5062. TPROCVARDEF
  5063. ***************************************************************************}
  5064. constructor tprocvardef.create(level:byte);
  5065. begin
  5066. inherited create(procvardef,level);
  5067. end;
  5068. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  5069. begin
  5070. inherited ppuload(procvardef,ppufile);
  5071. { load para symtable }
  5072. parast:=tparasymtable.create(self,ppufile.getbyte);
  5073. ppuload_platform(ppufile);
  5074. tparasymtable(parast).ppuload(ppufile);
  5075. end;
  5076. function tprocvardef.getcopy : tstoreddef;
  5077. var
  5078. i : tcallercallee;
  5079. j : longint;
  5080. begin
  5081. result:=cprocvardef.create(parast.symtablelevel);
  5082. tprocvardef(result).returndef:=returndef;
  5083. tprocvardef(result).returndefderef:=returndefderef;
  5084. tprocvardef(result).parast:=parast.getcopy;
  5085. tprocvardef(result).savesize:=savesize;
  5086. { create paralist copy }
  5087. calcparas;
  5088. tprocvardef(result).paras:=tparalist.create(false);
  5089. tprocvardef(result).paras.count:=paras.count;
  5090. for j:=0 to paras.count-1 do
  5091. tprocvardef(result).paras[j]:=paras[j];
  5092. tprocvardef(result).proctypeoption:=proctypeoption;
  5093. tprocvardef(result).proccalloption:=proccalloption;
  5094. tprocvardef(result).procoptions:=procoptions;
  5095. tprocvardef(result).callerargareasize:=callerargareasize;
  5096. tprocvardef(result).calleeargareasize:=calleeargareasize;
  5097. tprocvardef(result).maxparacount:=maxparacount;
  5098. tprocvardef(result).minparacount:=minparacount;
  5099. for i:=low(tcallercallee) to high(tcallercallee) do
  5100. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  5101. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  5102. {$ifdef m68k}
  5103. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  5104. {$endif}
  5105. end;
  5106. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  5107. begin
  5108. inherited ppuwrite(ppufile);
  5109. { Save the para symtable level (necessary to distinguish nested
  5110. procvars) }
  5111. ppufile.putbyte(parast.symtablelevel);
  5112. { Write this entry }
  5113. writeentry(ppufile,ibprocvardef);
  5114. { Save the para symtable, this is taken from the interface }
  5115. tparasymtable(parast).ppuwrite(ppufile);
  5116. end;
  5117. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  5118. begin
  5119. case t of
  5120. gs_para :
  5121. GetSymtable:=parast;
  5122. else
  5123. GetSymtable:=nil;
  5124. end;
  5125. end;
  5126. function tprocvardef.size : asizeint;
  5127. begin
  5128. if ((po_methodpointer in procoptions) or
  5129. is_nested_pd(self)) and
  5130. not(po_addressonly in procoptions) then
  5131. begin
  5132. if is_nested_pd(self) then
  5133. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  5134. else
  5135. size:=voidcodepointertype.size+voidpointertype.size;
  5136. end
  5137. else
  5138. size:=voidcodepointertype.size;
  5139. end;
  5140. function tprocvardef.is_methodpointer:boolean;
  5141. begin
  5142. result:=(po_methodpointer in procoptions);
  5143. end;
  5144. function tprocvardef.is_addressonly:boolean;
  5145. begin
  5146. result:=(not(po_methodpointer in procoptions) and
  5147. not is_nested_pd(self)) or
  5148. (po_addressonly in procoptions);
  5149. end;
  5150. function tprocvardef.getmangledparaname:TSymStr;
  5151. begin
  5152. if not(po_methodpointer in procoptions) then
  5153. if not is_nested_pd(self) then
  5154. result:='procvar'
  5155. else
  5156. result:='nestedprovar'
  5157. else
  5158. result:='procvarofobj'
  5159. end;
  5160. function tprocvardef.is_publishable : boolean;
  5161. begin
  5162. is_publishable:=(po_methodpointer in procoptions);
  5163. end;
  5164. function tprocvardef.GetTypeName : string;
  5165. var
  5166. s: string;
  5167. pno : tprocnameoptions;
  5168. begin
  5169. {$ifdef EXTDEBUG}
  5170. pno:=[pno_showhidden];
  5171. {$else EXTDEBUG}
  5172. pno:=[];
  5173. {$endif EXTDEBUG}
  5174. s:='<';
  5175. if po_classmethod in procoptions then
  5176. s := s+'class method type of'
  5177. else
  5178. if po_addressonly in procoptions then
  5179. s := s+'address of'
  5180. else
  5181. s := s+'procedure variable type of';
  5182. if assigned(returndef) and
  5183. (returndef<>voidtype) then
  5184. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5185. else
  5186. s:=s+' procedure'+typename_paras(pno);
  5187. if po_methodpointer in procoptions then
  5188. s := s+' of object';
  5189. if is_nested_pd(self) then
  5190. s := s+' is nested';
  5191. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5192. end;
  5193. {***************************************************************************
  5194. TOBJECTDEF
  5195. ***************************************************************************}
  5196. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  5197. begin
  5198. inherited create(n,objectdef);
  5199. fcurrent_dispid:=0;
  5200. objecttype:=ot;
  5201. childof:=nil;
  5202. if objecttype=odt_helper then
  5203. owner.includeoption(sto_has_helper);
  5204. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  5205. { create space for vmt !! }
  5206. vmtentries:=TFPList.Create;
  5207. vmt_offset:=0;
  5208. set_parent(c);
  5209. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5210. prepareguid;
  5211. { setup implemented interfaces }
  5212. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5213. ImplementedInterfaces:=TFPObjectList.Create(true)
  5214. else
  5215. ImplementedInterfaces:=nil;
  5216. writing_class_record_dbginfo:=false;
  5217. end;
  5218. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  5219. var
  5220. i,
  5221. implintfcount : longint;
  5222. d, getterd : tderef;
  5223. ImplIntf : TImplementedInterface;
  5224. vmtentry : pvmtentry;
  5225. begin
  5226. inherited ppuload(objectdef,ppufile);
  5227. objecttype:=tobjecttyp(ppufile.getbyte);
  5228. objextname:=stringdup(ppufile.getstring);
  5229. { only used for external Objective-C classes/protocols }
  5230. if (objextname^='') then
  5231. stringdispose(objextname);
  5232. symtable:=tObjectSymtable.create(self,objrealname^,0);
  5233. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  5234. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  5235. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  5236. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  5237. vmt_offset:=ppufile.getlongint;
  5238. ppufile.getderef(childofderef);
  5239. { load guid }
  5240. iidstr:=nil;
  5241. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5242. begin
  5243. new(iidguid);
  5244. ppufile.getguid(iidguid^);
  5245. iidstr:=stringdup(ppufile.getstring);
  5246. end;
  5247. abstractcnt:=ppufile.getlongint;
  5248. if objecttype=odt_helper then
  5249. ppufile.getderef(extendeddefderef);
  5250. vmtentries:=TFPList.Create;
  5251. vmtentries.count:=ppufile.getlongint;
  5252. for i:=0 to vmtentries.count-1 do
  5253. begin
  5254. ppufile.getderef(d);
  5255. new(vmtentry);
  5256. vmtentry^.procdef:=nil;
  5257. vmtentry^.procdefderef:=d;
  5258. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  5259. vmtentries[i]:=vmtentry;
  5260. end;
  5261. { load implemented interfaces }
  5262. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5263. begin
  5264. ImplementedInterfaces:=TFPObjectList.Create(true);
  5265. implintfcount:=ppufile.getlongint;
  5266. for i:=0 to implintfcount-1 do
  5267. begin
  5268. ppufile.getderef(d);
  5269. ppufile.getderef(getterd);
  5270. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  5271. ImplIntf.IOffset:=ppufile.getlongint;
  5272. byte(ImplIntf.IType):=ppufile.getbyte;
  5273. ImplementedInterfaces.Add(ImplIntf);
  5274. end;
  5275. end
  5276. else
  5277. ImplementedInterfaces:=nil;
  5278. if df_copied_def in defoptions then
  5279. begin
  5280. ppufile.getderef(cloneddefderef);
  5281. ppuload_platform(ppufile);
  5282. end
  5283. else
  5284. begin
  5285. ppuload_platform(ppufile);
  5286. tObjectSymtable(symtable).ppuload(ppufile);
  5287. end;
  5288. { handles the predefined class tobject }
  5289. { the last TOBJECT which is loaded gets }
  5290. { it ! }
  5291. if (childof=nil) and
  5292. (objecttype in [odt_class,odt_javaclass]) and
  5293. (objname^='TOBJECT') then
  5294. class_tobject:=self;
  5295. if (childof=nil) and
  5296. (objecttype=odt_interfacecom) then
  5297. if (objname^='IUNKNOWN') then
  5298. interface_iunknown:=self
  5299. else
  5300. if (objname^='IDISPATCH') then
  5301. interface_idispatch:=self;
  5302. if (childof=nil) and
  5303. (objecttype=odt_objcclass) and
  5304. (objname^='PROTOCOL') then
  5305. objc_protocoltype:=self;
  5306. if (objecttype=odt_javaclass) and
  5307. not(oo_is_formal in objectoptions) then
  5308. begin
  5309. if (objname^='JLOBJECT') then
  5310. java_jlobject:=self
  5311. else if (objname^='JLTHROWABLE') then
  5312. java_jlthrowable:=self
  5313. else if (objname^='FPCBASERECORDTYPE') then
  5314. java_fpcbaserecordtype:=self
  5315. else if (objname^='JLSTRING') then
  5316. java_jlstring:=self
  5317. else if (objname^='ANSISTRINGCLASS') then
  5318. java_ansistring:=self
  5319. else if (objname^='SHORTSTRINGCLASS') then
  5320. java_shortstring:=self
  5321. else if (objname^='JLENUM') then
  5322. java_jlenum:=self
  5323. else if (objname^='JUENUMSET') then
  5324. java_juenumset:=self
  5325. else if (objname^='FPCBITSET') then
  5326. java_jubitset:=self
  5327. else if (objname^='FPCBASEPROCVARTYPE') then
  5328. java_procvarbase:=self;
  5329. end;
  5330. writing_class_record_dbginfo:=false;
  5331. end;
  5332. destructor tobjectdef.destroy;
  5333. begin
  5334. if assigned(symtable) then
  5335. begin
  5336. symtable.free;
  5337. symtable:=nil;
  5338. end;
  5339. stringdispose(objextname);
  5340. stringdispose(iidstr);
  5341. if assigned(ImplementedInterfaces) then
  5342. begin
  5343. ImplementedInterfaces.free;
  5344. ImplementedInterfaces:=nil;
  5345. end;
  5346. if assigned(iidguid) then
  5347. begin
  5348. dispose(iidguid);
  5349. iidguid:=nil;
  5350. end;
  5351. if assigned(vmtentries) then
  5352. begin
  5353. resetvmtentries;
  5354. vmtentries.free;
  5355. vmtentries:=nil;
  5356. end;
  5357. if assigned(vmcallstaticinfo) then
  5358. begin
  5359. freemem(vmcallstaticinfo);
  5360. vmcallstaticinfo:=nil;
  5361. end;
  5362. inherited destroy;
  5363. end;
  5364. function tobjectdef.getcopy : tstoreddef;
  5365. var
  5366. i : longint;
  5367. begin
  5368. result:=cobjectdef.create(objecttype,objrealname^,childof);
  5369. { the constructor allocates a symtable which we release to avoid memory leaks }
  5370. tobjectdef(result).symtable.free;
  5371. tobjectdef(result).symtable:=symtable.getcopy;
  5372. if assigned(objextname) then
  5373. tobjectdef(result).objextname:=stringdup(objextname^);
  5374. if assigned(import_lib) then
  5375. tobjectdef(result).import_lib:=stringdup(import_lib^);
  5376. tobjectdef(result).objectoptions:=objectoptions;
  5377. include(tobjectdef(result).defoptions,df_copied_def);
  5378. tobjectdef(result).extendeddef:=extendeddef;
  5379. if assigned(tcinitcode) then
  5380. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  5381. tobjectdef(result).vmt_offset:=vmt_offset;
  5382. if assigned(iidguid) then
  5383. begin
  5384. new(tobjectdef(result).iidguid);
  5385. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  5386. end;
  5387. if assigned(iidstr) then
  5388. tobjectdef(result).iidstr:=stringdup(iidstr^);
  5389. tobjectdef(result).abstractcnt:=abstractcnt;
  5390. if assigned(ImplementedInterfaces) then
  5391. begin
  5392. for i:=0 to ImplementedInterfaces.count-1 do
  5393. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  5394. end;
  5395. if assigned(vmtentries) then
  5396. begin
  5397. tobjectdef(result).vmtentries:=TFPList.Create;
  5398. tobjectdef(result).copyvmtentries(self);
  5399. end;
  5400. end;
  5401. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  5402. var
  5403. i : longint;
  5404. vmtentry : pvmtentry;
  5405. ImplIntf : TImplementedInterface;
  5406. old_do_indirect_crc: boolean;
  5407. begin
  5408. { if class1 in unit A changes, and class2 in unit B inherits from it
  5409. (so unit B uses unit A), then unit B with class2 will be recompiled.
  5410. However, if there is also a class3 in unit C that only depends on
  5411. unit B, then unit C will not be recompiled because nothing changed
  5412. to the interface of unit B. Nevertheless, unit C can indirectly
  5413. depend on unit A via derefs, and these must be updated -> the
  5414. indirect crc keeps track of such changes. }
  5415. old_do_indirect_crc:=ppufile.do_indirect_crc;
  5416. ppufile.do_indirect_crc:=true;
  5417. inherited ppuwrite(ppufile);
  5418. ppufile.putbyte(byte(objecttype));
  5419. if assigned(objextname) then
  5420. ppufile.putstring(objextname^)
  5421. else
  5422. ppufile.putstring('');
  5423. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  5424. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  5425. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  5426. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  5427. ppufile.putlongint(vmt_offset);
  5428. ppufile.putderef(childofderef);
  5429. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5430. begin
  5431. ppufile.putguid(iidguid^);
  5432. ppufile.putstring(iidstr^);
  5433. end;
  5434. ppufile.putlongint(abstractcnt);
  5435. if objecttype=odt_helper then
  5436. ppufile.putderef(extendeddefderef);
  5437. ppufile.putlongint(vmtentries.count);
  5438. for i:=0 to vmtentries.count-1 do
  5439. begin
  5440. vmtentry:=pvmtentry(vmtentries[i]);
  5441. ppufile.putderef(vmtentry^.procdefderef);
  5442. ppufile.putbyte(byte(vmtentry^.visibility));
  5443. end;
  5444. if assigned(ImplementedInterfaces) then
  5445. begin
  5446. ppufile.putlongint(ImplementedInterfaces.Count);
  5447. for i:=0 to ImplementedInterfaces.Count-1 do
  5448. begin
  5449. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5450. ppufile.putderef(ImplIntf.intfdefderef);
  5451. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  5452. ppufile.putlongint(ImplIntf.Ioffset);
  5453. ppufile.putbyte(byte(ImplIntf.IType));
  5454. end;
  5455. end;
  5456. if df_copied_def in defoptions then
  5457. ppufile.putderef(cloneddefderef);
  5458. writeentry(ppufile,ibobjectdef);
  5459. if not(df_copied_def in defoptions) then
  5460. tObjectSymtable(symtable).ppuwrite(ppufile);
  5461. ppufile.do_indirect_crc:=old_do_indirect_crc;
  5462. end;
  5463. function tobjectdef.GetTypeName:string;
  5464. begin
  5465. { in this case we will go in endless recursion, because then }
  5466. { there is no tsym associated yet with the def. It can occur }
  5467. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  5468. { instead of the actual type name }
  5469. if not assigned(typesym) then
  5470. result:='<Currently Parsed Class>'
  5471. else
  5472. result:=typesymbolprettyname;
  5473. end;
  5474. procedure tobjectdef.buildderef;
  5475. var
  5476. i : longint;
  5477. vmtentry : pvmtentry;
  5478. begin
  5479. inherited buildderef;
  5480. childofderef.build(childof);
  5481. if df_copied_def in defoptions then
  5482. cloneddefderef.build(symtable.defowner)
  5483. else
  5484. tstoredsymtable(symtable).buildderef;
  5485. if objecttype=odt_helper then
  5486. extendeddefderef.build(extendeddef);
  5487. for i:=0 to vmtentries.count-1 do
  5488. begin
  5489. vmtentry:=pvmtentry(vmtentries[i]);
  5490. vmtentry^.procdefderef.build(vmtentry^.procdef);
  5491. end;
  5492. if assigned(ImplementedInterfaces) then
  5493. begin
  5494. for i:=0 to ImplementedInterfaces.count-1 do
  5495. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  5496. end;
  5497. end;
  5498. procedure tobjectdef.deref;
  5499. var
  5500. i : longint;
  5501. vmtentry : pvmtentry;
  5502. begin
  5503. inherited deref;
  5504. childof:=tobjectdef(childofderef.resolve);
  5505. if df_copied_def in defoptions then
  5506. begin
  5507. cloneddef:=tobjectdef(cloneddefderef.resolve);
  5508. symtable:=cloneddef.symtable.getcopy;
  5509. end
  5510. else
  5511. tstoredsymtable(symtable).deref;
  5512. if objecttype=odt_helper then
  5513. extendeddef:=tdef(extendeddefderef.resolve);
  5514. for i:=0 to vmtentries.count-1 do
  5515. begin
  5516. vmtentry:=pvmtentry(vmtentries[i]);
  5517. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  5518. end;
  5519. if assigned(ImplementedInterfaces) then
  5520. begin
  5521. for i:=0 to ImplementedInterfaces.count-1 do
  5522. TImplementedInterface(ImplementedInterfaces[i]).deref;
  5523. end;
  5524. end;
  5525. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  5526. var
  5527. pd: tprocdef absolute def;
  5528. st: tsymtable;
  5529. psym: tsym;
  5530. nname: TIDString;
  5531. begin
  5532. if (tdef(def).typ<>procdef) then
  5533. exit;
  5534. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  5535. owner = symtable in which objcclassdef is defined
  5536. }
  5537. st:=pd.owner.defowner.owner;
  5538. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  5539. { check for an existing procsym with our special name }
  5540. psym:=tsym(st.find(nname));
  5541. if not assigned(psym) then
  5542. begin
  5543. psym:=cprocsym.create(nname);
  5544. { avoid warning about this symbol being unused }
  5545. psym.IncRefCount;
  5546. { don't check for duplicates:
  5547. a) we checked above
  5548. b) in case we are in the implementation section of a unit, this
  5549. will also check for this symbol in the interface section
  5550. (since you normally cannot have symbols with the same name
  5551. both interface and implementation), and it's possible to
  5552. have class helpers for the same class in the interface and
  5553. in the implementation, and they cannot be merged since only
  5554. the once in the interface must be saved to the ppu/visible
  5555. from other units }
  5556. st.insert(psym,false);
  5557. end
  5558. else if (psym.typ<>procsym) then
  5559. internalerror(2009111501);
  5560. { add ourselves to this special procsym }
  5561. tprocsym(psym).procdeflist.add(def);
  5562. end;
  5563. procedure tobjectdef.derefimpl;
  5564. begin
  5565. inherited derefimpl;
  5566. { the procdefs are not owned by the class helper procsyms, so they
  5567. are not stored/restored either -> re-add them here }
  5568. if (objecttype=odt_objcclass) or
  5569. (oo_is_classhelper in objectoptions) then
  5570. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  5571. end;
  5572. procedure tobjectdef.resetvmtentries;
  5573. var
  5574. i : longint;
  5575. begin
  5576. for i:=0 to vmtentries.Count-1 do
  5577. Dispose(pvmtentry(vmtentries[i]));
  5578. vmtentries.clear;
  5579. end;
  5580. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  5581. var
  5582. i : longint;
  5583. vmtentry : pvmtentry;
  5584. begin
  5585. resetvmtentries;
  5586. vmtentries.count:=objdef.vmtentries.count;
  5587. for i:=0 to objdef.vmtentries.count-1 do
  5588. begin
  5589. new(vmtentry);
  5590. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  5591. vmtentries[i]:=vmtentry;
  5592. end;
  5593. end;
  5594. function tobjectdef.getparentdef:tdef;
  5595. begin
  5596. { TODO: Remove getparentdef hack}
  5597. { With 2 forward declared classes with the child class before the
  5598. parent class the child class is written earlier to the ppu. Leaving it
  5599. possible to have a reference to the parent class for property overriding,
  5600. but the parent class still has the childof not resolved yet (PFV) }
  5601. if childof=nil then
  5602. childof:=tobjectdef(childofderef.resolve);
  5603. result:=childof;
  5604. end;
  5605. procedure tobjectdef.prepareguid;
  5606. begin
  5607. { set up guid }
  5608. if not assigned(iidguid) then
  5609. begin
  5610. new(iidguid);
  5611. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  5612. end;
  5613. { setup iidstring }
  5614. if not assigned(iidstr) then
  5615. iidstr:=stringdup(''); { default is empty string }
  5616. end;
  5617. procedure tobjectdef.set_parent( c : tobjectdef);
  5618. begin
  5619. if assigned(childof) then
  5620. exit;
  5621. childof:=c;
  5622. if not assigned(c) then
  5623. exit;
  5624. { inherit options and status }
  5625. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  5626. { initially has the same number of abstract methods as the parent }
  5627. abstractcnt:=c.abstractcnt;
  5628. { add the data of the anchestor class/object }
  5629. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  5630. begin
  5631. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  5632. { inherit recordalignment }
  5633. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  5634. { if both the parent and this record use C-alignment, also inherit
  5635. the current field alignment }
  5636. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  5637. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  5638. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  5639. { the padding is not inherited for Objective-C classes (maybe not
  5640. for cppclass either?) }
  5641. if objecttype=odt_objcclass then
  5642. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  5643. if (oo_has_vmt in objectoptions) and
  5644. (oo_has_vmt in c.objectoptions) then
  5645. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  5646. { if parent has a vmt field then the offset is the same for the child PM }
  5647. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  5648. begin
  5649. vmt_offset:=c.vmt_offset;
  5650. include(objectoptions,oo_has_vmt);
  5651. end;
  5652. end;
  5653. end;
  5654. procedure tobjectdef.insertvmt;
  5655. var
  5656. vs: tfieldvarsym;
  5657. begin
  5658. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5659. exit;
  5660. if (oo_has_vmt in objectoptions) then
  5661. internalerror(12345)
  5662. else
  5663. begin
  5664. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  5665. tObjectSymtable(symtable).fieldalignment);
  5666. if (tf_requires_proper_alignment in target_info.flags) then
  5667. begin
  5668. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  5669. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  5670. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  5671. end;
  5672. vs:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  5673. hidesym(vs);
  5674. tObjectSymtable(symtable).insert(vs);
  5675. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  5676. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  5677. vmt_offset:=vs.fieldoffset
  5678. else
  5679. vmt_offset:=vs.fieldoffset div 8;
  5680. include(objectoptions,oo_has_vmt);
  5681. end;
  5682. end;
  5683. procedure tobjectdef.check_forwards;
  5684. begin
  5685. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  5686. inherited;
  5687. if (oo_is_forward in objectoptions) then
  5688. begin
  5689. { ok, in future, the forward can be resolved }
  5690. Message1(sym_e_class_forward_not_resolved,objrealname^);
  5691. exclude(objectoptions,oo_is_forward);
  5692. end;
  5693. end;
  5694. function tobjectdef.find_destructor: tprocdef;
  5695. var
  5696. objdef: tobjectdef;
  5697. begin
  5698. objdef:=self;
  5699. while assigned(objdef) do
  5700. begin
  5701. result:=objdef.find_procdef_bytype(potype_destructor);
  5702. if assigned(result) then
  5703. exit;
  5704. objdef:=objdef.childof;
  5705. end;
  5706. result:=nil;
  5707. end;
  5708. function tobjectdef.implements_any_interfaces: boolean;
  5709. begin
  5710. result := (ImplementedInterfaces.Count > 0) or
  5711. (assigned(childof) and childof.implements_any_interfaces);
  5712. end;
  5713. function tobjectdef.size : asizeint;
  5714. begin
  5715. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5716. result:=voidpointertype.size
  5717. else
  5718. result:=tObjectSymtable(symtable).datasize;
  5719. end;
  5720. function tobjectdef.alignment:shortint;
  5721. begin
  5722. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5723. alignment:=voidpointertype.size
  5724. else
  5725. alignment:=tObjectSymtable(symtable).recordalignment;
  5726. end;
  5727. function tobjectdef.vmtmethodoffset(index:longint):longint;
  5728. begin
  5729. { for offset of methods for classes, see rtl/inc/objpash.inc }
  5730. case objecttype of
  5731. odt_class:
  5732. { the +2*sizeof(pint) is size and -size }
  5733. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  5734. odt_helper,
  5735. odt_objcclass,
  5736. odt_objcprotocol:
  5737. vmtmethodoffset:=0;
  5738. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  5739. vmtmethodoffset:=index*voidcodepointertype.size;
  5740. odt_javaclass,
  5741. odt_interfacejava:
  5742. { invalid }
  5743. vmtmethodoffset:=-1;
  5744. else
  5745. { the +2*sizeof(pint) is size and -size }
  5746. {$ifdef WITHDMT}
  5747. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  5748. {$else WITHDMT}
  5749. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  5750. {$endif WITHDMT}
  5751. end;
  5752. end;
  5753. function tobjectdef.vmt_mangledname : TSymStr;
  5754. begin
  5755. if not(oo_has_vmt in objectoptions) then
  5756. Message1(parser_n_object_has_no_vmt,objrealname^);
  5757. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  5758. end;
  5759. function tobjectdef.needs_inittable : boolean;
  5760. var
  5761. hp : tobjectdef;
  5762. begin
  5763. case objecttype of
  5764. odt_helper,
  5765. odt_class :
  5766. needs_inittable:=false;
  5767. odt_dispinterface,
  5768. odt_interfacecom:
  5769. needs_inittable:=true;
  5770. odt_interfacecorba:
  5771. begin
  5772. hp:=childof;
  5773. while assigned(hp) do
  5774. begin
  5775. if hp=interface_iunknown then
  5776. begin
  5777. needs_inittable:=true;
  5778. exit;
  5779. end;
  5780. hp:=hp.childof;
  5781. end;
  5782. needs_inittable:=false;
  5783. end;
  5784. odt_object:
  5785. needs_inittable:=
  5786. tObjectSymtable(symtable).needs_init_final or
  5787. (assigned(childof) and
  5788. childof.needs_inittable);
  5789. odt_cppclass,
  5790. odt_objcclass,
  5791. odt_objcprotocol,
  5792. odt_javaclass,
  5793. odt_interfacejava:
  5794. needs_inittable:=false;
  5795. else
  5796. internalerror(200108267);
  5797. end;
  5798. end;
  5799. function tobjectdef.needs_separate_initrtti : boolean;
  5800. begin
  5801. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5802. end;
  5803. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  5804. begin
  5805. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  5806. result:=inherited rtti_mangledname(rt)
  5807. else
  5808. begin
  5809. { necessary in case of a dynamic array of nsobject, or
  5810. if an nsobject field appears in a record that needs
  5811. init/finalisation }
  5812. if rt=initrtti then
  5813. begin
  5814. result:=voidpointertype.rtti_mangledname(rt);
  5815. exit;
  5816. end;
  5817. if not(target_info.system in systems_objc_nfabi) then
  5818. begin
  5819. result:=target_asm.labelprefix;
  5820. case objecttype of
  5821. odt_objcclass:
  5822. begin
  5823. case rt of
  5824. objcclassrtti:
  5825. if not(oo_is_classhelper in objectoptions) then
  5826. result:=result+'_OBJC_CLASS_'
  5827. else
  5828. result:=result+'_OBJC_CATEGORY_';
  5829. objcmetartti:
  5830. if not(oo_is_classhelper in objectoptions) then
  5831. result:=result+'_OBJC_METACLASS_'
  5832. else
  5833. internalerror(2009111511);
  5834. else
  5835. internalerror(2009092302);
  5836. end;
  5837. end;
  5838. odt_objcprotocol:
  5839. result:=result+'_OBJC_PROTOCOL_';
  5840. end;
  5841. end
  5842. else
  5843. begin
  5844. case objecttype of
  5845. odt_objcclass:
  5846. begin
  5847. if (oo_is_classhelper in objectoptions) and
  5848. (rt<>objcclassrtti) then
  5849. internalerror(2009111512);
  5850. case rt of
  5851. objcclassrtti:
  5852. if not(oo_is_classhelper in objectoptions) then
  5853. result:='_OBJC_CLASS_$_'
  5854. else
  5855. result:='_OBJC_$_CATEGORY_';
  5856. objcmetartti:
  5857. result:='_OBJC_METACLASS_$_';
  5858. objcclassrortti:
  5859. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  5860. objcmetarortti:
  5861. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  5862. else
  5863. internalerror(2009092303);
  5864. end;
  5865. end;
  5866. odt_objcprotocol:
  5867. begin
  5868. result:=lower(target_asm.labelprefix);
  5869. case rt of
  5870. objcclassrtti:
  5871. result:=result+'_OBJC_PROTOCOL_$_';
  5872. objcmetartti:
  5873. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  5874. else
  5875. internalerror(2009092501);
  5876. end;
  5877. end;
  5878. else
  5879. internalerror(2013113005);
  5880. end;
  5881. end;
  5882. result:=result+objextname^;
  5883. end;
  5884. end;
  5885. function tobjectdef.members_need_inittable : boolean;
  5886. begin
  5887. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  5888. end;
  5889. function tobjectdef.is_publishable : boolean;
  5890. begin
  5891. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  5892. end;
  5893. function tobjectdef.get_next_dispid: longint;
  5894. begin
  5895. inc(fcurrent_dispid);
  5896. result:=fcurrent_dispid;
  5897. end;
  5898. function tobjectdef.search_enumerator_get: tprocdef;
  5899. begin
  5900. result:=inherited;
  5901. if not assigned(result) and assigned(childof) then
  5902. result:=childof.search_enumerator_get;
  5903. end;
  5904. function tobjectdef.search_enumerator_move: tprocdef;
  5905. begin
  5906. result:=inherited;
  5907. if not assigned(result) and assigned(childof) then
  5908. result:=childof.search_enumerator_move;
  5909. end;
  5910. function tobjectdef.search_enumerator_current: tsym;
  5911. begin
  5912. result:=inherited;
  5913. if not assigned(result) and assigned(childof) then
  5914. result:=childof.search_enumerator_current;
  5915. end;
  5916. procedure tobjectdef.register_created_classref_type;
  5917. begin
  5918. if not classref_created_in_current_module then
  5919. begin
  5920. classref_created_in_current_module:=true;
  5921. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  5922. end;
  5923. end;
  5924. procedure tobjectdef.register_created_object_type;
  5925. begin
  5926. if not created_in_current_module then
  5927. begin
  5928. created_in_current_module:=true;
  5929. current_module.wpoinfo.addcreatedobjtype(self);
  5930. end;
  5931. end;
  5932. procedure tobjectdef.register_maybe_created_object_type;
  5933. begin
  5934. { if we know it has been created for sure, no need
  5935. to also record that it maybe can be created in
  5936. this module
  5937. }
  5938. if not (created_in_current_module) and
  5939. not (maybe_created_in_current_module) then
  5940. begin
  5941. maybe_created_in_current_module:=true;
  5942. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  5943. end;
  5944. end;
  5945. procedure tobjectdef.register_vmt_call(index: longint);
  5946. begin
  5947. if (is_object(self) or is_class(self)) then
  5948. current_module.wpoinfo.addcalledvmtentry(self,index);
  5949. end;
  5950. procedure check_and_finish_msg(data: tobject; arg: pointer);
  5951. var
  5952. def: tdef absolute data;
  5953. pd: tprocdef absolute data;
  5954. i,
  5955. paracount: longint;
  5956. begin
  5957. if (def.typ=procdef) then
  5958. begin
  5959. { add all messages also under a dummy name to the symtable in
  5960. which the objcclass/protocol/category is declared, so they can
  5961. be called via id.<name>
  5962. }
  5963. create_class_helper_for_procdef(pd,nil);
  5964. { we have to wait until now to set the mangled name because it
  5965. depends on the (possibly external) class name, which is defined
  5966. at the very end. }
  5967. if not(po_msgstr in pd.procoptions) then
  5968. begin
  5969. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  5970. { recover to avoid internalerror later on }
  5971. include(pd.procoptions,po_msgstr);
  5972. pd.messageinf.str:=stringdup('MissingDeclaration');
  5973. end;
  5974. { Mangled name is already set in case this is a copy of
  5975. another type. }
  5976. if not(po_has_mangledname in pd.procoptions) then
  5977. begin
  5978. { check whether the number of formal parameters is correct,
  5979. and whether they have valid Objective-C types }
  5980. paracount:=0;
  5981. for i:=1 to length(pd.messageinf.str^) do
  5982. if pd.messageinf.str^[i]=':' then
  5983. inc(paracount);
  5984. for i:=0 to pd.paras.count-1 do
  5985. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  5986. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  5987. dec(paracount);
  5988. if (paracount<>0) then
  5989. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  5990. pd.setmangledname(pd.objcmangledname);
  5991. end
  5992. else
  5993. { all checks already done }
  5994. exit;
  5995. if not(oo_is_external in pd.struct.objectoptions) then
  5996. begin
  5997. if (po_varargs in pd.procoptions) then
  5998. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  5999. else
  6000. begin
  6001. { check for "array of const" parameters }
  6002. for i:=0 to pd.parast.symlist.count-1 do
  6003. begin
  6004. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  6005. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  6006. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  6007. end;
  6008. end;
  6009. end;
  6010. end;
  6011. end;
  6012. procedure mark_private_fields_used(data: tobject; arg: pointer);
  6013. var
  6014. sym: tsym absolute data;
  6015. begin
  6016. if (sym.typ=fieldvarsym) and
  6017. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  6018. sym.IncRefCount;
  6019. end;
  6020. procedure tobjectdef.finish_objc_data;
  6021. begin
  6022. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  6023. if (oo_is_external in objectoptions) then
  6024. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  6025. end;
  6026. procedure verify_objc_vardef(data: tobject; arg: pointer);
  6027. var
  6028. sym: tabstractvarsym absolute data;
  6029. res: pboolean absolute arg;
  6030. founderrordef: tdef;
  6031. begin
  6032. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  6033. exit;
  6034. if (sym.typ=paravarsym) and
  6035. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  6036. is_array_of_const(tparavarsym(sym).vardef)) then
  6037. exit;
  6038. if not objcchecktype(sym.vardef,founderrordef) then
  6039. begin
  6040. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6041. res^:=false;
  6042. end;
  6043. end;
  6044. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  6045. var
  6046. def: tdef absolute data;
  6047. res: pboolean absolute arg;
  6048. founderrordef: tdef;
  6049. begin
  6050. if (def.typ<>procdef) then
  6051. exit;
  6052. { check parameter types for validity }
  6053. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  6054. { check the result type for validity }
  6055. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  6056. begin
  6057. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6058. res^:=false;
  6059. end;
  6060. end;
  6061. function tobjectdef.check_objc_types: boolean;
  6062. begin
  6063. { done in separate step from finish_objc_data, because when
  6064. finish_objc_data is called, not all forwarddefs have been resolved
  6065. yet and we need to know all types here }
  6066. result:=true;
  6067. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  6068. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  6069. end;
  6070. procedure do_cpp_import_info(data: tobject; arg: pointer);
  6071. var
  6072. def: tdef absolute data;
  6073. pd: tprocdef absolute data;
  6074. begin
  6075. if (def.typ=procdef) then
  6076. begin
  6077. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  6078. if (oo_is_external in pd.struct.objectoptions) then
  6079. begin
  6080. { copied from psub.read_proc }
  6081. if assigned(tobjectdef(pd.struct).import_lib) then
  6082. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  6083. else
  6084. begin
  6085. { add import name to external list for DLL scanning }
  6086. if tf_has_dllscanner in target_info.flags then
  6087. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  6088. end;
  6089. end;
  6090. end;
  6091. end;
  6092. procedure tobjectdef.finish_cpp_data;
  6093. begin
  6094. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  6095. end;
  6096. {****************************************************************************
  6097. TImplementedInterface
  6098. ****************************************************************************}
  6099. function TImplementedInterface.GetIOffset: longint;
  6100. begin
  6101. if (fIOffset=-1) and
  6102. (IType in [etFieldValue,etFieldValueClass]) then
  6103. result:=tfieldvarsym(ImplementsField).fieldoffset
  6104. else
  6105. result:=fIOffset;
  6106. end;
  6107. constructor TImplementedInterface.create(aintf: tobjectdef);
  6108. begin
  6109. inherited create;
  6110. intfdef:=aintf;
  6111. IOffset:=-1;
  6112. IType:=etStandard;
  6113. NameMappings:=nil;
  6114. procdefs:=nil;
  6115. end;
  6116. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  6117. begin
  6118. inherited create;
  6119. intfdef:=nil;
  6120. intfdefderef:=intfd;
  6121. ImplementsGetterDeref:=getterd;
  6122. IOffset:=-1;
  6123. IType:=etStandard;
  6124. NameMappings:=nil;
  6125. procdefs:=nil;
  6126. end;
  6127. destructor TImplementedInterface.destroy;
  6128. var
  6129. i : longint;
  6130. mappedname : pshortstring;
  6131. begin
  6132. if assigned(NameMappings) then
  6133. begin
  6134. for i:=0 to NameMappings.Count-1 do
  6135. begin
  6136. mappedname:=pshortstring(NameMappings[i]);
  6137. stringdispose(mappedname);
  6138. end;
  6139. NameMappings.free;
  6140. NameMappings:=nil;
  6141. end;
  6142. if assigned(procdefs) then
  6143. begin
  6144. procdefs.free;
  6145. procdefs:=nil;
  6146. end;
  6147. inherited destroy;
  6148. end;
  6149. procedure TImplementedInterface.buildderef;
  6150. begin
  6151. intfdefderef.build(intfdef);
  6152. ImplementsGetterDeref.build(ImplementsGetter);
  6153. end;
  6154. procedure TImplementedInterface.deref;
  6155. begin
  6156. intfdef:=tobjectdef(intfdefderef.resolve);
  6157. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6158. end;
  6159. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6160. begin
  6161. if not assigned(NameMappings) then
  6162. NameMappings:=TFPHashList.Create;
  6163. NameMappings.Add(origname,stringdup(newname));
  6164. end;
  6165. function TImplementedInterface.GetMapping(const origname: string):string;
  6166. var
  6167. mappedname : pshortstring;
  6168. begin
  6169. result:='';
  6170. if not assigned(NameMappings) then
  6171. exit;
  6172. mappedname:=PShortstring(NameMappings.Find(origname));
  6173. if assigned(mappedname) then
  6174. result:=mappedname^;
  6175. end;
  6176. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6177. begin
  6178. if not assigned(procdefs) then
  6179. procdefs:=TFPObjectList.Create(false);
  6180. { duplicate entries must be stored, because multiple }
  6181. { interfaces can declare methods with the same name }
  6182. { and all of these get their own VMT entry }
  6183. procdefs.Add(pd);
  6184. end;
  6185. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  6186. var
  6187. i : longint;
  6188. begin
  6189. result:=false;
  6190. { interfaces being implemented through delegation are not mergable (FK) }
  6191. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  6192. exit;
  6193. weight:=0;
  6194. { empty interface is mergeable }
  6195. if ProcDefs.Count=0 then
  6196. begin
  6197. result:=true;
  6198. exit;
  6199. end;
  6200. { The interface to merge must at least the number of
  6201. procedures of this interface }
  6202. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  6203. exit;
  6204. for i:=0 to ProcDefs.Count-1 do
  6205. begin
  6206. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  6207. exit;
  6208. end;
  6209. weight:=ProcDefs.Count;
  6210. result:=true;
  6211. end;
  6212. function TImplementedInterface.getcopy:TImplementedInterface;
  6213. begin
  6214. Result:=TImplementedInterface.Create(nil);
  6215. { 1) the procdefs list will be freed once for each copy
  6216. 2) since the procdefs list owns its elements, those will also be freed for each copy
  6217. 3) idem for the name mappings
  6218. }
  6219. { warning: this is completely wrong on so many levels...
  6220. Move(pointer(self)^,pointer(result)^,InstanceSize);
  6221. We need to make clean copies of the different fields
  6222. this is not implemented yet, and thus we generate an internal
  6223. error instead PM 2011-06-14 }
  6224. internalerror(2011061401);
  6225. end;
  6226. {****************************************************************************
  6227. TFORWARDDEF
  6228. ****************************************************************************}
  6229. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  6230. begin
  6231. inherited create(forwarddef);
  6232. tosymname:=stringdup(s);
  6233. forwardpos:=pos;
  6234. end;
  6235. function tforwarddef.GetTypeName:string;
  6236. begin
  6237. GetTypeName:='unresolved forward to '+tosymname^;
  6238. end;
  6239. destructor tforwarddef.destroy;
  6240. begin
  6241. stringdispose(tosymname);
  6242. inherited destroy;
  6243. end;
  6244. function tforwarddef.getcopy:tstoreddef;
  6245. begin
  6246. result:=cforwarddef.create(tosymname^, forwardpos);
  6247. end;
  6248. {****************************************************************************
  6249. TUNDEFINEDDEF
  6250. ****************************************************************************}
  6251. constructor tundefineddef.create;
  6252. begin
  6253. inherited create(undefineddef);
  6254. end;
  6255. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  6256. begin
  6257. inherited ppuload(undefineddef,ppufile);
  6258. ppuload_platform(ppufile);
  6259. end;
  6260. function tundefineddef.GetTypeName:string;
  6261. begin
  6262. GetTypeName:='<undefined type>';
  6263. end;
  6264. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  6265. begin
  6266. inherited ppuwrite(ppufile);
  6267. writeentry(ppufile,ibundefineddef);
  6268. end;
  6269. {****************************************************************************
  6270. TERRORDEF
  6271. ****************************************************************************}
  6272. constructor terrordef.create;
  6273. begin
  6274. inherited create(errordef);
  6275. { prevent consecutive faults }
  6276. savesize:=1;
  6277. end;
  6278. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  6279. begin
  6280. { Can't write errordefs to ppu }
  6281. internalerror(200411063);
  6282. end;
  6283. function terrordef.GetTypeName:string;
  6284. begin
  6285. GetTypeName:='<erroneous type>';
  6286. end;
  6287. function terrordef.getmangledparaname:TSymStr;
  6288. begin
  6289. getmangledparaname:='error';
  6290. end;
  6291. {****************************************************************************
  6292. Definition Helpers
  6293. ****************************************************************************}
  6294. function is_interfacecom(def: tdef): boolean;
  6295. begin
  6296. is_interfacecom:=
  6297. assigned(def) and
  6298. (def.typ=objectdef) and
  6299. (tobjectdef(def).objecttype=odt_interfacecom);
  6300. end;
  6301. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  6302. begin
  6303. is_interfacecom_or_dispinterface:=
  6304. assigned(def) and
  6305. (def.typ=objectdef) and
  6306. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  6307. end;
  6308. function is_any_interface_kind(def: tdef): boolean;
  6309. begin
  6310. result:=
  6311. assigned(def) and
  6312. (def.typ=objectdef) and
  6313. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  6314. is_objccategory(def));
  6315. end;
  6316. function is_interfacecorba(def: tdef): boolean;
  6317. begin
  6318. is_interfacecorba:=
  6319. assigned(def) and
  6320. (def.typ=objectdef) and
  6321. (tobjectdef(def).objecttype=odt_interfacecorba);
  6322. end;
  6323. function is_interface(def: tdef): boolean;
  6324. begin
  6325. is_interface:=
  6326. assigned(def) and
  6327. (def.typ=objectdef) and
  6328. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  6329. end;
  6330. function is_dispinterface(def: tdef): boolean;
  6331. begin
  6332. result:=
  6333. assigned(def) and
  6334. (def.typ=objectdef) and
  6335. (tobjectdef(def).objecttype=odt_dispinterface);
  6336. end;
  6337. function is_class(def: tdef): boolean;
  6338. begin
  6339. is_class:=
  6340. assigned(def) and
  6341. (def.typ=objectdef) and
  6342. (tobjectdef(def).objecttype=odt_class);
  6343. end;
  6344. function is_object(def: tdef): boolean;
  6345. begin
  6346. is_object:=
  6347. assigned(def) and
  6348. (def.typ=objectdef) and
  6349. (tobjectdef(def).objecttype=odt_object);
  6350. end;
  6351. function is_cppclass(def: tdef): boolean;
  6352. begin
  6353. is_cppclass:=
  6354. assigned(def) and
  6355. (def.typ=objectdef) and
  6356. (tobjectdef(def).objecttype=odt_cppclass);
  6357. end;
  6358. function is_objcclass(def: tdef): boolean;
  6359. begin
  6360. is_objcclass:=
  6361. assigned(def) and
  6362. (def.typ=objectdef) and
  6363. (tobjectdef(def).objecttype=odt_objcclass);
  6364. end;
  6365. function is_objectpascal_helper(def: tdef): boolean;
  6366. begin
  6367. result:=
  6368. assigned(def) and
  6369. (def.typ=objectdef) and
  6370. (tobjectdef(def).objecttype=odt_helper);
  6371. end;
  6372. function is_objcclassref(def: tdef): boolean;
  6373. begin
  6374. is_objcclassref:=
  6375. assigned(def) and
  6376. (def.typ=classrefdef) and
  6377. is_objcclass(tclassrefdef(def).pointeddef);
  6378. end;
  6379. function is_objcprotocol(def: tdef): boolean;
  6380. begin
  6381. result:=
  6382. assigned(def) and
  6383. (def.typ=objectdef) and
  6384. (tobjectdef(def).objecttype=odt_objcprotocol);
  6385. end;
  6386. function is_objccategory(def: tdef): boolean;
  6387. begin
  6388. result:=
  6389. assigned(def) and
  6390. (def.typ=objectdef) and
  6391. { if used as a forward type }
  6392. ((tobjectdef(def).objecttype=odt_objccategory) or
  6393. { if used as after it has been resolved }
  6394. ((tobjectdef(def).objecttype=odt_objcclass) and
  6395. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6396. end;
  6397. function is_objc_class_or_protocol(def: tdef): boolean;
  6398. begin
  6399. result:=
  6400. assigned(def) and
  6401. (def.typ=objectdef) and
  6402. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  6403. end;
  6404. function is_objc_protocol_or_category(def: tdef): boolean;
  6405. begin
  6406. result:=
  6407. assigned(def) and
  6408. (def.typ=objectdef) and
  6409. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  6410. ((tobjectdef(def).objecttype = odt_objcclass) and
  6411. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6412. end;
  6413. function is_classhelper(def: tdef): boolean;
  6414. begin
  6415. result:=
  6416. is_objectpascal_helper(def) or
  6417. is_objccategory(def);
  6418. end;
  6419. function is_class_or_interface(def: tdef): boolean;
  6420. begin
  6421. result:=
  6422. assigned(def) and
  6423. (def.typ=objectdef) and
  6424. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  6425. end;
  6426. function is_class_or_interface_or_objc(def: tdef): boolean;
  6427. begin
  6428. result:=
  6429. assigned(def) and
  6430. (def.typ=objectdef) and
  6431. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  6432. end;
  6433. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  6434. begin
  6435. result:=
  6436. assigned(def) and
  6437. (def.typ=objectdef) and
  6438. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6439. end;
  6440. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  6441. begin
  6442. result:=
  6443. assigned(def) and
  6444. (def.typ=objectdef) and
  6445. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6446. end;
  6447. function is_class_or_interface_or_object(def: tdef): boolean;
  6448. begin
  6449. result:=
  6450. assigned(def) and
  6451. (def.typ=objectdef) and
  6452. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  6453. end;
  6454. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  6455. begin
  6456. result:=
  6457. assigned(def) and
  6458. (def.typ=objectdef) and
  6459. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6460. end;
  6461. function is_implicit_pointer_object_type(def: tdef): boolean;
  6462. begin
  6463. result:=
  6464. assigned(def) and
  6465. (((def.typ=objectdef) and
  6466. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  6467. ((target_info.system in systems_jvm) and
  6468. (def.typ=recorddef)));
  6469. end;
  6470. function is_implicit_array_pointer(def: tdef): boolean;
  6471. begin
  6472. result:=is_dynamic_array(def) or is_dynamicstring(def);
  6473. end;
  6474. function is_class_or_object(def: tdef): boolean;
  6475. begin
  6476. result:=
  6477. assigned(def) and
  6478. (def.typ=objectdef) and
  6479. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  6480. end;
  6481. function is_record(def: tdef): boolean;
  6482. begin
  6483. result:=
  6484. assigned(def) and
  6485. (def.typ=recorddef);
  6486. end;
  6487. function is_javaclass(def: tdef): boolean;
  6488. begin
  6489. result:=
  6490. assigned(def) and
  6491. (def.typ=objectdef) and
  6492. (tobjectdef(def).objecttype=odt_javaclass);
  6493. end;
  6494. function is_javaclassref(def: tdef): boolean;
  6495. begin
  6496. is_javaclassref:=
  6497. assigned(def) and
  6498. (def.typ=classrefdef) and
  6499. is_javaclass(tclassrefdef(def).pointeddef);
  6500. end;
  6501. function is_javainterface(def: tdef): boolean;
  6502. begin
  6503. result:=
  6504. assigned(def) and
  6505. (def.typ=objectdef) and
  6506. (tobjectdef(def).objecttype=odt_interfacejava);
  6507. end;
  6508. function is_java_class_or_interface(def: tdef): boolean;
  6509. begin
  6510. result:=
  6511. assigned(def) and
  6512. (def.typ=objectdef) and
  6513. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  6514. end;
  6515. procedure loadobjctypes;
  6516. begin
  6517. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  6518. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  6519. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  6520. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  6521. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  6522. end;
  6523. procedure maybeloadcocoatypes;
  6524. var
  6525. tsym: ttypesym;
  6526. cocoaunit: string[15];
  6527. begin
  6528. if assigned(objc_fastenumeration) then
  6529. exit;
  6530. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim]) then
  6531. cocoaunit:='COCOAALL'
  6532. else
  6533. cocoaunit:='IPHONEALL';
  6534. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  6535. if assigned(tsym) then
  6536. objc_fastenumeration:=tobjectdef(tsym.typedef)
  6537. else
  6538. objc_fastenumeration:=nil;
  6539. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  6540. if assigned(tsym) then
  6541. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  6542. else
  6543. objc_fastenumerationstate:=nil;
  6544. end;
  6545. function use_vectorfpu(def : tdef) : boolean;
  6546. begin
  6547. {$ifdef x86}
  6548. {$define use_vectorfpuimplemented}
  6549. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  6550. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  6551. {$endif x86}
  6552. {$ifdef arm}
  6553. {$define use_vectorfpuimplemented}
  6554. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  6555. {$endif arm}
  6556. {$ifndef use_vectorfpuimplemented}
  6557. use_vectorfpu:=false;
  6558. {$endif}
  6559. end;
  6560. function getpointerdef(def: tdef): tpointerdef;
  6561. var
  6562. res: PHashSetItem;
  6563. oldsymtablestack: tsymtablestack;
  6564. begin
  6565. if not assigned(current_module) then
  6566. internalerror(2011071101);
  6567. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  6568. if not assigned(res^.Data) then
  6569. begin
  6570. { since these pointerdefs can be reused anywhere in the current
  6571. unit, add them to the global/staticsymtable }
  6572. oldsymtablestack:=symtablestack;
  6573. { do not simply push/pop current_module.localsymtable, because
  6574. that can have side-effects (e.g., it removes helpers) }
  6575. symtablestack:=nil;
  6576. res^.Data:=cpointerdef.create(def);
  6577. if assigned(current_module.localsymtable) then
  6578. current_module.localsymtable.insertdef(tdef(res^.Data))
  6579. else
  6580. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6581. symtablestack:=oldsymtablestack;
  6582. end;
  6583. result:=tpointerdef(res^.Data);
  6584. end;
  6585. function getsingletonarraydef(def: tdef): tarraydef;
  6586. begin
  6587. result:=getarraydef(def,1);
  6588. end;
  6589. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  6590. var
  6591. res: PHashSetItem;
  6592. oldsymtablestack: tsymtablestack;
  6593. arrdesc: packed record
  6594. def: tdef;
  6595. elecount: asizeint;
  6596. end;
  6597. begin
  6598. if not assigned(current_module) then
  6599. internalerror(2011081301);
  6600. arrdesc.def:=def;
  6601. arrdesc.elecount:=elecount;
  6602. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  6603. if not assigned(res^.Data) then
  6604. begin
  6605. { since these arraydef can be reused anywhere in the current
  6606. unit, add them to the global/staticsymtable }
  6607. oldsymtablestack:=symtablestack;
  6608. symtablestack:=nil;
  6609. res^.Data:=carraydef.create(0,elecount-1,ptrsinttype);
  6610. tarraydef(res^.Data).elementdef:=def;
  6611. if assigned(current_module.localsymtable) then
  6612. current_module.localsymtable.insertdef(tdef(res^.Data))
  6613. else
  6614. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6615. symtablestack:=oldsymtablestack;
  6616. end;
  6617. result:=tarraydef(res^.Data);
  6618. end;
  6619. end.