symdef.pas 247 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545
  1. {
  2. Symbol table implementation for the definitions
  3. Copyright (c) 1998-2005 by Florian Klaempfl, Pierre Muller
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symdef;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { ppu }
  28. ppu,
  29. { node }
  30. node,
  31. { aasm }
  32. aasmbase,aasmtai,
  33. cpubase,cpuinfo,
  34. cgbase,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. tgenericconstraintdata=class
  42. interfaces : tfpobjectlist;
  43. interfacesderef : tfplist;
  44. flags : tgenericconstraintflags;
  45. constructor create;
  46. destructor destroy;override;
  47. procedure ppuload(ppufile:tcompilerppufile);
  48. procedure ppuwrite(ppufile:tcompilerppufile);
  49. procedure buildderef;
  50. procedure deref;
  51. end;
  52. { tstoreddef }
  53. tstoreddef = class(tdef)
  54. private
  55. _fullownerhierarchyname : pshortstring;
  56. procedure writeentry(ppufile: tcompilerppufile; ibnr: byte);
  57. protected
  58. typesymderef : tderef;
  59. procedure ppuwrite_platform(ppufile:tcompilerppufile);virtual;
  60. procedure ppuload_platform(ppufile:tcompilerppufile);virtual;
  61. public
  62. {$ifdef EXTDEBUG}
  63. fileinfo : tfileposinfo;
  64. {$endif}
  65. { generic support }
  66. genericdef : tstoreddef;
  67. genericdefderef : tderef;
  68. generictokenbuf : tdynamicarray;
  69. { this list contains references to the symbols that make up the
  70. generic parameters; the symbols are not owned by this list
  71. Note: this list is allocated on demand! }
  72. genericparas : tfphashobjectlist;
  73. genericparaderefs : tfplist;
  74. { contains additional data if this def is a generic constraint
  75. Note: this class is allocated on demand! }
  76. genconstraintdata : tgenericconstraintdata;
  77. constructor create(dt:tdeftyp);
  78. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  79. destructor destroy;override;
  80. function getcopy : tstoreddef;virtual;
  81. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  82. procedure buildderef;override;
  83. procedure buildderefimpl;override;
  84. procedure deref;override;
  85. procedure derefimpl;override;
  86. function size:asizeint;override;
  87. function getvardef:longint;override;
  88. function alignment:shortint;override;
  89. function is_publishable : boolean;override;
  90. function needs_inittable : boolean;override;
  91. function rtti_mangledname(rt:trttitype):string;override;
  92. function OwnerHierarchyName: string; override;
  93. function fullownerhierarchyname:string;override;
  94. function needs_separate_initrtti:boolean;override;
  95. function in_currentunit: boolean;
  96. { regvars }
  97. function is_intregable : boolean;
  98. function is_fpuregable : boolean;
  99. { def can be put into a register if it is const/immutable }
  100. function is_const_intregable : boolean;
  101. { generics }
  102. procedure initgeneric;
  103. { this function can be used to determine whether a def is really a
  104. generic declaration or just a normal type declared inside another
  105. generic }
  106. function is_generic:boolean;inline;
  107. { same as above for specializations }
  108. function is_specialization:boolean;inline;
  109. private
  110. savesize : asizeuint;
  111. end;
  112. tfiletyp = (ft_text,ft_typed,ft_untyped);
  113. tfiledef = class(tstoreddef)
  114. filetyp : tfiletyp;
  115. typedfiledef : tdef;
  116. typedfiledefderef : tderef;
  117. constructor createtext;virtual;
  118. constructor createuntyped;virtual;
  119. constructor createtyped(def : tdef);virtual;
  120. constructor ppuload(ppufile:tcompilerppufile);
  121. function getcopy : tstoreddef;override;
  122. { do not override this routine in platform-specific subclasses,
  123. override ppuwrite_platform instead }
  124. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  125. procedure buildderef;override;
  126. procedure deref;override;
  127. function GetTypeName:string;override;
  128. function getmangledparaname:TSymStr;override;
  129. function size:asizeint;override;
  130. procedure setsize;
  131. end;
  132. tfiledefclass = class of tfiledef;
  133. tvariantdef = class(tstoreddef)
  134. varianttype : tvarianttype;
  135. constructor create(v : tvarianttype);virtual;
  136. constructor ppuload(ppufile:tcompilerppufile);
  137. function getcopy : tstoreddef;override;
  138. function GetTypeName:string;override;
  139. { do not override this routine in platform-specific subclasses,
  140. override ppuwrite_platform instead }
  141. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  142. function getvardef:longint;override;
  143. procedure setsize;
  144. function is_publishable : boolean;override;
  145. function needs_inittable : boolean;override;
  146. end;
  147. tvariantdefclass = class of tvariantdef;
  148. tformaldef = class(tstoreddef)
  149. typed:boolean;
  150. constructor create(Atyped:boolean);virtual;
  151. constructor ppuload(ppufile:tcompilerppufile);
  152. { do not override this routine in platform-specific subclasses,
  153. override ppuwrite_platform instead }
  154. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  155. function GetTypeName:string;override;
  156. end;
  157. tformaldefclass = class of tformaldef;
  158. tforwarddef = class(tstoreddef)
  159. tosymname : pshortstring;
  160. forwardpos : tfileposinfo;
  161. constructor create(const s:string;const pos:tfileposinfo);virtual;
  162. destructor destroy;override;
  163. function getcopy:tstoreddef;override;
  164. function GetTypeName:string;override;
  165. end;
  166. tforwarddefclass = class of tforwarddef;
  167. tundefineddef = class(tstoreddef)
  168. constructor create;virtual;
  169. constructor ppuload(ppufile:tcompilerppufile);
  170. { do not override this routine in platform-specific subclasses,
  171. override ppuwrite_platform instead }
  172. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  173. function GetTypeName:string;override;
  174. end;
  175. tundefineddefclass = class of tundefineddef;
  176. terrordef = class(tstoreddef)
  177. constructor create;virtual;
  178. { do not override this routine in platform-specific subclasses,
  179. override ppuwrite_platform instead }
  180. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  181. function GetTypeName:string;override;
  182. function getmangledparaname : TSymStr;override;
  183. end;
  184. terrordefclass = class of terrordef;
  185. tabstractpointerdef = class(tstoreddef)
  186. pointeddef : tdef;
  187. pointeddefderef : tderef;
  188. constructor create(dt:tdeftyp;def:tdef);
  189. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  190. procedure ppuwrite(ppufile:tcompilerppufile);override;
  191. procedure buildderef;override;
  192. procedure deref;override;
  193. function size:asizeint;override;
  194. function alignment:shortint;override;
  195. end;
  196. { tpointerdef }
  197. tpointerdef = class(tabstractpointerdef)
  198. has_pointer_math : boolean;
  199. constructor create(def:tdef);virtual;
  200. function size:asizeint;override;
  201. function getcopy:tstoreddef;override;
  202. constructor ppuload(ppufile:tcompilerppufile);
  203. { do not override this routine in platform-specific subclasses,
  204. override ppuwrite_platform instead }
  205. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  206. function GetTypeName:string;override;
  207. {# returns the appropriate int type for pointer arithmetic with the given pointer type.
  208. When adding or subtracting a number to/from a pointer, this function returns the
  209. int type to which that number has to be converted, before the operation can be performed.
  210. Normally, this is sinttype, except on i8086, where it takes into account the
  211. special i8086 pointer types (near, far, huge). }
  212. function pointer_arithmetic_int_type:tdef;virtual;
  213. {# returns the int type produced when subtracting two pointers of the given type.
  214. Normally, this is sinttype, except on i8086, where it takes into account the
  215. special i8086 pointer types (near, far, huge). }
  216. function pointer_subtraction_result_type:tdef;virtual;
  217. end;
  218. tpointerdefclass = class of tpointerdef;
  219. tprocdef = class;
  220. tabstractrecorddef= class(tstoreddef)
  221. private
  222. rttistring : string;
  223. public
  224. objname,
  225. objrealname : PShortString;
  226. { for C++ classes: name of the library this class is imported from }
  227. { for Java classes/records: package name }
  228. import_lib : PShortString;
  229. symtable : TSymtable;
  230. cloneddef : tabstractrecorddef;
  231. cloneddefderef : tderef;
  232. objectoptions : tobjectoptions;
  233. { for targets that initialise typed constants via explicit assignments
  234. instead of by generating an initialised data sectino }
  235. tcinitcode : tnode;
  236. constructor create(const n:string; dt:tdeftyp);
  237. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  238. procedure ppuwrite(ppufile:tcompilerppufile);override;
  239. destructor destroy; override;
  240. procedure buildderefimpl;override;
  241. procedure derefimpl;override;
  242. procedure check_forwards; virtual;
  243. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  244. function GetSymtable(t:tGetSymtable):TSymtable;override;
  245. function is_packed:boolean;
  246. function RttiName: string;
  247. { enumerator support }
  248. function search_enumerator_get: tprocdef; virtual;
  249. function search_enumerator_move: tprocdef; virtual;
  250. function search_enumerator_current: tsym; virtual;
  251. { JVM }
  252. function jvm_full_typename(with_package_name: boolean): string;
  253. { check if the symtable contains a float field }
  254. function contains_float_field : boolean;
  255. end;
  256. pvariantrecdesc = ^tvariantrecdesc;
  257. tvariantrecbranch = record
  258. { we store only single values here and no ranges because tvariantrecdesc is only needed in iso mode
  259. which does not support range expressions in variant record definitions }
  260. values : array of Tconstexprint;
  261. nestedvariant : pvariantrecdesc;
  262. end;
  263. ppvariantrecdesc = ^pvariantrecdesc;
  264. tvariantrecdesc = record
  265. variantselector : tsym;
  266. variantselectorderef : tderef;
  267. branches : array of tvariantrecbranch;
  268. end;
  269. trecorddef = class(tabstractrecorddef)
  270. public
  271. variantrecdesc : pvariantrecdesc;
  272. isunion : boolean;
  273. constructor create(const n:string; p:TSymtable);virtual;
  274. constructor ppuload(ppufile:tcompilerppufile);
  275. destructor destroy;override;
  276. function getcopy : tstoreddef;override;
  277. { do not override this routine in platform-specific subclasses,
  278. override ppuwrite_platform instead }
  279. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  280. procedure buildderef;override;
  281. procedure deref;override;
  282. function size:asizeint;override;
  283. function alignment : shortint;override;
  284. function padalignment: shortint;
  285. function GetTypeName:string;override;
  286. { debug }
  287. function needs_inittable : boolean;override;
  288. function needs_separate_initrtti:boolean;override;
  289. end;
  290. trecorddefclass = class of trecorddef;
  291. tobjectdef = class;
  292. { TImplementedInterface }
  293. TImplementedInterface = class
  294. private
  295. fIOffset : longint;
  296. function GetIOffset: longint;
  297. public
  298. IntfDef : tobjectdef;
  299. IntfDefDeref : tderef;
  300. IType : tinterfaceentrytype;
  301. VtblImplIntf : TImplementedInterface;
  302. NameMappings : TFPHashList;
  303. ProcDefs : TFPObjectList;
  304. ImplementsGetter : tsym;
  305. ImplementsGetterDeref : tderef;
  306. ImplementsField : tsym;
  307. constructor create(aintf: tobjectdef);virtual;
  308. constructor create_deref(intfd,getterd:tderef);virtual;
  309. destructor destroy; override;
  310. function getcopy:TImplementedInterface;
  311. procedure buildderef;
  312. procedure deref;
  313. procedure AddMapping(const origname, newname: string);
  314. function GetMapping(const origname: string):string;
  315. procedure AddImplProc(pd:tprocdef);
  316. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  317. property IOffset: longint read GetIOffset write fIOffset;
  318. end;
  319. timplementedinterfaceclass = class of timplementedinterface;
  320. { tvmtentry }
  321. tvmtentry = record
  322. procdef : tprocdef;
  323. procdefderef : tderef;
  324. visibility : tvisibility;
  325. end;
  326. pvmtentry = ^tvmtentry;
  327. { tobjectdef }
  328. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  329. pmvcallstaticinfo = ^tmvcallstaticinfo;
  330. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  331. tobjectdef = class(tabstractrecorddef)
  332. private
  333. fcurrent_dispid: longint;
  334. public
  335. dwarf_struct_lab : tasmsymbol;
  336. childof : tobjectdef;
  337. childofderef : tderef;
  338. { for Object Pascal helpers }
  339. extendeddef : tdef;
  340. extendeddefderef: tderef;
  341. { for Objective-C: protocols and classes can have the same name there }
  342. objextname : pshortstring;
  343. { to be able to have a variable vmt position }
  344. { and no vmt field for objects without virtuals }
  345. vmtentries : TFPList;
  346. vmcallstaticinfo : pmvcallstaticinfo;
  347. vmt_offset : longint;
  348. iidguid : pguid;
  349. iidstr : pshortstring;
  350. { store implemented interfaces defs and name mappings }
  351. ImplementedInterfaces : TFPObjectList;
  352. { number of abstract methods (used by JVM target to determine whether
  353. or not the class should be marked as abstract: must be done if 1 or
  354. more abstract methods) }
  355. abstractcnt : longint;
  356. writing_class_record_dbginfo,
  357. { a class of this type has been created in this module }
  358. created_in_current_module,
  359. { a loadvmtnode for this class has been created in this
  360. module, so if a classrefdef variable of this or a parent
  361. class is used somewhere to instantiate a class, then this
  362. class may be instantiated
  363. }
  364. maybe_created_in_current_module,
  365. { a "class of" this particular class has been created in
  366. this module
  367. }
  368. classref_created_in_current_module : boolean;
  369. objecttype : tobjecttyp;
  370. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);virtual;
  371. constructor ppuload(ppufile:tcompilerppufile);
  372. destructor destroy;override;
  373. function getcopy : tstoreddef;override;
  374. { do not override this routine in platform-specific subclasses,
  375. override ppuwrite_platform instead }
  376. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  377. function GetTypeName:string;override;
  378. procedure buildderef;override;
  379. procedure deref;override;
  380. procedure derefimpl;override;
  381. procedure resetvmtentries;
  382. procedure copyvmtentries(objdef:tobjectdef);
  383. function getparentdef:tdef;override;
  384. function size : asizeint;override;
  385. function alignment:shortint;override;
  386. function vmtmethodoffset(index:longint):longint;
  387. function members_need_inittable : boolean;
  388. { this should be called when this class implements an interface }
  389. procedure prepareguid;
  390. function is_publishable : boolean;override;
  391. function needs_inittable : boolean;override;
  392. function needs_separate_initrtti : boolean;override;
  393. function rtti_mangledname(rt:trttitype):string;override;
  394. function vmt_mangledname : TSymStr;
  395. procedure check_forwards; override;
  396. procedure insertvmt;
  397. procedure set_parent(c : tobjectdef);
  398. function find_destructor: tprocdef;
  399. function implements_any_interfaces: boolean;
  400. { dispinterface support }
  401. function get_next_dispid: longint;
  402. { enumerator support }
  403. function search_enumerator_get: tprocdef; override;
  404. function search_enumerator_move: tprocdef; override;
  405. function search_enumerator_current: tsym; override;
  406. { WPO }
  407. procedure register_created_object_type;override;
  408. procedure register_maybe_created_object_type;
  409. procedure register_created_classref_type;
  410. procedure register_vmt_call(index:longint);
  411. { ObjC }
  412. procedure finish_objc_data;
  413. function check_objc_types: boolean;
  414. { C++ }
  415. procedure finish_cpp_data;
  416. end;
  417. tobjectdefclass = class of tobjectdef;
  418. tclassrefdef = class(tabstractpointerdef)
  419. constructor create(def:tdef);virtual;
  420. constructor ppuload(ppufile:tcompilerppufile);
  421. { do not override this routine in platform-specific subclasses,
  422. override ppuwrite_platform instead }
  423. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  424. function getcopy:tstoreddef;override;
  425. function GetTypeName:string;override;
  426. function is_publishable : boolean;override;
  427. function rtti_mangledname(rt:trttitype):string;override;
  428. procedure register_created_object_type;override;
  429. end;
  430. tclassrefdefclass = class of tclassrefdef;
  431. tarraydef = class(tstoreddef)
  432. lowrange,
  433. highrange : asizeint;
  434. rangedef : tdef;
  435. rangedefderef : tderef;
  436. arrayoptions : tarraydefoptions;
  437. symtable : TSymtable;
  438. protected
  439. _elementdef : tdef;
  440. _elementdefderef : tderef;
  441. procedure setelementdef(def:tdef);
  442. public
  443. function elesize : asizeint;
  444. function elepackedbitsize : asizeint;
  445. function elecount : asizeuint;
  446. constructor create_from_pointer(def:tpointerdef);virtual;
  447. constructor create(l,h:asizeint;def:tdef);virtual;
  448. constructor ppuload(ppufile:tcompilerppufile);
  449. destructor destroy; override;
  450. function getcopy : tstoreddef;override;
  451. { do not override this routine in platform-specific subclasses,
  452. override ppuwrite_platform instead }
  453. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  454. function GetTypeName:string;override;
  455. function getmangledparaname : TSymStr;override;
  456. procedure buildderef;override;
  457. procedure deref;override;
  458. function size : asizeint;override;
  459. function alignment : shortint;override;
  460. { returns the label of the range check string }
  461. function needs_inittable : boolean;override;
  462. function needs_separate_initrtti : boolean;override;
  463. property elementdef : tdef read _elementdef write setelementdef;
  464. function is_publishable : boolean;override;
  465. end;
  466. tarraydefclass = class of tarraydef;
  467. torddef = class(tstoreddef)
  468. low,high : TConstExprInt;
  469. ordtype : tordtype;
  470. constructor create(t : tordtype;v,b : TConstExprInt);virtual;
  471. constructor ppuload(ppufile:tcompilerppufile);
  472. function getcopy : tstoreddef;override;
  473. { do not override this routine in platform-specific subclasses,
  474. override ppuwrite_platform instead }
  475. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  476. function is_publishable : boolean;override;
  477. function GetTypeName:string;override;
  478. function alignment:shortint;override;
  479. procedure setsize;
  480. function packedbitsize: asizeint; override;
  481. function getvardef : longint;override;
  482. end;
  483. torddefclass = class of torddef;
  484. tfloatdef = class(tstoreddef)
  485. floattype : tfloattype;
  486. constructor create(t : tfloattype);virtual;
  487. constructor ppuload(ppufile:tcompilerppufile);
  488. function getcopy : tstoreddef;override;
  489. { do not override this routine in platform-specific subclasses,
  490. override ppuwrite_platform instead }
  491. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  492. function GetTypeName:string;override;
  493. function is_publishable : boolean;override;
  494. function alignment:shortint;override;
  495. function structalignment: shortint;override;
  496. procedure setsize;
  497. function getvardef:longint;override;
  498. end;
  499. tfloatdefclass = class of tfloatdef;
  500. { tabstractprocdef }
  501. tprocnameoption = (pno_showhidden, pno_proctypeoption, pno_paranames,
  502. pno_ownername, pno_noclassmarker, pno_noleadingdollar,
  503. pno_mangledname);
  504. tprocnameoptions = set of tprocnameoption;
  505. tproccopytyp = (pc_normal,
  506. { always creates a top-level function, removes all
  507. special parameters (self, vmt, parentfp, ...) }
  508. pc_bareproc
  509. );
  510. tabstractprocdef = class(tstoreddef)
  511. { saves a definition to the return type }
  512. returndef : tdef;
  513. returndefderef : tderef;
  514. parast : TSymtable;
  515. paras : tparalist;
  516. proctypeoption : tproctypeoption;
  517. proccalloption : tproccalloption;
  518. procoptions : tprocoptions;
  519. callerargareasize,
  520. calleeargareasize: pint;
  521. {$ifdef m68k}
  522. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  523. {$endif}
  524. funcretloc : array[tcallercallee] of TCGPara;
  525. has_paraloc_info : tcallercallee; { paraloc info is available }
  526. { number of user visible parameters }
  527. maxparacount,
  528. minparacount : byte;
  529. constructor create(dt:tdeftyp;level:byte);
  530. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  531. destructor destroy;override;
  532. procedure ppuwrite(ppufile:tcompilerppufile);override;
  533. procedure buildderef;override;
  534. procedure deref;override;
  535. procedure calcparas;
  536. function typename_paras(pno: tprocnameoptions): ansistring;
  537. function is_methodpointer:boolean;virtual;
  538. function is_addressonly:boolean;virtual;
  539. function no_self_node:boolean;
  540. { get either a copy as a procdef or procvardef }
  541. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; virtual;
  542. procedure check_mark_as_nested;
  543. procedure init_paraloc_info(side: tcallercallee);
  544. function stack_tainting_parameter(side: tcallercallee): boolean;
  545. function is_pushleftright: boolean;virtual;
  546. function address_type:tdef;virtual;
  547. procedure declared_far;virtual;
  548. procedure declared_near;virtual;
  549. private
  550. procedure count_para(p:TObject;arg:pointer);
  551. procedure insert_para(p:TObject;arg:pointer);
  552. end;
  553. tprocvardef = class(tabstractprocdef)
  554. constructor create(level:byte);virtual;
  555. constructor ppuload(ppufile:tcompilerppufile);
  556. function getcopy : tstoreddef;override;
  557. { do not override this routine in platform-specific subclasses,
  558. override ppuwrite_platform instead }
  559. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  560. function GetSymtable(t:tGetSymtable):TSymtable;override;
  561. function size : asizeint;override;
  562. function GetTypeName:string;override;
  563. function is_publishable : boolean;override;
  564. function is_methodpointer:boolean;override;
  565. function is_addressonly:boolean;override;
  566. function getmangledparaname:TSymStr;override;
  567. end;
  568. tprocvardefclass = class of tprocvardef;
  569. tmessageinf = record
  570. case integer of
  571. 0 : (str : pshortstring);
  572. 1 : (i : longint);
  573. end;
  574. tinlininginfo = record
  575. { node tree }
  576. code : tnode;
  577. flags : tprocinfoflags;
  578. end;
  579. pinlininginfo = ^tinlininginfo;
  580. {$ifdef oldregvars}
  581. { register variables }
  582. pregvarinfo = ^tregvarinfo;
  583. tregvarinfo = record
  584. regvars : array[1..maxvarregs] of tsym;
  585. regvars_para : array[1..maxvarregs] of boolean;
  586. regvars_refs : array[1..maxvarregs] of longint;
  587. fpuregvars : array[1..maxfpuvarregs] of tsym;
  588. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  589. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  590. end;
  591. {$endif oldregvars}
  592. timplprocdefinfo = record
  593. resultname : pshortstring;
  594. parentfpstruct: tsym;
  595. parentfpstructptrtype: tdef;
  596. parentfpinitblock: tnode;
  597. procstarttai,
  598. procendtai : tai;
  599. skpara: pointer;
  600. forwarddef,
  601. interfacedef : boolean;
  602. hasforward : boolean;
  603. end;
  604. pimplprocdefinfo = ^timplprocdefinfo;
  605. { tprocdef }
  606. tprocdef = class(tabstractprocdef)
  607. protected
  608. {$ifdef symansistr}
  609. _mangledname : ansistring;
  610. {$else symansistr}
  611. _mangledname : pshortstring;
  612. {$endif}
  613. { information that is only required until the implementation of the
  614. procdef has been handled }
  615. implprocdefinfo : pimplprocdefinfo;
  616. function GetResultName: PShortString;
  617. procedure SetResultName(AValue: PShortString);
  618. function GetParentFPStruct: tsym;
  619. procedure SetParentFPStruct(AValue: tsym);
  620. function GetParentFPStructPtrType: tdef;
  621. procedure SetParentFPStructPtrType(AValue: tdef);
  622. function GetParentFPInitBlock: tnode;
  623. procedure SetParentFPInitBlock(AValue: tnode);
  624. function Getprocstarttai: tai;
  625. procedure Setprocstarttai(AValue: tai);
  626. function Getprocendtai: tai;
  627. procedure Setprocendtai(AValue: tai);
  628. function Getskpara: pointer;
  629. procedure Setskpara(AValue: pointer);
  630. function Getforwarddef: boolean;
  631. procedure Setforwarddef(AValue: boolean);
  632. function Getinterfacedef: boolean;
  633. procedure Setinterfacedef(AValue: boolean);
  634. function Gethasforward: boolean;
  635. procedure Sethasforward(AValue: boolean);
  636. function GetIsEmpty: boolean;
  637. procedure SetIsEmpty(AValue: boolean);
  638. function GetHasInliningInfo: boolean;
  639. procedure SetHasInliningInfo(AValue: boolean);
  640. public
  641. messageinf : tmessageinf;
  642. dispid : longint;
  643. {$ifndef EXTDEBUG}
  644. { where is this function defined and what were the symbol
  645. flags, needed here because there
  646. is only one symbol for all overloaded functions
  647. EXTDEBUG has fileinfo in tdef (PFV) }
  648. fileinfo : tfileposinfo;
  649. {$endif}
  650. symoptions : tsymoptions;
  651. deprecatedmsg : pshortstring;
  652. { symbol owning this definition }
  653. procsym : tsym;
  654. procsymderef : tderef;
  655. { alias names }
  656. aliasnames : TCmdStrList;
  657. { symtables }
  658. localst : TSymtable;
  659. funcretsym : tsym;
  660. funcretsymderef : tderef;
  661. struct : tabstractrecorddef;
  662. structderef : tderef;
  663. implprocoptions: timplprocoptions;
  664. { import info }
  665. import_dll,
  666. import_name : pshortstring;
  667. { info for inlining the subroutine, if this pointer is nil,
  668. the procedure can't be inlined }
  669. inlininginfo : pinlininginfo;
  670. {$ifdef oldregvars}
  671. regvarinfo: pregvarinfo;
  672. {$endif oldregvars}
  673. import_nr : word;
  674. extnumber : word;
  675. visibility : tvisibility;
  676. { set to a value different from tsk_none in case this procdef is for
  677. a routine that has to be internally generated by the compiler }
  678. synthetickind : tsynthetickind;
  679. constructor create(level:byte);virtual;
  680. constructor ppuload(ppufile:tcompilerppufile);
  681. destructor destroy;override;
  682. { do not override this routine in platform-specific subclasses,
  683. override ppuwrite_platform instead }
  684. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  685. procedure buildderef;override;
  686. procedure buildderefimpl;override;
  687. procedure deref;override;
  688. procedure derefimpl;override;
  689. function GetSymtable(t:tGetSymtable):TSymtable;override;
  690. { warnings:
  691. * the symtablestack top has to be the symtable to which the copy
  692. should be added
  693. * getcopy does not create a finished/ready-to-use procdef; it
  694. needs to be finalised afterwards by calling
  695. symcreat.finish_copied_procdef() afterwards
  696. }
  697. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; override;
  698. function getcopy: tstoreddef; override;
  699. function GetTypeName : string;override;
  700. function mangledname : TSymStr; virtual;
  701. procedure setmangledname(const s : TSymStr);
  702. function fullprocname(showhidden:boolean):string;
  703. function customprocname(pno: tprocnameoptions):ansistring;
  704. function defaultmangledname: TSymStr;
  705. function cplusplusmangledname : TSymStr;
  706. function objcmangledname : TSymStr;
  707. function is_methodpointer:boolean;override;
  708. function is_addressonly:boolean;override;
  709. procedure make_external;
  710. { aliases to fields only required when a function is implemented in
  711. the current unit }
  712. property resultname: PShortString read GetResultName write SetResultName;
  713. { temporary reference to structure containing copies of all local
  714. variables and parameters accessed by nested routines; reference to
  715. this structure is passed as "parent frame pointer" on targets that
  716. lack this concept (at least JVM and LLVM); no need to save to/
  717. restore from ppu, since nested routines are always in the same
  718. unit }
  719. property parentfpstruct: tsym read GetParentFPStruct write SetParentFPStruct;
  720. { pointer to parentfpstruct's type (not yet valid during parsing, so
  721. cannot be used for $parentfp parameter) (no need to save to ppu) }
  722. property parentfpstructptrtype: tdef read GetParentFPStructPtrType write SetParentFPStructPtrType;
  723. { code to copy the parameters accessed from nested routines into the
  724. parentfpstruct (no need to save to ppu) }
  725. property parentfpinitblock: tnode read GetParentFPInitBlock write SetParentFPInitBlock;
  726. { First/last assembler symbol/instruction in aasmoutput list.
  727. Note: initialised after compiling the code for the procdef, but
  728. not saved to/restored from ppu. Used when inserting debug info }
  729. property procstarttai: tai read Getprocstarttai write Setprocstarttai;
  730. property procendtai: tai read Getprocendtai write Setprocendtai;
  731. { optional parameter for the synthetic routine generation logic }
  732. property skpara: pointer read Getskpara write Setskpara;
  733. { true, if the procedure is only declared
  734. (forward procedure) }
  735. property forwarddef: boolean read Getforwarddef write Setforwarddef;
  736. { true if the procedure is declared in the interface }
  737. property interfacedef: boolean read Getinterfacedef write Setinterfacedef;
  738. { true if the procedure has a forward declaration }
  739. property hasforward: boolean read Gethasforward write Sethasforward;
  740. { true if the routine's body is empty }
  741. property isempty: boolean read GetIsEmpty write SetIsEmpty;
  742. { true if all information required to inline this routine is available }
  743. property has_inlininginfo: boolean read GetHasInliningInfo write SetHasInliningInfo;
  744. end;
  745. tprocdefclass = class of tprocdef;
  746. { single linked list of overloaded procs }
  747. pprocdeflist = ^tprocdeflist;
  748. tprocdeflist = record
  749. def : tprocdef;
  750. defderef : tderef;
  751. next : pprocdeflist;
  752. end;
  753. tstringdef = class(tstoreddef)
  754. encoding : tstringencoding;
  755. stringtype : tstringtype;
  756. len : asizeint;
  757. constructor createshort(l : byte);virtual;
  758. constructor loadshort(ppufile:tcompilerppufile);
  759. constructor createlong(l : asizeint);virtual;
  760. constructor loadlong(ppufile:tcompilerppufile);
  761. constructor createansi(aencoding:tstringencoding);virtual;
  762. constructor loadansi(ppufile:tcompilerppufile);
  763. constructor createwide;virtual;
  764. constructor loadwide(ppufile:tcompilerppufile);
  765. constructor createunicode;virtual;
  766. constructor loadunicode(ppufile:tcompilerppufile);virtual;
  767. function getcopy : tstoreddef;override;
  768. function stringtypname:string;
  769. { do not override this routine in platform-specific subclasses,
  770. override ppuwrite_platform instead }
  771. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  772. function GetTypeName:string;override;
  773. function getmangledparaname:TSymStr;override;
  774. function is_publishable : boolean;override;
  775. function size:asizeint;override;
  776. function alignment : shortint;override;
  777. function needs_inittable : boolean;override;
  778. function getvardef:longint;override;
  779. end;
  780. tstringdefclass = class of tstringdef;
  781. { tenumdef }
  782. tenumdef = class(tstoreddef)
  783. minval,
  784. maxval : asizeint;
  785. basedef : tenumdef;
  786. basedefderef : tderef;
  787. symtable : TSymtable;
  788. has_jumps : boolean;
  789. constructor create;virtual;
  790. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);virtual;
  791. constructor ppuload(ppufile:tcompilerppufile);
  792. destructor destroy;override;
  793. function getcopy : tstoreddef;override;
  794. { do not override this routine in platform-specific subclasses,
  795. override ppuwrite_platform instead }
  796. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  797. procedure buildderef;override;
  798. procedure deref;override;
  799. function GetTypeName:string;override;
  800. function is_publishable : boolean;override;
  801. procedure calcsavesize;
  802. function packedbitsize: asizeint; override;
  803. procedure setmax(_max:asizeint);
  804. procedure setmin(_min:asizeint);
  805. function min:asizeint;
  806. function max:asizeint;
  807. function getfirstsym:tsym;
  808. function int2enumsym(l: asizeint): tsym;
  809. { returns basedef if assigned, otherwise self }
  810. function getbasedef: tenumdef;
  811. end;
  812. tenumdefclass = class of tenumdef;
  813. tsetdef = class(tstoreddef)
  814. elementdef : tdef;
  815. elementdefderef : tderef;
  816. setbase,
  817. setmax : aword;
  818. constructor create(def:tdef;low, high : asizeint);virtual;
  819. constructor ppuload(ppufile:tcompilerppufile);
  820. function getcopy : tstoreddef;override;
  821. { do not override this routine in platform-specific subclasses,
  822. override ppuwrite_platform instead }
  823. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  824. procedure buildderef;override;
  825. procedure deref;override;
  826. function GetTypeName:string;override;
  827. function is_publishable : boolean;override;
  828. end;
  829. tsetdefclass = class of tsetdef;
  830. tgenericdummyentry = class
  831. dummysym : tsym;
  832. resolvedsym : tsym;
  833. end;
  834. tdefawaresymtablestack = class(TSymtablestack)
  835. private
  836. procedure add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  837. procedure remove_helpers_and_generics(st:tsymtable);inline;
  838. procedure remove_helpers(st:tsymtable);
  839. procedure remove_generics(st:tsymtable);
  840. procedure pushcommon(st:tsymtable);inline;
  841. public
  842. procedure push(st: TSymtable); override;
  843. procedure pushafter(st,afterst:TSymtable); override;
  844. procedure pop(st: TSymtable); override;
  845. end;
  846. var
  847. current_structdef: tabstractrecorddef; { used for private functions check !! }
  848. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  849. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  850. cfiledef: tfiledefclass;
  851. cvariantdef: tvariantdefclass;
  852. cformaldef: tformaldefclass;
  853. cforwarddef: tforwarddefclass;
  854. cundefineddef: tundefineddefclass;
  855. cerrordef: terrordefclass;
  856. cpointerdef: tpointerdefclass;
  857. crecorddef: trecorddefclass;
  858. cimplementedinterface: timplementedinterfaceclass;
  859. cobjectdef: tobjectdefclass;
  860. cclassrefdef: tclassrefdefclass;
  861. carraydef: tarraydefclass;
  862. corddef: torddefclass;
  863. cfloatdef: tfloatdefclass;
  864. cprocvardef: tprocvardefclass;
  865. cprocdef: tprocdefclass;
  866. cstringdef: tstringdefclass;
  867. cenumdef: tenumdefclass;
  868. csetdef: tsetdefclass;
  869. { default types }
  870. generrordef, { error in definition }
  871. voidpointertype, { pointer for Void-pointeddef }
  872. charpointertype, { pointer for Char-pointeddef }
  873. widecharpointertype, { pointer for WideChar-pointeddef }
  874. voidcodepointertype, { pointer to code; corresponds to System.CodePointer }
  875. voidstackpointertype, { the pointer type used for accessing parameters and local vars on the stack }
  876. parentfpvoidpointertype, { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  877. {$ifdef x86}
  878. voidnearpointertype,
  879. voidnearcspointertype,
  880. voidneardspointertype,
  881. voidnearsspointertype,
  882. voidnearespointertype,
  883. voidnearfspointertype,
  884. voidneargspointertype,
  885. {$ifdef i8086}
  886. voidfarpointertype,
  887. voidhugepointertype,
  888. charnearpointertype,
  889. charfarpointertype,
  890. charhugepointertype,
  891. bytefarpointertype, { used for Mem[] }
  892. wordfarpointertype, { used for MemW[] }
  893. longintfarpointertype, { used for MemL[] }
  894. {$endif i8086}
  895. {$endif x86}
  896. cundefinedtype,
  897. cformaltype, { unique formal definition }
  898. ctypedformaltype, { unique typed formal definition }
  899. voidtype, { Void (procedure) }
  900. cansichartype, { Char }
  901. cwidechartype, { WideChar }
  902. pasbool8type, { boolean type }
  903. pasbool16type,
  904. pasbool32type,
  905. pasbool64type,
  906. bool8type,
  907. bool16type,
  908. bool32type,
  909. bool64type, { implement me }
  910. u8inttype, { 8-Bit unsigned integer }
  911. s8inttype, { 8-Bit signed integer }
  912. u16inttype, { 16-Bit unsigned integer }
  913. s16inttype, { 16-Bit signed integer }
  914. u32inttype, { 32-Bit unsigned integer }
  915. s32inttype, { 32-Bit signed integer }
  916. u64inttype, { 64-bit unsigned integer }
  917. s64inttype, { 64-bit signed integer }
  918. s32floattype, { 32 bit floating point number }
  919. s64floattype, { 64 bit floating point number }
  920. s80floattype, { 80 bit floating point number }
  921. sc80floattype, { 80 bit floating point number but stored like in C }
  922. s64currencytype, { pointer to a currency type }
  923. cshortstringtype, { pointer to type of short string const }
  924. clongstringtype, { pointer to type of long string const }
  925. cansistringtype, { pointer to type of ansi string const }
  926. cwidestringtype, { pointer to type of wide string const }
  927. cunicodestringtype,
  928. openshortstringtype, { pointer to type of an open shortstring,
  929. needed for readln() }
  930. openchararraytype, { pointer to type of an open array of char,
  931. needed for readln() }
  932. cfiletype, { get the same definition for all file }
  933. { used for stabs }
  934. methodpointertype, { typecasting of methodpointers to extract self }
  935. nestedprocpointertype, { typecasting of nestedprocpointers to extract parentfp }
  936. hresultdef,
  937. { we use only one variant def for every variant class }
  938. cvarianttype,
  939. colevarianttype,
  940. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  941. sinttype,
  942. uinttype,
  943. { integer types corresponding to OS_SINT/OS_INT }
  944. ossinttype,
  945. osuinttype,
  946. { unsigned and signed ord type with the same size as a pointer }
  947. ptruinttype,
  948. ptrsinttype,
  949. { several types to simulate more or less C++ objects for GDB }
  950. vmttype,
  951. vmtarraytype,
  952. pvmttype : tdef; { type of classrefs, used for stabs }
  953. { pointer to the anchestor of all classes }
  954. class_tobject : tobjectdef;
  955. { pointer to the ancestor of all COM interfaces }
  956. interface_iunknown : tobjectdef;
  957. { pointer to the ancestor of all dispinterfaces }
  958. interface_idispatch : tobjectdef;
  959. { pointer to the TGUID type
  960. of all interfaces }
  961. rec_tguid : trecorddef;
  962. { pointer to jump buffer }
  963. rec_jmp_buf : trecorddef;
  964. { Objective-C base types }
  965. objc_metaclasstype,
  966. objc_superclasstype,
  967. objc_idtype,
  968. objc_seltype : tpointerdef;
  969. objc_objecttype : trecorddef;
  970. { base type of @protocol(protocolname) Objective-C statements }
  971. objc_protocoltype : tobjectdef;
  972. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  973. objc_fastenumeration : tobjectdef;
  974. objc_fastenumerationstate : trecorddef;
  975. { Java base types }
  976. { java.lang.Object }
  977. java_jlobject : tobjectdef;
  978. { java.lang.Throwable }
  979. java_jlthrowable : tobjectdef;
  980. { FPC base type for records }
  981. java_fpcbaserecordtype : tobjectdef;
  982. { java.lang.String }
  983. java_jlstring : tobjectdef;
  984. { java.lang.Enum }
  985. java_jlenum : tobjectdef;
  986. { java.util.EnumSet }
  987. java_juenumset : tobjectdef;
  988. { java.util.BitSet }
  989. java_jubitset : tobjectdef;
  990. { FPC java implementation of ansistrings }
  991. java_ansistring : tobjectdef;
  992. { FPC java implementation of shortstrings }
  993. java_shortstring : tobjectdef;
  994. { FPC java procvar base class }
  995. java_procvarbase : tobjectdef;
  996. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  997. function make_dllmangledname(const dllname,importname:TSymStr;
  998. import_nr : word; pco : tproccalloption):TSymStr;
  999. { should be in the types unit, but the types unit uses the node stuff :( }
  1000. function is_interfacecom(def: tdef): boolean;
  1001. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  1002. function is_any_interface_kind(def: tdef): boolean;
  1003. function is_interfacecorba(def: tdef): boolean;
  1004. function is_interface(def: tdef): boolean;
  1005. function is_dispinterface(def: tdef): boolean;
  1006. function is_object(def: tdef): boolean;
  1007. function is_class(def: tdef): boolean;
  1008. function is_cppclass(def: tdef): boolean;
  1009. function is_objectpascal_helper(def: tdef): boolean;
  1010. function is_objcclass(def: tdef): boolean;
  1011. function is_objcclassref(def: tdef): boolean;
  1012. function is_objcprotocol(def: tdef): boolean;
  1013. function is_objccategory(def: tdef): boolean;
  1014. function is_objc_class_or_protocol(def: tdef): boolean;
  1015. function is_objc_protocol_or_category(def: tdef): boolean;
  1016. function is_classhelper(def: tdef): boolean;
  1017. function is_class_or_interface(def: tdef): boolean;
  1018. function is_class_or_interface_or_objc(def: tdef): boolean;
  1019. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  1020. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  1021. function is_class_or_interface_or_object(def: tdef): boolean;
  1022. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  1023. function is_implicit_pointer_object_type(def: tdef): boolean;
  1024. { returns true, if def is a type which is an implicit pointer to an array (dyn. array or dyn. string) }
  1025. function is_implicit_array_pointer(def: tdef): boolean;
  1026. function is_class_or_object(def: tdef): boolean;
  1027. function is_record(def: tdef): boolean;
  1028. function is_javaclass(def: tdef): boolean;
  1029. function is_javaclassref(def: tdef): boolean;
  1030. function is_javainterface(def: tdef): boolean;
  1031. function is_java_class_or_interface(def: tdef): boolean;
  1032. procedure loadobjctypes;
  1033. procedure maybeloadcocoatypes;
  1034. function use_vectorfpu(def : tdef) : boolean;
  1035. { returns a pointerdef for def, reusing an existing one in case it exists
  1036. in the current module }
  1037. function getpointerdef(def: tdef): tpointerdef;
  1038. { returns an arraydef for an array containing a single array of def, resuing
  1039. an existing one in case it exists in the current module }
  1040. function getsingletonarraydef(def: tdef): tarraydef;
  1041. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  1042. function getansistringcodepage:tstringencoding; inline;
  1043. function getansistringdef:tstringdef;
  1044. function getparaencoding(def:tdef):tstringencoding; inline;
  1045. implementation
  1046. uses
  1047. SysUtils,
  1048. cutils,
  1049. { global }
  1050. verbose,
  1051. { target }
  1052. systems,paramgr,
  1053. { symtable }
  1054. symsym,symtable,defutil,objcdef,
  1055. { parser }
  1056. pgenutil,
  1057. { module }
  1058. fmodule,
  1059. { other }
  1060. gendef,
  1061. fpccrc
  1062. ;
  1063. {****************************************************************************
  1064. Helpers
  1065. ****************************************************************************}
  1066. function getansistringcodepage:tstringencoding; inline;
  1067. begin
  1068. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1069. result:=current_settings.sourcecodepage
  1070. else
  1071. result:=0;
  1072. end;
  1073. function getansistringdef:tstringdef;
  1074. var
  1075. symtable:tsymtable;
  1076. begin
  1077. { if a codepage is explicitly defined in this mudule we need to return
  1078. a replacement for ansistring def }
  1079. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1080. begin
  1081. if not assigned(current_module) then
  1082. internalerror(2011101301);
  1083. { codepage can be redeclared only once per unit so we don't need a list of
  1084. redefined ansistring but only one pointer }
  1085. if not assigned(current_module.ansistrdef) then
  1086. begin
  1087. { if we did not create it yet we need to do this now }
  1088. if current_module.in_interface then
  1089. symtable:=current_module.globalsymtable
  1090. else
  1091. symtable:=current_module.localsymtable;
  1092. symtablestack.push(symtable);
  1093. current_module.ansistrdef:=cstringdef.createansi(current_settings.sourcecodepage);
  1094. symtablestack.pop(symtable);
  1095. end;
  1096. result:=tstringdef(current_module.ansistrdef);
  1097. end
  1098. else
  1099. result:=tstringdef(cansistringtype);
  1100. end;
  1101. function getparaencoding(def:tdef):tstringencoding; inline;
  1102. begin
  1103. { don't pass CP_NONE encoding to internal functions
  1104. they expect 0 encoding instead
  1105. exception: result of string concatenation, because if you pass the
  1106. result of a string concatenation to a rawbytestring, the result of
  1107. that concatenation shouldn't be converted to defaultsystemcodepage
  1108. if all strings have the same type }
  1109. result:=tstringdef(def).encoding;
  1110. if result=globals.CP_NONE then
  1111. result:=0
  1112. end;
  1113. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1114. var
  1115. s,hs,
  1116. prefix : TSymStr;
  1117. oldlen,
  1118. newlen,
  1119. i : longint;
  1120. crc : dword;
  1121. hp : tparavarsym;
  1122. begin
  1123. prefix:='';
  1124. hp:=nil;
  1125. if not assigned(st) then
  1126. internalerror(200204212);
  1127. { sub procedures }
  1128. while (st.symtabletype=localsymtable) do
  1129. begin
  1130. if st.defowner.typ<>procdef then
  1131. internalerror(200204173);
  1132. { Add the full mangledname of procedure to prevent
  1133. conflicts with 2 overloads having both a nested procedure
  1134. with the same name, see tb0314 (PFV) }
  1135. s:=tprocdef(st.defowner).procsym.name;
  1136. oldlen:=length(s);
  1137. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  1138. begin
  1139. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  1140. if not(vo_is_hidden_para in hp.varoptions) then
  1141. s:=s+'$'+hp.vardef.mangledparaname;
  1142. end;
  1143. if not is_void(tprocdef(st.defowner).returndef) then
  1144. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  1145. newlen:=length(s);
  1146. { Replace with CRC if the parameter line is very long }
  1147. if (newlen-oldlen>12) and
  1148. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  1149. begin
  1150. crc:=0;
  1151. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  1152. begin
  1153. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  1154. if not(vo_is_hidden_para in hp.varoptions) then
  1155. begin
  1156. hs:=hp.vardef.mangledparaname;
  1157. crc:=UpdateCrc32(crc,hs[1],length(hs));
  1158. end;
  1159. end;
  1160. if not is_void(tprocdef(st.defowner).returndef) then
  1161. begin
  1162. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  1163. hs:='$$'+tprocdef(st.defowner).returndef.mangledparaname;
  1164. crc:=UpdateCrc32(crc,hs[1],length(hs));
  1165. end;
  1166. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  1167. end;
  1168. if prefix<>'' then
  1169. prefix:=s+'_'+prefix
  1170. else
  1171. prefix:=s;
  1172. if length(prefix)>100 then
  1173. begin
  1174. crc:=0;
  1175. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  1176. prefix:='$CRC'+hexstr(crc,8);
  1177. end;
  1178. st:=st.defowner.owner;
  1179. end;
  1180. { object/classes symtable, nested type definitions in classes require the while loop }
  1181. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  1182. begin
  1183. if not (st.defowner.typ in [objectdef,recorddef]) then
  1184. internalerror(200204174);
  1185. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  1186. st:=st.defowner.owner;
  1187. end;
  1188. { symtable must now be static or global }
  1189. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  1190. internalerror(200204175);
  1191. { The mangled name is made out of at most 4 parts:
  1192. 1) Optional typeprefix given as first parameter
  1193. with '_$' appended if not empty
  1194. 2) Unit name or 'P$'+program name (never empty)
  1195. 3) optional prefix variable that contains a unique
  1196. name for the local symbol table (prepended with '$_$'
  1197. if not empty)
  1198. 4) suffix as given as third parameter,
  1199. also optional (i.e. can be empty)
  1200. prepended by '_$$_' if not empty }
  1201. result:='';
  1202. if typeprefix<>'' then
  1203. result:=result+typeprefix+'_$';
  1204. { Add P$ for program, which can have the same name as
  1205. a unit }
  1206. if (TSymtable(main_module.localsymtable)=st) and
  1207. (not main_module.is_unit) then
  1208. result:=result+'P$'+st.name^
  1209. else
  1210. result:=result+st.name^;
  1211. if prefix<>'' then
  1212. result:=result+'$_$'+prefix;
  1213. if suffix<>'' then
  1214. result:=result+'_$$_'+suffix;
  1215. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1216. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1217. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1218. { those in the debug map, leading to troubles with dsymutil). So always }
  1219. { add an underscore on darwin. }
  1220. if (target_info.system in systems_darwin) then
  1221. result := '_' + result;
  1222. end;
  1223. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1224. var
  1225. crc : cardinal;
  1226. i : longint;
  1227. use_crc : boolean;
  1228. dllprefix : TSymStr;
  1229. begin
  1230. if (target_info.system in (systems_all_windows + systems_nativent +
  1231. [system_i386_emx, system_i386_os2]))
  1232. and (dllname <> '') then
  1233. begin
  1234. dllprefix:=lower(ExtractFileName(dllname));
  1235. { Remove .dll suffix if present }
  1236. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1237. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1238. use_crc:=false;
  1239. for i:=1 to length(dllprefix) do
  1240. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1241. begin
  1242. use_crc:=true;
  1243. break;
  1244. end;
  1245. if use_crc then
  1246. begin
  1247. crc:=0;
  1248. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1249. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1250. end
  1251. else
  1252. dllprefix:='_$dll$'+dllprefix+'$';
  1253. if importname<>'' then
  1254. result:=dllprefix+importname
  1255. else
  1256. result:=dllprefix+'_index_'+tostr(import_nr);
  1257. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1258. { This allows to import VC++ mangled names from DLLs. }
  1259. { Do not perform replacement, if external symbol is not imported from DLL. }
  1260. if (dllname<>'') then
  1261. begin
  1262. Replace(result,'?','__q$$');
  1263. {$ifdef arm}
  1264. { @ symbol is not allowed in ARM assembler only }
  1265. Replace(result,'@','__a$$');
  1266. {$endif arm}
  1267. end;
  1268. end
  1269. else
  1270. begin
  1271. if importname<>'' then
  1272. begin
  1273. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1274. result:=importname
  1275. else
  1276. result:=target_info.Cprefix+importname;
  1277. end
  1278. else
  1279. result:='_index_'+tostr(import_nr);
  1280. end;
  1281. end;
  1282. {****************************************************************************
  1283. TDEFAWARESYMTABLESTACK
  1284. (symtablestack descendant that does some special actions on
  1285. the pushed/popped symtables)
  1286. ****************************************************************************}
  1287. procedure tdefawaresymtablestack.add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1288. var
  1289. i: integer;
  1290. s: string;
  1291. list: TFPObjectList;
  1292. def: tdef;
  1293. sym,srsym : tsym;
  1294. srsymtable : tsymtable;
  1295. entry : tgenericdummyentry;
  1296. begin
  1297. { search the symtable from first to last; the helper to use will be the
  1298. last one in the list }
  1299. for i:=0 to st.symlist.count-1 do
  1300. begin
  1301. if not (st.symlist[i] is ttypesym) then
  1302. continue;
  1303. def:=ttypesym(st.SymList[i]).typedef;
  1304. sym:=tsym(st.symlist[i]);
  1305. if is_objectpascal_helper(def) then
  1306. begin
  1307. s:=generate_objectpascal_helper_key(tobjectdef(def).extendeddef);
  1308. Message1(sym_d_adding_helper_for,s);
  1309. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1310. if not assigned(list) then
  1311. begin
  1312. list:=TFPObjectList.Create(false);
  1313. current_module.extendeddefs.Add(s,list);
  1314. end;
  1315. list.Add(def);
  1316. end
  1317. else
  1318. begin
  1319. if addgenerics and
  1320. (sp_generic_dummy in sym.symoptions)
  1321. then
  1322. begin
  1323. { did we already search for a generic with that name? }
  1324. list:=tfpobjectlist(current_module.genericdummysyms.find(sym.name));
  1325. if not assigned(list) then
  1326. begin
  1327. list:=tfpobjectlist.create(true);
  1328. current_module.genericdummysyms.add(sym.name,list);
  1329. end;
  1330. { is the dummy sym still "dummy"? }
  1331. if (sym.typ=typesym) and
  1332. (
  1333. { dummy sym defined in mode Delphi }
  1334. (ttypesym(sym).typedef.typ=undefineddef) or
  1335. { dummy sym defined in non-Delphi mode }
  1336. (tstoreddef(ttypesym(sym).typedef).is_generic)
  1337. ) then
  1338. begin
  1339. { do we have a non-generic type of the same name
  1340. available? }
  1341. if not searchsym_with_flags(sym.name,srsym,srsymtable,[ssf_no_addsymref]) then
  1342. srsym:=nil;
  1343. end
  1344. else
  1345. { dummy symbol is already not so dummy anymore }
  1346. srsym:=nil;
  1347. if assigned(srsym) then
  1348. begin
  1349. entry:=tgenericdummyentry.create;
  1350. entry.resolvedsym:=srsym;
  1351. entry.dummysym:=sym;
  1352. list.add(entry);
  1353. end;
  1354. end;
  1355. { add nested helpers as well }
  1356. if (def.typ in [recorddef,objectdef]) and
  1357. (sto_has_helper in tabstractrecorddef(def).symtable.tableoptions) then
  1358. add_helpers_and_generics(tabstractrecorddef(def).symtable,false);
  1359. end;
  1360. end;
  1361. end;
  1362. procedure tdefawaresymtablestack.remove_helpers_and_generics(st:tsymtable);
  1363. begin
  1364. if sto_has_helper in st.tableoptions then
  1365. remove_helpers(st);
  1366. if sto_has_generic in st.tableoptions then
  1367. remove_generics(st);
  1368. end;
  1369. procedure tdefawaresymtablestack.remove_helpers(st:TSymtable);
  1370. var
  1371. i, j: integer;
  1372. tmpst: TSymtable;
  1373. list: TFPObjectList;
  1374. begin
  1375. for i:=current_module.extendeddefs.count-1 downto 0 do
  1376. begin
  1377. list:=TFPObjectList(current_module.extendeddefs[i]);
  1378. for j:=list.count-1 downto 0 do
  1379. begin
  1380. if not (list[j] is tobjectdef) then
  1381. Internalerror(2011031501);
  1382. tmpst:=tobjectdef(list[j]).owner;
  1383. repeat
  1384. if tmpst=st then
  1385. begin
  1386. list.delete(j);
  1387. break;
  1388. end
  1389. else
  1390. begin
  1391. if assigned(tmpst.defowner) then
  1392. tmpst:=tmpst.defowner.owner
  1393. else
  1394. tmpst:=nil;
  1395. end;
  1396. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1397. end;
  1398. if list.count=0 then
  1399. current_module.extendeddefs.delete(i);
  1400. end;
  1401. end;
  1402. procedure tdefawaresymtablestack.remove_generics(st:tsymtable);
  1403. var
  1404. i,j : longint;
  1405. entry : tgenericdummyentry;
  1406. list : tfpobjectlist;
  1407. begin
  1408. for i:=current_module.genericdummysyms.count-1 downto 0 do
  1409. begin
  1410. list:=tfpobjectlist(current_module.genericdummysyms[i]);
  1411. if not assigned(list) then
  1412. continue;
  1413. for j:=list.count-1 downto 0 do
  1414. begin
  1415. entry:=tgenericdummyentry(list[j]);
  1416. if entry.dummysym.owner=st then
  1417. list.delete(j);
  1418. end;
  1419. if list.count=0 then
  1420. current_module.genericdummysyms.delete(i);
  1421. end;
  1422. end;
  1423. procedure tdefawaresymtablestack.pushcommon(st:tsymtable);
  1424. begin
  1425. if (sto_has_generic in st.tableoptions) or
  1426. (
  1427. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1428. (sto_has_helper in st.tableoptions)
  1429. ) then
  1430. { nested helpers will be added as well }
  1431. add_helpers_and_generics(st,true);
  1432. end;
  1433. procedure tdefawaresymtablestack.push(st: TSymtable);
  1434. begin
  1435. pushcommon(st);
  1436. inherited push(st);
  1437. end;
  1438. procedure tdefawaresymtablestack.pushafter(st,afterst:TSymtable);
  1439. begin
  1440. pushcommon(st);
  1441. inherited pushafter(st,afterst);
  1442. end;
  1443. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1444. begin
  1445. inherited pop(st);
  1446. if (sto_has_generic in st.tableoptions) or
  1447. (
  1448. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1449. (sto_has_helper in st.tableoptions)
  1450. ) then
  1451. { nested helpers will be removed as well }
  1452. remove_helpers_and_generics(st);
  1453. end;
  1454. {****************************************************************************
  1455. TDEF (base class for definitions)
  1456. ****************************************************************************}
  1457. constructor tgenericconstraintdata.create;
  1458. begin
  1459. interfaces:=tfpobjectlist.create(false);
  1460. interfacesderef:=tfplist.create;
  1461. end;
  1462. destructor tgenericconstraintdata.destroy;
  1463. var
  1464. i : longint;
  1465. begin
  1466. for i:=0 to interfacesderef.count-1 do
  1467. dispose(pderef(interfacesderef[i]));
  1468. interfacesderef.free;
  1469. interfaces.free;
  1470. inherited destroy;
  1471. end;
  1472. procedure tgenericconstraintdata.ppuload(ppufile: tcompilerppufile);
  1473. var
  1474. cnt,i : longint;
  1475. intfderef : pderef;
  1476. begin
  1477. ppufile.getsmallset(flags);
  1478. cnt:=ppufile.getlongint;
  1479. for i:=0 to cnt-1 do
  1480. begin
  1481. new(intfderef);
  1482. ppufile.getderef(intfderef^);
  1483. interfacesderef.add(intfderef);
  1484. end;
  1485. end;
  1486. procedure tgenericconstraintdata.ppuwrite(ppufile: tcompilerppufile);
  1487. var
  1488. i : longint;
  1489. begin
  1490. ppufile.putsmallset(flags);
  1491. ppufile.putlongint(interfacesderef.count);
  1492. for i:=0 to interfacesderef.count-1 do
  1493. ppufile.putderef(pderef(interfacesderef[i])^);
  1494. end;
  1495. procedure tgenericconstraintdata.buildderef;
  1496. var
  1497. intfderef : pderef;
  1498. i : longint;
  1499. begin
  1500. for i:=0 to interfaces.count-1 do
  1501. begin
  1502. new(intfderef);
  1503. intfderef^.build(tobjectdef(interfaces[i]));
  1504. interfacesderef.add(intfderef);
  1505. end;
  1506. end;
  1507. procedure tgenericconstraintdata.deref;
  1508. var
  1509. i : longint;
  1510. begin
  1511. for i:=0 to interfacesderef.count-1 do
  1512. interfaces.add(pderef(interfacesderef[i])^.resolve);
  1513. end;
  1514. procedure tstoreddef.writeentry(ppufile: tcompilerppufile; ibnr: byte);
  1515. begin
  1516. ppuwrite_platform(ppufile);
  1517. ppufile.writeentry(ibnr);
  1518. end;
  1519. procedure tstoreddef.ppuwrite_platform(ppufile: tcompilerppufile);
  1520. begin
  1521. { by default: do nothing }
  1522. end;
  1523. procedure tstoreddef.ppuload_platform(ppufile: tcompilerppufile);
  1524. begin
  1525. { by default: do nothing }
  1526. end;
  1527. constructor tstoreddef.create(dt:tdeftyp);
  1528. var
  1529. insertstack : psymtablestackitem;
  1530. begin
  1531. inherited create(dt);
  1532. savesize := 0;
  1533. {$ifdef EXTDEBUG}
  1534. fileinfo := current_filepos;
  1535. {$endif}
  1536. generictokenbuf:=nil;
  1537. genericdef:=nil;
  1538. { Don't register forwarddefs, they are disposed at the
  1539. end of an type block }
  1540. if (dt=forwarddef) then
  1541. exit;
  1542. { Register in current_module }
  1543. if assigned(current_module) then
  1544. begin
  1545. current_module.deflist.Add(self);
  1546. DefId:=current_module.deflist.Count-1;
  1547. end;
  1548. { Register in symtable stack }
  1549. if assigned(symtablestack) then
  1550. begin
  1551. insertstack:=symtablestack.stack;
  1552. while assigned(insertstack) and
  1553. (insertstack^.symtable.symtabletype=withsymtable) do
  1554. insertstack:=insertstack^.next;
  1555. if not assigned(insertstack) then
  1556. internalerror(200602044);
  1557. insertstack^.symtable.insertdef(self);
  1558. end;
  1559. end;
  1560. destructor tstoreddef.destroy;
  1561. var
  1562. i : longint;
  1563. begin
  1564. { Direct calls are not allowed, use symtable.deletedef() }
  1565. if assigned(owner) then
  1566. internalerror(200612311);
  1567. if assigned(generictokenbuf) then
  1568. begin
  1569. generictokenbuf.free;
  1570. generictokenbuf:=nil;
  1571. end;
  1572. genericparas.free;
  1573. if assigned(genericparaderefs) then
  1574. for i:=0 to genericparaderefs.count-1 do
  1575. dispose(pderef(genericparaderefs[i]));
  1576. genericparaderefs.free;
  1577. genconstraintdata.free;
  1578. stringdispose(_fullownerhierarchyname);
  1579. inherited destroy;
  1580. end;
  1581. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1582. var
  1583. sizeleft,i,cnt : longint;
  1584. buf : array[0..255] of byte;
  1585. symderef : pderef;
  1586. begin
  1587. inherited create(dt);
  1588. DefId:=ppufile.getlongint;
  1589. current_module.deflist[DefId]:=self;
  1590. {$ifdef EXTDEBUG}
  1591. fillchar(fileinfo,sizeof(fileinfo),0);
  1592. {$endif}
  1593. { load }
  1594. ppufile.getderef(typesymderef);
  1595. ppufile.getsmallset(defoptions);
  1596. ppufile.getsmallset(defstates);
  1597. if df_genconstraint in defoptions then
  1598. begin
  1599. genconstraintdata:=tgenericconstraintdata.create;
  1600. genconstraintdata.ppuload(ppufile);
  1601. end;
  1602. if [df_generic,df_specialization]*defoptions<>[] then
  1603. begin
  1604. cnt:=ppufile.getlongint;
  1605. if cnt>0 then
  1606. begin
  1607. genericparas:=tfphashobjectlist.create(false);
  1608. genericparaderefs:=tfplist.create;
  1609. for i:=0 to cnt-1 do
  1610. begin
  1611. genericparas.add(ppufile.getstring,nil);
  1612. New(symderef);
  1613. ppufile.getderef(symderef^);
  1614. genericparaderefs.add(symderef);
  1615. end;
  1616. end;
  1617. end;
  1618. if df_generic in defoptions then
  1619. begin
  1620. sizeleft:=ppufile.getlongint;
  1621. initgeneric;
  1622. while sizeleft>0 do
  1623. begin
  1624. if sizeleft>sizeof(buf) then
  1625. i:=sizeof(buf)
  1626. else
  1627. i:=sizeleft;
  1628. ppufile.getdata(buf,i);
  1629. generictokenbuf.write(buf,i);
  1630. dec(sizeleft,i);
  1631. end;
  1632. end;
  1633. if df_specialization in defoptions then
  1634. ppufile.getderef(genericdefderef);
  1635. end;
  1636. function tstoreddef.needs_separate_initrtti:boolean;
  1637. begin
  1638. result:=false;
  1639. end;
  1640. function tstoreddef.rtti_mangledname(rt : trttitype) : string;
  1641. var
  1642. prefix : string[4];
  1643. begin
  1644. if (rt=fullrtti) or (not needs_separate_initrtti) then
  1645. begin
  1646. prefix:='RTTI';
  1647. include(defstates,ds_rtti_table_used);
  1648. end
  1649. else
  1650. begin
  1651. prefix:='INIT';
  1652. include(defstates,ds_init_table_used);
  1653. end;
  1654. if assigned(typesym) and
  1655. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1656. result:=make_mangledname(prefix,typesym.owner,typesym.name)
  1657. else
  1658. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1659. end;
  1660. function tstoreddef.OwnerHierarchyName: string;
  1661. var
  1662. tmp: tdef;
  1663. begin
  1664. tmp:=self;
  1665. result:='';
  1666. repeat
  1667. { can be not assigned in case of a forwarddef }
  1668. if assigned(tmp.owner) and
  1669. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1670. tmp:=tdef(tmp.owner.defowner)
  1671. else
  1672. break;
  1673. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1674. until tmp=nil;
  1675. end;
  1676. function tstoreddef.fullownerhierarchyname: string;
  1677. var
  1678. tmp: tdef;
  1679. begin
  1680. if assigned(_fullownerhierarchyname) then
  1681. begin
  1682. result:=_fullownerhierarchyname^;
  1683. exit;
  1684. end;
  1685. { the def can only reside inside structured types or
  1686. procedures/functions/methods }
  1687. tmp:=self;
  1688. result:='';
  1689. repeat
  1690. { can be not assigned in case of a forwarddef }
  1691. if not assigned(tmp.owner) then
  1692. break
  1693. else
  1694. tmp:=tdef(tmp.owner.defowner);
  1695. if not assigned(tmp) then
  1696. break;
  1697. if tmp.typ in [recorddef,objectdef] then
  1698. result:=tabstractrecorddef(tmp).objrealname^+'.'+result
  1699. else
  1700. if tmp.typ=procdef then
  1701. result:=tprocdef(tmp).customprocname([pno_paranames,pno_proctypeoption])+'.'+result;
  1702. until tmp=nil;
  1703. _fullownerhierarchyname:=stringdup(result);
  1704. end;
  1705. function tstoreddef.in_currentunit: boolean;
  1706. var
  1707. st: tsymtable;
  1708. begin
  1709. st:=owner;
  1710. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1711. st:=st.defowner.owner;
  1712. result:=st.iscurrentunit;
  1713. end;
  1714. function tstoreddef.getcopy : tstoreddef;
  1715. begin
  1716. Message(sym_e_cant_create_unique_type);
  1717. getcopy:=cerrordef.create;
  1718. end;
  1719. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1720. var
  1721. sizeleft,i : longint;
  1722. buf : array[0..255] of byte;
  1723. oldintfcrc : boolean;
  1724. begin
  1725. ppufile.putlongint(DefId);
  1726. ppufile.putderef(typesymderef);
  1727. ppufile.putsmallset(defoptions);
  1728. oldintfcrc:=ppufile.do_crc;
  1729. ppufile.do_crc:=false;
  1730. ppufile.putsmallset(defstates);
  1731. if df_genconstraint in defoptions then
  1732. genconstraintdata.ppuwrite(ppufile);
  1733. if [df_generic,df_specialization]*defoptions<>[] then
  1734. begin
  1735. if not assigned(genericparas) then
  1736. ppufile.putlongint(0)
  1737. else
  1738. begin
  1739. if not assigned(genericparaderefs) then
  1740. internalerror(2014052305);
  1741. ppufile.putlongint(genericparas.count);
  1742. for i:=0 to genericparas.count-1 do
  1743. begin
  1744. ppufile.putstring(genericparas.nameofindex(i));
  1745. ppufile.putderef(pderef(genericparaderefs[i])^);
  1746. end;
  1747. end;
  1748. end;
  1749. if df_generic in defoptions then
  1750. begin
  1751. if assigned(generictokenbuf) then
  1752. begin
  1753. sizeleft:=generictokenbuf.size;
  1754. generictokenbuf.seek(0);
  1755. end
  1756. else
  1757. sizeleft:=0;
  1758. ppufile.putlongint(sizeleft);
  1759. while sizeleft>0 do
  1760. begin
  1761. if sizeleft>sizeof(buf) then
  1762. i:=sizeof(buf)
  1763. else
  1764. i:=sizeleft;
  1765. generictokenbuf.read(buf,i);
  1766. ppufile.putdata(buf,i);
  1767. dec(sizeleft,i);
  1768. end;
  1769. end;
  1770. ppufile.do_crc:=oldintfcrc;
  1771. if df_specialization in defoptions then
  1772. ppufile.putderef(genericdefderef);
  1773. end;
  1774. procedure tstoreddef.buildderef;
  1775. var
  1776. i : longint;
  1777. sym : tsym;
  1778. symderef : pderef;
  1779. begin
  1780. typesymderef.build(typesym);
  1781. genericdefderef.build(genericdef);
  1782. if assigned(genconstraintdata) then
  1783. genconstraintdata.buildderef;
  1784. if assigned(genericparas) then
  1785. begin
  1786. if not assigned(genericparaderefs) then
  1787. genericparaderefs:=tfplist.create;
  1788. for i:=0 to genericparas.count-1 do
  1789. begin
  1790. sym:=tsym(genericparas.items[i]);
  1791. new(symderef);
  1792. symderef^.build(sym);
  1793. genericparaderefs.add(symderef);
  1794. end;
  1795. end;
  1796. end;
  1797. procedure tstoreddef.buildderefimpl;
  1798. begin
  1799. end;
  1800. procedure tstoreddef.deref;
  1801. var
  1802. symderef : pderef;
  1803. i : longint;
  1804. begin
  1805. typesym:=ttypesym(typesymderef.resolve);
  1806. if df_specialization in defoptions then
  1807. genericdef:=tstoreddef(genericdefderef.resolve);
  1808. if assigned(genconstraintdata) then
  1809. genconstraintdata.deref;
  1810. if assigned(genericparas) then
  1811. begin
  1812. if not assigned(genericparaderefs) then
  1813. internalerror(2014052302);
  1814. if genericparas.count<>genericparaderefs.count then
  1815. internalerror(2014052303);
  1816. for i:=0 to genericparaderefs.count-1 do
  1817. begin
  1818. symderef:=pderef(genericparaderefs[i]);
  1819. genericparas.items[i]:=symderef^.resolve;
  1820. dispose(symderef);
  1821. end;
  1822. genericparaderefs.free;
  1823. genericparaderefs:=nil;
  1824. end;
  1825. end;
  1826. procedure tstoreddef.derefimpl;
  1827. begin
  1828. end;
  1829. function tstoreddef.size : asizeint;
  1830. begin
  1831. size:=savesize;
  1832. end;
  1833. function tstoreddef.getvardef:longint;
  1834. begin
  1835. result:=varUndefined;
  1836. end;
  1837. function tstoreddef.alignment : shortint;
  1838. begin
  1839. { natural alignment by default }
  1840. alignment:=size_2_align(savesize);
  1841. { can happen if savesize = 0, e.g. for voiddef or
  1842. an empty record
  1843. }
  1844. if (alignment=0) then
  1845. alignment:=1;
  1846. end;
  1847. { returns true, if the definition can be published }
  1848. function tstoreddef.is_publishable : boolean;
  1849. begin
  1850. is_publishable:=false;
  1851. end;
  1852. { needs an init table }
  1853. function tstoreddef.needs_inittable : boolean;
  1854. begin
  1855. needs_inittable:=false;
  1856. end;
  1857. function tstoreddef.is_intregable : boolean;
  1858. var
  1859. recsize,temp: longint;
  1860. begin
  1861. is_intregable:=false;
  1862. case typ of
  1863. orddef,
  1864. pointerdef,
  1865. enumdef,
  1866. classrefdef:
  1867. is_intregable:=true;
  1868. procvardef :
  1869. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  1870. objectdef:
  1871. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1872. setdef:
  1873. is_intregable:=is_smallset(self);
  1874. recorddef:
  1875. begin
  1876. recsize:=size;
  1877. is_intregable:=
  1878. ispowerof2(recsize,temp) and
  1879. { sizeof(asizeint)*2 records in int registers is currently broken for endian_big targets }
  1880. (((recsize <= sizeof(asizeint)*2) and (target_info.endian=endian_little)
  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) then
  3549. begin
  3550. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  3551. exit;
  3552. { search recursively }
  3553. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  3554. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  3555. exit;
  3556. end;
  3557. end;
  3558. result:=false;
  3559. end;
  3560. {***************************************************************************
  3561. trecorddef
  3562. ***************************************************************************}
  3563. constructor trecorddef.create(const n:string; p:TSymtable);
  3564. begin
  3565. inherited create(n,recorddef);
  3566. symtable:=p;
  3567. { we can own the symtable only if nobody else owns a copy so far }
  3568. if symtable.refcount=1 then
  3569. symtable.defowner:=self;
  3570. isunion:=false;
  3571. end;
  3572. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  3573. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  3574. var
  3575. i,j : longint;
  3576. begin
  3577. if ppufile.getbyte=1 then
  3578. begin
  3579. new(variantrecdesc);
  3580. ppufile.getderef(variantrecdesc^.variantselectorderef);
  3581. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  3582. for i:=0 to high(variantrecdesc^.branches) do
  3583. begin
  3584. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  3585. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3586. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  3587. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3588. end;
  3589. end
  3590. else
  3591. variantrecdesc:=nil;
  3592. end;
  3593. begin
  3594. inherited ppuload(recorddef,ppufile);
  3595. if df_copied_def in defoptions then
  3596. begin
  3597. ppufile.getderef(cloneddefderef);
  3598. ppuload_platform(ppufile);
  3599. end
  3600. else
  3601. begin
  3602. ppuload_platform(ppufile);
  3603. symtable:=trecordsymtable.create(objrealname^,0);
  3604. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  3605. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  3606. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  3607. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  3608. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  3609. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  3610. trecordsymtable(symtable).ppuload(ppufile);
  3611. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3612. but because iso mode supports no units, there is no need to store the variantrecdesc
  3613. in the ppu
  3614. }
  3615. // readvariantrecdesc(variantrecdesc);
  3616. { requires usefieldalignment to be set }
  3617. symtable.defowner:=self;
  3618. end;
  3619. isunion:=false;
  3620. end;
  3621. destructor trecorddef.destroy;
  3622. begin
  3623. if assigned(symtable) then
  3624. begin
  3625. symtable.free;
  3626. symtable:=nil;
  3627. end;
  3628. inherited destroy;
  3629. end;
  3630. function trecorddef.getcopy : tstoreddef;
  3631. begin
  3632. result:=crecorddef.create(objrealname^,symtable.getcopy);
  3633. trecorddef(result).isunion:=isunion;
  3634. include(trecorddef(result).defoptions,df_copied_def);
  3635. if assigned(tcinitcode) then
  3636. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  3637. if assigned(import_lib) then
  3638. trecorddef(result).import_lib:=stringdup(import_lib^);
  3639. end;
  3640. function trecorddef.needs_inittable : boolean;
  3641. begin
  3642. needs_inittable:=trecordsymtable(symtable).needs_init_final
  3643. end;
  3644. function trecorddef.needs_separate_initrtti : boolean;
  3645. begin
  3646. result:=true;
  3647. end;
  3648. procedure trecorddef.buildderef;
  3649. begin
  3650. inherited buildderef;
  3651. if df_copied_def in defoptions then
  3652. cloneddefderef.build(symtable.defowner)
  3653. else
  3654. tstoredsymtable(symtable).buildderef;
  3655. end;
  3656. procedure trecorddef.deref;
  3657. begin
  3658. inherited deref;
  3659. { now dereference the definitions }
  3660. if df_copied_def in defoptions then
  3661. begin
  3662. cloneddef:=trecorddef(cloneddefderef.resolve);
  3663. symtable:=cloneddef.symtable.getcopy;
  3664. end
  3665. else
  3666. tstoredsymtable(symtable).deref;
  3667. { assign TGUID? load only from system unit }
  3668. if not(assigned(rec_tguid)) and
  3669. (upper(typename)='TGUID') and
  3670. assigned(owner) and
  3671. assigned(owner.name) and
  3672. (owner.name^='SYSTEM') then
  3673. rec_tguid:=self;
  3674. { assign JMP_BUF? load only from system unit }
  3675. if not(assigned(rec_jmp_buf)) and
  3676. (upper(typename)='JMP_BUF') and
  3677. assigned(owner) and
  3678. assigned(owner.name) and
  3679. (owner.name^='SYSTEM') then
  3680. rec_jmp_buf:=self;
  3681. end;
  3682. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  3683. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  3684. var
  3685. i,j : longint;
  3686. begin
  3687. if assigned(variantrecdesc) then
  3688. begin
  3689. ppufile.putbyte(1);
  3690. ppufile.putderef(variantrecdesc^.variantselectorderef);
  3691. ppufile.putasizeint(length(variantrecdesc^.branches));
  3692. for i:=0 to high(variantrecdesc^.branches) do
  3693. begin
  3694. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  3695. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3696. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  3697. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3698. end;
  3699. end
  3700. else
  3701. ppufile.putbyte(0);
  3702. end;
  3703. begin
  3704. inherited ppuwrite(ppufile);
  3705. if df_copied_def in defoptions then
  3706. ppufile.putderef(cloneddefderef)
  3707. else
  3708. begin
  3709. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  3710. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  3711. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  3712. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  3713. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  3714. ppufile.putword(trecordsymtable(symtable).paddingsize);
  3715. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3716. but because iso mode supports no units, there is no need to store the variantrecdesc
  3717. in the ppu
  3718. }
  3719. // writevariantrecdesc(variantrecdesc);
  3720. end;
  3721. writeentry(ppufile,ibrecorddef);
  3722. if not(df_copied_def in defoptions) then
  3723. trecordsymtable(symtable).ppuwrite(ppufile);
  3724. end;
  3725. function trecorddef.size:asizeint;
  3726. begin
  3727. result:=trecordsymtable(symtable).datasize;
  3728. end;
  3729. function trecorddef.alignment:shortint;
  3730. begin
  3731. alignment:=trecordsymtable(symtable).recordalignment;
  3732. end;
  3733. function trecorddef.padalignment:shortint;
  3734. begin
  3735. padalignment := trecordsymtable(symtable).padalignment;
  3736. end;
  3737. function trecorddef.GetTypeName : string;
  3738. begin
  3739. GetTypeName:='<record type>'
  3740. end;
  3741. {***************************************************************************
  3742. TABSTRACTPROCDEF
  3743. ***************************************************************************}
  3744. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  3745. begin
  3746. inherited create(dt);
  3747. parast:=tparasymtable.create(self,level);
  3748. paras:=nil;
  3749. minparacount:=0;
  3750. maxparacount:=0;
  3751. proctypeoption:=potype_none;
  3752. proccalloption:=pocall_none;
  3753. procoptions:=[];
  3754. returndef:=voidtype;
  3755. savesize:=sizeof(pint);
  3756. callerargareasize:=0;
  3757. calleeargareasize:=0;
  3758. has_paraloc_info:=callnoside;
  3759. funcretloc[callerside].init;
  3760. funcretloc[calleeside].init;
  3761. check_mark_as_nested;
  3762. end;
  3763. destructor tabstractprocdef.destroy;
  3764. begin
  3765. if assigned(paras) then
  3766. begin
  3767. {$ifdef MEMDEBUG}
  3768. memprocpara.start;
  3769. {$endif MEMDEBUG}
  3770. paras.free;
  3771. paras:=nil;
  3772. {$ifdef MEMDEBUG}
  3773. memprocpara.stop;
  3774. {$endif MEMDEBUG}
  3775. end;
  3776. if assigned(parast) then
  3777. begin
  3778. {$ifdef MEMDEBUG}
  3779. memprocparast.start;
  3780. {$endif MEMDEBUG}
  3781. parast.free;
  3782. parast:=nil;
  3783. {$ifdef MEMDEBUG}
  3784. memprocparast.stop;
  3785. {$endif MEMDEBUG}
  3786. end;
  3787. funcretloc[callerside].done;
  3788. funcretloc[calleeside].done;
  3789. inherited destroy;
  3790. end;
  3791. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  3792. begin
  3793. if (tsym(p).typ<>paravarsym) then
  3794. exit;
  3795. inc(plongint(arg)^);
  3796. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  3797. begin
  3798. if not assigned(tparavarsym(p).defaultconstsym) then
  3799. inc(minparacount);
  3800. inc(maxparacount);
  3801. end;
  3802. end;
  3803. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  3804. begin
  3805. if (tsym(p).typ<>paravarsym) then
  3806. exit;
  3807. paras.add(p);
  3808. end;
  3809. procedure tabstractprocdef.calcparas;
  3810. var
  3811. paracount : longint;
  3812. begin
  3813. { This can already be assigned when
  3814. we need to reresolve this unit (PFV) }
  3815. if assigned(paras) then
  3816. paras.free;
  3817. paras:=tparalist.create(false);
  3818. paracount:=0;
  3819. minparacount:=0;
  3820. maxparacount:=0;
  3821. parast.SymList.ForEachCall(@count_para,@paracount);
  3822. paras.capacity:=paracount;
  3823. { Insert parameters in table }
  3824. parast.SymList.ForEachCall(@insert_para,nil);
  3825. { Order parameters }
  3826. paras.sortparas;
  3827. end;
  3828. procedure tabstractprocdef.buildderef;
  3829. begin
  3830. { released procdef? }
  3831. if not assigned(parast) then
  3832. exit;
  3833. inherited buildderef;
  3834. returndefderef.build(returndef);
  3835. if po_explicitparaloc in procoptions then
  3836. funcretloc[callerside].buildderef;
  3837. { parast }
  3838. tparasymtable(parast).buildderef;
  3839. end;
  3840. procedure tabstractprocdef.deref;
  3841. begin
  3842. inherited deref;
  3843. returndef:=tdef(returndefderef.resolve);
  3844. if po_explicitparaloc in procoptions then
  3845. begin
  3846. funcretloc[callerside].deref;
  3847. has_paraloc_info:=callerside;
  3848. end
  3849. else
  3850. begin
  3851. { deref is called after loading from a ppu, but also after another
  3852. unit has been reloaded/recompiled and all references must be
  3853. re-resolved. Since the funcretloc contains a reference to a tdef,
  3854. reset it so that we won't try to access the stale def }
  3855. funcretloc[callerside].init;
  3856. has_paraloc_info:=callnoside;
  3857. end;
  3858. { parast }
  3859. tparasymtable(parast).deref;
  3860. { recalculated parameters }
  3861. calcparas;
  3862. end;
  3863. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3864. begin
  3865. inherited ppuload(dt,ppufile);
  3866. parast:=nil;
  3867. Paras:=nil;
  3868. minparacount:=0;
  3869. maxparacount:=0;
  3870. ppufile.getderef(returndefderef);
  3871. proctypeoption:=tproctypeoption(ppufile.getbyte);
  3872. proccalloption:=tproccalloption(ppufile.getbyte);
  3873. ppufile.getnormalset(procoptions);
  3874. funcretloc[callerside].init;
  3875. if po_explicitparaloc in procoptions then
  3876. funcretloc[callerside].ppuload(ppufile);
  3877. savesize:=sizeof(pint);
  3878. if (po_explicitparaloc in procoptions) then
  3879. has_paraloc_info:=callerside;
  3880. end;
  3881. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  3882. var
  3883. oldintfcrc : boolean;
  3884. begin
  3885. { released procdef? }
  3886. if not assigned(parast) then
  3887. exit;
  3888. inherited ppuwrite(ppufile);
  3889. ppufile.putderef(returndefderef);
  3890. oldintfcrc:=ppufile.do_interface_crc;
  3891. ppufile.do_interface_crc:=false;
  3892. ppufile.putbyte(ord(proctypeoption));
  3893. ppufile.putbyte(ord(proccalloption));
  3894. ppufile.putnormalset(procoptions);
  3895. ppufile.do_interface_crc:=oldintfcrc;
  3896. if (po_explicitparaloc in procoptions) then
  3897. funcretloc[callerside].ppuwrite(ppufile);
  3898. end;
  3899. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  3900. var
  3901. hs,s : ansistring;
  3902. hp : TParavarsym;
  3903. hpc : tconstsym;
  3904. first : boolean;
  3905. i : integer;
  3906. begin
  3907. s:='';
  3908. first:=true;
  3909. for i:=0 to paras.count-1 do
  3910. begin
  3911. hp:=tparavarsym(paras[i]);
  3912. if not(vo_is_hidden_para in hp.varoptions) or
  3913. (pno_showhidden in pno) then
  3914. begin
  3915. if first then
  3916. begin
  3917. s:=s+'(';
  3918. first:=false;
  3919. end
  3920. else
  3921. s:=s+';';
  3922. if vo_is_hidden_para in hp.varoptions then
  3923. s:=s+'<';
  3924. case hp.varspez of
  3925. vs_var :
  3926. s:=s+'var ';
  3927. vs_const :
  3928. s:=s+'const ';
  3929. vs_out :
  3930. s:=s+'out ';
  3931. vs_constref :
  3932. s:=s+'constref ';
  3933. end;
  3934. if (pno_paranames in pno) then
  3935. s:=s+hp.realname+':';
  3936. if hp.univpara then
  3937. s:=s+'univ ';
  3938. if assigned(hp.vardef.typesym) then
  3939. begin
  3940. hs:=hp.vardef.typesym.realname;
  3941. if hs[1]<>'$' then
  3942. s:=s+hp.vardef.OwnerHierarchyName+hs
  3943. else
  3944. s:=s+hp.vardef.GetTypeName;
  3945. end
  3946. else
  3947. s:=s+hp.vardef.GetTypeName;
  3948. { default value }
  3949. if assigned(hp.defaultconstsym) then
  3950. begin
  3951. hpc:=tconstsym(hp.defaultconstsym);
  3952. hs:='';
  3953. case hpc.consttyp of
  3954. conststring,
  3955. constresourcestring :
  3956. begin
  3957. If hpc.value.len>0 then
  3958. begin
  3959. setLength(hs,hpc.value.len);
  3960. { don't write past the end of hs if the constant
  3961. is > 255 chars }
  3962. move(hpc.value.valueptr^,hs[1],length(hs));
  3963. { make sure that constant strings with newline chars
  3964. don't create a linebreak in the assembler code,
  3965. since comments are line-based. Also remove nulls
  3966. because the comments are written as a pchar. }
  3967. ReplaceCase(hs,#0,'.');
  3968. ReplaceCase(hs,#10,'.');
  3969. ReplaceCase(hs,#13,'.');
  3970. end;
  3971. end;
  3972. constreal :
  3973. str(pbestreal(hpc.value.valueptr)^,hs);
  3974. constpointer :
  3975. hs:=tostr(hpc.value.valueordptr);
  3976. constord :
  3977. begin
  3978. if is_boolean(hpc.constdef) then
  3979. begin
  3980. if hpc.value.valueord<>0 then
  3981. hs:='TRUE'
  3982. else
  3983. hs:='FALSE';
  3984. end
  3985. else
  3986. hs:=tostr(hpc.value.valueord);
  3987. end;
  3988. constnil :
  3989. hs:='nil';
  3990. constset :
  3991. hs:='<set>';
  3992. end;
  3993. if hs<>'' then
  3994. s:=s+'="'+hs+'"';
  3995. end;
  3996. if vo_is_hidden_para in hp.varoptions then
  3997. s:=s+'>';
  3998. end;
  3999. end;
  4000. if not first then
  4001. s:=s+')';
  4002. if (po_varargs in procoptions) then
  4003. s:=s+';VarArgs';
  4004. typename_paras:=s;
  4005. end;
  4006. function tabstractprocdef.is_methodpointer:boolean;
  4007. begin
  4008. result:=false;
  4009. end;
  4010. function tabstractprocdef.is_addressonly:boolean;
  4011. begin
  4012. result:=true;
  4013. end;
  4014. function tabstractprocdef.no_self_node: boolean;
  4015. begin
  4016. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  4017. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  4018. end;
  4019. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  4020. var
  4021. j, nestinglevel: longint;
  4022. pvs, npvs: tparavarsym;
  4023. begin
  4024. nestinglevel:=parast.symtablelevel;
  4025. if newtyp=procdef then
  4026. begin
  4027. if (copytyp<>pc_bareproc) then
  4028. result:=cprocdef.create(nestinglevel)
  4029. else
  4030. result:=cprocdef.create(normal_function_level);
  4031. tprocdef(result).visibility:=vis_public;
  4032. end
  4033. else
  4034. begin
  4035. result:=cprocvardef.create(nestinglevel);
  4036. end;
  4037. tabstractprocdef(result).returndef:=returndef;
  4038. tabstractprocdef(result).returndefderef:=returndefderef;
  4039. pvs:=nil;
  4040. npvs:=nil;
  4041. for j:=0 to parast.symlist.count-1 do
  4042. begin
  4043. case tsym(parast.symlist[j]).typ of
  4044. paravarsym:
  4045. begin
  4046. pvs:=tparavarsym(parast.symlist[j]);
  4047. { in case of bare proc, don't copy self, vmt or framepointer
  4048. parameters }
  4049. if (copytyp=pc_bareproc) and
  4050. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  4051. continue;
  4052. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  4053. pvs.vardef,pvs.varoptions);
  4054. npvs.defaultconstsym:=pvs.defaultconstsym;
  4055. tabstractprocdef(result).parast.insert(npvs);
  4056. end;
  4057. constsym:
  4058. begin
  4059. // ignore, reuse original constym. Should also be duplicated
  4060. // be safe though
  4061. end;
  4062. symconst.typesym:
  4063. begin
  4064. // reuse original, part of generic declaration
  4065. end
  4066. else
  4067. internalerror(201160604);
  4068. end;
  4069. end;
  4070. tabstractprocdef(result).savesize:=savesize;
  4071. tabstractprocdef(result).proctypeoption:=proctypeoption;
  4072. tabstractprocdef(result).proccalloption:=proccalloption;
  4073. tabstractprocdef(result).procoptions:=procoptions;
  4074. if (copytyp=pc_bareproc) then
  4075. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  4076. if newtyp=procvardef then
  4077. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  4078. tabstractprocdef(result).callerargareasize:=callerargareasize;
  4079. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  4080. tabstractprocdef(result).maxparacount:=maxparacount;
  4081. tabstractprocdef(result).minparacount:=minparacount;
  4082. if po_explicitparaloc in procoptions then
  4083. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  4084. { recalculate parameter info }
  4085. tabstractprocdef(result).has_paraloc_info:=callnoside;
  4086. {$ifdef m68k}
  4087. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  4088. {$endif}
  4089. if (typ=procdef) and
  4090. (newtyp=procvardef) and
  4091. (owner.symtabletype=ObjectSymtable) then
  4092. include(tprocvardef(result).procoptions,po_methodpointer);
  4093. end;
  4094. procedure tabstractprocdef.check_mark_as_nested;
  4095. begin
  4096. { nested procvars require that nested functions use the Delphi-style
  4097. nested procedure calling convention }
  4098. if (parast.symtablelevel>normal_function_level) and
  4099. (m_nested_procvars in current_settings.modeswitches) then
  4100. include(procoptions,po_delphi_nested_cc);
  4101. end;
  4102. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  4103. begin
  4104. if (side in [callerside,callbothsides]) and
  4105. not(has_paraloc_info in [callerside,callbothsides]) then
  4106. begin
  4107. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  4108. if has_paraloc_info in [calleeside,callbothsides] then
  4109. has_paraloc_info:=callbothsides
  4110. else
  4111. has_paraloc_info:=callerside;
  4112. end;
  4113. if (side in [calleeside,callbothsides]) and
  4114. not(has_paraloc_info in [calleeside,callbothsides]) then
  4115. begin
  4116. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  4117. if has_paraloc_info in [callerside,callbothsides] then
  4118. has_paraloc_info:=callbothsides
  4119. else
  4120. has_paraloc_info:=calleeside;
  4121. end;
  4122. end;
  4123. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  4124. var
  4125. p: tparavarsym;
  4126. ploc: PCGParalocation;
  4127. i: longint;
  4128. begin
  4129. result:=false;
  4130. init_paraloc_info(side);
  4131. for i:=0 to parast.SymList.Count-1 do
  4132. if tsym(parast.SymList[i]).typ=paravarsym then
  4133. begin
  4134. p:=tparavarsym(parast.SymList[i]);
  4135. { check if no parameter is located on the stack }
  4136. if is_open_array(p.vardef) or
  4137. is_array_of_const(p.vardef) then
  4138. begin
  4139. result:=true;
  4140. exit;
  4141. end;
  4142. ploc:=p.paraloc[side].location;
  4143. while assigned(ploc) do
  4144. begin
  4145. if (ploc^.loc=LOC_REFERENCE) then
  4146. begin
  4147. result:=true;
  4148. exit
  4149. end;
  4150. ploc:=ploc^.next;
  4151. end;
  4152. end;
  4153. end;
  4154. function tabstractprocdef.is_pushleftright: boolean;
  4155. begin
  4156. result:=false;
  4157. end;
  4158. function tabstractprocdef.address_type: tdef;
  4159. begin
  4160. result:=voidcodepointertype;
  4161. end;
  4162. procedure tabstractprocdef.declared_far;
  4163. begin
  4164. Message1(parser_w_proc_directive_ignored,'FAR');
  4165. end;
  4166. procedure tabstractprocdef.declared_near;
  4167. begin
  4168. Message1(parser_w_proc_directive_ignored,'NEAR');
  4169. end;
  4170. {***************************************************************************
  4171. TPROCDEF
  4172. ***************************************************************************}
  4173. function tprocdef.GetResultName: PShortString;
  4174. begin
  4175. if not assigned(implprocdefinfo) then
  4176. internalerror(2014010301);
  4177. result:=implprocdefinfo^.resultname;
  4178. end;
  4179. procedure tprocdef.SetResultName(AValue: PShortString);
  4180. begin
  4181. if not assigned(implprocdefinfo) then
  4182. internalerror(2014010302);
  4183. implprocdefinfo^.resultname:=AValue;
  4184. end;
  4185. function tprocdef.GetParentFPInitBlock: tnode;
  4186. begin
  4187. if not assigned(implprocdefinfo) then
  4188. internalerror(2014010303);
  4189. result:=implprocdefinfo^.parentfpinitblock;
  4190. end;
  4191. function tprocdef.GetParentFPStruct: tsym;
  4192. begin
  4193. if not assigned(implprocdefinfo) then
  4194. internalerror(2014010304);
  4195. result:=implprocdefinfo^.parentfpstruct;
  4196. end;
  4197. function tprocdef.GetParentFPStructPtrType: tdef;
  4198. begin
  4199. if not assigned(implprocdefinfo) then
  4200. internalerror(2014010305);
  4201. result:=implprocdefinfo^.parentfpstructptrtype;
  4202. end;
  4203. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  4204. begin
  4205. if not assigned(implprocdefinfo) then
  4206. internalerror(2014010306);
  4207. implprocdefinfo^.parentfpinitblock:=AValue;
  4208. end;
  4209. function tprocdef.Getprocendtai: tai;
  4210. begin
  4211. if not assigned(implprocdefinfo) then
  4212. internalerror(2014010307);
  4213. result:=implprocdefinfo^.procendtai;
  4214. end;
  4215. function tprocdef.Getprocstarttai: tai;
  4216. begin
  4217. if not assigned(implprocdefinfo) then
  4218. internalerror(2014010308);
  4219. result:=implprocdefinfo^.procstarttai;
  4220. end;
  4221. procedure tprocdef.Setprocendtai(AValue: tai);
  4222. begin
  4223. if not assigned(implprocdefinfo) then
  4224. internalerror(2014010309);
  4225. implprocdefinfo^.procendtai:=AValue;
  4226. end;
  4227. function tprocdef.Getskpara: pointer;
  4228. begin
  4229. if not assigned(implprocdefinfo) then
  4230. internalerror(2014010310);
  4231. result:=implprocdefinfo^.skpara;
  4232. end;
  4233. procedure tprocdef.Setskpara(AValue: pointer);
  4234. begin
  4235. if not assigned(implprocdefinfo) then
  4236. internalerror(2014010311);
  4237. implprocdefinfo^.skpara:=AValue;
  4238. end;
  4239. function tprocdef.Getforwarddef: boolean;
  4240. begin
  4241. if not assigned(implprocdefinfo) then
  4242. internalerror(2014010312);
  4243. result:=implprocdefinfo^.forwarddef;
  4244. end;
  4245. function tprocdef.Gethasforward: boolean;
  4246. begin
  4247. if not assigned(implprocdefinfo) then
  4248. internalerror(2014010313);
  4249. result:=implprocdefinfo^.hasforward;
  4250. end;
  4251. function tprocdef.Getinterfacedef: boolean;
  4252. begin
  4253. if not assigned(implprocdefinfo) then
  4254. internalerror(2014010314);
  4255. result:=implprocdefinfo^.interfacedef;
  4256. end;
  4257. procedure tprocdef.Setforwarddef(AValue: boolean);
  4258. begin
  4259. if not assigned(implprocdefinfo) then
  4260. internalerror(2014010315);
  4261. implprocdefinfo^.forwarddef:=AValue;
  4262. end;
  4263. procedure tprocdef.Sethasforward(AValue: boolean);
  4264. begin
  4265. if not assigned(implprocdefinfo) then
  4266. internalerror(2014010316);
  4267. implprocdefinfo^.hasforward:=AValue;
  4268. end;
  4269. function tprocdef.GetIsEmpty: boolean;
  4270. begin
  4271. result:=pio_empty in implprocoptions;
  4272. end;
  4273. procedure tprocdef.SetIsEmpty(AValue: boolean);
  4274. begin
  4275. if AValue then
  4276. include(implprocoptions,pio_empty)
  4277. else
  4278. include(implprocoptions,pio_empty);
  4279. end;
  4280. function tprocdef.GetHasInliningInfo: boolean;
  4281. begin
  4282. result:=pio_has_inlininginfo in implprocoptions;
  4283. end;
  4284. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  4285. begin
  4286. if AValue then
  4287. include(implprocoptions,pio_has_inlininginfo)
  4288. else
  4289. exclude(implprocoptions,pio_has_inlininginfo);
  4290. end;
  4291. procedure tprocdef.Setinterfacedef(AValue: boolean);
  4292. begin
  4293. if not assigned(implprocdefinfo) then
  4294. internalerror(2014010317);
  4295. implprocdefinfo^.interfacedef:=AValue;
  4296. end;
  4297. procedure tprocdef.Setprocstarttai(AValue: tai);
  4298. begin
  4299. if not assigned(implprocdefinfo) then
  4300. internalerror(2014010318);
  4301. implprocdefinfo^.procstarttai:=AValue;
  4302. end;
  4303. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  4304. begin
  4305. if not assigned(implprocdefinfo) then
  4306. internalerror(2014010319);
  4307. implprocdefinfo^.parentfpstruct:=AValue;
  4308. end;
  4309. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  4310. begin
  4311. if not assigned(implprocdefinfo) then
  4312. internalerror(2014010320);
  4313. implprocdefinfo^.parentfpstructptrtype:=AValue;
  4314. end;
  4315. constructor tprocdef.create(level:byte);
  4316. begin
  4317. inherited create(procdef,level);
  4318. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  4319. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  4320. {$ifdef symansistr}
  4321. _mangledname:='';
  4322. {$else symansistr}
  4323. _mangledname:=nil;
  4324. {$endif symansistr}
  4325. fileinfo:=current_filepos;
  4326. extnumber:=$ffff;
  4327. aliasnames:=TCmdStrList.create;
  4328. funcretsym:=nil;
  4329. forwarddef:=true;
  4330. interfacedef:=false;
  4331. hasforward:=false;
  4332. struct := nil;
  4333. import_dll:=nil;
  4334. import_name:=nil;
  4335. import_nr:=0;
  4336. inlininginfo:=nil;
  4337. deprecatedmsg:=nil;
  4338. end;
  4339. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  4340. var
  4341. i,aliasnamescount : longint;
  4342. level : byte;
  4343. begin
  4344. inherited ppuload(procdef,ppufile);
  4345. {$ifdef symansistr}
  4346. if po_has_mangledname in procoptions then
  4347. _mangledname:=ppufile.getansistring
  4348. else
  4349. _mangledname:='';
  4350. {$else symansistr}
  4351. if po_has_mangledname in procoptions then
  4352. _mangledname:=stringdup(ppufile.getstring)
  4353. else
  4354. _mangledname:=nil;
  4355. {$endif symansistr}
  4356. extnumber:=ppufile.getword;
  4357. level:=ppufile.getbyte;
  4358. ppufile.getderef(structderef);
  4359. ppufile.getderef(procsymderef);
  4360. ppufile.getposinfo(fileinfo);
  4361. visibility:=tvisibility(ppufile.getbyte);
  4362. ppufile.getsmallset(symoptions);
  4363. if sp_has_deprecated_msg in symoptions then
  4364. deprecatedmsg:=stringdup(ppufile.getstring)
  4365. else
  4366. deprecatedmsg:=nil;
  4367. synthetickind:=tsynthetickind(ppufile.getbyte);
  4368. { import stuff }
  4369. if po_has_importdll in procoptions then
  4370. import_dll:=stringdup(ppufile.getstring)
  4371. else
  4372. import_dll:=nil;
  4373. if po_has_importname in procoptions then
  4374. import_name:=stringdup(ppufile.getstring)
  4375. else
  4376. import_name:=nil;
  4377. import_nr:=ppufile.getword;
  4378. if (po_msgint in procoptions) then
  4379. messageinf.i:=ppufile.getlongint;
  4380. if (po_msgstr in procoptions) then
  4381. messageinf.str:=stringdup(ppufile.getstring);
  4382. if (po_dispid in procoptions) then
  4383. dispid:=ppufile.getlongint;
  4384. { inline stuff }
  4385. ppufile.getsmallset(implprocoptions);
  4386. if has_inlininginfo then
  4387. begin
  4388. ppufile.getderef(funcretsymderef);
  4389. new(inlininginfo);
  4390. ppufile.getsmallset(inlininginfo^.flags);
  4391. end
  4392. else
  4393. begin
  4394. inlininginfo:=nil;
  4395. funcretsym:=nil;
  4396. end;
  4397. aliasnames:=TCmdStrList.create;
  4398. { count alias names }
  4399. aliasnamescount:=ppufile.getbyte;
  4400. for i:=1 to aliasnamescount do
  4401. aliasnames.insert(ppufile.getstring);
  4402. ppuload_platform(ppufile);
  4403. { load para symtable }
  4404. parast:=tparasymtable.create(self,level);
  4405. tparasymtable(parast).ppuload(ppufile);
  4406. { load local symtable }
  4407. if has_inlininginfo then
  4408. begin
  4409. localst:=tlocalsymtable.create(self,level);
  4410. tlocalsymtable(localst).ppuload(ppufile);
  4411. end
  4412. else
  4413. localst:=nil;
  4414. { inline stuff }
  4415. if has_inlininginfo then
  4416. inlininginfo^.code:=ppuloadnodetree(ppufile);
  4417. { default values for no persistent data }
  4418. if (cs_link_deffile in current_settings.globalswitches) and
  4419. (tf_need_export in target_info.flags) and
  4420. (po_exports in procoptions) then
  4421. deffile.AddExport(mangledname);
  4422. { Disable po_has_inlining until the derefimpl is done }
  4423. has_inlininginfo:=false;
  4424. end;
  4425. destructor tprocdef.destroy;
  4426. begin
  4427. aliasnames.free;
  4428. aliasnames:=nil;
  4429. if assigned(localst) and
  4430. (localst.symtabletype<>staticsymtable) then
  4431. begin
  4432. {$ifdef MEMDEBUG}
  4433. memproclocalst.start;
  4434. {$endif MEMDEBUG}
  4435. localst.free;
  4436. localst:=nil;
  4437. {$ifdef MEMDEBUG}
  4438. memproclocalst.start;
  4439. {$endif MEMDEBUG}
  4440. end;
  4441. if assigned(inlininginfo) then
  4442. begin
  4443. {$ifdef MEMDEBUG}
  4444. memprocnodetree.start;
  4445. {$endif MEMDEBUG}
  4446. tnode(inlininginfo^.code).free;
  4447. {$ifdef MEMDEBUG}
  4448. memprocnodetree.start;
  4449. {$endif MEMDEBUG}
  4450. dispose(inlininginfo);
  4451. inlininginfo:=nil;
  4452. end;
  4453. if assigned(implprocdefinfo) then
  4454. begin
  4455. stringdispose(implprocdefinfo^.resultname);
  4456. freemem(implprocdefinfo);
  4457. implprocdefinfo:=nil;
  4458. end;
  4459. stringdispose(import_dll);
  4460. stringdispose(import_name);
  4461. stringdispose(deprecatedmsg);
  4462. if (po_msgstr in procoptions) then
  4463. stringdispose(messageinf.str);
  4464. {$ifndef symansistr}
  4465. if assigned(_mangledname) then
  4466. begin
  4467. {$ifdef MEMDEBUG}
  4468. memmanglednames.start;
  4469. {$endif MEMDEBUG}
  4470. stringdispose(_mangledname);
  4471. {$ifdef MEMDEBUG}
  4472. memmanglednames.stop;
  4473. {$endif MEMDEBUG}
  4474. end;
  4475. {$endif symansistr}
  4476. inherited destroy;
  4477. end;
  4478. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  4479. var
  4480. oldintfcrc : boolean;
  4481. aliasnamescount : longint;
  4482. item : TCmdStrListItem;
  4483. begin
  4484. { released procdef? }
  4485. if not assigned(parast) then
  4486. exit;
  4487. inherited ppuwrite(ppufile);
  4488. {$ifdef symansistr}
  4489. if po_has_mangledname in procoptions then
  4490. ppufile.putansistring(_mangledname);
  4491. {$else symansistr}
  4492. if po_has_mangledname in procoptions then
  4493. ppufile.putstring(_mangledname^);
  4494. {$endif symansistr}
  4495. ppufile.putword(extnumber);
  4496. ppufile.putbyte(parast.symtablelevel);
  4497. ppufile.putderef(structderef);
  4498. ppufile.putderef(procsymderef);
  4499. ppufile.putposinfo(fileinfo);
  4500. ppufile.putbyte(byte(visibility));
  4501. ppufile.putsmallset(symoptions);
  4502. if sp_has_deprecated_msg in symoptions then
  4503. ppufile.putstring(deprecatedmsg^);
  4504. ppufile.putbyte(byte(synthetickind));
  4505. { import }
  4506. if po_has_importdll in procoptions then
  4507. ppufile.putstring(import_dll^);
  4508. if po_has_importname in procoptions then
  4509. ppufile.putstring(import_name^);
  4510. ppufile.putword(import_nr);
  4511. if (po_msgint in procoptions) then
  4512. ppufile.putlongint(messageinf.i);
  4513. if (po_msgstr in procoptions) then
  4514. ppufile.putstring(messageinf.str^);
  4515. if (po_dispid in procoptions) then
  4516. ppufile.putlongint(dispid);
  4517. { inline stuff }
  4518. oldintfcrc:=ppufile.do_crc;
  4519. ppufile.do_crc:=false;
  4520. ppufile.putsmallset(implprocoptions);
  4521. if has_inlininginfo then
  4522. begin
  4523. ppufile.putderef(funcretsymderef);
  4524. ppufile.putsmallset(inlininginfo^.flags);
  4525. end;
  4526. { count alias names }
  4527. aliasnamescount:=0;
  4528. item:=TCmdStrListItem(aliasnames.first);
  4529. while assigned(item) do
  4530. begin
  4531. inc(aliasnamescount);
  4532. item:=TCmdStrListItem(item.next);
  4533. end;
  4534. if aliasnamescount>255 then
  4535. internalerror(200711021);
  4536. ppufile.putbyte(aliasnamescount);
  4537. item:=TCmdStrListItem(aliasnames.first);
  4538. while assigned(item) do
  4539. begin
  4540. ppufile.putstring(item.str);
  4541. item:=TCmdStrListItem(item.next);
  4542. end;
  4543. ppufile.do_crc:=oldintfcrc;
  4544. { write this entry }
  4545. writeentry(ppufile,ibprocdef);
  4546. { Save the para symtable, this is taken from the interface }
  4547. tparasymtable(parast).ppuwrite(ppufile);
  4548. { save localsymtable for inline procedures or when local
  4549. browser info is requested, this has no influence on the crc }
  4550. if has_inlininginfo then
  4551. begin
  4552. oldintfcrc:=ppufile.do_crc;
  4553. ppufile.do_crc:=false;
  4554. tlocalsymtable(localst).ppuwrite(ppufile);
  4555. ppufile.do_crc:=oldintfcrc;
  4556. end;
  4557. { node tree for inlining }
  4558. oldintfcrc:=ppufile.do_crc;
  4559. ppufile.do_crc:=false;
  4560. if has_inlininginfo then
  4561. ppuwritenodetree(ppufile,inlininginfo^.code);
  4562. ppufile.do_crc:=oldintfcrc;
  4563. end;
  4564. function tprocdef.fullprocname(showhidden:boolean):string;
  4565. var
  4566. pno: tprocnameoptions;
  4567. begin
  4568. pno:=[];
  4569. if showhidden then
  4570. include(pno,pno_showhidden);
  4571. result:=customprocname(pno);
  4572. end;
  4573. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  4574. var
  4575. s, rn : ansistring;
  4576. t : ttoken;
  4577. begin
  4578. {$ifdef EXTDEBUG}
  4579. include(pno,pno_showhidden);
  4580. {$endif EXTDEBUG}
  4581. s:='';
  4582. if proctypeoption=potype_operator then
  4583. begin
  4584. for t:=NOTOKEN to last_overloaded do
  4585. if procsym.realname='$'+overloaded_names[t] then
  4586. begin
  4587. s:='operator ';
  4588. if (pno_ownername in pno) and
  4589. assigned(struct) then
  4590. s:=s+struct.RttiName+'.';
  4591. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  4592. break;
  4593. end;
  4594. end
  4595. else
  4596. begin
  4597. if (po_classmethod in procoptions) and
  4598. not(pno_noclassmarker in pno) then
  4599. s:='class ';
  4600. case proctypeoption of
  4601. potype_constructor,
  4602. potype_class_constructor:
  4603. s:=s+'constructor ';
  4604. potype_class_destructor,
  4605. potype_destructor:
  4606. s:=s+'destructor ';
  4607. else
  4608. if (pno_proctypeoption in pno) then
  4609. begin
  4610. if assigned(returndef) and
  4611. not(is_void(returndef)) then
  4612. s:=s+'function '
  4613. else
  4614. s:=s+'procedure ';
  4615. end;
  4616. end;
  4617. if (pno_ownername in pno) and
  4618. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  4619. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  4620. rn:=procsym.realname;
  4621. if (pno_noleadingdollar in pno) and
  4622. (rn[1]='$') then
  4623. delete(rn,1,1);
  4624. s:=s+rn+typename_paras(pno);
  4625. end;
  4626. if not(proctypeoption in [potype_constructor,potype_destructor,
  4627. potype_class_constructor,potype_class_destructor]) and
  4628. assigned(returndef) and
  4629. not(is_void(returndef)) then
  4630. s:=s+':'+returndef.GetTypeName;
  4631. if owner.symtabletype=localsymtable then
  4632. s:=s+' is nested';
  4633. s:=s+';';
  4634. { forced calling convention? }
  4635. if (po_hascallingconvention in procoptions) then
  4636. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  4637. if (po_staticmethod in procoptions) and
  4638. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  4639. s:=s+' Static;';
  4640. if pno_mangledname in pno then
  4641. s:=s+' -- mangled name: '+mangledname;
  4642. customprocname:=s;
  4643. end;
  4644. function tprocdef.is_methodpointer:boolean;
  4645. begin
  4646. { don't check assigned(_class), that's also the case for nested
  4647. procedures inside methods }
  4648. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  4649. end;
  4650. function tprocdef.is_addressonly:boolean;
  4651. begin
  4652. result:=assigned(owner) and
  4653. not is_methodpointer and
  4654. (not(m_nested_procvars in current_settings.modeswitches) or
  4655. not is_nested_pd(self));
  4656. end;
  4657. procedure tprocdef.make_external;
  4658. begin
  4659. include(procoptions,po_external);
  4660. forwarddef:=false;
  4661. end;
  4662. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  4663. begin
  4664. case t of
  4665. gs_local :
  4666. GetSymtable:=localst;
  4667. gs_para :
  4668. GetSymtable:=parast;
  4669. else
  4670. GetSymtable:=nil;
  4671. end;
  4672. end;
  4673. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  4674. var
  4675. j : longint;
  4676. begin
  4677. result:=inherited getcopyas(newtyp,copytyp);
  4678. if newtyp=procvardef then
  4679. exit;
  4680. { don't copy mangled name, can be different }
  4681. tprocdef(result).messageinf:=messageinf;
  4682. tprocdef(result).dispid:=dispid;
  4683. if po_msgstr in procoptions then
  4684. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  4685. tprocdef(result).symoptions:=symoptions;
  4686. if assigned(deprecatedmsg) then
  4687. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  4688. { will have to be associated with appropriate procsym }
  4689. tprocdef(result).procsym:=nil;
  4690. { don't create aliases for bare copies, nor copy the funcretsym as
  4691. the function result parameter will be inserted again if necessary
  4692. (e.g. if the calling convention is changed) }
  4693. if copytyp<>pc_bareproc then
  4694. begin
  4695. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  4696. if assigned(funcretsym) then
  4697. begin
  4698. if funcretsym.owner=parast then
  4699. begin
  4700. j:=parast.symlist.indexof(funcretsym);
  4701. if j<0 then
  4702. internalerror(2011040606);
  4703. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  4704. end
  4705. else if funcretsym.owner=localst then
  4706. begin
  4707. { nothing to do, will be inserted for the new procdef while
  4708. parsing its body (by pdecsub.insert_funcret_local) }
  4709. end
  4710. else
  4711. internalerror(2011040605);
  4712. end;
  4713. end;
  4714. { will have to be associated with a new struct }
  4715. tprocdef(result).struct:=nil;
  4716. if assigned(implprocdefinfo) then
  4717. begin
  4718. if assigned(resultname) then
  4719. tprocdef(result).resultname:=stringdup(resultname^);
  4720. end;
  4721. if assigned(import_dll) then
  4722. tprocdef(result).import_dll:=stringdup(import_dll^);
  4723. if assigned(import_name) then
  4724. tprocdef(result).import_name:=stringdup(import_name^);
  4725. tprocdef(result).import_nr:=import_nr;
  4726. tprocdef(result).extnumber:=$ffff;
  4727. tprocdef(result).visibility:=visibility;
  4728. tprocdef(result).synthetickind:=synthetickind;
  4729. { we need a separate implementation for the copied def }
  4730. tprocdef(result).forwarddef:=true;
  4731. tprocdef(result).interfacedef:=true;
  4732. { create new paralist }
  4733. tprocdef(result).calcparas;
  4734. end;
  4735. function tprocdef.getcopy: tstoreddef;
  4736. begin
  4737. result:=getcopyas(procdef,pc_normal);
  4738. end;
  4739. procedure tprocdef.buildderef;
  4740. begin
  4741. inherited buildderef;
  4742. structderef.build(struct);
  4743. { procsym that originaly defined this definition, should be in the
  4744. same symtable }
  4745. procsymderef.build(procsym);
  4746. end;
  4747. procedure tprocdef.buildderefimpl;
  4748. begin
  4749. inherited buildderefimpl;
  4750. { inline tree }
  4751. if has_inlininginfo then
  4752. begin
  4753. { Localst is not available for main/unit init }
  4754. if assigned(localst) then
  4755. begin
  4756. tlocalsymtable(localst).buildderef;
  4757. tlocalsymtable(localst).buildderefimpl;
  4758. end;
  4759. funcretsymderef.build(funcretsym);
  4760. inlininginfo^.code.buildderefimpl;
  4761. end;
  4762. end;
  4763. procedure tprocdef.deref;
  4764. begin
  4765. inherited deref;
  4766. struct:=tabstractrecorddef(structderef.resolve);
  4767. { procsym that originaly defined this definition, should be in the
  4768. same symtable }
  4769. procsym:=tprocsym(procsymderef.resolve);
  4770. end;
  4771. procedure tprocdef.derefimpl;
  4772. begin
  4773. { Enable has_inlininginfo when the inlininginfo
  4774. structure is available. The has_inlininginfo was disabled
  4775. after the load, since the data was invalid }
  4776. if assigned(inlininginfo) then
  4777. has_inlininginfo:=true;
  4778. { Inline }
  4779. if has_inlininginfo then
  4780. begin
  4781. { Locals }
  4782. if assigned(localst) then
  4783. begin
  4784. tlocalsymtable(localst).deref;
  4785. tlocalsymtable(localst).derefimpl;
  4786. end;
  4787. inlininginfo^.code.derefimpl;
  4788. { funcretsym, this is always located in the localst }
  4789. funcretsym:=tsym(funcretsymderef.resolve);
  4790. end
  4791. else
  4792. begin
  4793. { safety }
  4794. { Not safe! A unit may be reresolved after its interface has been
  4795. parsed but before its implementation has been parsed, and in that
  4796. case the funcretsym is still required!
  4797. funcretsym:=nil; }
  4798. end;
  4799. end;
  4800. function tprocdef.GetTypeName : string;
  4801. begin
  4802. GetTypeName := FullProcName(false);
  4803. end;
  4804. function tprocdef.mangledname : TSymStr;
  4805. begin
  4806. {$ifdef symansistr}
  4807. if _mangledname='' then
  4808. begin
  4809. result:=defaultmangledname;
  4810. _mangledname:=result;
  4811. end
  4812. else
  4813. result:=_mangledname;
  4814. {$else symansistr}
  4815. if not assigned(_mangledname) then
  4816. begin
  4817. result:=defaultmangledname;
  4818. _mangledname:=stringdup(mangledname);
  4819. end
  4820. else
  4821. result:=_mangledname^;
  4822. {$endif symansistr}
  4823. end;
  4824. function tprocdef.defaultmangledname: TSymStr;
  4825. var
  4826. hp : TParavarsym;
  4827. hs : TSymStr;
  4828. crc : dword;
  4829. newlen,
  4830. oldlen,
  4831. i : integer;
  4832. begin
  4833. hp:=nil;
  4834. { we need to use the symtable where the procsym is inserted,
  4835. because that is visible to the world }
  4836. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  4837. oldlen:=length(defaultmangledname);
  4838. { add parameter types }
  4839. for i:=0 to paras.count-1 do
  4840. begin
  4841. hp:=tparavarsym(paras[i]);
  4842. if not(vo_is_hidden_para in hp.varoptions) then
  4843. defaultmangledname:=defaultmangledname+'$'+hp.vardef.mangledparaname;
  4844. end;
  4845. { add resultdef, add $$ as separator to make it unique from a
  4846. parameter separator }
  4847. if not is_void(returndef) then
  4848. defaultmangledname:=defaultmangledname+'$$'+returndef.mangledparaname;
  4849. newlen:=length(defaultmangledname);
  4850. { Replace with CRC if the parameter line is very long }
  4851. if (newlen-oldlen>12) and
  4852. ((newlen>100) or (newlen-oldlen>64)) then
  4853. begin
  4854. crc:=0;
  4855. for i:=0 to paras.count-1 do
  4856. begin
  4857. hp:=tparavarsym(paras[i]);
  4858. if not(vo_is_hidden_para in hp.varoptions) then
  4859. begin
  4860. hs:=hp.vardef.mangledparaname;
  4861. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4862. end;
  4863. end;
  4864. if not is_void(returndef) then
  4865. begin
  4866. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  4867. hs:='$$'+returndef.mangledparaname;
  4868. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4869. end;
  4870. defaultmangledname:=Copy(defaultmangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  4871. end;
  4872. end;
  4873. function tprocdef.cplusplusmangledname : TSymStr;
  4874. function getcppparaname(p : tdef) : TSymStr;
  4875. const
  4876. {$ifdef NAMEMANGLING_GCC2}
  4877. ordtype2str : array[tordtype] of string[2] = (
  4878. '',
  4879. 'Uc','Us','Ui','Us',
  4880. 'Sc','s','i','x',
  4881. 'b','b','b','b','b',
  4882. 'c','w','x');
  4883. {$else NAMEMANGLING_GCC2}
  4884. ordtype2str : array[tordtype] of string[1] = (
  4885. 'v',
  4886. 'h','t','j','y',
  4887. 'a','s','i','x',
  4888. 'b','b','b','b',
  4889. 'b','b','b','b',
  4890. 'c','w','x');
  4891. floattype2str : array[tfloattype] of string[1] = (
  4892. 'f','d','e','e',
  4893. 'd','d','g');
  4894. {$endif NAMEMANGLING_GCC2}
  4895. var
  4896. s : TSymStr;
  4897. begin
  4898. case p.typ of
  4899. orddef:
  4900. s:=ordtype2str[torddef(p).ordtype];
  4901. pointerdef:
  4902. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  4903. {$ifndef NAMEMANGLING_GCC2}
  4904. floatdef:
  4905. s:=floattype2str[tfloatdef(p).floattype];
  4906. {$endif NAMEMANGLING_GCC2}
  4907. else
  4908. internalerror(2103001);
  4909. end;
  4910. getcppparaname:=s;
  4911. end;
  4912. var
  4913. s,s2 : TSymStr;
  4914. hp : TParavarsym;
  4915. i : integer;
  4916. begin
  4917. {$ifdef NAMEMANGLING_GCC2}
  4918. { outdated gcc 2.x name mangling scheme }
  4919. s := procsym.realname;
  4920. if procsym.owner.symtabletype=ObjectSymtable then
  4921. begin
  4922. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  4923. case proctypeoption of
  4924. potype_destructor:
  4925. s:='_$_'+tostr(length(s2))+s2;
  4926. potype_constructor:
  4927. s:='___'+tostr(length(s2))+s2;
  4928. else
  4929. s:='_'+s+'__'+tostr(length(s2))+s2;
  4930. end;
  4931. end
  4932. else s:=s+'__';
  4933. s:=s+'F';
  4934. { concat modifiers }
  4935. { !!!!! }
  4936. { now we handle the parameters }
  4937. if maxparacount>0 then
  4938. begin
  4939. for i:=0 to paras.count-1 do
  4940. begin
  4941. hp:=tparavarsym(paras[i]);
  4942. { no hidden parameters form part of a C++ mangled name:
  4943. a) self is not included
  4944. b) there are no "high" or other hidden parameters
  4945. }
  4946. if vo_is_hidden_para in hp.varoptions then
  4947. continue;
  4948. s2:=getcppparaname(hp.vardef);
  4949. if hp.varspez in [vs_var,vs_out] then
  4950. s2:='R'+s2;
  4951. s:=s+s2;
  4952. end;
  4953. end
  4954. else
  4955. s:=s+'v';
  4956. cplusplusmangledname:=s;
  4957. {$else NAMEMANGLING_GCC2}
  4958. { gcc 3.x and 4.x name mangling scheme }
  4959. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  4960. if procsym.owner.symtabletype=ObjectSymtable then
  4961. begin
  4962. s:='_ZN';
  4963. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  4964. s:=s+tostr(length(s2))+s2;
  4965. case proctypeoption of
  4966. potype_constructor:
  4967. s:=s+'C1';
  4968. potype_destructor:
  4969. s:=s+'D1';
  4970. else
  4971. s:=s+tostr(length(procsym.realname))+procsym.realname;
  4972. end;
  4973. s:=s+'E';
  4974. end
  4975. else
  4976. s:=procsym.realname;
  4977. { now we handle the parameters }
  4978. if maxparacount>0 then
  4979. begin
  4980. for i:=0 to paras.count-1 do
  4981. begin
  4982. hp:=tparavarsym(paras[i]);
  4983. { no hidden parameters form part of a C++ mangled name:
  4984. a) self is not included
  4985. b) there are no "high" or other hidden parameters
  4986. }
  4987. if vo_is_hidden_para in hp.varoptions then
  4988. continue;
  4989. s2:=getcppparaname(hp.vardef);
  4990. if hp.varspez in [vs_var,vs_out] then
  4991. s2:='R'+s2;
  4992. s:=s+s2;
  4993. end;
  4994. end
  4995. else
  4996. s:=s+'v';
  4997. cplusplusmangledname:=s;
  4998. {$endif NAMEMANGLING_GCC2}
  4999. end;
  5000. function tprocdef.objcmangledname : TSymStr;
  5001. var
  5002. manglednamelen: longint;
  5003. iscatmethod : boolean;
  5004. begin
  5005. if not (po_msgstr in procoptions) then
  5006. internalerror(2009030901);
  5007. { we may very well need longer strings to handle these... }
  5008. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  5009. length('+"[ ]"')+length(messageinf.str^);
  5010. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  5011. if (iscatmethod) then
  5012. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  5013. if manglednamelen>255 then
  5014. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  5015. if not(po_classmethod in procoptions) then
  5016. result:='"-['
  5017. else
  5018. result:='"+[';
  5019. { quotes are necessary because the +/- otherwise confuse the assembler
  5020. into expecting a number
  5021. }
  5022. if iscatmethod then
  5023. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  5024. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  5025. if iscatmethod then
  5026. result:=result+')';
  5027. result:=result+' '+messageinf.str^+']"';
  5028. end;
  5029. procedure tprocdef.setmangledname(const s : TSymStr);
  5030. begin
  5031. { This is not allowed anymore, the forward declaration
  5032. already needs to create the correct mangledname, no changes
  5033. afterwards are allowed (PFV) }
  5034. { Exception: interface definitions in mode macpas, since in that }
  5035. { case no reference to the old name can exist yet (JM) }
  5036. {$ifdef symansistr}
  5037. if _mangledname<>'' then
  5038. if ((m_mac in current_settings.modeswitches) and
  5039. (interfacedef)) then
  5040. _mangledname:=''
  5041. else
  5042. internalerror(200411171);
  5043. {$else symansistr}
  5044. if assigned(_mangledname) then
  5045. if ((m_mac in current_settings.modeswitches) and
  5046. (interfacedef)) then
  5047. stringdispose(_mangledname)
  5048. else
  5049. internalerror(200411171);
  5050. {$endif symansistr}
  5051. {$ifdef jvm}
  5052. { this routine can be called for compilerproces. can't set mangled
  5053. name since it must be calculated, but it uses import_name when set
  5054. -> set that one }
  5055. import_name:=stringdup(s);
  5056. include(procoptions,po_has_importname);
  5057. include(procoptions,po_has_mangledname);
  5058. {$else}
  5059. {$ifdef symansistr}
  5060. _mangledname:=s;
  5061. {$else symansistr}
  5062. _mangledname:=stringdup(s);
  5063. {$endif symansistr}
  5064. {$endif jvm}
  5065. include(procoptions,po_has_mangledname);
  5066. end;
  5067. {***************************************************************************
  5068. TPROCVARDEF
  5069. ***************************************************************************}
  5070. constructor tprocvardef.create(level:byte);
  5071. begin
  5072. inherited create(procvardef,level);
  5073. end;
  5074. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  5075. begin
  5076. inherited ppuload(procvardef,ppufile);
  5077. { load para symtable }
  5078. parast:=tparasymtable.create(self,ppufile.getbyte);
  5079. ppuload_platform(ppufile);
  5080. tparasymtable(parast).ppuload(ppufile);
  5081. end;
  5082. function tprocvardef.getcopy : tstoreddef;
  5083. var
  5084. i : tcallercallee;
  5085. j : longint;
  5086. begin
  5087. result:=cprocvardef.create(parast.symtablelevel);
  5088. tprocvardef(result).returndef:=returndef;
  5089. tprocvardef(result).returndefderef:=returndefderef;
  5090. tprocvardef(result).parast:=parast.getcopy;
  5091. tprocvardef(result).savesize:=savesize;
  5092. { create paralist copy }
  5093. calcparas;
  5094. tprocvardef(result).paras:=tparalist.create(false);
  5095. tprocvardef(result).paras.count:=paras.count;
  5096. for j:=0 to paras.count-1 do
  5097. tprocvardef(result).paras[j]:=paras[j];
  5098. tprocvardef(result).proctypeoption:=proctypeoption;
  5099. tprocvardef(result).proccalloption:=proccalloption;
  5100. tprocvardef(result).procoptions:=procoptions;
  5101. tprocvardef(result).callerargareasize:=callerargareasize;
  5102. tprocvardef(result).calleeargareasize:=calleeargareasize;
  5103. tprocvardef(result).maxparacount:=maxparacount;
  5104. tprocvardef(result).minparacount:=minparacount;
  5105. for i:=low(tcallercallee) to high(tcallercallee) do
  5106. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  5107. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  5108. {$ifdef m68k}
  5109. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  5110. {$endif}
  5111. end;
  5112. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  5113. begin
  5114. inherited ppuwrite(ppufile);
  5115. { Save the para symtable level (necessary to distinguish nested
  5116. procvars) }
  5117. ppufile.putbyte(parast.symtablelevel);
  5118. { Write this entry }
  5119. writeentry(ppufile,ibprocvardef);
  5120. { Save the para symtable, this is taken from the interface }
  5121. tparasymtable(parast).ppuwrite(ppufile);
  5122. end;
  5123. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  5124. begin
  5125. case t of
  5126. gs_para :
  5127. GetSymtable:=parast;
  5128. else
  5129. GetSymtable:=nil;
  5130. end;
  5131. end;
  5132. function tprocvardef.size : asizeint;
  5133. begin
  5134. if ((po_methodpointer in procoptions) or
  5135. is_nested_pd(self)) and
  5136. not(po_addressonly in procoptions) then
  5137. begin
  5138. if is_nested_pd(self) then
  5139. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  5140. else
  5141. size:=voidcodepointertype.size+voidpointertype.size;
  5142. end
  5143. else
  5144. size:=voidcodepointertype.size;
  5145. end;
  5146. function tprocvardef.is_methodpointer:boolean;
  5147. begin
  5148. result:=(po_methodpointer in procoptions);
  5149. end;
  5150. function tprocvardef.is_addressonly:boolean;
  5151. begin
  5152. result:=(not(po_methodpointer in procoptions) and
  5153. not is_nested_pd(self)) or
  5154. (po_addressonly in procoptions);
  5155. end;
  5156. function tprocvardef.getmangledparaname:TSymStr;
  5157. begin
  5158. if not(po_methodpointer in procoptions) then
  5159. if not is_nested_pd(self) then
  5160. result:='procvar'
  5161. else
  5162. result:='nestedprovar'
  5163. else
  5164. result:='procvarofobj'
  5165. end;
  5166. function tprocvardef.is_publishable : boolean;
  5167. begin
  5168. is_publishable:=(po_methodpointer in procoptions);
  5169. end;
  5170. function tprocvardef.GetTypeName : string;
  5171. var
  5172. s: string;
  5173. pno : tprocnameoptions;
  5174. begin
  5175. {$ifdef EXTDEBUG}
  5176. pno:=[pno_showhidden];
  5177. {$else EXTDEBUG}
  5178. pno:=[];
  5179. {$endif EXTDEBUG}
  5180. s:='<';
  5181. if po_classmethod in procoptions then
  5182. s := s+'class method type of'
  5183. else
  5184. if po_addressonly in procoptions then
  5185. s := s+'address of'
  5186. else
  5187. s := s+'procedure variable type of';
  5188. if assigned(returndef) and
  5189. (returndef<>voidtype) then
  5190. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5191. else
  5192. s:=s+' procedure'+typename_paras(pno);
  5193. if po_methodpointer in procoptions then
  5194. s := s+' of object';
  5195. if is_nested_pd(self) then
  5196. s := s+' is nested';
  5197. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5198. end;
  5199. {***************************************************************************
  5200. TOBJECTDEF
  5201. ***************************************************************************}
  5202. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  5203. begin
  5204. inherited create(n,objectdef);
  5205. fcurrent_dispid:=0;
  5206. objecttype:=ot;
  5207. childof:=nil;
  5208. if objecttype=odt_helper then
  5209. owner.includeoption(sto_has_helper);
  5210. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  5211. { create space for vmt !! }
  5212. vmtentries:=TFPList.Create;
  5213. vmt_offset:=0;
  5214. set_parent(c);
  5215. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5216. prepareguid;
  5217. { setup implemented interfaces }
  5218. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5219. ImplementedInterfaces:=TFPObjectList.Create(true)
  5220. else
  5221. ImplementedInterfaces:=nil;
  5222. writing_class_record_dbginfo:=false;
  5223. end;
  5224. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  5225. var
  5226. i,
  5227. implintfcount : longint;
  5228. d, getterd : tderef;
  5229. ImplIntf : TImplementedInterface;
  5230. vmtentry : pvmtentry;
  5231. begin
  5232. inherited ppuload(objectdef,ppufile);
  5233. objecttype:=tobjecttyp(ppufile.getbyte);
  5234. objextname:=stringdup(ppufile.getstring);
  5235. { only used for external Objective-C classes/protocols }
  5236. if (objextname^='') then
  5237. stringdispose(objextname);
  5238. symtable:=tObjectSymtable.create(self,objrealname^,0);
  5239. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  5240. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  5241. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  5242. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  5243. vmt_offset:=ppufile.getlongint;
  5244. ppufile.getderef(childofderef);
  5245. { load guid }
  5246. iidstr:=nil;
  5247. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5248. begin
  5249. new(iidguid);
  5250. ppufile.getguid(iidguid^);
  5251. iidstr:=stringdup(ppufile.getstring);
  5252. end;
  5253. abstractcnt:=ppufile.getlongint;
  5254. if objecttype=odt_helper then
  5255. ppufile.getderef(extendeddefderef);
  5256. vmtentries:=TFPList.Create;
  5257. vmtentries.count:=ppufile.getlongint;
  5258. for i:=0 to vmtentries.count-1 do
  5259. begin
  5260. ppufile.getderef(d);
  5261. new(vmtentry);
  5262. vmtentry^.procdef:=nil;
  5263. vmtentry^.procdefderef:=d;
  5264. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  5265. vmtentries[i]:=vmtentry;
  5266. end;
  5267. { load implemented interfaces }
  5268. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5269. begin
  5270. ImplementedInterfaces:=TFPObjectList.Create(true);
  5271. implintfcount:=ppufile.getlongint;
  5272. for i:=0 to implintfcount-1 do
  5273. begin
  5274. ppufile.getderef(d);
  5275. ppufile.getderef(getterd);
  5276. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  5277. ImplIntf.IOffset:=ppufile.getlongint;
  5278. byte(ImplIntf.IType):=ppufile.getbyte;
  5279. ImplementedInterfaces.Add(ImplIntf);
  5280. end;
  5281. end
  5282. else
  5283. ImplementedInterfaces:=nil;
  5284. if df_copied_def in defoptions then
  5285. begin
  5286. ppufile.getderef(cloneddefderef);
  5287. ppuload_platform(ppufile);
  5288. end
  5289. else
  5290. begin
  5291. ppuload_platform(ppufile);
  5292. tObjectSymtable(symtable).ppuload(ppufile);
  5293. end;
  5294. { handles the predefined class tobject }
  5295. { the last TOBJECT which is loaded gets }
  5296. { it ! }
  5297. if (childof=nil) and
  5298. (objecttype in [odt_class,odt_javaclass]) and
  5299. (objname^='TOBJECT') then
  5300. class_tobject:=self;
  5301. if (childof=nil) and
  5302. (objecttype=odt_interfacecom) then
  5303. if (objname^='IUNKNOWN') then
  5304. interface_iunknown:=self
  5305. else
  5306. if (objname^='IDISPATCH') then
  5307. interface_idispatch:=self;
  5308. if (childof=nil) and
  5309. (objecttype=odt_objcclass) and
  5310. (objname^='PROTOCOL') then
  5311. objc_protocoltype:=self;
  5312. if (objecttype=odt_javaclass) and
  5313. not(oo_is_formal in objectoptions) then
  5314. begin
  5315. if (objname^='JLOBJECT') then
  5316. java_jlobject:=self
  5317. else if (objname^='JLTHROWABLE') then
  5318. java_jlthrowable:=self
  5319. else if (objname^='FPCBASERECORDTYPE') then
  5320. java_fpcbaserecordtype:=self
  5321. else if (objname^='JLSTRING') then
  5322. java_jlstring:=self
  5323. else if (objname^='ANSISTRINGCLASS') then
  5324. java_ansistring:=self
  5325. else if (objname^='SHORTSTRINGCLASS') then
  5326. java_shortstring:=self
  5327. else if (objname^='JLENUM') then
  5328. java_jlenum:=self
  5329. else if (objname^='JUENUMSET') then
  5330. java_juenumset:=self
  5331. else if (objname^='FPCBITSET') then
  5332. java_jubitset:=self
  5333. else if (objname^='FPCBASEPROCVARTYPE') then
  5334. java_procvarbase:=self;
  5335. end;
  5336. writing_class_record_dbginfo:=false;
  5337. end;
  5338. destructor tobjectdef.destroy;
  5339. begin
  5340. if assigned(symtable) then
  5341. begin
  5342. symtable.free;
  5343. symtable:=nil;
  5344. end;
  5345. stringdispose(objextname);
  5346. stringdispose(iidstr);
  5347. if assigned(ImplementedInterfaces) then
  5348. begin
  5349. ImplementedInterfaces.free;
  5350. ImplementedInterfaces:=nil;
  5351. end;
  5352. if assigned(iidguid) then
  5353. begin
  5354. dispose(iidguid);
  5355. iidguid:=nil;
  5356. end;
  5357. if assigned(vmtentries) then
  5358. begin
  5359. resetvmtentries;
  5360. vmtentries.free;
  5361. vmtentries:=nil;
  5362. end;
  5363. if assigned(vmcallstaticinfo) then
  5364. begin
  5365. freemem(vmcallstaticinfo);
  5366. vmcallstaticinfo:=nil;
  5367. end;
  5368. inherited destroy;
  5369. end;
  5370. function tobjectdef.getcopy : tstoreddef;
  5371. var
  5372. i : longint;
  5373. begin
  5374. result:=cobjectdef.create(objecttype,objrealname^,childof);
  5375. { the constructor allocates a symtable which we release to avoid memory leaks }
  5376. tobjectdef(result).symtable.free;
  5377. tobjectdef(result).symtable:=symtable.getcopy;
  5378. if assigned(objextname) then
  5379. tobjectdef(result).objextname:=stringdup(objextname^);
  5380. if assigned(import_lib) then
  5381. tobjectdef(result).import_lib:=stringdup(import_lib^);
  5382. tobjectdef(result).objectoptions:=objectoptions;
  5383. include(tobjectdef(result).defoptions,df_copied_def);
  5384. tobjectdef(result).extendeddef:=extendeddef;
  5385. if assigned(tcinitcode) then
  5386. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  5387. tobjectdef(result).vmt_offset:=vmt_offset;
  5388. if assigned(iidguid) then
  5389. begin
  5390. new(tobjectdef(result).iidguid);
  5391. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  5392. end;
  5393. if assigned(iidstr) then
  5394. tobjectdef(result).iidstr:=stringdup(iidstr^);
  5395. tobjectdef(result).abstractcnt:=abstractcnt;
  5396. if assigned(ImplementedInterfaces) then
  5397. begin
  5398. for i:=0 to ImplementedInterfaces.count-1 do
  5399. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  5400. end;
  5401. if assigned(vmtentries) then
  5402. begin
  5403. tobjectdef(result).vmtentries:=TFPList.Create;
  5404. tobjectdef(result).copyvmtentries(self);
  5405. end;
  5406. end;
  5407. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  5408. var
  5409. i : longint;
  5410. vmtentry : pvmtentry;
  5411. ImplIntf : TImplementedInterface;
  5412. old_do_indirect_crc: boolean;
  5413. begin
  5414. { if class1 in unit A changes, and class2 in unit B inherits from it
  5415. (so unit B uses unit A), then unit B with class2 will be recompiled.
  5416. However, if there is also a class3 in unit C that only depends on
  5417. unit B, then unit C will not be recompiled because nothing changed
  5418. to the interface of unit B. Nevertheless, unit C can indirectly
  5419. depend on unit A via derefs, and these must be updated -> the
  5420. indirect crc keeps track of such changes. }
  5421. old_do_indirect_crc:=ppufile.do_indirect_crc;
  5422. ppufile.do_indirect_crc:=true;
  5423. inherited ppuwrite(ppufile);
  5424. ppufile.putbyte(byte(objecttype));
  5425. if assigned(objextname) then
  5426. ppufile.putstring(objextname^)
  5427. else
  5428. ppufile.putstring('');
  5429. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  5430. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  5431. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  5432. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  5433. ppufile.putlongint(vmt_offset);
  5434. ppufile.putderef(childofderef);
  5435. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5436. begin
  5437. ppufile.putguid(iidguid^);
  5438. ppufile.putstring(iidstr^);
  5439. end;
  5440. ppufile.putlongint(abstractcnt);
  5441. if objecttype=odt_helper then
  5442. ppufile.putderef(extendeddefderef);
  5443. ppufile.putlongint(vmtentries.count);
  5444. for i:=0 to vmtentries.count-1 do
  5445. begin
  5446. vmtentry:=pvmtentry(vmtentries[i]);
  5447. ppufile.putderef(vmtentry^.procdefderef);
  5448. ppufile.putbyte(byte(vmtentry^.visibility));
  5449. end;
  5450. if assigned(ImplementedInterfaces) then
  5451. begin
  5452. ppufile.putlongint(ImplementedInterfaces.Count);
  5453. for i:=0 to ImplementedInterfaces.Count-1 do
  5454. begin
  5455. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5456. ppufile.putderef(ImplIntf.intfdefderef);
  5457. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  5458. ppufile.putlongint(ImplIntf.Ioffset);
  5459. ppufile.putbyte(byte(ImplIntf.IType));
  5460. end;
  5461. end;
  5462. if df_copied_def in defoptions then
  5463. ppufile.putderef(cloneddefderef);
  5464. writeentry(ppufile,ibobjectdef);
  5465. if not(df_copied_def in defoptions) then
  5466. tObjectSymtable(symtable).ppuwrite(ppufile);
  5467. ppufile.do_indirect_crc:=old_do_indirect_crc;
  5468. end;
  5469. function tobjectdef.GetTypeName:string;
  5470. begin
  5471. { in this case we will go in endless recursion, because then }
  5472. { there is no tsym associated yet with the def. It can occur }
  5473. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  5474. { instead of the actual type name }
  5475. if not assigned(typesym) then
  5476. result:='<Currently Parsed Class>'
  5477. else
  5478. result:=typesymbolprettyname;
  5479. end;
  5480. procedure tobjectdef.buildderef;
  5481. var
  5482. i : longint;
  5483. vmtentry : pvmtentry;
  5484. begin
  5485. inherited buildderef;
  5486. childofderef.build(childof);
  5487. if df_copied_def in defoptions then
  5488. cloneddefderef.build(symtable.defowner)
  5489. else
  5490. tstoredsymtable(symtable).buildderef;
  5491. if objecttype=odt_helper then
  5492. extendeddefderef.build(extendeddef);
  5493. for i:=0 to vmtentries.count-1 do
  5494. begin
  5495. vmtentry:=pvmtentry(vmtentries[i]);
  5496. vmtentry^.procdefderef.build(vmtentry^.procdef);
  5497. end;
  5498. if assigned(ImplementedInterfaces) then
  5499. begin
  5500. for i:=0 to ImplementedInterfaces.count-1 do
  5501. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  5502. end;
  5503. end;
  5504. procedure tobjectdef.deref;
  5505. var
  5506. i : longint;
  5507. vmtentry : pvmtentry;
  5508. begin
  5509. inherited deref;
  5510. childof:=tobjectdef(childofderef.resolve);
  5511. if df_copied_def in defoptions then
  5512. begin
  5513. cloneddef:=tobjectdef(cloneddefderef.resolve);
  5514. symtable:=cloneddef.symtable.getcopy;
  5515. end
  5516. else
  5517. tstoredsymtable(symtable).deref;
  5518. if objecttype=odt_helper then
  5519. extendeddef:=tdef(extendeddefderef.resolve);
  5520. for i:=0 to vmtentries.count-1 do
  5521. begin
  5522. vmtentry:=pvmtentry(vmtentries[i]);
  5523. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  5524. end;
  5525. if assigned(ImplementedInterfaces) then
  5526. begin
  5527. for i:=0 to ImplementedInterfaces.count-1 do
  5528. TImplementedInterface(ImplementedInterfaces[i]).deref;
  5529. end;
  5530. end;
  5531. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  5532. var
  5533. pd: tprocdef absolute def;
  5534. st: tsymtable;
  5535. psym: tsym;
  5536. nname: TIDString;
  5537. begin
  5538. if (tdef(def).typ<>procdef) then
  5539. exit;
  5540. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  5541. owner = symtable in which objcclassdef is defined
  5542. }
  5543. st:=pd.owner.defowner.owner;
  5544. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  5545. { check for an existing procsym with our special name }
  5546. psym:=tsym(st.find(nname));
  5547. if not assigned(psym) then
  5548. begin
  5549. psym:=cprocsym.create(nname);
  5550. { avoid warning about this symbol being unused }
  5551. psym.IncRefCount;
  5552. { don't check for duplicates:
  5553. a) we checked above
  5554. b) in case we are in the implementation section of a unit, this
  5555. will also check for this symbol in the interface section
  5556. (since you normally cannot have symbols with the same name
  5557. both interface and implementation), and it's possible to
  5558. have class helpers for the same class in the interface and
  5559. in the implementation, and they cannot be merged since only
  5560. the once in the interface must be saved to the ppu/visible
  5561. from other units }
  5562. st.insert(psym,false);
  5563. end
  5564. else if (psym.typ<>procsym) then
  5565. internalerror(2009111501);
  5566. { add ourselves to this special procsym }
  5567. tprocsym(psym).procdeflist.add(def);
  5568. end;
  5569. procedure tobjectdef.derefimpl;
  5570. begin
  5571. inherited derefimpl;
  5572. { the procdefs are not owned by the class helper procsyms, so they
  5573. are not stored/restored either -> re-add them here }
  5574. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  5575. (oo_is_classhelper in objectoptions) then
  5576. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  5577. end;
  5578. procedure tobjectdef.resetvmtentries;
  5579. var
  5580. i : longint;
  5581. begin
  5582. for i:=0 to vmtentries.Count-1 do
  5583. Dispose(pvmtentry(vmtentries[i]));
  5584. vmtentries.clear;
  5585. end;
  5586. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  5587. var
  5588. i : longint;
  5589. vmtentry : pvmtentry;
  5590. begin
  5591. resetvmtentries;
  5592. vmtentries.count:=objdef.vmtentries.count;
  5593. for i:=0 to objdef.vmtentries.count-1 do
  5594. begin
  5595. new(vmtentry);
  5596. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  5597. vmtentries[i]:=vmtentry;
  5598. end;
  5599. end;
  5600. function tobjectdef.getparentdef:tdef;
  5601. begin
  5602. { TODO: Remove getparentdef hack}
  5603. { With 2 forward declared classes with the child class before the
  5604. parent class the child class is written earlier to the ppu. Leaving it
  5605. possible to have a reference to the parent class for property overriding,
  5606. but the parent class still has the childof not resolved yet (PFV) }
  5607. if childof=nil then
  5608. childof:=tobjectdef(childofderef.resolve);
  5609. result:=childof;
  5610. end;
  5611. procedure tobjectdef.prepareguid;
  5612. begin
  5613. { set up guid }
  5614. if not assigned(iidguid) then
  5615. begin
  5616. new(iidguid);
  5617. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  5618. end;
  5619. { setup iidstring }
  5620. if not assigned(iidstr) then
  5621. iidstr:=stringdup(''); { default is empty string }
  5622. end;
  5623. procedure tobjectdef.set_parent( c : tobjectdef);
  5624. begin
  5625. if assigned(childof) then
  5626. exit;
  5627. childof:=c;
  5628. if not assigned(c) then
  5629. exit;
  5630. { inherit options and status }
  5631. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  5632. { initially has the same number of abstract methods as the parent }
  5633. abstractcnt:=c.abstractcnt;
  5634. { add the data of the anchestor class/object }
  5635. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  5636. begin
  5637. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  5638. { inherit recordalignment }
  5639. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  5640. { if both the parent and this record use C-alignment, also inherit
  5641. the current field alignment }
  5642. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  5643. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  5644. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  5645. { the padding is not inherited for Objective-C classes (maybe not
  5646. for cppclass either?) }
  5647. if objecttype=odt_objcclass then
  5648. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  5649. if (oo_has_vmt in objectoptions) and
  5650. (oo_has_vmt in c.objectoptions) then
  5651. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  5652. { if parent has a vmt field then the offset is the same for the child PM }
  5653. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  5654. begin
  5655. vmt_offset:=c.vmt_offset;
  5656. include(objectoptions,oo_has_vmt);
  5657. end;
  5658. end;
  5659. end;
  5660. procedure tobjectdef.insertvmt;
  5661. var
  5662. vs: tfieldvarsym;
  5663. begin
  5664. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5665. exit;
  5666. if (oo_has_vmt in objectoptions) then
  5667. internalerror(12345)
  5668. else
  5669. begin
  5670. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  5671. tObjectSymtable(symtable).fieldalignment);
  5672. if (tf_requires_proper_alignment in target_info.flags) then
  5673. begin
  5674. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  5675. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  5676. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  5677. end;
  5678. vs:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  5679. hidesym(vs);
  5680. tObjectSymtable(symtable).insert(vs);
  5681. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  5682. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  5683. vmt_offset:=vs.fieldoffset
  5684. else
  5685. vmt_offset:=vs.fieldoffset div 8;
  5686. include(objectoptions,oo_has_vmt);
  5687. end;
  5688. end;
  5689. procedure tobjectdef.check_forwards;
  5690. begin
  5691. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  5692. inherited;
  5693. if (oo_is_forward in objectoptions) then
  5694. begin
  5695. { ok, in future, the forward can be resolved }
  5696. Message1(sym_e_class_forward_not_resolved,objrealname^);
  5697. exclude(objectoptions,oo_is_forward);
  5698. end;
  5699. end;
  5700. function tobjectdef.find_destructor: tprocdef;
  5701. var
  5702. objdef: tobjectdef;
  5703. begin
  5704. objdef:=self;
  5705. while assigned(objdef) do
  5706. begin
  5707. result:=objdef.find_procdef_bytype(potype_destructor);
  5708. if assigned(result) then
  5709. exit;
  5710. objdef:=objdef.childof;
  5711. end;
  5712. result:=nil;
  5713. end;
  5714. function tobjectdef.implements_any_interfaces: boolean;
  5715. begin
  5716. result := (ImplementedInterfaces.Count > 0) or
  5717. (assigned(childof) and childof.implements_any_interfaces);
  5718. end;
  5719. function tobjectdef.size : asizeint;
  5720. begin
  5721. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5722. result:=voidpointertype.size
  5723. else
  5724. result:=tObjectSymtable(symtable).datasize;
  5725. end;
  5726. function tobjectdef.alignment:shortint;
  5727. begin
  5728. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5729. alignment:=voidpointertype.size
  5730. else
  5731. alignment:=tObjectSymtable(symtable).recordalignment;
  5732. end;
  5733. function tobjectdef.vmtmethodoffset(index:longint):longint;
  5734. begin
  5735. { for offset of methods for classes, see rtl/inc/objpash.inc }
  5736. case objecttype of
  5737. odt_class:
  5738. { the +2*sizeof(pint) is size and -size }
  5739. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  5740. odt_helper,
  5741. odt_objcclass,
  5742. odt_objcprotocol:
  5743. vmtmethodoffset:=0;
  5744. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  5745. vmtmethodoffset:=index*voidcodepointertype.size;
  5746. odt_javaclass,
  5747. odt_interfacejava:
  5748. { invalid }
  5749. vmtmethodoffset:=-1;
  5750. else
  5751. { the +2*sizeof(pint) is size and -size }
  5752. {$ifdef WITHDMT}
  5753. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  5754. {$else WITHDMT}
  5755. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  5756. {$endif WITHDMT}
  5757. end;
  5758. end;
  5759. function tobjectdef.vmt_mangledname : TSymStr;
  5760. begin
  5761. if not(oo_has_vmt in objectoptions) then
  5762. Message1(parser_n_object_has_no_vmt,objrealname^);
  5763. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  5764. end;
  5765. function tobjectdef.needs_inittable : boolean;
  5766. var
  5767. hp : tobjectdef;
  5768. begin
  5769. case objecttype of
  5770. odt_helper,
  5771. odt_class :
  5772. needs_inittable:=false;
  5773. odt_dispinterface,
  5774. odt_interfacecom:
  5775. needs_inittable:=true;
  5776. odt_interfacecorba:
  5777. begin
  5778. hp:=childof;
  5779. while assigned(hp) do
  5780. begin
  5781. if hp=interface_iunknown then
  5782. begin
  5783. needs_inittable:=true;
  5784. exit;
  5785. end;
  5786. hp:=hp.childof;
  5787. end;
  5788. needs_inittable:=false;
  5789. end;
  5790. odt_object:
  5791. needs_inittable:=
  5792. tObjectSymtable(symtable).needs_init_final or
  5793. (assigned(childof) and
  5794. childof.needs_inittable);
  5795. odt_cppclass,
  5796. odt_objcclass,
  5797. odt_objcprotocol,
  5798. odt_javaclass,
  5799. odt_interfacejava:
  5800. needs_inittable:=false;
  5801. else
  5802. internalerror(200108267);
  5803. end;
  5804. end;
  5805. function tobjectdef.needs_separate_initrtti : boolean;
  5806. begin
  5807. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5808. end;
  5809. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  5810. begin
  5811. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  5812. result:=inherited rtti_mangledname(rt)
  5813. else
  5814. begin
  5815. { necessary in case of a dynamic array of nsobject, or
  5816. if an nsobject field appears in a record that needs
  5817. init/finalisation }
  5818. if rt=initrtti then
  5819. begin
  5820. result:=voidpointertype.rtti_mangledname(rt);
  5821. exit;
  5822. end;
  5823. if not(target_info.system in systems_objc_nfabi) then
  5824. begin
  5825. result:=target_asm.labelprefix;
  5826. case objecttype of
  5827. odt_objcclass:
  5828. begin
  5829. case rt of
  5830. objcclassrtti:
  5831. if not(oo_is_classhelper in objectoptions) then
  5832. result:=result+'_OBJC_CLASS_'
  5833. else
  5834. result:=result+'_OBJC_CATEGORY_';
  5835. objcmetartti:
  5836. if not(oo_is_classhelper in objectoptions) then
  5837. result:=result+'_OBJC_METACLASS_'
  5838. else
  5839. internalerror(2009111511);
  5840. else
  5841. internalerror(2009092302);
  5842. end;
  5843. end;
  5844. odt_objcprotocol:
  5845. result:=result+'_OBJC_PROTOCOL_';
  5846. end;
  5847. end
  5848. else
  5849. begin
  5850. case objecttype of
  5851. odt_objcclass:
  5852. begin
  5853. if (oo_is_classhelper in objectoptions) and
  5854. (rt<>objcclassrtti) then
  5855. internalerror(2009111512);
  5856. case rt of
  5857. objcclassrtti:
  5858. if not(oo_is_classhelper in objectoptions) then
  5859. result:='_OBJC_CLASS_$_'
  5860. else
  5861. result:='_OBJC_$_CATEGORY_';
  5862. objcmetartti:
  5863. result:='_OBJC_METACLASS_$_';
  5864. objcclassrortti:
  5865. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  5866. objcmetarortti:
  5867. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  5868. else
  5869. internalerror(2009092303);
  5870. end;
  5871. end;
  5872. odt_objcprotocol:
  5873. begin
  5874. result:=lower(target_asm.labelprefix);
  5875. case rt of
  5876. objcclassrtti:
  5877. result:=result+'_OBJC_PROTOCOL_$_';
  5878. objcmetartti:
  5879. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  5880. else
  5881. internalerror(2009092501);
  5882. end;
  5883. end;
  5884. else
  5885. internalerror(2013113005);
  5886. end;
  5887. end;
  5888. result:=result+objextname^;
  5889. end;
  5890. end;
  5891. function tobjectdef.members_need_inittable : boolean;
  5892. begin
  5893. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  5894. end;
  5895. function tobjectdef.is_publishable : boolean;
  5896. begin
  5897. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  5898. end;
  5899. function tobjectdef.get_next_dispid: longint;
  5900. begin
  5901. inc(fcurrent_dispid);
  5902. result:=fcurrent_dispid;
  5903. end;
  5904. function tobjectdef.search_enumerator_get: tprocdef;
  5905. begin
  5906. result:=inherited;
  5907. if not assigned(result) and assigned(childof) then
  5908. result:=childof.search_enumerator_get;
  5909. end;
  5910. function tobjectdef.search_enumerator_move: tprocdef;
  5911. begin
  5912. result:=inherited;
  5913. if not assigned(result) and assigned(childof) then
  5914. result:=childof.search_enumerator_move;
  5915. end;
  5916. function tobjectdef.search_enumerator_current: tsym;
  5917. begin
  5918. result:=inherited;
  5919. if not assigned(result) and assigned(childof) then
  5920. result:=childof.search_enumerator_current;
  5921. end;
  5922. procedure tobjectdef.register_created_classref_type;
  5923. begin
  5924. if not classref_created_in_current_module then
  5925. begin
  5926. classref_created_in_current_module:=true;
  5927. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  5928. end;
  5929. end;
  5930. procedure tobjectdef.register_created_object_type;
  5931. begin
  5932. if not created_in_current_module then
  5933. begin
  5934. created_in_current_module:=true;
  5935. current_module.wpoinfo.addcreatedobjtype(self);
  5936. end;
  5937. end;
  5938. procedure tobjectdef.register_maybe_created_object_type;
  5939. begin
  5940. { if we know it has been created for sure, no need
  5941. to also record that it maybe can be created in
  5942. this module
  5943. }
  5944. if not (created_in_current_module) and
  5945. not (maybe_created_in_current_module) then
  5946. begin
  5947. maybe_created_in_current_module:=true;
  5948. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  5949. end;
  5950. end;
  5951. procedure tobjectdef.register_vmt_call(index: longint);
  5952. begin
  5953. if (is_object(self) or is_class(self)) then
  5954. current_module.wpoinfo.addcalledvmtentry(self,index);
  5955. end;
  5956. procedure check_and_finish_msg(data: tobject; arg: pointer);
  5957. var
  5958. def: tdef absolute data;
  5959. pd: tprocdef absolute data;
  5960. i,
  5961. paracount: longint;
  5962. begin
  5963. if (def.typ=procdef) then
  5964. begin
  5965. { add all messages also under a dummy name to the symtable in
  5966. which the objcclass/protocol/category is declared, so they can
  5967. be called via id.<name>
  5968. }
  5969. create_class_helper_for_procdef(pd,nil);
  5970. { we have to wait until now to set the mangled name because it
  5971. depends on the (possibly external) class name, which is defined
  5972. at the very end. }
  5973. if not(po_msgstr in pd.procoptions) then
  5974. begin
  5975. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  5976. { recover to avoid internalerror later on }
  5977. include(pd.procoptions,po_msgstr);
  5978. pd.messageinf.str:=stringdup('MissingDeclaration');
  5979. end;
  5980. { Mangled name is already set in case this is a copy of
  5981. another type. }
  5982. if not(po_has_mangledname in pd.procoptions) then
  5983. begin
  5984. { check whether the number of formal parameters is correct,
  5985. and whether they have valid Objective-C types }
  5986. paracount:=0;
  5987. for i:=1 to length(pd.messageinf.str^) do
  5988. if pd.messageinf.str^[i]=':' then
  5989. inc(paracount);
  5990. for i:=0 to pd.paras.count-1 do
  5991. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  5992. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  5993. dec(paracount);
  5994. if (paracount<>0) then
  5995. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  5996. pd.setmangledname(pd.objcmangledname);
  5997. end
  5998. else
  5999. { all checks already done }
  6000. exit;
  6001. if not(oo_is_external in pd.struct.objectoptions) then
  6002. begin
  6003. if (po_varargs in pd.procoptions) then
  6004. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  6005. else
  6006. begin
  6007. { check for "array of const" parameters }
  6008. for i:=0 to pd.parast.symlist.count-1 do
  6009. begin
  6010. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  6011. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  6012. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  6013. end;
  6014. end;
  6015. end;
  6016. end;
  6017. end;
  6018. procedure mark_private_fields_used(data: tobject; arg: pointer);
  6019. var
  6020. sym: tsym absolute data;
  6021. begin
  6022. if (sym.typ=fieldvarsym) and
  6023. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  6024. sym.IncRefCount;
  6025. end;
  6026. procedure tobjectdef.finish_objc_data;
  6027. begin
  6028. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  6029. if (oo_is_external in objectoptions) then
  6030. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  6031. end;
  6032. procedure verify_objc_vardef(data: tobject; arg: pointer);
  6033. var
  6034. sym: tabstractvarsym absolute data;
  6035. res: pboolean absolute arg;
  6036. founderrordef: tdef;
  6037. begin
  6038. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  6039. exit;
  6040. if (sym.typ=paravarsym) and
  6041. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  6042. is_array_of_const(tparavarsym(sym).vardef)) then
  6043. exit;
  6044. if not objcchecktype(sym.vardef,founderrordef) then
  6045. begin
  6046. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6047. res^:=false;
  6048. end;
  6049. end;
  6050. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  6051. var
  6052. def: tdef absolute data;
  6053. res: pboolean absolute arg;
  6054. founderrordef: tdef;
  6055. begin
  6056. if (def.typ<>procdef) then
  6057. exit;
  6058. { check parameter types for validity }
  6059. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  6060. { check the result type for validity }
  6061. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  6062. begin
  6063. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6064. res^:=false;
  6065. end;
  6066. end;
  6067. function tobjectdef.check_objc_types: boolean;
  6068. begin
  6069. { done in separate step from finish_objc_data, because when
  6070. finish_objc_data is called, not all forwarddefs have been resolved
  6071. yet and we need to know all types here }
  6072. result:=true;
  6073. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  6074. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  6075. end;
  6076. procedure do_cpp_import_info(data: tobject; arg: pointer);
  6077. var
  6078. def: tdef absolute data;
  6079. pd: tprocdef absolute data;
  6080. begin
  6081. if (def.typ=procdef) then
  6082. begin
  6083. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  6084. if (oo_is_external in pd.struct.objectoptions) then
  6085. begin
  6086. { copied from psub.read_proc }
  6087. if assigned(tobjectdef(pd.struct).import_lib) then
  6088. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  6089. else
  6090. begin
  6091. { add import name to external list for DLL scanning }
  6092. if tf_has_dllscanner in target_info.flags then
  6093. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  6094. end;
  6095. end;
  6096. end;
  6097. end;
  6098. procedure tobjectdef.finish_cpp_data;
  6099. begin
  6100. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  6101. end;
  6102. {****************************************************************************
  6103. TImplementedInterface
  6104. ****************************************************************************}
  6105. function TImplementedInterface.GetIOffset: longint;
  6106. begin
  6107. if (fIOffset=-1) and
  6108. (IType in [etFieldValue,etFieldValueClass]) then
  6109. result:=tfieldvarsym(ImplementsField).fieldoffset
  6110. else
  6111. result:=fIOffset;
  6112. end;
  6113. constructor TImplementedInterface.create(aintf: tobjectdef);
  6114. begin
  6115. inherited create;
  6116. intfdef:=aintf;
  6117. IOffset:=-1;
  6118. IType:=etStandard;
  6119. NameMappings:=nil;
  6120. procdefs:=nil;
  6121. end;
  6122. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  6123. begin
  6124. inherited create;
  6125. intfdef:=nil;
  6126. intfdefderef:=intfd;
  6127. ImplementsGetterDeref:=getterd;
  6128. IOffset:=-1;
  6129. IType:=etStandard;
  6130. NameMappings:=nil;
  6131. procdefs:=nil;
  6132. end;
  6133. destructor TImplementedInterface.destroy;
  6134. var
  6135. i : longint;
  6136. mappedname : pshortstring;
  6137. begin
  6138. if assigned(NameMappings) then
  6139. begin
  6140. for i:=0 to NameMappings.Count-1 do
  6141. begin
  6142. mappedname:=pshortstring(NameMappings[i]);
  6143. stringdispose(mappedname);
  6144. end;
  6145. NameMappings.free;
  6146. NameMappings:=nil;
  6147. end;
  6148. if assigned(procdefs) then
  6149. begin
  6150. procdefs.free;
  6151. procdefs:=nil;
  6152. end;
  6153. inherited destroy;
  6154. end;
  6155. procedure TImplementedInterface.buildderef;
  6156. begin
  6157. intfdefderef.build(intfdef);
  6158. ImplementsGetterDeref.build(ImplementsGetter);
  6159. end;
  6160. procedure TImplementedInterface.deref;
  6161. begin
  6162. intfdef:=tobjectdef(intfdefderef.resolve);
  6163. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6164. end;
  6165. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6166. begin
  6167. if not assigned(NameMappings) then
  6168. NameMappings:=TFPHashList.Create;
  6169. NameMappings.Add(origname,stringdup(newname));
  6170. end;
  6171. function TImplementedInterface.GetMapping(const origname: string):string;
  6172. var
  6173. mappedname : pshortstring;
  6174. begin
  6175. result:='';
  6176. if not assigned(NameMappings) then
  6177. exit;
  6178. mappedname:=PShortstring(NameMappings.Find(origname));
  6179. if assigned(mappedname) then
  6180. result:=mappedname^;
  6181. end;
  6182. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6183. begin
  6184. if not assigned(procdefs) then
  6185. procdefs:=TFPObjectList.Create(false);
  6186. { duplicate entries must be stored, because multiple }
  6187. { interfaces can declare methods with the same name }
  6188. { and all of these get their own VMT entry }
  6189. procdefs.Add(pd);
  6190. end;
  6191. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  6192. var
  6193. i : longint;
  6194. begin
  6195. result:=false;
  6196. { interfaces being implemented through delegation are not mergable (FK) }
  6197. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  6198. exit;
  6199. weight:=0;
  6200. { empty interface is mergeable }
  6201. if ProcDefs.Count=0 then
  6202. begin
  6203. result:=true;
  6204. exit;
  6205. end;
  6206. { The interface to merge must at least the number of
  6207. procedures of this interface }
  6208. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  6209. exit;
  6210. for i:=0 to ProcDefs.Count-1 do
  6211. begin
  6212. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  6213. exit;
  6214. end;
  6215. weight:=ProcDefs.Count;
  6216. result:=true;
  6217. end;
  6218. function TImplementedInterface.getcopy:TImplementedInterface;
  6219. begin
  6220. Result:=TImplementedInterface.Create(nil);
  6221. { 1) the procdefs list will be freed once for each copy
  6222. 2) since the procdefs list owns its elements, those will also be freed for each copy
  6223. 3) idem for the name mappings
  6224. }
  6225. { warning: this is completely wrong on so many levels...
  6226. Move(pointer(self)^,pointer(result)^,InstanceSize);
  6227. We need to make clean copies of the different fields
  6228. this is not implemented yet, and thus we generate an internal
  6229. error instead PM 2011-06-14 }
  6230. internalerror(2011061401);
  6231. end;
  6232. {****************************************************************************
  6233. TFORWARDDEF
  6234. ****************************************************************************}
  6235. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  6236. begin
  6237. inherited create(forwarddef);
  6238. tosymname:=stringdup(s);
  6239. forwardpos:=pos;
  6240. end;
  6241. function tforwarddef.GetTypeName:string;
  6242. begin
  6243. GetTypeName:='unresolved forward to '+tosymname^;
  6244. end;
  6245. destructor tforwarddef.destroy;
  6246. begin
  6247. stringdispose(tosymname);
  6248. inherited destroy;
  6249. end;
  6250. function tforwarddef.getcopy:tstoreddef;
  6251. begin
  6252. result:=cforwarddef.create(tosymname^, forwardpos);
  6253. end;
  6254. {****************************************************************************
  6255. TUNDEFINEDDEF
  6256. ****************************************************************************}
  6257. constructor tundefineddef.create;
  6258. begin
  6259. inherited create(undefineddef);
  6260. end;
  6261. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  6262. begin
  6263. inherited ppuload(undefineddef,ppufile);
  6264. ppuload_platform(ppufile);
  6265. end;
  6266. function tundefineddef.GetTypeName:string;
  6267. begin
  6268. GetTypeName:='<undefined type>';
  6269. end;
  6270. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  6271. begin
  6272. inherited ppuwrite(ppufile);
  6273. writeentry(ppufile,ibundefineddef);
  6274. end;
  6275. {****************************************************************************
  6276. TERRORDEF
  6277. ****************************************************************************}
  6278. constructor terrordef.create;
  6279. begin
  6280. inherited create(errordef);
  6281. { prevent consecutive faults }
  6282. savesize:=1;
  6283. end;
  6284. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  6285. begin
  6286. { Can't write errordefs to ppu }
  6287. internalerror(200411063);
  6288. end;
  6289. function terrordef.GetTypeName:string;
  6290. begin
  6291. GetTypeName:='<erroneous type>';
  6292. end;
  6293. function terrordef.getmangledparaname:TSymStr;
  6294. begin
  6295. getmangledparaname:='error';
  6296. end;
  6297. {****************************************************************************
  6298. Definition Helpers
  6299. ****************************************************************************}
  6300. function is_interfacecom(def: tdef): boolean;
  6301. begin
  6302. is_interfacecom:=
  6303. assigned(def) and
  6304. (def.typ=objectdef) and
  6305. (tobjectdef(def).objecttype=odt_interfacecom);
  6306. end;
  6307. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  6308. begin
  6309. is_interfacecom_or_dispinterface:=
  6310. assigned(def) and
  6311. (def.typ=objectdef) and
  6312. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  6313. end;
  6314. function is_any_interface_kind(def: tdef): boolean;
  6315. begin
  6316. result:=
  6317. assigned(def) and
  6318. (def.typ=objectdef) and
  6319. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  6320. is_objccategory(def));
  6321. end;
  6322. function is_interfacecorba(def: tdef): boolean;
  6323. begin
  6324. is_interfacecorba:=
  6325. assigned(def) and
  6326. (def.typ=objectdef) and
  6327. (tobjectdef(def).objecttype=odt_interfacecorba);
  6328. end;
  6329. function is_interface(def: tdef): boolean;
  6330. begin
  6331. is_interface:=
  6332. assigned(def) and
  6333. (def.typ=objectdef) and
  6334. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  6335. end;
  6336. function is_dispinterface(def: tdef): boolean;
  6337. begin
  6338. result:=
  6339. assigned(def) and
  6340. (def.typ=objectdef) and
  6341. (tobjectdef(def).objecttype=odt_dispinterface);
  6342. end;
  6343. function is_class(def: tdef): boolean;
  6344. begin
  6345. is_class:=
  6346. assigned(def) and
  6347. (def.typ=objectdef) and
  6348. (tobjectdef(def).objecttype=odt_class);
  6349. end;
  6350. function is_object(def: tdef): boolean;
  6351. begin
  6352. is_object:=
  6353. assigned(def) and
  6354. (def.typ=objectdef) and
  6355. (tobjectdef(def).objecttype=odt_object);
  6356. end;
  6357. function is_cppclass(def: tdef): boolean;
  6358. begin
  6359. is_cppclass:=
  6360. assigned(def) and
  6361. (def.typ=objectdef) and
  6362. (tobjectdef(def).objecttype=odt_cppclass);
  6363. end;
  6364. function is_objcclass(def: tdef): boolean;
  6365. begin
  6366. is_objcclass:=
  6367. assigned(def) and
  6368. (def.typ=objectdef) and
  6369. (tobjectdef(def).objecttype=odt_objcclass);
  6370. end;
  6371. function is_objectpascal_helper(def: tdef): boolean;
  6372. begin
  6373. result:=
  6374. assigned(def) and
  6375. (def.typ=objectdef) and
  6376. (tobjectdef(def).objecttype=odt_helper);
  6377. end;
  6378. function is_objcclassref(def: tdef): boolean;
  6379. begin
  6380. is_objcclassref:=
  6381. assigned(def) and
  6382. (def.typ=classrefdef) and
  6383. is_objcclass(tclassrefdef(def).pointeddef);
  6384. end;
  6385. function is_objcprotocol(def: tdef): boolean;
  6386. begin
  6387. result:=
  6388. assigned(def) and
  6389. (def.typ=objectdef) and
  6390. (tobjectdef(def).objecttype=odt_objcprotocol);
  6391. end;
  6392. function is_objccategory(def: tdef): boolean;
  6393. begin
  6394. result:=
  6395. assigned(def) and
  6396. (def.typ=objectdef) and
  6397. { if used as a forward type }
  6398. ((tobjectdef(def).objecttype=odt_objccategory) or
  6399. { if used as after it has been resolved }
  6400. ((tobjectdef(def).objecttype=odt_objcclass) and
  6401. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6402. end;
  6403. function is_objc_class_or_protocol(def: tdef): boolean;
  6404. begin
  6405. result:=
  6406. assigned(def) and
  6407. (def.typ=objectdef) and
  6408. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  6409. end;
  6410. function is_objc_protocol_or_category(def: tdef): boolean;
  6411. begin
  6412. result:=
  6413. assigned(def) and
  6414. (def.typ=objectdef) and
  6415. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  6416. ((tobjectdef(def).objecttype = odt_objcclass) and
  6417. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6418. end;
  6419. function is_classhelper(def: tdef): boolean;
  6420. begin
  6421. result:=
  6422. is_objectpascal_helper(def) or
  6423. is_objccategory(def);
  6424. end;
  6425. function is_class_or_interface(def: tdef): boolean;
  6426. begin
  6427. result:=
  6428. assigned(def) and
  6429. (def.typ=objectdef) and
  6430. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  6431. end;
  6432. function is_class_or_interface_or_objc(def: tdef): boolean;
  6433. begin
  6434. result:=
  6435. assigned(def) and
  6436. (def.typ=objectdef) and
  6437. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  6438. end;
  6439. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  6440. begin
  6441. result:=
  6442. assigned(def) and
  6443. (def.typ=objectdef) and
  6444. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6445. end;
  6446. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  6447. begin
  6448. result:=
  6449. assigned(def) and
  6450. (def.typ=objectdef) and
  6451. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6452. end;
  6453. function is_class_or_interface_or_object(def: tdef): boolean;
  6454. begin
  6455. result:=
  6456. assigned(def) and
  6457. (def.typ=objectdef) and
  6458. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  6459. end;
  6460. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  6461. begin
  6462. result:=
  6463. assigned(def) and
  6464. (def.typ=objectdef) and
  6465. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6466. end;
  6467. function is_implicit_pointer_object_type(def: tdef): boolean;
  6468. begin
  6469. result:=
  6470. assigned(def) and
  6471. (((def.typ=objectdef) and
  6472. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  6473. ((target_info.system in systems_jvm) and
  6474. (def.typ=recorddef)));
  6475. end;
  6476. function is_implicit_array_pointer(def: tdef): boolean;
  6477. begin
  6478. result:=is_dynamic_array(def) or is_dynamicstring(def);
  6479. end;
  6480. function is_class_or_object(def: tdef): boolean;
  6481. begin
  6482. result:=
  6483. assigned(def) and
  6484. (def.typ=objectdef) and
  6485. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  6486. end;
  6487. function is_record(def: tdef): boolean;
  6488. begin
  6489. result:=
  6490. assigned(def) and
  6491. (def.typ=recorddef);
  6492. end;
  6493. function is_javaclass(def: tdef): boolean;
  6494. begin
  6495. result:=
  6496. assigned(def) and
  6497. (def.typ=objectdef) and
  6498. (tobjectdef(def).objecttype=odt_javaclass);
  6499. end;
  6500. function is_javaclassref(def: tdef): boolean;
  6501. begin
  6502. is_javaclassref:=
  6503. assigned(def) and
  6504. (def.typ=classrefdef) and
  6505. is_javaclass(tclassrefdef(def).pointeddef);
  6506. end;
  6507. function is_javainterface(def: tdef): boolean;
  6508. begin
  6509. result:=
  6510. assigned(def) and
  6511. (def.typ=objectdef) and
  6512. (tobjectdef(def).objecttype=odt_interfacejava);
  6513. end;
  6514. function is_java_class_or_interface(def: tdef): boolean;
  6515. begin
  6516. result:=
  6517. assigned(def) and
  6518. (def.typ=objectdef) and
  6519. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  6520. end;
  6521. procedure loadobjctypes;
  6522. begin
  6523. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  6524. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  6525. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  6526. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  6527. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  6528. end;
  6529. procedure maybeloadcocoatypes;
  6530. var
  6531. tsym: ttypesym;
  6532. cocoaunit: string[15];
  6533. begin
  6534. if assigned(objc_fastenumeration) then
  6535. exit;
  6536. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim]) then
  6537. cocoaunit:='COCOAALL'
  6538. else
  6539. cocoaunit:='IPHONEALL';
  6540. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  6541. if assigned(tsym) then
  6542. objc_fastenumeration:=tobjectdef(tsym.typedef)
  6543. else
  6544. objc_fastenumeration:=nil;
  6545. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  6546. if assigned(tsym) then
  6547. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  6548. else
  6549. objc_fastenumerationstate:=nil;
  6550. end;
  6551. function use_vectorfpu(def : tdef) : boolean;
  6552. begin
  6553. {$ifdef x86}
  6554. {$define use_vectorfpuimplemented}
  6555. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  6556. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  6557. {$endif x86}
  6558. {$ifdef arm}
  6559. {$define use_vectorfpuimplemented}
  6560. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  6561. {$endif arm}
  6562. {$ifndef use_vectorfpuimplemented}
  6563. use_vectorfpu:=false;
  6564. {$endif}
  6565. end;
  6566. function getpointerdef(def: tdef): tpointerdef;
  6567. var
  6568. res: PHashSetItem;
  6569. oldsymtablestack: tsymtablestack;
  6570. begin
  6571. if not assigned(current_module) then
  6572. internalerror(2011071101);
  6573. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  6574. if not assigned(res^.Data) then
  6575. begin
  6576. { since these pointerdefs can be reused anywhere in the current
  6577. unit, add them to the global/staticsymtable }
  6578. oldsymtablestack:=symtablestack;
  6579. { do not simply push/pop current_module.localsymtable, because
  6580. that can have side-effects (e.g., it removes helpers) }
  6581. symtablestack:=nil;
  6582. res^.Data:=cpointerdef.create(def);
  6583. if assigned(current_module.localsymtable) then
  6584. current_module.localsymtable.insertdef(tdef(res^.Data))
  6585. else
  6586. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6587. symtablestack:=oldsymtablestack;
  6588. end;
  6589. result:=tpointerdef(res^.Data);
  6590. end;
  6591. function getsingletonarraydef(def: tdef): tarraydef;
  6592. begin
  6593. result:=getarraydef(def,1);
  6594. end;
  6595. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  6596. var
  6597. res: PHashSetItem;
  6598. oldsymtablestack: tsymtablestack;
  6599. arrdesc: packed record
  6600. def: tdef;
  6601. elecount: asizeint;
  6602. end;
  6603. begin
  6604. if not assigned(current_module) then
  6605. internalerror(2011081301);
  6606. arrdesc.def:=def;
  6607. arrdesc.elecount:=elecount;
  6608. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  6609. if not assigned(res^.Data) then
  6610. begin
  6611. { since these arraydef can be reused anywhere in the current
  6612. unit, add them to the global/staticsymtable }
  6613. oldsymtablestack:=symtablestack;
  6614. symtablestack:=nil;
  6615. res^.Data:=carraydef.create(0,elecount-1,ptrsinttype);
  6616. tarraydef(res^.Data).elementdef:=def;
  6617. if assigned(current_module.localsymtable) then
  6618. current_module.localsymtable.insertdef(tdef(res^.Data))
  6619. else
  6620. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6621. symtablestack:=oldsymtablestack;
  6622. end;
  6623. result:=tarraydef(res^.Data);
  6624. end;
  6625. end.