symdef.pas 330 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741
  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,widestr,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { node }
  28. node,
  29. { aasm }
  30. aasmtai,
  31. cpuinfo,
  32. cgbase,
  33. parabase
  34. ;
  35. type
  36. {************************************************
  37. TDef
  38. ************************************************}
  39. tgenericconstraintdata=class
  40. interfaces : tfpobjectlist;
  41. interfacesderef : tfplist;
  42. flags : tgenericconstraintflags;
  43. { only required while parsing }
  44. fileinfo : tfileposinfo;
  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. { trtti_attribute_list }
  53. trtti_attribute = class
  54. typesym : tsym;
  55. typesymderef : tderef;
  56. typeconstr : tdef;
  57. typeconstrderef : tderef;
  58. { these two are not stored in PPU }
  59. constructorcall : tnode;
  60. constructorpd : tdef;
  61. paras : array of tnode;
  62. constructor ppuload(ppufile:tcompilerppufile);
  63. procedure ppuwrite(ppufile:tcompilerppufile);
  64. procedure ppuload_subentries(ppufile:tcompilerppufile);
  65. procedure ppuwrite_subentries(ppufile:tcompilerppufile);
  66. destructor destroy;override;
  67. procedure buildderef;
  68. procedure deref;
  69. end;
  70. trtti_attribute_list = class
  71. rtti_attributes : TFPObjectList;
  72. { if the attribute list is bound to a def or symbol }
  73. is_bound : Boolean;
  74. class procedure bind(var dangling,owned:trtti_attribute_list);
  75. class procedure copyandbind(alist :trtti_attribute_list; var owned : trtti_attribute_list);
  76. procedure addattribute(atypesym:tsym;typeconstr:tdef;constructorcall:tnode;constref paras:array of tnode);
  77. procedure addattribute(attr:trtti_attribute);
  78. destructor destroy; override;
  79. function get_attribute_count:longint;
  80. procedure buildderef;
  81. procedure deref;
  82. class function ppuload(ppufile:tcompilerppufile):trtti_attribute_list;
  83. class procedure ppuwrite(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  84. class procedure ppuload_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  85. class procedure ppuwrite_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  86. end;
  87. { tstoreddef }
  88. tstoreddef = class(tdef)
  89. private
  90. {$ifdef symansistr}
  91. _fullownerhierarchyname : ansistring;
  92. {$else symansistr}
  93. _fullownerhierarchyname : pshortstring;
  94. {$endif symansistr}
  95. procedure writeentry(ppufile: tcompilerppufile; ibnr: byte);
  96. protected
  97. typesymderef : tderef;
  98. procedure ppuwrite_platform(ppufile:tcompilerppufile);virtual;
  99. procedure ppuload_platform(ppufile:tcompilerppufile);virtual;
  100. { a (possibly) reusable def is always created on the basis of another
  101. def, and contains a reference to this other def. If this other
  102. def is in a non-persistent symboltable, the new def cannot actually
  103. be safely reused everywhere in the current module. This routine
  104. abtracts that checking, and also restores the symtable stack
  105. (which had to be reset before creating the new def, so that the new
  106. def did not automatically get added to its top) }
  107. class procedure setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  108. public
  109. {$ifdef EXTDEBUG}
  110. fileinfo : tfileposinfo;
  111. {$endif}
  112. { generic support }
  113. genericdef : tstoreddef;
  114. genericdefderef : tderef;
  115. generictokenbuf : tdynamicarray;
  116. { this list contains references to the symbols that make up the
  117. generic parameters; the symbols are not owned by this list
  118. Note: this list is allocated on demand! }
  119. genericparas : tfphashobjectlist;
  120. genericparaderefs : tfplist;
  121. { contains additional data if this def is a generic constraint
  122. Note: this class is allocated on demand! }
  123. genconstraintdata : tgenericconstraintdata;
  124. { this is Nil if the def has no RTTI attributes }
  125. rtti_attribute_list : trtti_attribute_list;
  126. { original def for "type <name>" declarations }
  127. orgdef : tstoreddef;
  128. orgdefderef : tderef;
  129. constructor create(dt:tdeftyp;doregister:boolean);
  130. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  131. destructor destroy;override;
  132. function getcopy : tstoreddef;virtual;
  133. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  134. { this is called directly after ppuload }
  135. procedure ppuload_subentries(ppufile:tcompilerppufile);virtual;
  136. { this is called directly after ppuwrite }
  137. procedure ppuwrite_subentries(ppufile:tcompilerppufile);virtual;
  138. procedure buildderef;override;
  139. procedure buildderefimpl;override;
  140. procedure deref;override;
  141. procedure derefimpl;override;
  142. function size:asizeint;override;
  143. function getvardef:longint;override;
  144. function alignment:shortint;override;
  145. function is_publishable : tpublishproperty;override;
  146. function needs_inittable : boolean;override;
  147. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  148. function rtti_mangledname(rt:trttitype):TSymStr;override;
  149. function OwnerHierarchyName: string; override;
  150. function OwnerHierarchyPrettyName: string; override;
  151. function fullownerhierarchyname(skipprocparams:boolean):TSymStr;override;
  152. function needs_separate_initrtti:boolean;override;
  153. function in_currentunit: boolean;
  154. { regvars }
  155. function is_intregable : boolean;
  156. function is_fpuregable : boolean;
  157. { def can be put into a register if it is const/immutable }
  158. function is_const_intregable : boolean;
  159. { generics }
  160. procedure initgeneric;
  161. { this function can be used to determine whether a def is really a
  162. generic declaration or just a normal type declared inside another
  163. generic }
  164. function is_generic:boolean;
  165. { same as above for specializations }
  166. function is_specialization:boolean;
  167. { generic utilities }
  168. function is_generic_param_const(index:integer):boolean;inline;
  169. function get_generic_param_def(index:integer):tdef;inline;
  170. { registers this def in the unit's deflist; no-op if already registered }
  171. procedure register_def; override;
  172. { add the def to the top of the symtable stack if it's not yet owned
  173. by another symtable }
  174. procedure maybe_put_in_symtable_stack;
  175. private
  176. savesize : asizeuint;
  177. end;
  178. tfiletyp = (ft_text,ft_typed,ft_untyped);
  179. tfiledef = class(tstoreddef)
  180. filetyp : tfiletyp;
  181. typedfiledef : tdef;
  182. typedfiledefderef : tderef;
  183. constructor createtext;virtual;
  184. constructor createuntyped;virtual;
  185. constructor createtyped(def : tdef);virtual;
  186. constructor ppuload(ppufile:tcompilerppufile);
  187. function getcopy : tstoreddef;override;
  188. { do not override this routine in platform-specific subclasses,
  189. override ppuwrite_platform instead }
  190. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  191. procedure buildderef;override;
  192. procedure deref;override;
  193. function GetTypeName:string;override;
  194. function getmangledparaname:TSymStr;override;
  195. function size:asizeint;override;
  196. procedure setsize;
  197. function alignment: shortint; override;
  198. end;
  199. tfiledefclass = class of tfiledef;
  200. tvariantdef = class(tstoreddef)
  201. varianttype : tvarianttype;
  202. constructor create(v : tvarianttype);virtual;
  203. constructor ppuload(ppufile:tcompilerppufile);
  204. function getcopy : tstoreddef;override;
  205. function GetTypeName:string;override;
  206. function alignment : shortint;override;
  207. { do not override this routine in platform-specific subclasses,
  208. override ppuwrite_platform instead }
  209. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  210. function getvardef:longint;override;
  211. procedure setsize;
  212. function is_publishable : tpublishproperty;override;
  213. function needs_inittable : boolean;override;
  214. end;
  215. tvariantdefclass = class of tvariantdef;
  216. tformaldef = class(tstoreddef)
  217. typed:boolean;
  218. constructor create(Atyped:boolean);virtual;
  219. constructor ppuload(ppufile:tcompilerppufile);
  220. { do not override this routine in platform-specific subclasses,
  221. override ppuwrite_platform instead }
  222. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  223. function GetTypeName:string;override;
  224. end;
  225. tformaldefclass = class of tformaldef;
  226. tforwarddef = class(tstoreddef)
  227. tosymname : pshortstring;
  228. forwardpos : tfileposinfo;
  229. constructor create(const s:string;const pos:tfileposinfo);virtual;
  230. destructor destroy;override;
  231. function getcopy:tstoreddef;override;
  232. function GetTypeName:string;override;
  233. end;
  234. tforwarddefclass = class of tforwarddef;
  235. tundefineddef = class(tstoreddef)
  236. constructor create(doregister:boolean);virtual;
  237. constructor ppuload(ppufile:tcompilerppufile);
  238. { do not override this routine in platform-specific subclasses,
  239. override ppuwrite_platform instead }
  240. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  241. function GetTypeName:string;override;
  242. end;
  243. tundefineddefclass = class of tundefineddef;
  244. terrordef = class(tstoreddef)
  245. constructor create;virtual;
  246. { do not override this routine in platform-specific subclasses,
  247. override ppuwrite_platform instead }
  248. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  249. function GetTypeName:string;override;
  250. function getmangledparaname : TSymStr;override;
  251. end;
  252. terrordefclass = class of terrordef;
  253. tabstractpointerdef = class(tstoreddef)
  254. pointeddef : tdef;
  255. pointeddefderef : tderef;
  256. constructor create(dt:tdeftyp;def:tdef);
  257. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  258. procedure ppuwrite(ppufile:tcompilerppufile);override;
  259. procedure buildderef;override;
  260. procedure deref;override;
  261. function size:asizeint;override;
  262. function alignment:shortint;override;
  263. end;
  264. tpointerdef = class(tabstractpointerdef)
  265. has_pointer_math : boolean;
  266. constructor create(def:tdef);virtual;
  267. { returns a pointerdef for def, reusing an existing one in case it
  268. exists in the current module }
  269. class function getreusable(def: tdef): tpointerdef; virtual;
  270. { same as above, but in case the def must never be freed after the
  271. current module has been compiled -- even if the def was not written
  272. to the ppu file (for defs in para locations, as we don't reset them
  273. so we don't have to recalculate them all the time) }
  274. class function getreusable_no_free(def: tdef): tpointerdef;
  275. function size:asizeint;override;
  276. function getcopy:tstoreddef;override;
  277. constructor ppuload(ppufile:tcompilerppufile);
  278. { do not override this routine in platform-specific subclasses,
  279. override ppuwrite_platform instead }
  280. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  281. function GetTypeName:string;override;
  282. {# returns the appropriate int type for pointer arithmetic with the given pointer type.
  283. When adding or subtracting a number to/from a pointer, this function returns the
  284. int type to which that number has to be converted, before the operation can be performed.
  285. Normally, this is sinttype, except on i8086, where it takes into account the
  286. special i8086 pointer types (near, far, huge). }
  287. function pointer_arithmetic_int_type:tdef;virtual;
  288. {# the unsigned version of pointer_arithmetic_int_type. Used with inc/dec. }
  289. function pointer_arithmetic_uint_type:tdef;virtual;
  290. {# returns the int type produced when subtracting two pointers of the given type.
  291. Normally, this is sinttype, except on i8086, where it takes into account the
  292. special i8086 pointer types (near, far, huge). }
  293. function pointer_subtraction_result_type:tdef;virtual;
  294. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  295. {# the integer index type used to convert the pointer to array (i.e. denotes int_type in: ptr[int_type]) }
  296. function converted_pointer_to_array_range_type:tdef;virtual;
  297. end;
  298. tpointerdefclass = class of tpointerdef;
  299. tprocdef = class;
  300. tabstractrecorddef= class(tstoreddef)
  301. private
  302. rttistring : string;
  303. {$ifdef DEBUG_NODE_XML}
  304. protected
  305. procedure XMLPrintDefData(var T: Text; Sym: TSym); override;
  306. {$endif DEBUG_NODE_XML}
  307. public
  308. objname,
  309. objrealname : PShortString;
  310. { for C++ classes: name of the library this class is imported from }
  311. { for Java classes/records: package name }
  312. import_lib : PShortString;
  313. symtable : TSymtable;
  314. cloneddef : tabstractrecorddef;
  315. cloneddefderef : tderef;
  316. objectoptions : tobjectoptions;
  317. rtti : trtti_directive;
  318. { for targets that initialise typed constants via explicit assignments
  319. instead of by generating an initialised data sectino }
  320. tcinitcode : tnode;
  321. constructor create(const n:string; dt:tdeftyp;doregister:boolean);
  322. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  323. procedure ppuwrite(ppufile:tcompilerppufile);override;
  324. destructor destroy; override;
  325. procedure buildderefimpl;override;
  326. procedure derefimpl;override;
  327. procedure check_forwards; virtual;
  328. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  329. function GetSymtable(t:tGetSymtable):TSymtable;override;
  330. function is_packed:boolean;
  331. function RttiName: string;
  332. { enumerator support }
  333. function search_enumerator_get: tprocdef; virtual;
  334. function search_enumerator_move: tprocdef; virtual;
  335. function search_enumerator_current: tsym; virtual;
  336. { JVM }
  337. function jvm_full_typename(with_package_name: boolean): string;
  338. { check if the symtable contains a float field }
  339. function contains_float_field : boolean;
  340. { check if the symtable contains a field that spans an aword boundary }
  341. function contains_cross_aword_field: boolean;
  342. { extended RTTI }
  343. procedure apply_rtti_directive(dir: trtti_directive); virtual;
  344. function is_visible_for_rtti(option: trtti_option; vis: tvisibility): boolean; inline;
  345. function rtti_visibilities_for_option(option: trtti_option): tvisibilities; inline;
  346. function has_extended_rtti: boolean; inline;
  347. end;
  348. pvariantrecdesc = ^tvariantrecdesc;
  349. tvariantrecbranch = record
  350. { we store only single values here and no ranges because tvariantrecdesc is only needed in iso mode
  351. which does not support range expressions in variant record definitions }
  352. values : array of Tconstexprint;
  353. nestedvariant : pvariantrecdesc;
  354. end;
  355. ppvariantrecdesc = ^pvariantrecdesc;
  356. tvariantrecdesc = record
  357. variantselector : tsym;
  358. variantselectorderef : tderef;
  359. branches : array of tvariantrecbranch;
  360. end;
  361. trecorddef = class(tabstractrecorddef)
  362. {$ifdef DEBUG_NODE_XML}
  363. protected
  364. function XMLPrintType: ansistring; override;
  365. {$endif DEBUG_NODE_XML}
  366. public
  367. variantrecdesc : pvariantrecdesc;
  368. isunion : boolean;
  369. constructor create(const n:string; p:TSymtable);virtual;
  370. constructor create_global_internal(const n: string; packrecords, recordalignmin: shortint); virtual;
  371. constructor create_internal(const n: string; packrecords, recordalignmin: shortint; where: tsymtable); virtual;
  372. function add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  373. procedure add_fields_from_deflist(fieldtypes: tfplist);
  374. constructor ppuload(ppufile:tcompilerppufile);
  375. destructor destroy;override;
  376. function getcopy : tstoreddef;override;
  377. { do not override this routine in platform-specific subclasses,
  378. override ppuwrite_platform instead }
  379. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  380. procedure buildderef;override;
  381. procedure deref;override;
  382. function size:asizeint;override;
  383. function alignment : shortint;override;
  384. function padalignment: shortint;
  385. function GetTypeName:string;override;
  386. { debug }
  387. function needs_inittable : boolean;override;
  388. function needs_separate_initrtti:boolean;override;
  389. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  390. end;
  391. trecorddefclass = class of trecorddef;
  392. tobjectdef = class;
  393. { TImplementedInterface }
  394. TImplementedInterface = class
  395. private
  396. fIOffset : longint;
  397. function GetIOffset: longint;
  398. public
  399. IntfDef : tobjectdef;
  400. IntfDefDeref : tderef;
  401. IType : tinterfaceentrytype;
  402. VtblImplIntf : TImplementedInterface;
  403. NameMappings : TFPHashList;
  404. ProcDefs : TFPObjectList;
  405. ImplementsGetter : tsym;
  406. ImplementsGetterDeref : tderef;
  407. ImplementsField : tsym;
  408. constructor create(aintf: tobjectdef);virtual;
  409. constructor create_deref(intfd,getterd:tderef);virtual;
  410. destructor destroy; override;
  411. function getcopy:TImplementedInterface;
  412. procedure buildderef;
  413. procedure deref;
  414. procedure AddMapping(const origname, newname: string);
  415. function GetMapping(const origname: string):string;
  416. procedure AddImplProc(pd:tprocdef);
  417. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  418. property IOffset: longint read GetIOffset write fIOffset;
  419. end;
  420. timplementedinterfaceclass = class of timplementedinterface;
  421. { tvmtentry }
  422. tvmtentry = record
  423. procdef : tprocdef;
  424. procdefderef : tderef;
  425. visibility : tvisibility;
  426. end;
  427. pvmtentry = ^tvmtentry;
  428. { tobjectdef }
  429. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  430. pmvcallstaticinfo = ^tmvcallstaticinfo;
  431. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  432. tobjectdef = class(tabstractrecorddef)
  433. private
  434. fcurrent_dispid: longint;
  435. {$ifdef DEBUG_NODE_XML}
  436. protected
  437. function XMLPrintType: ansistring; override;
  438. procedure XMLPrintDefInfo(var T: Text; Sym: TSym); override;
  439. procedure XMLPrintDefData(var T: Text; Sym: TSym); override;
  440. {$endif DEBUG_NODE_XML}
  441. public
  442. childof : tobjectdef;
  443. childofderef : tderef;
  444. { for Object Pascal helpers }
  445. extendeddef : tdef;
  446. extendeddefderef: tderef;
  447. helpertype : thelpertype;
  448. { for Objective-C: protocols and classes can have the same name there }
  449. objextname : pshortstring;
  450. { to be able to have a variable vmt position }
  451. { and no vmt field for objects without virtuals }
  452. vmtentries : TFPList;
  453. vmcallstaticinfo : pmvcallstaticinfo;
  454. vmt_field : tsym;
  455. vmt_fieldderef : tderef;
  456. iidguid : pguid;
  457. iidstr : pshortstring;
  458. { store implemented interfaces defs and name mappings }
  459. ImplementedInterfaces : TFPObjectList;
  460. { number of abstract methods (used by JVM target to determine whether
  461. or not the class should be marked as abstract: must be done if 1 or
  462. more abstract methods) }
  463. abstractcnt : longint;
  464. writing_class_record_dbginfo,
  465. { a class of this type has been created in this module }
  466. created_in_current_module,
  467. { a loadvmtnode for this class has been created in this
  468. module, so if a classrefdef variable of this or a parent
  469. class is used somewhere to instantiate a class, then this
  470. class may be instantiated
  471. }
  472. maybe_created_in_current_module,
  473. { a "class of" this particular class has been created in
  474. this module
  475. }
  476. classref_created_in_current_module : boolean;
  477. objecttype : tobjecttyp;
  478. { for interfaces that can be invoked using Invoke(),
  479. this is the definition of the hidden class that is generated by the compiler.
  480. we need this definition to reference it in the RTTI.
  481. Since interfaces can inherit, so can these hidden classes,
  482. so we need to write this to the ppu to be able to reference the parents.
  483. }
  484. hiddenclassdef : tobjectdef;
  485. hiddenclassdefref : tderef;
  486. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);virtual;
  487. constructor ppuload(ppufile:tcompilerppufile);
  488. destructor destroy;override;
  489. function getcopy : tstoreddef;override;
  490. { do not override this routine in platform-specific subclasses,
  491. override ppuwrite_platform instead }
  492. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  493. function GetTypeName:string;override;
  494. procedure buildderef;override;
  495. procedure deref;override;
  496. procedure derefimpl;override;
  497. procedure copyvmtentries(objdef:tobjectdef);
  498. function getparentdef:tdef;override;
  499. function size : asizeint;override;
  500. function alignment:shortint;override;
  501. function vmtmethodoffset(index:longint):longint;
  502. function members_need_inittable : boolean;
  503. { this should be called when this class implements an interface }
  504. procedure prepareguid;
  505. function is_publishable : tpublishproperty;override;
  506. function needs_inittable : boolean;override;
  507. function needs_separate_initrtti : boolean;override;
  508. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  509. function rtti_mangledname(rt:trttitype):TSymStr;override;
  510. function is_unique_objpasdef: boolean;
  511. function vmt_mangledname : TSymStr;
  512. function vmt_def: trecorddef;
  513. procedure check_forwards; override;
  514. procedure insertvmt;
  515. function vmt_offset: asizeint;
  516. procedure set_parent(c : tobjectdef);
  517. function find_destructor: tprocdef;
  518. function implements_any_interfaces: boolean;
  519. function register_implemented_interface(intfdef:tobjectdef;useguid:boolean):timplementedinterface;
  520. { dispinterface support }
  521. function get_next_dispid: longint;
  522. { enumerator support }
  523. function search_enumerator_get: tprocdef; override;
  524. function search_enumerator_move: tprocdef; override;
  525. function search_enumerator_current: tsym; override;
  526. { WPO }
  527. procedure register_created_object_type;override;
  528. procedure register_maybe_created_object_type;
  529. procedure register_created_classref_type;
  530. procedure register_vmt_call(index:longint);
  531. { ObjC }
  532. procedure finish_objc_data;
  533. function check_objc_types: boolean;
  534. { C++ }
  535. procedure finish_cpp_data;
  536. procedure apply_rtti_directive(dir: trtti_directive); override;
  537. end;
  538. tobjectdefclass = class of tobjectdef;
  539. tclassrefdef = class(tabstractpointerdef)
  540. constructor create(def:tdef);virtual;
  541. constructor ppuload(ppufile:tcompilerppufile);
  542. { do not override this routine in platform-specific subclasses,
  543. override ppuwrite_platform instead }
  544. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  545. function getcopy:tstoreddef;override;
  546. function GetTypeName:string;override;
  547. function is_publishable : tpublishproperty;override;
  548. function rtti_mangledname(rt:trttitype):TSymStr;override;
  549. procedure register_created_object_type;override;
  550. end;
  551. tclassrefdefclass = class of tclassrefdef;
  552. tarraydef = class(tstoreddef)
  553. lowrange,
  554. highrange : asizeint;
  555. rangedef : tdef;
  556. rangedefderef : tderef;
  557. arrayoptions : tarraydefoptions;
  558. symtable : TSymtable;
  559. protected
  560. _elementdef : tdef;
  561. _elementdefderef : tderef;
  562. procedure setelementdef(def:tdef);
  563. class function getreusable_intern(def: tdef; elems: asizeint; const options: tarraydefoptions): tarraydef;
  564. public
  565. function elesize : asizeint;
  566. function elepackedbitsize : asizeint;
  567. function elecount : asizeuint;
  568. constructor create_from_pointer(def: tpointerdef);virtual;
  569. constructor create(l, h: asizeint; def: tdef);virtual;
  570. constructor create_vector(l,h:asizeint; def:tdef);
  571. constructor create_openarray;virtual;
  572. class function getreusable(def: tdef; elems: asizeint): tarraydef;
  573. class function getreusable_vector(def: tdef; elems: asizeint): tarraydef;
  574. { same as above, but in case the def must never be freed after the
  575. current module has been compiled -- even if the def was not written
  576. to the ppu file (for defs in para locations, as we don't reset them
  577. so we don't have to recalculate them all the time) }
  578. class function getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  579. class function getreusable_no_free_vector(def: tdef; elems: asizeint): tarraydef;
  580. constructor ppuload(ppufile:tcompilerppufile);
  581. destructor destroy; override;
  582. function getcopy : tstoreddef;override;
  583. { do not override this routine in platform-specific subclasses,
  584. override ppuwrite_platform instead }
  585. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  586. function GetTypeName:string;override;
  587. function getmangledparaname : TSymStr;override;
  588. procedure buildderef;override;
  589. procedure deref;override;
  590. function size : asizeint;override;
  591. function alignment : shortint;override;
  592. { returns the label of the range check string }
  593. function needs_inittable : boolean;override;
  594. function needs_separate_initrtti : boolean;override;
  595. property elementdef : tdef read _elementdef write setelementdef;
  596. function is_publishable : tpublishproperty;override;
  597. function is_hwvector: boolean;
  598. end;
  599. tarraydefclass = class of tarraydef;
  600. torddef = class(tstoreddef)
  601. low,high : TConstExprInt;
  602. ordtype : tordtype;
  603. constructor create(t : tordtype;v,b : TConstExprInt; doregister: boolean);virtual;
  604. constructor ppuload(ppufile:tcompilerppufile);
  605. function getcopy : tstoreddef;override;
  606. { do not override this routine in platform-specific subclasses,
  607. override ppuwrite_platform instead }
  608. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  609. function is_publishable : tpublishproperty;override;
  610. function GetTypeName:string;override;
  611. function alignment:shortint;override;
  612. procedure setsize;
  613. function packedbitsize: asizeint; override;
  614. function getvardef : longint;override;
  615. end;
  616. torddefclass = class of torddef;
  617. tfloatdef = class(tstoreddef)
  618. floattype : tfloattype;
  619. constructor create(t: tfloattype; doregister: boolean);virtual;
  620. constructor ppuload(ppufile:tcompilerppufile);
  621. function getcopy : tstoreddef;override;
  622. { do not override this routine in platform-specific subclasses,
  623. override ppuwrite_platform instead }
  624. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  625. function GetTypeName:string;override;
  626. function is_publishable : tpublishproperty;override;
  627. function alignment:shortint;override;
  628. function structalignment: shortint;override;
  629. procedure setsize;
  630. function getvardef:longint;override;
  631. end;
  632. tfloatdefclass = class of tfloatdef;
  633. { tabstractprocdef }
  634. tprocnameoption = (pno_showhidden, pno_proctypeoption, pno_paranames,
  635. pno_ownername, pno_noclassmarker, pno_noleadingdollar,
  636. pno_mangledname, pno_noparams, pno_prettynames);
  637. tprocnameoptions = set of tprocnameoption;
  638. tproccopytyp = (pc_normal,
  639. {$ifdef i8086}
  640. { the address in a far format }
  641. pc_far_address,
  642. { the offset part of the far address }
  643. pc_offset,
  644. {$endif i8086}
  645. { creates a procvardef describing only the code pointer
  646. of a method/netsted function/... }
  647. pc_address_only,
  648. { everything except for hidden parameters }
  649. pc_normal_no_hidden,
  650. { everything except for the parameters (because they
  651. might be moved over to the copy) }
  652. pc_normal_no_paras,
  653. { always creates a top-level function, removes all
  654. special parameters (self, vmt, parentfp, ...) }
  655. pc_bareproc
  656. );
  657. tcacheableproccopytyp = pc_normal..pc_address_only;
  658. tabstractprocdef = class(tstoreddef)
  659. { saves a definition to the return type }
  660. returndef : tdef;
  661. returndefderef : tderef;
  662. parast : TSymtable;
  663. paras : tparalist;
  664. proctypeoption : tproctypeoption;
  665. proccalloption : tproccalloption;
  666. procoptions : tprocoptions;
  667. callerargareasize,
  668. calleeargareasize: pint;
  669. {$ifdef m68k}
  670. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  671. {$endif}
  672. funcretloc : array[callerside..calleeside] of TCGPara;
  673. has_paraloc_info : tcallercallee; { paraloc info is available }
  674. { number of user visible parameters }
  675. maxparacount,
  676. minparacount : byte;
  677. constructor create(dt:tdeftyp;level:byte;doregister:boolean);
  678. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  679. destructor destroy;override;
  680. procedure ppuwrite(ppufile:tcompilerppufile);override;
  681. procedure buildderef;override;
  682. procedure deref;override;
  683. procedure calcparas;
  684. function mangledprocparanames(oldlen : longint) : string;
  685. function typename_paras(pno: tprocnameoptions): ansistring;
  686. function is_methodpointer:boolean;virtual;
  687. function is_addressonly:boolean;virtual;
  688. function no_self_node:boolean;
  689. { get either a copy as a procdef or procvardef }
  690. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef; virtual;
  691. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  692. procedure check_mark_as_nested;
  693. procedure init_paraloc_info(side: tcallercallee);
  694. procedure done_paraloc_info(side: tcallercallee);
  695. function stack_tainting_parameter(side: tcallercallee): boolean;
  696. function is_pushleftright: boolean;virtual;
  697. function address_type:tdef;virtual;
  698. { address type, generated for ofs() }
  699. function ofs_address_type:tdef;virtual;
  700. procedure declared_far;virtual;
  701. procedure declared_near;virtual;
  702. function generate_safecall_wrapper: boolean; virtual;
  703. { returns true if the def is a generic param of the procdef }
  704. function is_generic_param(def:tdef): boolean;
  705. function wpo_may_create_instance(optionalmethodpointer: tnode): boolean;
  706. private
  707. procedure count_para(p:TObject;arg:pointer);
  708. procedure insert_para(p:TObject;arg:pointer);
  709. end;
  710. tprocvardef = class(tabstractprocdef)
  711. constructor create(level:byte;doregister:boolean);virtual;
  712. { returns a procvardef that represents the address of a proc(var)def }
  713. class function getreusableprocaddr(def: tabstractprocdef; copytyp: tcacheableproccopytyp): tprocvardef; virtual;
  714. { same as above, but in case the def must never be freed after the
  715. current module has been compiled -- even if the def was not written
  716. to the ppu file (for defs in para locations, as we don't reset them
  717. so we don't have to recalculate them all the time) }
  718. class function getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  719. constructor ppuload(ppufile:tcompilerppufile);
  720. function getcopy : tstoreddef;override;
  721. { do not override this routine in platform-specific subclasses,
  722. override ppuwrite_platform instead }
  723. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  724. function GetSymtable(t:tGetSymtable):TSymtable;override;
  725. function size : asizeint;override;
  726. function GetTypeName:string;override;
  727. function is_publishable : tpublishproperty;override;
  728. function is_methodpointer:boolean;override;
  729. function is_addressonly:boolean;override;
  730. function getmangledparaname:TSymStr;override;
  731. function getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef; override;
  732. end;
  733. tprocvardefclass = class of tprocvardef;
  734. tmessageinf = record
  735. case integer of
  736. 0 : (str : pshortstring);
  737. 1 : (i : longint);
  738. end;
  739. tinlininginfo = record
  740. { node tree }
  741. code : tnode;
  742. flags : tprocinfoflags;
  743. end;
  744. pinlininginfo = ^tinlininginfo;
  745. tcapturedsyminfo = record
  746. sym : tsym;
  747. def : tdef;
  748. { the location where the symbol was first encountered }
  749. fileinfo : tfileposinfo;
  750. end;
  751. pcapturedsyminfo = ^tcapturedsyminfo;
  752. timplprocdefinfo = record
  753. resultname : pshortstring;
  754. parentfpstruct: tsym;
  755. parentfpstructptrtype: tdef;
  756. parentfpinitblock: tnode;
  757. procstarttai,
  758. procendtai : tai;
  759. skpara: pointer;
  760. personality: tprocdef;
  761. was_anonymous,
  762. has_capturer,
  763. forwarddef,
  764. interfacedef : boolean;
  765. hasforward : boolean;
  766. is_implemented : boolean;
  767. capturedsyms : tfplist;
  768. end;
  769. pimplprocdefinfo = ^timplprocdefinfo;
  770. { tprocdef }
  771. tprocdef = class(tabstractprocdef)
  772. protected
  773. {$ifdef symansistr}
  774. _mangledname : ansistring;
  775. {$else symansistr}
  776. _mangledname : pshortstring;
  777. {$endif}
  778. _parentfpsym : tsym;
  779. { information that is only required until the implementation of the
  780. procdef has been handled }
  781. implprocdefinfo : pimplprocdefinfo;
  782. function store_localst:boolean;
  783. function GetResultName: PShortString;
  784. procedure SetResultName(AValue: PShortString);
  785. function GetParentFPStruct: tsym;
  786. procedure SetParentFPStruct(AValue: tsym);
  787. function GetParentFPStructPtrType: tdef;
  788. procedure SetParentFPStructPtrType(AValue: tdef);
  789. function GetParentFPInitBlock: tnode;
  790. procedure SetParentFPInitBlock(AValue: tnode);
  791. function Getprocstarttai: tai;
  792. procedure Setprocstarttai(AValue: tai);
  793. function Getprocendtai: tai;
  794. procedure Setprocendtai(AValue: tai);
  795. function Getskpara: pointer;
  796. procedure Setskpara(AValue: pointer);
  797. function Getpersonality: tprocdef;
  798. procedure Setpersonality(AValue: tprocdef);
  799. function Getforwarddef: boolean;
  800. procedure Setforwarddef(AValue: boolean);
  801. function Getinterfacedef: boolean;
  802. procedure Setinterfacedef(AValue: boolean);virtual;
  803. function Gethasforward: boolean;
  804. procedure Sethasforward(AValue: boolean);
  805. function GetIsEmpty: boolean;
  806. procedure SetIsEmpty(AValue: boolean);
  807. function GetHasInliningInfo: boolean;
  808. procedure SetHasInliningInfo(AValue: boolean);
  809. function Getis_implemented: boolean;
  810. procedure Setis_implemented(AValue: boolean);
  811. function getwas_anonymous:boolean;
  812. procedure setwas_anonymous(avalue:boolean);
  813. function gethas_capturer:boolean;
  814. procedure sethas_capturer(avalue:boolean);
  815. function Getcapturedsyms:tfplist;
  816. function getparentfpsym: tsym;
  817. public
  818. messageinf : tmessageinf;
  819. dispid : longint;
  820. {$ifndef EXTDEBUG}
  821. { where is this function defined and what were the symbol
  822. flags, needed here because there
  823. is only one symbol for all overloaded functions
  824. EXTDEBUG has fileinfo in tdef (PFV) }
  825. fileinfo : tfileposinfo;
  826. {$endif}
  827. symoptions : tsymoptions;
  828. deprecatedmsg : pshortstring;
  829. { generic support }
  830. genericdecltokenbuf : tdynamicarray;
  831. { symbol owning this definition }
  832. procsym : tsym;
  833. procsymderef : tderef;
  834. { alias names }
  835. aliasnames : TCmdStrList;
  836. { symtables }
  837. localst : TSymtable;
  838. funcretsym : tsym;
  839. funcretsymderef : tderef;
  840. struct : tabstractrecorddef;
  841. structderef : tderef;
  842. implprocoptions: timplprocoptions;
  843. { import info }
  844. import_dll,
  845. import_name : pshortstring;
  846. { info for inlining the subroutine, if this pointer is nil,
  847. the procedure can't be inlined }
  848. inlininginfo : pinlininginfo;
  849. import_nr : word;
  850. extnumber : word;
  851. { set to a value different from tsk_none in case this procdef is for
  852. a routine that has to be internally generated by the compiler }
  853. synthetickind: tsynthetickind;
  854. visibility : tvisibility;
  855. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  856. seenmarker : pointer; // used for filtering in tcandidate
  857. {$endif}
  858. {$ifdef symansistr}
  859. section: ansistring;
  860. {$else symansistr}
  861. section: pshortstring;
  862. {$endif}
  863. { only needed when actually compiling a unit, no need to save/load from ppu }
  864. invoke_helper : tprocdef;
  865. copied_from : tprocdef;
  866. constructor create(level:byte;doregister:boolean);virtual;
  867. constructor ppuload(ppufile:tcompilerppufile);
  868. destructor destroy;override;
  869. procedure freeimplprocdefinfo;
  870. { do not override this routine in platform-specific subclasses,
  871. override ppuwrite_platform instead }
  872. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  873. procedure buildderef;override;
  874. procedure buildderefimpl;override;
  875. procedure deref;override;
  876. procedure derefimpl;override;
  877. function GetSymtable(t:tGetSymtable):TSymtable;override;
  878. { warnings:
  879. * the symtablestack top has to be the symtable to which the copy
  880. should be added
  881. * getcopy does not create a finished/ready-to-use procdef; it
  882. needs to be finalised afterwards by calling
  883. symcreat.finish_copied_procdef() afterwards
  884. }
  885. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef; override;
  886. function getcopy: tstoreddef; override;
  887. function GetTypeName : string;override;
  888. function mangledname : TSymStr; virtual;
  889. procedure setmangledname(const s : TSymStr);
  890. function needsglobalasmsym: boolean;
  891. procedure setcompilerprocname;
  892. function fullprocname(showhidden:boolean):string;
  893. function customprocname(pno: tprocnameoptions):ansistring;
  894. function defaultmangledname: TSymStr;
  895. function cplusplusmangledname : TSymStr;
  896. function objcmangledname : TSymStr;
  897. function is_methodpointer:boolean;override;
  898. function is_addressonly:boolean;override;
  899. procedure make_external;
  900. procedure init_genericdecl;
  901. function get_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean; virtual;
  902. function get_safecall_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean; virtual;
  903. procedure add_captured_sym(sym:tsym;def:tdef;const filepos:tfileposinfo);
  904. { returns whether the mangled name or any of its aliases is equal to
  905. s }
  906. function has_alias_name(const s: TSymStr):boolean;
  907. { aliases to fields only required when a function is implemented in
  908. the current unit }
  909. property resultname: PShortString read GetResultName write SetResultName;
  910. { temporary reference to structure containing copies of all local
  911. variables and parameters accessed by nested routines; reference to
  912. this structure is passed as "parent frame pointer" on targets that
  913. lack this concept (at least JVM and LLVM); no need to save to/
  914. restore from ppu, since nested routines are always in the same
  915. unit }
  916. property parentfpstruct: tsym read GetParentFPStruct write SetParentFPStruct;
  917. { pointer to parentfpstruct's type (not yet valid during parsing, so
  918. cannot be used for $parentfp parameter) (no need to save to ppu) }
  919. property parentfpstructptrtype: tdef read GetParentFPStructPtrType write SetParentFPStructPtrType;
  920. { code to copy the parameters accessed from nested routines into the
  921. parentfpstruct (no need to save to ppu) }
  922. property parentfpinitblock: tnode read GetParentFPInitBlock write SetParentFPInitBlock;
  923. { First/last assembler symbol/instruction in aasmoutput list.
  924. Note: initialised after compiling the code for the procdef, but
  925. not saved to/restored from ppu. Used when inserting debug info }
  926. property procstarttai: tai read Getprocstarttai write Setprocstarttai;
  927. property procendtai: tai read Getprocendtai write Setprocendtai;
  928. { optional parameter for the synthetic routine generation logic }
  929. property skpara: pointer read Getskpara write Setskpara;
  930. { ABI-conformant exception handling personality function }
  931. property personality: tprocdef read Getpersonality write Setpersonality;
  932. { true, if the procedure is only declared
  933. (forward procedure) }
  934. property forwarddef: boolean read Getforwarddef write Setforwarddef;
  935. { true if the procedure is declared in the interface }
  936. property interfacedef: boolean read Getinterfacedef write Setinterfacedef;
  937. { true if the procedure has a forward declaration }
  938. property hasforward: boolean read Gethasforward write Sethasforward;
  939. { true if the routine's body is empty }
  940. property isempty: boolean read GetIsEmpty write SetIsEmpty;
  941. { true if all information required to inline this routine is available }
  942. property has_inlininginfo: boolean read GetHasInliningInfo write SetHasInliningInfo;
  943. { returns the $parentfp parameter for nested routines }
  944. property parentfpsym: tsym read getparentfpsym;
  945. { true if the implementation part for this procdef has been handled }
  946. property is_implemented: boolean read Getis_implemented write Setis_implemented;
  947. { valid if the procdef captures any symbols from outer scopes }
  948. property capturedsyms:tfplist read Getcapturedsyms;
  949. { true if this procdef was originally an anonymous function }
  950. property was_anonymous:boolean read getwas_anonymous write setwas_anonymous;
  951. { true if the procdef has a capturer for anonymous functions }
  952. property has_capturer:boolean read gethas_capturer write sethas_capturer;
  953. end;
  954. tprocdefclass = class of tprocdef;
  955. { single linked list of overloaded procs }
  956. pprocdeflist = ^tprocdeflist;
  957. tprocdeflist = record
  958. def : tprocdef;
  959. defderef : tderef;
  960. next : pprocdeflist;
  961. end;
  962. tstringdef = class(tstoreddef)
  963. encoding : tstringencoding;
  964. stringtype : tstringtype;
  965. len : asizeint;
  966. constructor createshort(l: byte; doregister: boolean);virtual;
  967. constructor loadshort(ppufile:tcompilerppufile);
  968. constructor createlong(l: asizeint; doregister: boolean);virtual;
  969. constructor loadlong(ppufile:tcompilerppufile);
  970. constructor createansi(aencoding: tstringencoding; doregister: boolean);virtual;
  971. constructor loadansi(ppufile:tcompilerppufile);
  972. constructor createwide(doregister: boolean);virtual;
  973. constructor loadwide(ppufile:tcompilerppufile);
  974. constructor createunicode(doregister: boolean);virtual;
  975. constructor loadunicode(ppufile:tcompilerppufile);virtual;
  976. function getcopy : tstoreddef;override;
  977. function stringtypname:string;
  978. { do not override this routine in platform-specific subclasses,
  979. override ppuwrite_platform instead }
  980. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  981. function GetTypeName:string;override;
  982. function getmangledparaname:TSymStr;override;
  983. function is_publishable : tpublishproperty;override;
  984. function size:asizeint;override;
  985. function alignment : shortint;override;
  986. function needs_inittable : boolean;override;
  987. function getvardef:longint;override;
  988. { returns the default char type def for the string }
  989. function get_default_char_type: tdef;
  990. { returns the default string type }
  991. function get_default_string_type: tdef;
  992. end;
  993. tstringdefclass = class of tstringdef;
  994. { tenumdef }
  995. tenumdef = class(tstoreddef)
  996. minval,
  997. maxval : asizeint;
  998. basedef : tenumdef;
  999. basedefderef : tderef;
  1000. symtable : TSymtable;
  1001. has_jumps : boolean;
  1002. constructor create;virtual;
  1003. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);virtual;
  1004. constructor ppuload(ppufile:tcompilerppufile);
  1005. destructor destroy;override;
  1006. function getcopy : tstoreddef;override;
  1007. { do not override this routine in platform-specific subclasses,
  1008. override ppuwrite_platform instead }
  1009. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  1010. procedure buildderef;override;
  1011. procedure deref;override;
  1012. function GetTypeName:string;override;
  1013. function is_publishable : tpublishproperty;override;
  1014. procedure calcsavesize(packenum: shortint);
  1015. function packedbitsize: asizeint; override;
  1016. procedure setmax(_max:asizeint);
  1017. procedure setmin(_min:asizeint);
  1018. function min:asizeint;
  1019. function max:asizeint;
  1020. function getfirstsym:tsym;
  1021. function int2enumsym(l: asizeint): tsym;
  1022. { returns basedef if assigned, otherwise self }
  1023. function getbasedef: tenumdef;
  1024. end;
  1025. tenumdefclass = class of tenumdef;
  1026. tsetdef = class(tstoreddef)
  1027. elementdef : tdef;
  1028. elementdefderef : tderef;
  1029. setbase,
  1030. setmax : asizeint;
  1031. constructor create(def: tdef; low, high: asizeint; doregister: boolean);virtual;
  1032. constructor ppuload(ppufile:tcompilerppufile);
  1033. function getcopy : tstoreddef;override;
  1034. { do not override this routine in platform-specific subclasses,
  1035. override ppuwrite_platform instead }
  1036. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  1037. procedure buildderef;override;
  1038. procedure deref;override;
  1039. function GetTypeName:string;override;
  1040. function is_publishable : tpublishproperty;override;
  1041. function alignment: shortint; override;
  1042. end;
  1043. tsetdefclass = class of tsetdef;
  1044. tgenericdummyentry = class
  1045. dummysym : tsym;
  1046. resolvedsym : tsym;
  1047. end;
  1048. tdefawaresymtablestack = class(TSymtablestack)
  1049. private
  1050. procedure add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1051. procedure remove_helpers_and_generics(st:tsymtable);inline;
  1052. procedure remove_helpers(st:tsymtable);
  1053. procedure remove_generics(st:tsymtable);
  1054. procedure pushcommon(st:tsymtable);inline;
  1055. public
  1056. procedure push(st: TSymtable); override;
  1057. procedure pushafter(st,afterst:TSymtable); override;
  1058. procedure pop(st: TSymtable); override;
  1059. end;
  1060. var
  1061. current_structdef: tabstractrecorddef; { used for private functions check !! }
  1062. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  1063. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  1064. cfiledef: tfiledefclass;
  1065. cvariantdef: tvariantdefclass;
  1066. cformaldef: tformaldefclass;
  1067. cforwarddef: tforwarddefclass;
  1068. cundefineddef: tundefineddefclass;
  1069. cerrordef: terrordefclass;
  1070. cpointerdef: tpointerdefclass;
  1071. crecorddef: trecorddefclass;
  1072. cimplementedinterface: timplementedinterfaceclass;
  1073. cobjectdef: tobjectdefclass;
  1074. cclassrefdef: tclassrefdefclass;
  1075. carraydef: tarraydefclass;
  1076. corddef: torddefclass;
  1077. cfloatdef: tfloatdefclass;
  1078. cprocvardef: tprocvardefclass;
  1079. cprocdef: tprocdefclass;
  1080. cstringdef: tstringdefclass;
  1081. cenumdef: tenumdefclass;
  1082. csetdef: tsetdefclass;
  1083. { default types }
  1084. generrordef, { error in definition }
  1085. voidpointertype, { pointer for Void-pointeddef }
  1086. charpointertype, { pointer for Char-pointeddef }
  1087. widecharpointertype, { pointer for WideChar-pointeddef }
  1088. voidcodepointertype, { pointer to code; corresponds to System.CodePointer }
  1089. voidstackpointertype, { the pointer type used for accessing parameters and local vars on the stack }
  1090. parentfpvoidpointertype, { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  1091. {$ifdef x86}
  1092. voidnearpointertype,
  1093. voidnearcspointertype,
  1094. voidneardspointertype,
  1095. voidnearsspointertype,
  1096. voidnearespointertype,
  1097. voidnearfspointertype,
  1098. voidneargspointertype,
  1099. {$ifdef i8086}
  1100. voidfarpointertype,
  1101. voidhugepointertype,
  1102. charnearpointertype,
  1103. charfarpointertype,
  1104. charhugepointertype,
  1105. bytefarpointertype, { used for Mem[] }
  1106. wordfarpointertype, { used for MemW[] }
  1107. longintfarpointertype, { used for MemL[] }
  1108. {$endif i8086}
  1109. {$endif x86}
  1110. {$ifdef wasm}
  1111. wasmvoidexternreftype,
  1112. {$endif wasm}
  1113. cundefinedtype,
  1114. cformaltype, { unique formal definition }
  1115. ctypedformaltype, { unique typed formal definition }
  1116. voidtype, { Void (procedure) }
  1117. cansichartype, { Char }
  1118. cwidechartype, { WideChar }
  1119. cchartype, { either cansichartype or cwidechartype. Do not free }
  1120. pasbool1type, { boolean type }
  1121. pasbool8type,
  1122. pasbool16type,
  1123. pasbool32type,
  1124. pasbool64type,
  1125. bool8type,
  1126. bool16type,
  1127. bool32type,
  1128. bool64type, { implement me }
  1129. {$ifdef llvm}
  1130. llvmbool1type, { LLVM i1 type }
  1131. {$endif llvm}
  1132. u8inttype, { 8-Bit unsigned integer }
  1133. s8inttype, { 8-Bit signed integer }
  1134. u16inttype, { 16-Bit unsigned integer }
  1135. s16inttype, { 16-Bit signed integer }
  1136. u24inttype, { 24-Bit unsigned integer }
  1137. s24inttype, { 24-Bit signed integer }
  1138. u32inttype, { 32-Bit unsigned integer }
  1139. s32inttype, { 32-Bit signed integer }
  1140. u40inttype, { 40-Bit unsigned integer }
  1141. s40inttype, { 40-Bit signed integer }
  1142. u48inttype, { 48-Bit unsigned integer }
  1143. s48inttype, { 48-Bit signed integer }
  1144. u56inttype, { 56-Bit unsigned integer }
  1145. s56inttype, { 56-Bit signed integer }
  1146. u64inttype, { 64-bit unsigned integer }
  1147. s64inttype, { 64-bit signed integer }
  1148. u128inttype, { 128-bit unsigned integer }
  1149. s128inttype, { 128-bit signed integer }
  1150. s32floattype, { 32 bit floating point number }
  1151. s64floattype, { 64 bit floating point number }
  1152. s80floattype, { 80 bit floating point number }
  1153. sc80floattype, { 80 bit floating point number but stored like in C }
  1154. s64currencytype, { pointer to a currency type }
  1155. cshortstringtype, { pointer to type of short string const }
  1156. clongstringtype, { pointer to type of long string const }
  1157. cansistringtype, { pointer to type of ansi string const }
  1158. cwidestringtype, { pointer to type of wide string const }
  1159. cunicodestringtype,
  1160. openshortstringtype, { pointer to type of an open shortstring,
  1161. needed for readln() }
  1162. openchararraytype, { pointer to type of an open array of char,
  1163. needed for readln() }
  1164. cfiletype, { get the same definition for all file }
  1165. { used for stabs }
  1166. methodpointertype, { typecasting of methodpointers to extract self }
  1167. nestedprocpointertype, { typecasting of nestedprocpointers to extract parentfp }
  1168. hresultdef,
  1169. typekindtype, { def of TTypeKind for correct handling of GetTypeKind parameters }
  1170. { we use only one variant def for every variant class }
  1171. cvarianttype,
  1172. colevarianttype,
  1173. { default integer type, normally s32inttype on 32 bit systems and s64bittype on 64 bit systems }
  1174. sinttype,
  1175. uinttype,
  1176. { integer types corresponding to OS_SINT/OS_INT }
  1177. ossinttype,
  1178. osuinttype,
  1179. { integer types corresponding to the ALU size, sizeof(aint) and the ALUSInt/ALUUInt types in the system unit }
  1180. alusinttype,
  1181. aluuinttype,
  1182. { integer types corresponding to SizeInt and SizeUInt for the target platform }
  1183. sizeuinttype,
  1184. sizesinttype,
  1185. { unsigned and signed ord type with the same size as a pointer }
  1186. ptruinttype,
  1187. ptrsinttype,
  1188. { unsigned and signed ord type with the same size as a codepointer }
  1189. codeptruinttype,
  1190. codeptrsinttype,
  1191. { several types to simulate more or less C++ objects for GDB }
  1192. vmttype,
  1193. vmtarraytype,
  1194. { type of classrefs, used for stabs }
  1195. pvmttype,
  1196. { return type of the setjmp function }
  1197. exceptionreasontype : tdef;
  1198. { pointer to the anchestor of all classes }
  1199. class_tobject : tobjectdef;
  1200. { pointer to the base type for custom attributes }
  1201. class_tcustomattribute : tobjectdef;
  1202. { pointer to the ancestor of all COM interfaces }
  1203. interface_iunknown : tobjectdef;
  1204. { pointer to the ancestor of all dispinterfaces }
  1205. interface_idispatch : tobjectdef;
  1206. { pointer to the TGUID type
  1207. of all interfaces }
  1208. rec_tguid : trecorddef;
  1209. { jump buffer type, used by setjmp }
  1210. rec_jmp_buf : trecorddef;
  1211. { system.texceptaddr type, used by fpc_pushexceptaddr }
  1212. rec_exceptaddr: trecorddef;
  1213. { Objective-C base types }
  1214. objc_metaclasstype,
  1215. objc_superclasstype,
  1216. objc_idtype,
  1217. objc_seltype : tpointerdef;
  1218. objc_objecttype : trecorddef;
  1219. { base type of @protocol(protocolname) Objective-C statements }
  1220. objc_protocoltype : tobjectdef;
  1221. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  1222. objc_fastenumeration : tobjectdef;
  1223. objc_fastenumerationstate : trecorddef;
  1224. {$ifdef llvm}
  1225. { llvm types }
  1226. { a unique def to identify any kind of metadata }
  1227. llvm_metadatatype : tdef;
  1228. {$endif llvm}
  1229. { Java base types }
  1230. { java.lang.Object }
  1231. java_jlobject : tobjectdef;
  1232. { java.lang.Throwable }
  1233. java_jlthrowable : tobjectdef;
  1234. { FPC base type for records }
  1235. java_fpcbaserecordtype : tobjectdef;
  1236. { java.lang.String }
  1237. java_jlstring : tobjectdef;
  1238. { java.lang.Enum }
  1239. java_jlenum : tobjectdef;
  1240. { java.util.EnumSet }
  1241. java_juenumset : tobjectdef;
  1242. { java.util.BitSet }
  1243. java_jubitset : tobjectdef;
  1244. { FPC java implementation of ansistrings }
  1245. java_ansistring : tobjectdef;
  1246. { FPC java implementation of shortstrings }
  1247. java_shortstring : tobjectdef;
  1248. { FPC java procvar base class }
  1249. java_procvarbase : tobjectdef;
  1250. { x86 vector types }
  1251. x86_m64type,
  1252. x86_m128type,
  1253. x86_m128dtype,
  1254. x86_m128itype,
  1255. x86_m256type,
  1256. x86_m256dtype,
  1257. x86_m256itype : tdef;
  1258. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1259. function make_dllmangledname(const dllname,importname:TSymStr;
  1260. import_nr : word; pco : tproccalloption):TSymStr;
  1261. { should be in the types unit, but the types unit uses the node stuff :( }
  1262. function is_interfacecom(def: tdef): boolean;
  1263. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  1264. function is_any_interface_kind(def: tdef): boolean;
  1265. function is_interfacecorba(def: tdef): boolean;
  1266. function is_interface(def: tdef): boolean;
  1267. function is_dispinterface(def: tdef): boolean;
  1268. function is_object(def: tdef): boolean;
  1269. function is_class(def: tdef): boolean;
  1270. function is_cppclass(def: tdef): boolean;
  1271. function is_objectpascal_helper(def: tdef): boolean;
  1272. function is_objcclass(def: tdef): boolean;
  1273. function is_objcclassref(def: tdef): boolean;
  1274. function is_objcprotocol(def: tdef): boolean;
  1275. function is_objccategory(def: tdef): boolean;
  1276. function is_objc_class_or_protocol(def: tdef): boolean;
  1277. function is_objc_protocol_or_category(def: tdef): boolean;
  1278. function is_classhelper(def: tdef): boolean;
  1279. function is_class_or_interface(def: tdef): boolean;
  1280. function is_class_or_interface_or_objc(def: tdef): boolean;
  1281. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  1282. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  1283. function is_class_or_interface_or_object(def: tdef): boolean;
  1284. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  1285. function is_implicit_pointer_object_type(def: tdef): boolean;
  1286. { returns true, if def is a type which is an implicit pointer to an array (dyn. array or dyn. string) }
  1287. function is_implicit_array_pointer(def: tdef): boolean;
  1288. function is_class_or_object(def: tdef): boolean;
  1289. function is_record(def: tdef): boolean;
  1290. function is_javaclass(def: tdef): boolean;
  1291. function is_javaclassref(def: tdef): boolean;
  1292. function is_javainterface(def: tdef): boolean;
  1293. function is_java_class_or_interface(def: tdef): boolean;
  1294. procedure loadobjctypes;
  1295. procedure maybeloadcocoatypes;
  1296. function use_vectorfpu(def : tdef) : boolean;
  1297. function getansistringcodepage:tstringencoding; inline;
  1298. function getansistringdef:tstringdef;
  1299. function getparaencoding(def:tdef):tstringencoding; inline;
  1300. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1301. function get_recorddef(prefix:tinternaltypeprefix;const fields:array of tdef; packrecords:shortint): trecorddef;
  1302. { get a table def of the form
  1303. record
  1304. count: countdef;
  1305. elements: array[0..count-1] of elementdef
  1306. end;
  1307. Returns both the outer record and the inner arraydef
  1308. }
  1309. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1310. function fileinfo_of_typesym_in_def(def:tdef;sym:tsym;out filepos:tfileposinfo):boolean;
  1311. procedure reset_all_default_types; {housekeeping for Textmode IDE sanity}
  1312. implementation
  1313. uses
  1314. SysUtils,
  1315. cutils,
  1316. { global }
  1317. verbose,
  1318. { target }
  1319. systems,paramgr,
  1320. { symtable }
  1321. symsym,symtable,defutil,objcdef,
  1322. { parser }
  1323. pgenutil,
  1324. { module }
  1325. fmodule,ppu,
  1326. { other }
  1327. aasmbase,
  1328. gendef,
  1329. fpchash,
  1330. entfile
  1331. ;
  1332. {****************************************************************************
  1333. Helpers
  1334. ****************************************************************************}
  1335. function fileinfo_of_typesym_in_def(def:tdef;sym:tsym;out filepos:tfileposinfo):boolean;
  1336. var
  1337. gst : tgetsymtable;
  1338. st : tsymtable;
  1339. i : longint;
  1340. srsym : tsym;
  1341. begin
  1342. result:=false;
  1343. if sym.typ<>typesym then
  1344. exit;
  1345. for gst:=low(tgetsymtable) to high(tgetsymtable) do
  1346. begin
  1347. st:=def.getsymtable(gst);
  1348. if not assigned(st) then
  1349. continue;
  1350. for i:=0 to st.symlist.count-1 do
  1351. begin
  1352. srsym:=tsym(st.symlist[i]);
  1353. case srsym.typ of
  1354. fieldvarsym,
  1355. paravarsym:
  1356. if tabstractvarsym(srsym).vardef.typesym=sym then
  1357. begin
  1358. filepos:=srsym.fileinfo;
  1359. result:=true;
  1360. break;
  1361. end;
  1362. typesym:
  1363. begin
  1364. result:=fileinfo_of_typesym_in_def(ttypesym(srsym).typedef,sym,filepos);
  1365. if result then
  1366. break;
  1367. end
  1368. else
  1369. ;
  1370. end;
  1371. end;
  1372. if result then
  1373. break;
  1374. end;
  1375. { for this we would need the position of the result declaration :/ }
  1376. {if not result and
  1377. (def.typ in [procdef,procvardef]) and
  1378. assigned(tabstractprocdef(def).returndef) and
  1379. (tabstractprocdef(def).returndef.typesym=sym) then
  1380. begin
  1381. result:=true;
  1382. end;}
  1383. end;
  1384. function getansistringcodepage:tstringencoding; inline;
  1385. begin
  1386. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1387. result:=current_settings.sourcecodepage
  1388. else
  1389. result:=0;
  1390. end;
  1391. function getansistringdef:tstringdef;
  1392. var
  1393. symtable:tsymtable;
  1394. oldstack : tsymtablestack;
  1395. begin
  1396. { if a codepage is explicitly defined in this mudule we need to return
  1397. a replacement for ansistring def }
  1398. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1399. begin
  1400. if not assigned(current_module) then
  1401. internalerror(2011101301);
  1402. { codepage can be redeclared only once per unit so we don't need a list of
  1403. redefined ansistring but only one pointer }
  1404. if not assigned(current_module.ansistrdef) then
  1405. begin
  1406. { if we did not create it yet we need to do this now }
  1407. if current_module.in_interface then
  1408. symtable:=current_module.globalsymtable
  1409. else
  1410. symtable:=current_module.localsymtable;
  1411. { create a temporary stack as it's not good (TM) to mess around
  1412. with the order if the unit contains generics or helpers; don't
  1413. use a def aware symtablestack though }
  1414. oldstack:=symtablestack;
  1415. symtablestack:=tsymtablestack.create;
  1416. symtablestack.push(symtable);
  1417. current_module.ansistrdef:=cstringdef.createansi(current_settings.sourcecodepage,true);
  1418. symtablestack.pop(symtable);
  1419. symtablestack.free;
  1420. symtablestack:=oldstack;
  1421. end;
  1422. result:=tstringdef(current_module.ansistrdef);
  1423. end
  1424. else
  1425. result:=tstringdef(cansistringtype);
  1426. end;
  1427. function getparaencoding(def:tdef):tstringencoding; inline;
  1428. begin
  1429. { don't pass CP_NONE encoding to internal functions
  1430. they expect 0 encoding instead
  1431. exception: result of string concatenation, because if you pass the
  1432. result of a string concatenation to a rawbytestring, the result of
  1433. that concatenation shouldn't be converted to defaultsystemcodepage
  1434. if all strings have the same type }
  1435. result:=tstringdef(def).encoding;
  1436. if result=globals.CP_NONE then
  1437. result:=0
  1438. end;
  1439. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1440. var
  1441. typ: ttypesym;
  1442. name: string;
  1443. begin
  1444. name:=internaltypeprefixName[itp_threadvar_record]+def.unique_id_str;
  1445. typ:=try_search_current_module_type(name);
  1446. if assigned(typ) then
  1447. begin
  1448. result:=trecorddef(ttypesym(typ).typedef);
  1449. index_field:=tsym(result.symtable.symlist[0]);
  1450. non_mt_data_field:=tsym(result.symtable.symlist[1]);
  1451. exit;
  1452. end;
  1453. { set recordalinmin to sizeof(pint), so the second field gets put at
  1454. offset = sizeof(pint) as expected }
  1455. result:=crecorddef.create_global_internal(
  1456. name,sizeof(pint),sizeof(pint));
  1457. {$ifdef cpu16bitaddr}
  1458. index_field:=result.add_field_by_def('',u16inttype);
  1459. {$else cpu16bitaddr}
  1460. index_field:=result.add_field_by_def('',u32inttype);
  1461. {$endif cpu16bitaddr}
  1462. non_mt_data_field:=result.add_field_by_def('',def);
  1463. { no need to add alignment padding, we won't create arrays of these }
  1464. end;
  1465. function get_recorddef(prefix:tinternaltypeprefix; const fields:array of tdef; packrecords:shortint): trecorddef;
  1466. var
  1467. fieldlist: tfplist;
  1468. srsym: tsym;
  1469. srsymtable: tsymtable;
  1470. i: longint;
  1471. name : TIDString;
  1472. begin
  1473. name:=copy(internaltypeprefixName[prefix],2,length(internaltypeprefixName[prefix]));
  1474. if searchsym_type(name,srsym,srsymtable) then
  1475. begin
  1476. result:=trecorddef(ttypesym(srsym).typedef);
  1477. exit
  1478. end;
  1479. { also always search in the current module (symtables are popped for
  1480. RTTI related code already) }
  1481. if searchsym_in_module(pointer(current_module),name,srsym,srsymtable) then
  1482. begin
  1483. result:=trecorddef(ttypesym(srsym).typedef);
  1484. exit;
  1485. end;
  1486. fieldlist:=tfplist.create;
  1487. for i:=low(fields) to high(fields) do
  1488. fieldlist.add(fields[i]);
  1489. result:=crecorddef.create_global_internal(internaltypeprefixName[prefix],packrecords,
  1490. targetinfos[target_info.system]^.alignment.recordalignmin);
  1491. result.add_fields_from_deflist(fieldlist);
  1492. fieldlist.free;
  1493. end;
  1494. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1495. var
  1496. fields: tfplist;
  1497. name: TIDString;
  1498. srsym: tsym;
  1499. fieldvarsym: tfieldvarsym;
  1500. srsymtable: tsymtable;
  1501. begin
  1502. { already created a message string table with this number of elements
  1503. in this unit -> reuse the def }
  1504. name:=internaltypeprefixName[prefix]+tostr(count);
  1505. if searchsym_type(copy(name,2,length(name)),srsym,srsymtable) then
  1506. begin
  1507. recdef:=trecorddef(ttypesym(srsym).typedef);
  1508. fieldvarsym:=trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size);
  1509. if fieldvarsym<>nil then
  1510. arrdef:=tarraydef(fieldvarsym.vardef)
  1511. else
  1512. arrdef:=nil;
  1513. exit
  1514. end;
  1515. { also always search in the current module (symtables are popped for
  1516. RTTI related code already) }
  1517. if searchsym_in_module(pointer(current_module),copy(name,2,length(name)),srsym,srsymtable) then
  1518. begin
  1519. recdef:=trecorddef(ttypesym(srsym).typedef);
  1520. fieldvarsym:=trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size);
  1521. if fieldvarsym<>nil then
  1522. arrdef:=tarraydef(fieldvarsym.vardef)
  1523. else
  1524. arrdef:=nil;
  1525. exit;
  1526. end;
  1527. recdef:=crecorddef.create_global_internal(name,packrecords,
  1528. targetinfos[target_info.system]^.alignment.recordalignmin);
  1529. fields:=tfplist.create;
  1530. fields.add(countdef);
  1531. if count>0 then
  1532. begin
  1533. arrdef:=carraydef.create(0,count-1,sizeuinttype);
  1534. arrdef.elementdef:=elementdef;
  1535. fields.add(arrdef);
  1536. end
  1537. else
  1538. arrdef:=nil;
  1539. recdef.add_fields_from_deflist(fields);
  1540. fields.free;
  1541. end;
  1542. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1543. var
  1544. s,t,
  1545. prefix : TSymStr;
  1546. hash : qword;
  1547. begin
  1548. prefix:='';
  1549. if not assigned(st) then
  1550. internalerror(200204212);
  1551. repeat
  1552. { sub procedures }
  1553. while (st.symtabletype in [localsymtable,parasymtable]) do
  1554. begin
  1555. if st.defowner.typ<>procdef then
  1556. internalerror(200204173);
  1557. { Add the full mangledname of the routine to prevent
  1558. conflicts with two overloads both having a local entity
  1559. -- routine (tb0314), class, interface -- with the same name }
  1560. s:=tprocdef(st.defowner).procsym.name;
  1561. s:=s+tprocdef(st.defowner).mangledprocparanames(Length(s));
  1562. if prefix<>'' then
  1563. begin
  1564. if length(prefix)>(maxidlen-length(s)-1) then
  1565. begin
  1566. hash:=InitFnv64;
  1567. hash:=UpdateFnv64(hash,prefix[1],length(prefix));
  1568. prefix:='$H'+Base64Mangle(hash);
  1569. end;
  1570. prefix:=s+'_'+prefix
  1571. end
  1572. else
  1573. prefix:=s;
  1574. st:=st.defowner.owner;
  1575. end;
  1576. { object/classes symtable, nested type definitions in classes require the while loop }
  1577. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  1578. begin
  1579. if not (st.defowner.typ in [objectdef,recorddef]) then
  1580. internalerror(200204174);
  1581. if length(prefix)>(maxidlen-length(tabstractrecorddef(st.defowner).objname^)-3) then
  1582. begin
  1583. hash:=InitFnv64;
  1584. hash:=UpdateFnv64(hash,prefix[1],length(prefix));
  1585. prefix:='$H'+Base64Mangle(hash);
  1586. end;
  1587. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  1588. st:=st.defowner.owner;
  1589. end;
  1590. { local classes & interfaces are possible (because of closures)
  1591. or parasymtable for this case:
  1592. class function Trace<TResult>(const Func: TFunc<TLogToken, TResult>): TResult;
  1593. }
  1594. if not (st.symtabletype in [localsymtable,parasymtable]) then
  1595. break;
  1596. prefix:='$'+prefix;
  1597. until false;
  1598. { symtable must now be static or global }
  1599. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  1600. internalerror(200204175);
  1601. { The mangled name is made out of at most 4 parts:
  1602. 1) Optional typeprefix given as first parameter
  1603. with '_$' appended if not empty
  1604. 2) Unit name or 'P$'+program name (never empty)
  1605. 3) optional prefix variable that contains a unique
  1606. name for the local symbol table (prepended with '$_$'
  1607. if not empty)
  1608. 4) suffix as given as third parameter,
  1609. also optional (i.e. can be empty)
  1610. prepended by '_$$_' if not empty }
  1611. result:='';
  1612. if typeprefix<>'' then
  1613. result:=result+typeprefix+'_$';
  1614. { Add P$ for program, which can have the same name as
  1615. a unit }
  1616. if (TSymtable(main_module.localsymtable)=st) and
  1617. (not main_module.is_unit) then
  1618. result:=result+'P$'+st.name^
  1619. else
  1620. result:=result+st.name^;
  1621. if prefix<>'' then
  1622. result:=result+'$_$'+prefix;
  1623. if suffix<>'' then
  1624. result:=result+'_$$_'+suffix;
  1625. if length(result)>(maxidlen-1) then
  1626. begin
  1627. hash:=InitFnv64;
  1628. hash:=UpdateFnv64(hash,result[1],length(result));
  1629. result:=copy(result,1,maxidlen-(high(Base64OfUint64String)-low(Base64OfUint64String)+1)-2)+'$H'+Base64Mangle(hash);
  1630. end;
  1631. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1632. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1633. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1634. { those in the debug map, leading to troubles with dsymutil). So always }
  1635. { add an underscore on darwin. }
  1636. if (target_info.system in systems_darwin) then
  1637. result := '_' + result;
  1638. end;
  1639. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1640. var
  1641. crc : cardinal;
  1642. i : longint;
  1643. use_crc : boolean;
  1644. dllprefix : TSymStr;
  1645. begin
  1646. if (target_info.system in (systems_all_windows + systems_nativent +
  1647. [system_i386_emx, system_i386_os2]))
  1648. and (dllname <> '') then
  1649. begin
  1650. dllprefix:=lower(ExtractFileName(dllname));
  1651. { Remove .dll suffix if present }
  1652. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1653. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1654. use_crc:=false;
  1655. for i:=1 to length(dllprefix) do
  1656. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1657. begin
  1658. use_crc:=true;
  1659. break;
  1660. end;
  1661. if use_crc then
  1662. begin
  1663. crc:=0;
  1664. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1665. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1666. end
  1667. else
  1668. dllprefix:='_$dll$'+dllprefix+'$';
  1669. if importname<>'' then
  1670. result:=dllprefix+importname
  1671. else
  1672. result:=dllprefix+'_index_'+tostr(import_nr);
  1673. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1674. { This allows to import VC++ mangled names from DLLs. }
  1675. { Do not perform replacement, if external symbol is not imported from DLL. }
  1676. if (dllname<>'') then
  1677. begin
  1678. Replace(result,'?','__q$$');
  1679. {$ifdef arm}
  1680. { @ symbol is not allowed in ARM assembler only }
  1681. Replace(result,'@','__a$$');
  1682. {$endif arm}
  1683. end;
  1684. end
  1685. else
  1686. begin
  1687. if importname<>'' then
  1688. begin
  1689. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1690. result:=importname
  1691. else
  1692. result:=target_info.Cprefix+importname;
  1693. end
  1694. else
  1695. result:='_index_'+tostr(import_nr);
  1696. end;
  1697. end;
  1698. {****************************************************************************
  1699. TDEFAWARESYMTABLESTACK
  1700. (symtablestack descendant that does some special actions on
  1701. the pushed/popped symtables)
  1702. ****************************************************************************}
  1703. procedure tdefawaresymtablestack.add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1704. var
  1705. s: TSymStr;
  1706. list: TFPObjectList;
  1707. def: tdef;
  1708. sym : tsym;
  1709. i: integer;
  1710. begin
  1711. { search the symtable from first to last; the helper to use will be the
  1712. last one in the list }
  1713. for i:=0 to st.symlist.count-1 do
  1714. begin
  1715. sym:=tsym(st.symlist[i]);
  1716. if not (sym.typ in [typesym,procsym]) then
  1717. continue;
  1718. if sym.typ=typesym then
  1719. def:=ttypesym(st.SymList[i]).typedef
  1720. else
  1721. def:=nil;
  1722. if is_objectpascal_helper(def) then
  1723. begin
  1724. s:=generate_objectpascal_helper_key(tobjectdef(def).extendeddef);
  1725. Message1(sym_d_adding_helper_for,s);
  1726. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1727. if not assigned(list) then
  1728. begin
  1729. list:=TFPObjectList.Create(false);
  1730. current_module.extendeddefs.Add(s,list);
  1731. end;
  1732. list.Add(def);
  1733. end
  1734. else
  1735. begin
  1736. if addgenerics then
  1737. add_generic_dummysym(sym);
  1738. { add nested helpers as well }
  1739. if assigned(def) and
  1740. (def.typ in [recorddef,objectdef]) and
  1741. (sto_has_helper in tabstractrecorddef(def).symtable.tableoptions) and
  1742. not is_owned_by(def,tdef(st.defowner)) then
  1743. add_helpers_and_generics(tabstractrecorddef(def).symtable,false);
  1744. end;
  1745. end;
  1746. end;
  1747. procedure tdefawaresymtablestack.remove_helpers_and_generics(st:tsymtable);
  1748. begin
  1749. if sto_has_helper in st.tableoptions then
  1750. remove_helpers(st);
  1751. if sto_has_generic in st.tableoptions then
  1752. remove_generics(st);
  1753. end;
  1754. procedure tdefawaresymtablestack.remove_helpers(st:TSymtable);
  1755. var
  1756. i, j: integer;
  1757. tmpst: TSymtable;
  1758. list: TFPObjectList;
  1759. begin
  1760. for i:=current_module.extendeddefs.count-1 downto 0 do
  1761. begin
  1762. list:=TFPObjectList(current_module.extendeddefs[i]);
  1763. for j:=list.count-1 downto 0 do
  1764. begin
  1765. if not (list[j] is tobjectdef) then
  1766. Internalerror(2011031501);
  1767. tmpst:=tobjectdef(list[j]).owner;
  1768. repeat
  1769. if tmpst=st then
  1770. begin
  1771. list.delete(j);
  1772. break;
  1773. end
  1774. else
  1775. begin
  1776. if assigned(tmpst.defowner) then
  1777. tmpst:=tmpst.defowner.owner
  1778. else
  1779. tmpst:=nil;
  1780. end;
  1781. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1782. end;
  1783. if list.count=0 then
  1784. current_module.extendeddefs.delete(i);
  1785. end;
  1786. end;
  1787. procedure tdefawaresymtablestack.remove_generics(st:tsymtable);
  1788. var
  1789. i,j : longint;
  1790. entry : tgenericdummyentry;
  1791. list : tfpobjectlist;
  1792. begin
  1793. for i:=current_module.genericdummysyms.count-1 downto 0 do
  1794. begin
  1795. list:=tfpobjectlist(current_module.genericdummysyms[i]);
  1796. if not assigned(list) then
  1797. continue;
  1798. for j:=list.count-1 downto 0 do
  1799. begin
  1800. entry:=tgenericdummyentry(list[j]);
  1801. if entry.dummysym.owner=st then
  1802. list.delete(j);
  1803. end;
  1804. if list.count=0 then
  1805. current_module.genericdummysyms.delete(i);
  1806. end;
  1807. end;
  1808. procedure tdefawaresymtablestack.pushcommon(st:tsymtable);
  1809. begin
  1810. if (sto_has_generic in st.tableoptions) or
  1811. (
  1812. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1813. (sto_has_helper in st.tableoptions)
  1814. ) then
  1815. { nested helpers will be added as well }
  1816. add_helpers_and_generics(st,true);
  1817. end;
  1818. procedure tdefawaresymtablestack.push(st: TSymtable);
  1819. begin
  1820. pushcommon(st);
  1821. inherited push(st);
  1822. end;
  1823. procedure tdefawaresymtablestack.pushafter(st,afterst:TSymtable);
  1824. begin
  1825. pushcommon(st);
  1826. inherited pushafter(st,afterst);
  1827. end;
  1828. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1829. begin
  1830. inherited pop(st);
  1831. if (sto_has_generic in st.tableoptions) or
  1832. (
  1833. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1834. (sto_has_helper in st.tableoptions)
  1835. ) then
  1836. { nested helpers will be removed as well }
  1837. remove_helpers_and_generics(st);
  1838. end;
  1839. {****************************************************************************
  1840. TDEF (base class for definitions)
  1841. ****************************************************************************}
  1842. constructor tgenericconstraintdata.create;
  1843. begin
  1844. interfaces:=tfpobjectlist.create(false);
  1845. interfacesderef:=tfplist.create;
  1846. end;
  1847. destructor tgenericconstraintdata.destroy;
  1848. begin
  1849. TFPList.FreeAndNilDisposing(interfacesderef,typeinfo(tderef));
  1850. interfaces.free;
  1851. inherited destroy;
  1852. end;
  1853. procedure tgenericconstraintdata.ppuload(ppufile: tcompilerppufile);
  1854. var
  1855. cnt,i : longint;
  1856. intfderef : pderef;
  1857. begin
  1858. ppufile.getset(tppuset1(flags));
  1859. cnt:=ppufile.getlongint;
  1860. for i:=0 to cnt-1 do
  1861. begin
  1862. new(intfderef);
  1863. ppufile.getderef(intfderef^);
  1864. interfacesderef.add(intfderef);
  1865. end;
  1866. end;
  1867. procedure tgenericconstraintdata.ppuwrite(ppufile: tcompilerppufile);
  1868. var
  1869. i : longint;
  1870. begin
  1871. ppufile.putset(tppuset1(flags));
  1872. ppufile.putlongint(interfacesderef.count);
  1873. for i:=0 to interfacesderef.count-1 do
  1874. ppufile.putderef(pderef(interfacesderef[i])^);
  1875. end;
  1876. procedure tgenericconstraintdata.buildderef;
  1877. var
  1878. intfderef : pderef;
  1879. i : longint;
  1880. begin
  1881. interfacesderef.capacity:=interfacesderef.count+interfaces.count;
  1882. for i:=0 to interfaces.count-1 do
  1883. begin
  1884. new(intfderef);
  1885. intfderef^.build(tobjectdef(interfaces[i]));
  1886. interfacesderef.add(intfderef);
  1887. end;
  1888. end;
  1889. procedure tgenericconstraintdata.deref;
  1890. var
  1891. i : longint;
  1892. begin
  1893. interfaces.capacity:=interfaces.count+interfacesderef.count;
  1894. for i:=0 to interfacesderef.count-1 do
  1895. interfaces.add(pderef(interfacesderef[i])^.resolve);
  1896. end;
  1897. procedure tstoreddef.writeentry(ppufile: tcompilerppufile; ibnr: byte);
  1898. begin
  1899. ppuwrite_platform(ppufile);
  1900. ppufile.writeentry(ibnr);
  1901. end;
  1902. procedure tstoreddef.ppuwrite_platform(ppufile: tcompilerppufile);
  1903. begin
  1904. { by default: do nothing }
  1905. end;
  1906. procedure tstoreddef.ppuload_platform(ppufile: tcompilerppufile);
  1907. begin
  1908. { by default: do nothing }
  1909. end;
  1910. class procedure tstoreddef.setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  1911. var
  1912. reusablesymtab: tsymtable;
  1913. begin
  1914. { must not yet belong to a symtable }
  1915. if assigned(newdef.owner) then
  1916. internalerror(2015111503);
  1917. reusablesymtab:=origdef.getreusablesymtab;
  1918. res^.Data:=newdef;
  1919. reusablesymtab.insertdef(newdef);
  1920. symtablestack:=oldsymtablestack;
  1921. end;
  1922. constructor tstoreddef.create(dt:tdeftyp;doregister:boolean);
  1923. begin
  1924. inherited create(dt);
  1925. savesize := 0;
  1926. {$ifdef EXTDEBUG}
  1927. fileinfo := current_filepos;
  1928. {$endif}
  1929. generictokenbuf:=nil;
  1930. genericdef:=nil;
  1931. typesymderef.reset;
  1932. genericdefderef.reset;
  1933. { Don't register forwarddefs, they are disposed at the
  1934. end of an type block }
  1935. if (dt=forwarddef) then
  1936. exit;
  1937. { Register in symtable stack }
  1938. if doregister then
  1939. begin
  1940. { immediately register interface defs, as they will always be
  1941. written to the ppu, their defid influences the interface crc and
  1942. if we wait, depending on e.g. compiler defines they may get a
  1943. different defid (e.g. when a function is called, its procdef is
  1944. registered, so depending on whether or not, or when, an interface
  1945. procedure is called in the implementation, that may change its
  1946. defid otherwise) }
  1947. if assigned(current_module) and
  1948. current_module.in_interface then
  1949. register_def
  1950. else
  1951. maybe_put_in_symtable_stack;
  1952. end;
  1953. end;
  1954. destructor tstoreddef.destroy;
  1955. begin
  1956. { Direct calls are not allowed, use symtable.deletedef() }
  1957. if assigned(owner) then
  1958. internalerror(200612311);
  1959. if assigned(generictokenbuf) then
  1960. begin
  1961. generictokenbuf.free;
  1962. generictokenbuf:=nil;
  1963. end;
  1964. rtti_attribute_list.free;
  1965. genericparas.free;
  1966. TFPList.FreeAndNilDisposing(genericparaderefs,typeinfo(tderef));
  1967. genconstraintdata.free;
  1968. {$ifndef symansistr}
  1969. stringdispose(_fullownerhierarchyname);
  1970. {$endif not symansistr}
  1971. inherited destroy;
  1972. end;
  1973. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1974. var
  1975. sizeleft,i,cnt : longint;
  1976. buf : array[0..255] of byte;
  1977. symderef : pderef;
  1978. begin
  1979. inherited create(dt);
  1980. DefId:=ppufile.getlongint;
  1981. current_module.deflist[DefId]:=self;
  1982. {$ifdef EXTDEBUG}
  1983. fillchar(fileinfo,sizeof(fileinfo),0);
  1984. {$endif}
  1985. { load }
  1986. ppufile.getderef(typesymderef);
  1987. ppufile.getset(tppuset2(defoptions));
  1988. ppufile.getset(tppuset1(defstates));
  1989. if df_unique in defoptions then
  1990. ppufile.getderef(orgdefderef);
  1991. if df_genconstraint in defoptions then
  1992. begin
  1993. genconstraintdata:=tgenericconstraintdata.create;
  1994. genconstraintdata.ppuload(ppufile);
  1995. end;
  1996. if [df_generic,df_specialization]*defoptions<>[] then
  1997. begin
  1998. cnt:=ppufile.getlongint;
  1999. if cnt>0 then
  2000. begin
  2001. genericparas:=tfphashobjectlist.create(false);
  2002. genericparaderefs:=tfplist.create;
  2003. for i:=0 to cnt-1 do
  2004. begin
  2005. genericparas.add(ppufile.getstring,nil);
  2006. New(symderef);
  2007. ppufile.getderef(symderef^);
  2008. genericparaderefs.add(symderef);
  2009. end;
  2010. end;
  2011. end;
  2012. if df_generic in defoptions then
  2013. begin
  2014. sizeleft:=ppufile.getlongint;
  2015. initgeneric;
  2016. while sizeleft>0 do
  2017. begin
  2018. if sizeleft>sizeof(buf) then
  2019. i:=sizeof(buf)
  2020. else
  2021. i:=sizeleft;
  2022. ppufile.getdata(buf,i);
  2023. generictokenbuf.write(buf,i);
  2024. dec(sizeleft,i);
  2025. end;
  2026. end;
  2027. if df_specialization in defoptions then
  2028. ppufile.getderef(genericdefderef);
  2029. rtti_attribute_list:=trtti_attribute_list.ppuload(ppufile);
  2030. end;
  2031. function tstoreddef.needs_separate_initrtti:boolean;
  2032. begin
  2033. result:=false;
  2034. end;
  2035. function tstoreddef.rtti_mangledname(rt : trttitype) : TSymStr;
  2036. var
  2037. prefix : string[4];
  2038. begin
  2039. if (rt=fullrtti) or (not needs_separate_initrtti) then
  2040. begin
  2041. prefix:='RTTI';
  2042. include(defstates,ds_rtti_table_used);
  2043. end
  2044. else
  2045. begin
  2046. prefix:='INIT';
  2047. include(defstates,ds_init_table_used);
  2048. end;
  2049. if assigned(typesym) and
  2050. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  2051. result:=make_mangledname(prefix,typesym.owner,typesym.name)
  2052. else
  2053. result:=make_mangledname(prefix,findunitsymtable(owner),'def'+unique_id_str)
  2054. end;
  2055. function tstoreddef.OwnerHierarchyName: string;
  2056. var
  2057. tmp: tdef;
  2058. begin
  2059. tmp:=self;
  2060. result:='';
  2061. repeat
  2062. { can be not assigned in case of a forwarddef }
  2063. if assigned(tmp.owner) and
  2064. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2065. tmp:=tdef(tmp.owner.defowner)
  2066. else
  2067. break;
  2068. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  2069. until tmp=nil;
  2070. end;
  2071. function tstoreddef.OwnerHierarchyPrettyName: string;
  2072. var
  2073. tmp: tdef;
  2074. begin
  2075. tmp:=self;
  2076. result:='';
  2077. repeat
  2078. { can be not assigned in case of a forwarddef }
  2079. if assigned(tmp.owner) and
  2080. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2081. tmp:=tdef(tmp.owner.defowner)
  2082. else
  2083. break;
  2084. result:=tabstractrecorddef(tmp).typesymbolprettyname+'.'+result;
  2085. until tmp=nil;
  2086. end;
  2087. function tstoreddef.fullownerhierarchyname(skipprocparams:boolean): TSymStr;
  2088. var
  2089. lastowner: tsymtable;
  2090. tmp: tdef;
  2091. pno: tprocnameoptions;
  2092. begin
  2093. {$ifdef symansistr}
  2094. if not skipprocparams and (_fullownerhierarchyname<>'') then
  2095. exit(_fullownerhierarchyname);
  2096. {$else symansistr}
  2097. if not skipprocparams and assigned(_fullownerhierarchyname) then
  2098. exit(_fullownerhierarchyname^);
  2099. {$endif symansistr}
  2100. { the def can only reside inside structured types or
  2101. procedures/functions/methods }
  2102. tmp:=self;
  2103. result:='';
  2104. repeat
  2105. lastowner:=tmp.owner;
  2106. { can be not assigned in case of a forwarddef }
  2107. if not assigned(lastowner) then
  2108. break
  2109. else
  2110. tmp:=tdef(lastowner.defowner);
  2111. if not assigned(tmp) then
  2112. break;
  2113. if tmp.typ in [recorddef,objectdef] then
  2114. result:=tabstractrecorddef(tmp).objrealname^+'.'+result
  2115. else
  2116. if tmp.typ=procdef then
  2117. begin
  2118. pno:=[pno_paranames,pno_proctypeoption];
  2119. if skipprocparams then
  2120. include(pno,pno_noparams);
  2121. result:=tprocdef(tmp).customprocname(pno)+'.'+result;
  2122. end;
  2123. until tmp=nil;
  2124. { add the unit name }
  2125. if assigned(lastowner) and
  2126. assigned(lastowner.realname) then
  2127. result:=lastowner.realname^+'.'+result;
  2128. if not skipprocparams then
  2129. { don't store the name in this case }
  2130. {$ifdef symansistr}
  2131. _fullownerhierarchyname:=result;
  2132. {$else symansistr}
  2133. _fullownerhierarchyname:=stringdup(result);
  2134. {$endif symansistr}
  2135. end;
  2136. function tstoreddef.in_currentunit: boolean;
  2137. var
  2138. st: tsymtable;
  2139. begin
  2140. st:=owner;
  2141. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  2142. st:=st.defowner.owner;
  2143. result:=st.iscurrentunit;
  2144. end;
  2145. function tstoreddef.getcopy : tstoreddef;
  2146. begin
  2147. Message(sym_e_cant_create_unique_type);
  2148. getcopy:=cerrordef.create;
  2149. end;
  2150. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  2151. var
  2152. sizeleft,i : longint;
  2153. buf : array[0..255] of byte;
  2154. oldintfcrc : boolean;
  2155. begin
  2156. if defid<0 then
  2157. internalerror(2015101401);
  2158. ppufile.putlongint(DefId);
  2159. ppufile.putderef(typesymderef);
  2160. ppufile.putset(tppuset2(defoptions));
  2161. oldintfcrc:=ppufile.do_crc;
  2162. ppufile.do_crc:=false;
  2163. ppufile.putset(tppuset1(defstates));
  2164. if df_unique in defoptions then
  2165. ppufile.putderef(orgdefderef);
  2166. if df_genconstraint in defoptions then
  2167. genconstraintdata.ppuwrite(ppufile);
  2168. if [df_generic,df_specialization]*defoptions<>[] then
  2169. begin
  2170. if not assigned(genericparas) then
  2171. ppufile.putlongint(0)
  2172. else
  2173. begin
  2174. if not assigned(genericparaderefs) then
  2175. internalerror(2014052305);
  2176. ppufile.putlongint(genericparas.count);
  2177. for i:=0 to genericparas.count-1 do
  2178. begin
  2179. ppufile.putstring(genericparas.nameofindex(i));
  2180. ppufile.putderef(pderef(genericparaderefs[i])^);
  2181. end;
  2182. end;
  2183. end;
  2184. if df_generic in defoptions then
  2185. begin
  2186. if assigned(generictokenbuf) then
  2187. begin
  2188. sizeleft:=generictokenbuf.size;
  2189. generictokenbuf.seek(0);
  2190. end
  2191. else
  2192. sizeleft:=0;
  2193. ppufile.putlongint(sizeleft);
  2194. while sizeleft>0 do
  2195. begin
  2196. if sizeleft>sizeof(buf) then
  2197. i:=sizeof(buf)
  2198. else
  2199. i:=sizeleft;
  2200. generictokenbuf.read(buf,i);
  2201. ppufile.putdata(buf,i);
  2202. dec(sizeleft,i);
  2203. end;
  2204. end;
  2205. ppufile.do_crc:=oldintfcrc;
  2206. if df_specialization in defoptions then
  2207. ppufile.putderef(genericdefderef);
  2208. trtti_attribute_list.ppuwrite(rtti_attribute_list,ppufile);
  2209. end;
  2210. procedure tstoreddef.ppuload_subentries(ppufile: tcompilerppufile);
  2211. begin
  2212. trtti_attribute_list.ppuload_subentries(rtti_attribute_list,ppufile);
  2213. end;
  2214. procedure tstoreddef.ppuwrite_subentries(ppufile: tcompilerppufile);
  2215. begin
  2216. trtti_attribute_list.ppuwrite_subentries(rtti_attribute_list,ppufile);
  2217. end;
  2218. procedure tstoreddef.buildderef;
  2219. var
  2220. i : longint;
  2221. sym : tsym;
  2222. symderef : pderef;
  2223. begin
  2224. if not registered then
  2225. register_def;
  2226. typesymderef.build(typesym);
  2227. orgdefderef.build(orgdef);
  2228. genericdefderef.build(genericdef);
  2229. if assigned(rtti_attribute_list) then
  2230. rtti_attribute_list.buildderef;
  2231. if assigned(genconstraintdata) then
  2232. genconstraintdata.buildderef;
  2233. if assigned(genericparas) then
  2234. begin
  2235. if not assigned(genericparaderefs) then
  2236. genericparaderefs:=tfplist.create;
  2237. genericparaderefs.capacity:=genericparaderefs.count+genericparas.count;
  2238. for i:=0 to genericparas.count-1 do
  2239. begin
  2240. sym:=tsym(genericparas.items[i]);
  2241. new(symderef);
  2242. symderef^.build(sym);
  2243. genericparaderefs.add(symderef);
  2244. end;
  2245. end;
  2246. end;
  2247. procedure tstoreddef.buildderefimpl;
  2248. begin
  2249. end;
  2250. procedure tstoreddef.deref;
  2251. var
  2252. symderef : pderef;
  2253. i : longint;
  2254. begin
  2255. typesym:=ttypesym(typesymderef.resolve);
  2256. if df_unique in defoptions then
  2257. orgdef:=tstoreddef(orgdefderef.resolve);
  2258. if df_specialization in defoptions then
  2259. genericdef:=tstoreddef(genericdefderef.resolve);
  2260. if assigned(rtti_attribute_list) then
  2261. rtti_attribute_list.deref;
  2262. if assigned(genconstraintdata) then
  2263. genconstraintdata.deref;
  2264. if assigned(genericparas) then
  2265. begin
  2266. if not assigned(genericparaderefs) then
  2267. internalerror(2014052302);
  2268. if genericparas.count<>genericparaderefs.count then
  2269. internalerror(2014052303);
  2270. for i:=0 to genericparaderefs.count-1 do
  2271. begin
  2272. symderef:=pderef(genericparaderefs[i]);
  2273. genericparas.items[i]:=symderef^.resolve;
  2274. end;
  2275. end;
  2276. end;
  2277. procedure tstoreddef.derefimpl;
  2278. begin
  2279. end;
  2280. function tstoreddef.size : asizeint;
  2281. begin
  2282. size:=savesize;
  2283. end;
  2284. function tstoreddef.getvardef:longint;
  2285. begin
  2286. result:=varUndefined;
  2287. end;
  2288. function tstoreddef.alignment : shortint;
  2289. begin
  2290. { natural alignment by default }
  2291. alignment:=size_2_align(savesize);
  2292. { can happen if savesize = 0, e.g. for voiddef or
  2293. an empty record
  2294. }
  2295. if (alignment=0) then
  2296. alignment:=1;
  2297. end;
  2298. { returns true, if the definition can be published }
  2299. function tstoreddef.is_publishable : tpublishproperty;
  2300. begin
  2301. is_publishable:=pp_error;
  2302. end;
  2303. { needs an init table }
  2304. function tstoreddef.needs_inittable : boolean;
  2305. begin
  2306. needs_inittable:=false;
  2307. end;
  2308. function tstoreddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  2309. begin
  2310. result:=false;
  2311. end;
  2312. function tstoreddef.is_intregable : boolean;
  2313. {$ifndef cpuhighleveltarget}
  2314. var
  2315. recsize,temp: longint;
  2316. {$endif cpuhighleveltarget}
  2317. begin
  2318. case typ of
  2319. orddef,
  2320. pointerdef,
  2321. enumdef,
  2322. classrefdef:
  2323. is_intregable:=true;
  2324. procvardef :
  2325. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  2326. objectdef:
  2327. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  2328. setdef:
  2329. is_intregable:=is_smallset(self);
  2330. arraydef:
  2331. {$ifdef cpuhighleveltarget}
  2332. is_intregable:=false
  2333. {$else cpuhighleveltarget}
  2334. is_intregable:=not(is_special_array(self)) and
  2335. (tarraydef(self).size<=8) and (tarraydef(self).size in [1,2,4,8]) and
  2336. tstoreddef(tarraydef(self).elementdef).is_intregable
  2337. {$ifdef SUPPORT_MMX}
  2338. and not((cs_mmx in current_settings.localswitches) and
  2339. is_mmx_able_array(self))
  2340. {$endif SUPPORT_MMX}
  2341. {$endif cpuhighleveltarget}
  2342. ;
  2343. recorddef:
  2344. begin
  2345. {$ifdef cpuhighleveltarget}
  2346. is_intregable:=false;
  2347. {$else cpuhighleveltarget}
  2348. recsize:=size;
  2349. is_intregable:=
  2350. ispowerof2(recsize,temp) and
  2351. ((recsize<=sizeof(aint)*2) and
  2352. not trecorddef(self).contains_cross_aword_field and
  2353. { records cannot go into registers on 16 bit targets for now }
  2354. (sizeof(aint)>2) and
  2355. not trecorddef(self).contains_float_field
  2356. ) and
  2357. not needs_inittable;
  2358. {$endif cpuhighleveltarget}
  2359. end;
  2360. else
  2361. is_intregable:=false;
  2362. end;
  2363. end;
  2364. function tstoreddef.is_fpuregable : boolean;
  2365. begin
  2366. {$ifdef x86}
  2367. result:=use_vectorfpu(self);
  2368. {$else x86}
  2369. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches)
  2370. {$ifdef xtensa}
  2371. and (FPUXTENSA_SINGLE in fpu_capabilities[init_settings.fputype]) and (tfloatdef(self).floattype=s32real)
  2372. {$endif xtensa}
  2373. {$ifdef arm}
  2374. and (((FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[init_settings.fputype]) and (tfloatdef(self).floattype=s32real)) or
  2375. (FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[init_settings.fputype]))
  2376. {$endif arm}
  2377. ;
  2378. {$endif x86}
  2379. end;
  2380. function tstoreddef.is_const_intregable : boolean;
  2381. begin
  2382. case typ of
  2383. stringdef:
  2384. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  2385. arraydef:
  2386. result:=is_dynamic_array(self);
  2387. objectdef:
  2388. result:=is_interface(self);
  2389. else
  2390. result:=false;
  2391. end;
  2392. end;
  2393. procedure tstoreddef.initgeneric;
  2394. begin
  2395. if assigned(generictokenbuf) then
  2396. internalerror(200512131);
  2397. generictokenbuf:=tdynamicarray.create(256);
  2398. end;
  2399. function tstoreddef.is_generic: boolean;
  2400. var
  2401. sym: tsym;
  2402. i: longint;
  2403. begin
  2404. result:=assigned(genericparas) and
  2405. (genericparas.count>0) and
  2406. (df_generic in defoptions);
  2407. if result then
  2408. { if any of the type parameters does *not* belong to as (meaning it was passed
  2409. in from outside) then we aren't a generic, but a specialization }
  2410. for i:=0 to genericparas.count-1 do
  2411. begin
  2412. sym:=tsym(genericparas[i]);
  2413. { sym must be either a type or const }
  2414. if not (sym.typ in [symconst.typesym,symconst.constsym]) then
  2415. internalerror(2014050903);
  2416. if (sym.owner.defowner<>self) or (sp_generic_unnamed_type in sym.symoptions) then
  2417. exit(false);
  2418. if (sym.typ=symconst.constsym) and (sp_generic_const in sym.symoptions) then
  2419. exit(false);
  2420. end;
  2421. end;
  2422. function tstoreddef.is_generic_param_const(index:integer):boolean;
  2423. begin
  2424. result:=tsym(genericparas[index]).typ=constsym;
  2425. end;
  2426. function tstoreddef.get_generic_param_def(index:integer):tdef;
  2427. begin
  2428. if tsym(genericparas[index]).typ=constsym then
  2429. result:=tconstsym(genericparas[index]).constdef
  2430. else
  2431. result:=ttypesym(genericparas[index]).typedef;
  2432. end;
  2433. function tstoreddef.is_specialization: boolean;
  2434. var
  2435. i : longint;
  2436. sym : tsym;
  2437. begin
  2438. result:=assigned(genericparas) and
  2439. (genericparas.count>0) and
  2440. (df_specialization in defoptions);
  2441. if result then
  2442. begin
  2443. { if at least one of the generic parameters is not owned by us (meaning it was
  2444. passed in from outside) then we have a specialization, otherwise we have a generic }
  2445. for i:=0 to genericparas.count-1 do
  2446. begin
  2447. sym:=tsym(genericparas[i]);
  2448. { sym must be either a type or const }
  2449. if not (sym.typ in [symconst.typesym,symconst.constsym]) then
  2450. internalerror(2014050904);
  2451. if (sym.owner.defowner<>self) or (sp_generic_unnamed_type in sym.symoptions) then
  2452. exit(true);
  2453. if (sym.typ=symconst.constsym) and (sp_generic_const in sym.symoptions) then
  2454. exit(true);
  2455. end;
  2456. result:=false;
  2457. end;
  2458. end;
  2459. procedure tstoreddef.register_def;
  2460. var
  2461. gst : tgetsymtable;
  2462. st : tsymtable;
  2463. begin
  2464. if registered then
  2465. exit;
  2466. { Register in current_module }
  2467. if assigned(current_module) then
  2468. begin
  2469. exclude(defoptions,df_not_registered_no_free);
  2470. for gst:=low(tgetsymtable) to high(tgetsymtable) do
  2471. begin
  2472. st:=getsymtable(gst);
  2473. if assigned(st) then
  2474. tstoredsymtable(st).register_children;
  2475. end;
  2476. if defid<defid_not_registered then
  2477. defid:=deflist_index
  2478. else
  2479. begin
  2480. current_module.deflist.Add(self);
  2481. defid:=current_module.deflist.Count-1;
  2482. registered_in_module:=current_module;
  2483. end;
  2484. maybe_put_in_symtable_stack;
  2485. end
  2486. else
  2487. DefId:=defid_registered_nost;
  2488. end;
  2489. procedure tstoreddef.maybe_put_in_symtable_stack;
  2490. var
  2491. insertstack: psymtablestackitem;
  2492. begin
  2493. if assigned(symtablestack) and
  2494. not assigned(self.owner) then
  2495. begin
  2496. insertstack:=symtablestack.stack;
  2497. { don't insert defs in exception symtables, as they are freed before
  2498. the module is compiled, so we can get crashes on high level targets
  2499. if they still need it while e.g. writing assembler code }
  2500. while assigned(insertstack) and
  2501. (insertstack^.symtable.symtabletype in [exceptsymtable,withsymtable]) do
  2502. insertstack:=insertstack^.next;
  2503. if not assigned(insertstack) then
  2504. internalerror(200602044);
  2505. if insertstack^.symtable.sealed then
  2506. internalerror(2015022301);
  2507. insertstack^.symtable.insertdef(self);
  2508. end;
  2509. end;
  2510. {****************************************************************************
  2511. Tstringdef
  2512. ****************************************************************************}
  2513. constructor tstringdef.createshort(l: byte; doregister: boolean);
  2514. begin
  2515. inherited create(stringdef,doregister);
  2516. stringtype:=st_shortstring;
  2517. encoding:=0;
  2518. len:=l;
  2519. end;
  2520. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  2521. begin
  2522. inherited ppuload(stringdef,ppufile);
  2523. stringtype:=st_shortstring;
  2524. encoding:=0;
  2525. len:=ppufile.getbyte;
  2526. ppuload_platform(ppufile);
  2527. end;
  2528. constructor tstringdef.createlong(l: asizeint; doregister: boolean);
  2529. begin
  2530. inherited create(stringdef,doregister);
  2531. stringtype:=st_longstring;
  2532. encoding:=0;
  2533. len:=l;
  2534. end;
  2535. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  2536. begin
  2537. inherited ppuload(stringdef,ppufile);
  2538. stringtype:=st_longstring;
  2539. encoding:=0;
  2540. len:=ppufile.getasizeint;
  2541. ppuload_platform(ppufile);
  2542. end;
  2543. constructor tstringdef.createansi(aencoding: tstringencoding; doregister: boolean);
  2544. begin
  2545. inherited create(stringdef,doregister);
  2546. stringtype:=st_ansistring;
  2547. encoding:=aencoding;
  2548. len:=-1;
  2549. end;
  2550. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  2551. begin
  2552. inherited ppuload(stringdef,ppufile);
  2553. stringtype:=st_ansistring;
  2554. len:=ppufile.getasizeint;
  2555. encoding:=ppufile.getword;
  2556. ppuload_platform(ppufile);
  2557. end;
  2558. constructor tstringdef.createwide(doregister: boolean);
  2559. begin
  2560. inherited create(stringdef,doregister);
  2561. stringtype:=st_widestring;
  2562. if target_info.endian=endian_little then
  2563. encoding:=CP_UTF16LE
  2564. else
  2565. encoding:=CP_UTF16BE;
  2566. len:=-1;
  2567. end;
  2568. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2569. begin
  2570. inherited ppuload(stringdef,ppufile);
  2571. stringtype:=st_widestring;
  2572. if target_info.endian=endian_little then
  2573. encoding:=CP_UTF16LE
  2574. else
  2575. encoding:=CP_UTF16BE;
  2576. len:=ppufile.getasizeint;
  2577. ppuload_platform(ppufile);
  2578. end;
  2579. constructor tstringdef.createunicode(doregister: boolean);
  2580. begin
  2581. inherited create(stringdef,doregister);
  2582. stringtype:=st_unicodestring;
  2583. if target_info.endian=endian_little then
  2584. encoding:=CP_UTF16LE
  2585. else
  2586. encoding:=CP_UTF16BE;
  2587. len:=-1;
  2588. end;
  2589. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2590. begin
  2591. inherited ppuload(stringdef,ppufile);
  2592. stringtype:=st_unicodestring;
  2593. len:=ppufile.getasizeint;
  2594. encoding:=ppufile.getword;
  2595. ppuload_platform(ppufile);
  2596. end;
  2597. function tstringdef.getcopy : tstoreddef;
  2598. begin
  2599. result:=cstringdef.create(typ,true);
  2600. result.typ:=stringdef;
  2601. tstringdef(result).stringtype:=stringtype;
  2602. tstringdef(result).encoding:=encoding;
  2603. tstringdef(result).len:=len;
  2604. end;
  2605. function tstringdef.stringtypname:string;
  2606. const
  2607. typname:array[tstringtype] of string[10]=(
  2608. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2609. );
  2610. begin
  2611. stringtypname:=typname[stringtype];
  2612. end;
  2613. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2614. begin
  2615. inherited ppuwrite(ppufile);
  2616. if stringtype=st_shortstring then
  2617. begin
  2618. {$ifdef extdebug}
  2619. if len > 255 then internalerror(12122002);
  2620. {$endif}
  2621. ppufile.putbyte(byte(len))
  2622. end
  2623. else
  2624. ppufile.putasizeint(len);
  2625. if stringtype in [st_ansistring,st_unicodestring] then
  2626. ppufile.putword(encoding);
  2627. case stringtype of
  2628. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2629. st_longstring : writeentry(ppufile,iblongstringdef);
  2630. st_ansistring : writeentry(ppufile,ibansistringdef);
  2631. st_widestring : writeentry(ppufile,ibwidestringdef);
  2632. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2633. end;
  2634. end;
  2635. function tstringdef.needs_inittable : boolean;
  2636. begin
  2637. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2638. end;
  2639. function tstringdef.GetTypeName : string;
  2640. const
  2641. names : array[tstringtype] of string[15] = (
  2642. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2643. begin
  2644. GetTypeName:=names[stringtype];
  2645. case stringtype of
  2646. st_ansistring,st_unicodestring:
  2647. GetTypeName:=GetTypeName+'('+tostr(encoding)+')';
  2648. st_shortstring:
  2649. GetTypeName:=GetTypeName+'['+tostr(len)+']';
  2650. else
  2651. ;
  2652. end;
  2653. end;
  2654. function tstringdef.getvardef : longint;
  2655. const
  2656. vardef : array[tstringtype] of longint = (
  2657. varUndefined,varUndefined,varString,varOleStr,varUString);
  2658. begin
  2659. result:=vardef[stringtype];
  2660. end;
  2661. function tstringdef.get_default_char_type: tdef;
  2662. begin
  2663. case stringtype of
  2664. st_shortstring,
  2665. st_longstring,
  2666. st_ansistring:
  2667. result:=cansichartype;
  2668. st_unicodestring,
  2669. st_widestring:
  2670. result:=cwidechartype;
  2671. end;
  2672. end;
  2673. function tstringdef.get_default_string_type: tdef;
  2674. begin
  2675. case stringtype of
  2676. st_shortstring:
  2677. result:=cshortstringtype;
  2678. { st_longstring is currently not supported but
  2679. when it is this case will need to be supplied }
  2680. st_longstring:
  2681. internalerror(2021040801);
  2682. st_ansistring:
  2683. result:=cansistringtype;
  2684. st_widestring:
  2685. result:=cwidestringtype;
  2686. st_unicodestring:
  2687. result:=cunicodestringtype;
  2688. end
  2689. end;
  2690. function tstringdef.alignment : shortint;
  2691. begin
  2692. case stringtype of
  2693. st_unicodestring,
  2694. st_widestring,
  2695. st_ansistring:
  2696. alignment:=voidpointertype.alignment;
  2697. st_longstring,
  2698. st_shortstring:
  2699. { char to string accesses byte 0 and 1 with one word access }
  2700. if (tf_requires_proper_alignment in target_info.flags) or
  2701. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2702. (m_mac in current_settings.modeswitches) then
  2703. alignment:=size_2_align(2)
  2704. else
  2705. alignment:=size_2_align(1);
  2706. end;
  2707. end;
  2708. function tstringdef.getmangledparaname : TSymStr;
  2709. begin
  2710. getmangledparaname:='STRING';
  2711. end;
  2712. function tstringdef.is_publishable : tpublishproperty;
  2713. begin
  2714. is_publishable:=pp_publish;
  2715. end;
  2716. function tstringdef.size: asizeint;
  2717. begin
  2718. case stringtype of
  2719. st_shortstring:
  2720. Result:=len+1;
  2721. st_longstring,
  2722. st_ansistring,
  2723. st_widestring,
  2724. st_unicodestring:
  2725. Result:=voidpointertype.size;
  2726. end;
  2727. end;
  2728. {****************************************************************************
  2729. TENUMDEF
  2730. ****************************************************************************}
  2731. constructor tenumdef.create;
  2732. begin
  2733. inherited create(enumdef,true);
  2734. minval:=0;
  2735. maxval:=0;
  2736. calcsavesize(current_settings.packenum);
  2737. has_jumps:=false;
  2738. basedef:=nil;
  2739. basedefderef.reset;
  2740. symtable:=tenumsymtable.create(self);
  2741. end;
  2742. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2743. begin
  2744. inherited create(enumdef,true);
  2745. minval:=_min;
  2746. maxval:=_max;
  2747. basedef:=_basedef;
  2748. calcsavesize(current_settings.packenum);
  2749. has_jumps:=false;
  2750. symtable:=basedef.symtable.getcopy;
  2751. include(defoptions, df_copied_def);
  2752. end;
  2753. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2754. begin
  2755. inherited ppuload(enumdef,ppufile);
  2756. minval:=ppufile.getaint;
  2757. maxval:=ppufile.getaint;
  2758. savesize:=ppufile.getaint;
  2759. has_jumps:=false;
  2760. if df_copied_def in defoptions then
  2761. begin
  2762. symtable:=nil;
  2763. ppufile.getderef(basedefderef);
  2764. ppuload_platform(ppufile);
  2765. end
  2766. else
  2767. begin
  2768. ppuload_platform(ppufile);
  2769. // create with nil defowner first to prevent values changes on insert
  2770. symtable:=tenumsymtable.create(nil);
  2771. tenumsymtable(symtable).ppuload(ppufile);
  2772. symtable.defowner:=self;
  2773. end;
  2774. end;
  2775. destructor tenumdef.destroy;
  2776. begin
  2777. symtable.free;
  2778. symtable:=nil;
  2779. inherited destroy;
  2780. end;
  2781. function tenumdef.getcopy : tstoreddef;
  2782. begin
  2783. if assigned(basedef) then
  2784. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2785. else
  2786. begin
  2787. result:=cenumdef.create;
  2788. tenumdef(result).minval:=minval;
  2789. tenumdef(result).maxval:=maxval;
  2790. tenumdef(result).symtable.free;
  2791. tenumdef(result).symtable:=symtable.getcopy;
  2792. tenumdef(result).basedef:=self;
  2793. end;
  2794. tenumdef(result).has_jumps:=has_jumps;
  2795. tenumdef(result).basedefderef:=basedefderef;
  2796. include(tenumdef(result).defoptions,df_copied_def);
  2797. end;
  2798. procedure tenumdef.calcsavesize(packenum: shortint);
  2799. begin
  2800. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2801. if (packenum=8) or (int64(min)<low(longint)) or (int64(max)>high(cardinal)) then
  2802. savesize:=8
  2803. {$IFNDEF cpu64bitaddr} {$pop} {$ENDIF}
  2804. else
  2805. {$IFDEF cpu16bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2806. if (packenum=4) or (min<low(smallint)) or (max>high(word)) then
  2807. savesize:=4
  2808. {$IFDEF cpu16bitaddr} {$pop} {$ENDIF}
  2809. else
  2810. if (packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  2811. savesize:=2
  2812. else
  2813. savesize:=1;
  2814. end;
  2815. function tenumdef.packedbitsize: asizeint;
  2816. var
  2817. sizeval: tconstexprint;
  2818. power: longint;
  2819. begin
  2820. result := 0;
  2821. if (minval >= 0) and
  2822. (maxval <= 1) then
  2823. result := 1
  2824. else
  2825. begin
  2826. if (minval>=0) then
  2827. sizeval:=maxval
  2828. else
  2829. { don't count 0 twice, but take into account that range goes from -n-1..n }
  2830. sizeval:=(cutils.max(-minval,maxval+1)*2)-1;
  2831. { 256 must become 512 etc. }
  2832. nextpowerof2(sizeval+1,power);
  2833. result := power;
  2834. end;
  2835. end;
  2836. procedure tenumdef.setmax(_max:asizeint);
  2837. begin
  2838. maxval:=_max;
  2839. calcsavesize(current_settings.packenum);
  2840. end;
  2841. procedure tenumdef.setmin(_min:asizeint);
  2842. begin
  2843. minval:=_min;
  2844. calcsavesize(current_settings.packenum);
  2845. end;
  2846. function tenumdef.min:asizeint;
  2847. begin
  2848. min:=minval;
  2849. end;
  2850. function tenumdef.max:asizeint;
  2851. begin
  2852. max:=maxval;
  2853. end;
  2854. function tenumdef.getfirstsym: tsym;
  2855. var
  2856. i:integer;
  2857. begin
  2858. for i := 0 to symtable.SymList.Count - 1 do
  2859. begin
  2860. result:=tsym(symtable.SymList[i]);
  2861. if tenumsym(result).value=minval then
  2862. exit;
  2863. end;
  2864. result:=nil;
  2865. end;
  2866. function tenumdef.int2enumsym(l: asizeint): tsym;
  2867. var
  2868. i: longint;
  2869. sym: tsym;
  2870. bdef: tenumdef;
  2871. begin
  2872. result:=nil;
  2873. if (l<minval) or
  2874. (l>maxval) then
  2875. exit;
  2876. bdef:=getbasedef;
  2877. for i:=0 to bdef.symtable.symlist.count-1 do
  2878. begin
  2879. sym:=tsym(bdef.symtable.symlist[i]);
  2880. if (sym.typ=enumsym) and
  2881. (tenumsym(sym).value=l) then
  2882. begin
  2883. result:=sym;
  2884. exit;
  2885. end;
  2886. end;
  2887. end;
  2888. function tenumdef.getbasedef: tenumdef;
  2889. begin
  2890. if not assigned(basedef) then
  2891. result:=self
  2892. else
  2893. result:=basedef;
  2894. end;
  2895. procedure tenumdef.buildderef;
  2896. begin
  2897. inherited buildderef;
  2898. if df_copied_def in defoptions then
  2899. basedefderef.build(basedef)
  2900. else
  2901. tenumsymtable(symtable).buildderef;
  2902. end;
  2903. procedure tenumdef.deref;
  2904. begin
  2905. inherited deref;
  2906. if df_copied_def in defoptions then
  2907. begin
  2908. basedef:=tenumdef(basedefderef.resolve);
  2909. symtable:=basedef.symtable.getcopy;
  2910. end
  2911. else
  2912. tenumsymtable(symtable).deref(false);
  2913. end;
  2914. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2915. begin
  2916. inherited ppuwrite(ppufile);
  2917. ppufile.putaint(min);
  2918. ppufile.putaint(max);
  2919. ppufile.putaint(savesize);
  2920. if df_copied_def in defoptions then
  2921. ppufile.putderef(basedefderef);
  2922. writeentry(ppufile,ibenumdef);
  2923. if not (df_copied_def in defoptions) then
  2924. tenumsymtable(symtable).ppuwrite(ppufile);
  2925. end;
  2926. function tenumdef.is_publishable : tpublishproperty;
  2927. begin
  2928. if not has_jumps then
  2929. is_publishable:=pp_publish
  2930. else
  2931. if m_delphi in current_settings.modeswitches then
  2932. is_publishable:=pp_ignore
  2933. else
  2934. is_publishable:=pp_error;
  2935. end;
  2936. function tenumdef.GetTypeName : string;
  2937. begin
  2938. GetTypeName:='<enumeration type>';
  2939. end;
  2940. {****************************************************************************
  2941. TRTTI_ATTRIBUTE_LIST
  2942. ****************************************************************************}
  2943. constructor trtti_attribute.ppuload(ppufile: tcompilerppufile);
  2944. begin
  2945. ppufile.getderef(typesymderef);
  2946. ppufile.getderef(typeconstrderef);
  2947. setlength(paras,ppufile.getlongint);
  2948. end;
  2949. procedure trtti_attribute.ppuwrite(ppufile: tcompilerppufile);
  2950. begin
  2951. ppufile.putderef(typesymderef);
  2952. ppufile.putderef(typeconstrderef);
  2953. ppufile.putlongint(length(paras));
  2954. end;
  2955. procedure trtti_attribute.ppuload_subentries(ppufile: tcompilerppufile);
  2956. var
  2957. i : sizeint;
  2958. begin
  2959. for i:=0 to high(paras) do
  2960. paras[i]:=ppuloadnodetree(ppufile);
  2961. end;
  2962. procedure trtti_attribute.ppuwrite_subentries(ppufile: tcompilerppufile);
  2963. var
  2964. i : sizeint;
  2965. begin
  2966. for i:=0 to high(paras) do
  2967. ppuwritenodetree(ppufile,paras[i]);
  2968. end;
  2969. destructor trtti_attribute.destroy;
  2970. var
  2971. n : tnode;
  2972. begin
  2973. constructorcall.free;
  2974. for n in paras do
  2975. n.free;
  2976. inherited destroy;
  2977. end;
  2978. procedure trtti_attribute.buildderef;
  2979. var
  2980. i : sizeint;
  2981. begin
  2982. typesymderef.build(typesym);
  2983. typeconstrderef.build(typeconstr);
  2984. for i:=0 to high(paras) do
  2985. paras[i].buildderefimpl;
  2986. end;
  2987. procedure trtti_attribute.deref;
  2988. var
  2989. i : sizeint;
  2990. begin
  2991. typesym:=tsym(typesymderef.resolve);
  2992. typeconstr:=tdef(typeconstrderef.resolve);
  2993. for i:=0 to high(paras) do
  2994. paras[i].derefimpl;
  2995. end;
  2996. class procedure trtti_attribute_list.bind(var dangling,owned:trtti_attribute_list);
  2997. begin
  2998. if assigned(owned) then
  2999. internalerror(2019071001);
  3000. if not assigned(dangling) then
  3001. exit;
  3002. if dangling.is_bound then
  3003. internalerror(2019071002);
  3004. current_module.used_rtti_attrs.concatlistcopy(dangling.rtti_attributes);
  3005. dangling.is_bound:=true;
  3006. owned:=dangling;
  3007. dangling:=nil;
  3008. end;
  3009. class procedure trtti_attribute_list.copyandbind(alist : trtti_attribute_list; var owned: trtti_attribute_list);
  3010. var
  3011. i,j : Integer;
  3012. attr,newattribute : trtti_attribute;
  3013. begin
  3014. if owned=Nil then
  3015. owned:=trtti_attribute_list.Create;
  3016. owned.is_bound:=True;
  3017. for i:=0 to aList.rtti_attributes.Count-1 do
  3018. begin
  3019. attr:=trtti_attribute(alist.rtti_attributes[i]);
  3020. newattribute:=trtti_attribute.Create;
  3021. newattribute.typesym:=attr.typesym;
  3022. newattribute.typeconstr:=attr.typeconstr;
  3023. newattribute.constructorcall:=attr.constructorcall.getcopy;
  3024. setlength(newattribute.paras,length(attr.paras));
  3025. for j:=0 to length(attr.paras)-1 do
  3026. newattribute.paras[j]:=attr.paras[j].getcopy;
  3027. owned.AddAttribute(newattribute);
  3028. end;
  3029. current_module.used_rtti_attrs.concatlistcopy(owned.rtti_attributes);
  3030. end;
  3031. procedure trtti_attribute_list.addattribute(atypesym:tsym;typeconstr:tdef;constructorcall:tnode;constref paras:array of tnode);
  3032. var
  3033. newattribute : trtti_attribute;
  3034. i : sizeint;
  3035. begin
  3036. if not assigned(rtti_attributes) then
  3037. rtti_attributes:=TFPObjectList.Create(true);
  3038. newattribute:=trtti_attribute.Create;
  3039. newattribute.typesym:=atypesym;
  3040. newattribute.typeconstr:=typeconstr;
  3041. newattribute.constructorcall:=constructorcall;
  3042. setlength(newattribute.paras,length(paras));
  3043. for i:=0 to high(paras) do
  3044. newattribute.paras[i]:=paras[i];
  3045. rtti_attributes.Add(newattribute);
  3046. end;
  3047. procedure trtti_attribute_list.addattribute(attr:trtti_attribute);
  3048. begin
  3049. if not assigned(rtti_attributes) then
  3050. rtti_attributes:=TFPObjectList.Create(true);
  3051. rtti_attributes.add(attr);
  3052. end;
  3053. destructor trtti_attribute_list.destroy;
  3054. begin
  3055. rtti_attributes.Free;
  3056. inherited destroy;
  3057. end;
  3058. function trtti_attribute_list.get_attribute_count:longint;
  3059. begin
  3060. if assigned(rtti_attributes) then
  3061. result:=rtti_attributes.Count
  3062. else
  3063. result:=0;
  3064. end;
  3065. procedure trtti_attribute_list.buildderef;
  3066. var
  3067. i : sizeint;
  3068. begin
  3069. if not assigned(rtti_attributes) then
  3070. exit;
  3071. for i:=0 to rtti_attributes.count-1 do
  3072. trtti_attribute(rtti_attributes[i]).buildderef;
  3073. end;
  3074. procedure trtti_attribute_list.deref;
  3075. var
  3076. i : sizeint;
  3077. begin
  3078. if not assigned(rtti_attributes) then
  3079. exit;
  3080. for i:=0 to rtti_attributes.count-1 do
  3081. trtti_attribute(rtti_attributes[i]).deref;
  3082. end;
  3083. class procedure trtti_attribute_list.ppuload_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  3084. var
  3085. i : sizeint;
  3086. begin
  3087. if assigned(attrlist) then
  3088. begin
  3089. if not assigned(attrlist.rtti_attributes) then
  3090. internalerror(2019071101);
  3091. for i:=0 to attrlist.rtti_attributes.count-1 do
  3092. trtti_attribute(attrlist.rtti_attributes[i]).ppuload_subentries(ppufile);
  3093. end;
  3094. end;
  3095. class procedure trtti_attribute_list.ppuwrite_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  3096. var
  3097. i : sizeint;
  3098. begin
  3099. if assigned(attrlist) and assigned(attrlist.rtti_attributes) then
  3100. begin
  3101. for i:=0 to attrlist.rtti_attributes.count-1 do
  3102. trtti_attribute(attrlist.rtti_attributes[i]).ppuwrite_subentries(ppufile);
  3103. end;
  3104. end;
  3105. class function trtti_attribute_list.ppuload(ppufile:tcompilerppufile):trtti_attribute_list;
  3106. var
  3107. cnt,i : longint;
  3108. begin
  3109. cnt:=ppufile.getlongint;
  3110. if cnt>0 then
  3111. begin
  3112. result:=trtti_attribute_list.create;
  3113. for i:=0 to cnt-1 do
  3114. result.addattribute(trtti_attribute.ppuload(ppufile));
  3115. end
  3116. else
  3117. result:=nil;
  3118. end;
  3119. class procedure trtti_attribute_list.ppuwrite(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  3120. var
  3121. i : longint;
  3122. begin
  3123. if assigned(attrlist) and assigned(attrlist.rtti_attributes) then
  3124. begin
  3125. ppufile.putlongint(attrlist.rtti_attributes.count);
  3126. for i:=0 to attrlist.rtti_attributes.count-1 do
  3127. trtti_attribute(attrlist.rtti_attributes[i]).ppuwrite(ppufile);
  3128. end
  3129. else
  3130. ppufile.putlongint(0);
  3131. end;
  3132. {****************************************************************************
  3133. TORDDEF
  3134. ****************************************************************************}
  3135. constructor torddef.create(t : tordtype;v,b : TConstExprInt; doregister: boolean);
  3136. begin
  3137. inherited create(orddef,doregister);
  3138. low:=v;
  3139. high:=b;
  3140. ordtype:=t;
  3141. setsize;
  3142. end;
  3143. constructor torddef.ppuload(ppufile:tcompilerppufile);
  3144. begin
  3145. inherited ppuload(orddef,ppufile);
  3146. ordtype:=tordtype(ppufile.getbyte);
  3147. low:=ppufile.getexprint;
  3148. high:=ppufile.getexprint;
  3149. setsize;
  3150. ppuload_platform(ppufile);
  3151. end;
  3152. function torddef.getcopy : tstoreddef;
  3153. begin
  3154. result:=corddef.create(ordtype,low,high,true);
  3155. result.typ:=orddef;
  3156. torddef(result).low:=low;
  3157. torddef(result).high:=high;
  3158. torddef(result).ordtype:=ordtype;
  3159. torddef(result).savesize:=savesize;
  3160. end;
  3161. function torddef.alignment:shortint;
  3162. begin
  3163. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_ios]) and
  3164. (ordtype in [s64bit,u64bit]) then
  3165. result := 4
  3166. else
  3167. result := inherited alignment;
  3168. end;
  3169. procedure torddef.setsize;
  3170. const
  3171. sizetbl : array[tordtype] of longint = (
  3172. 0,
  3173. 1,2,4,8,16,
  3174. 1,2,4,8,16,
  3175. 1,1,2,4,8,
  3176. 1,2,4,8,
  3177. 1,2,8,system.high(longint)
  3178. );
  3179. begin
  3180. savesize:=sizetbl[ordtype];
  3181. if savesize=system.high(longint) then
  3182. savesize:=packedbitsize div 8;
  3183. end;
  3184. function torddef.packedbitsize: asizeint;
  3185. var
  3186. sizeval: tconstexprint;
  3187. power: longint;
  3188. begin
  3189. result := 0;
  3190. if ordtype = uvoid then
  3191. exit;
  3192. {$ifndef cpu64bitalu}
  3193. if (ordtype in [s64bit,u64bit]) then
  3194. {$else not cpu64bitalu}
  3195. if ((ordtype = u64bit) and
  3196. (high > system.high(int64))) or
  3197. ((ordtype = s64bit) and
  3198. ((low <= (system.low(int64) div 2)) or
  3199. ((low < 0) and
  3200. (high > (system.high(int64) div 2))))) then
  3201. {$endif cpu64bitalu}
  3202. result := 64
  3203. else if (
  3204. (low >= 0) and
  3205. (high <= 1)
  3206. ) or (
  3207. ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]
  3208. ) then
  3209. result := 1
  3210. else
  3211. begin
  3212. if (low>=0) then
  3213. sizeval:=high
  3214. else
  3215. { don't count 0 twice, but take into account that range goes from -n-1..n }
  3216. sizeval:=(cutils.max(-low,high+1)*2)-1;
  3217. { 256 must become 512 etc. }
  3218. nextpowerof2(sizeval+1,power);
  3219. result := power;
  3220. end;
  3221. end;
  3222. function torddef.getvardef : longint;
  3223. const
  3224. basetype2vardef : array[tordtype] of longint = (
  3225. varUndefined,
  3226. varbyte,varword,varlongword,varqword,varUndefined,
  3227. varshortint,varsmallint,varinteger,varint64,varUndefined,
  3228. varboolean,varboolean,varboolean,varboolean,varboolean,
  3229. varboolean,varboolean,varUndefined,varUndefined,
  3230. varUndefined,varUndefined,varCurrency,varEmpty);
  3231. begin
  3232. result:=basetype2vardef[ordtype];
  3233. if result=varEmpty then
  3234. result:=basetype2vardef[range_to_basetype(low,high)];
  3235. end;
  3236. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  3237. begin
  3238. inherited ppuwrite(ppufile);
  3239. ppufile.putbyte(byte(ordtype));
  3240. ppufile.putexprint(low);
  3241. ppufile.putexprint(high);
  3242. writeentry(ppufile,iborddef);
  3243. end;
  3244. function torddef.is_publishable : tpublishproperty;
  3245. begin
  3246. if ordtype<>uvoid then
  3247. is_publishable:=pp_publish
  3248. else
  3249. is_publishable:=pp_error;
  3250. end;
  3251. function torddef.GetTypeName : string;
  3252. const
  3253. names : array[tordtype] of string[20] = (
  3254. 'untyped',
  3255. 'Byte','Word','DWord','QWord','UInt128',
  3256. 'ShortInt','SmallInt','LongInt','Int64','Int128',
  3257. 'Boolean','Boolean8','Boolean16','Boolean32','Boolean64',
  3258. 'ByteBool','WordBool','LongBool','QWordBool',
  3259. 'AnsiChar','WideChar','Currency','CustomRange');
  3260. begin
  3261. GetTypeName:=names[ordtype];
  3262. end;
  3263. {****************************************************************************
  3264. TFLOATDEF
  3265. ****************************************************************************}
  3266. constructor tfloatdef.create(t: tfloattype; doregister: boolean);
  3267. begin
  3268. inherited create(floatdef,doregister);
  3269. floattype:=t;
  3270. setsize;
  3271. end;
  3272. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  3273. begin
  3274. inherited ppuload(floatdef,ppufile);
  3275. floattype:=tfloattype(ppufile.getbyte);
  3276. setsize;
  3277. ppuload_platform(ppufile);
  3278. end;
  3279. function tfloatdef.getcopy : tstoreddef;
  3280. begin
  3281. result:=cfloatdef.create(floattype,true);
  3282. result.typ:=floatdef;
  3283. tfloatdef(result).savesize:=savesize;
  3284. end;
  3285. function tfloatdef.alignment:shortint;
  3286. begin
  3287. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_ios]) then
  3288. case floattype of
  3289. sc80real,
  3290. s80real: result:=16;
  3291. s64real,
  3292. s64currency,
  3293. s64comp : result:=4;
  3294. else
  3295. result := inherited alignment;
  3296. end
  3297. else
  3298. result := inherited alignment;
  3299. end;
  3300. function tfloatdef.structalignment: shortint;
  3301. begin
  3302. { aix is really annoying: the recommended scalar alignment for both
  3303. int64 and double is 64 bits, but in structs int64 has to be aligned
  3304. to 8 bytes and double to 4 bytes }
  3305. if (target_info.system in systems_aix) and
  3306. (floattype=s64real) then
  3307. result:=4
  3308. else
  3309. result:=alignment;
  3310. end;
  3311. procedure tfloatdef.setsize;
  3312. begin
  3313. case floattype of
  3314. s32real : savesize:=4;
  3315. s80real : savesize:=10;
  3316. sc80real:
  3317. if target_info.system in [system_i386_darwin,
  3318. system_i386_iphonesim,system_x86_64_darwin,
  3319. system_x86_64_iphonesim,
  3320. system_x86_64_linux,system_x86_64_freebsd,
  3321. system_x86_64_openbsd,system_x86_64_netbsd,
  3322. system_x86_64_solaris,system_x86_64_embedded,
  3323. system_x86_64_dragonfly,system_x86_64_haiku] then
  3324. savesize:=16
  3325. else
  3326. savesize:=12;
  3327. s64real,
  3328. s64currency,
  3329. s64comp : savesize:=8;
  3330. else
  3331. savesize:=0;
  3332. end;
  3333. end;
  3334. function tfloatdef.getvardef : longint;
  3335. const
  3336. floattype2vardef : array[tfloattype] of longint = (
  3337. varSingle,varDouble,varUndefined,varUndefined,
  3338. varUndefined,varCurrency,varUndefined);
  3339. begin
  3340. if (upper(typename)='TDATETIME') and
  3341. assigned(owner) and
  3342. assigned(owner.name) and
  3343. (owner.name^='SYSTEM') then
  3344. result:=varDate
  3345. else
  3346. result:=floattype2vardef[floattype];
  3347. end;
  3348. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  3349. begin
  3350. inherited ppuwrite(ppufile);
  3351. ppufile.putbyte(byte(floattype));
  3352. writeentry(ppufile,ibfloatdef);
  3353. end;
  3354. function tfloatdef.is_publishable : tpublishproperty;
  3355. begin
  3356. is_publishable:=pp_publish;
  3357. end;
  3358. function tfloatdef.GetTypeName : string;
  3359. const
  3360. names : array[tfloattype] of string[20] = (
  3361. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  3362. begin
  3363. GetTypeName:=names[floattype];
  3364. end;
  3365. {****************************************************************************
  3366. TFILEDEF
  3367. ****************************************************************************}
  3368. constructor tfiledef.createtext;
  3369. begin
  3370. inherited create(filedef,true);
  3371. filetyp:=ft_text;
  3372. typedfiledef:=nil;
  3373. typedfiledefderef.reset;
  3374. end;
  3375. constructor tfiledef.createuntyped;
  3376. begin
  3377. inherited create(filedef,true);
  3378. filetyp:=ft_untyped;
  3379. typedfiledef:=nil;
  3380. end;
  3381. constructor tfiledef.createtyped(def:tdef);
  3382. begin
  3383. inherited create(filedef,true);
  3384. filetyp:=ft_typed;
  3385. typedfiledef:=def;
  3386. end;
  3387. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  3388. begin
  3389. inherited ppuload(filedef,ppufile);
  3390. filetyp:=tfiletyp(ppufile.getbyte);
  3391. if filetyp=ft_typed then
  3392. ppufile.getderef(typedfiledefderef)
  3393. else
  3394. typedfiledef:=nil;
  3395. ppuload_platform(ppufile);
  3396. end;
  3397. function tfiledef.getcopy : tstoreddef;
  3398. begin
  3399. case filetyp of
  3400. ft_typed:
  3401. result:=cfiledef.createtyped(typedfiledef);
  3402. ft_untyped:
  3403. result:=cfiledef.createuntyped;
  3404. ft_text:
  3405. result:=cfiledef.createtext;
  3406. end;
  3407. end;
  3408. procedure tfiledef.buildderef;
  3409. begin
  3410. inherited buildderef;
  3411. if filetyp=ft_typed then
  3412. typedfiledefderef.build(typedfiledef);
  3413. end;
  3414. procedure tfiledef.deref;
  3415. begin
  3416. inherited deref;
  3417. if filetyp=ft_typed then
  3418. typedfiledef:=tdef(typedfiledefderef.resolve);
  3419. end;
  3420. function tfiledef.size:asizeint;
  3421. begin
  3422. if savesize=0 then
  3423. setsize;
  3424. size:=savesize;
  3425. end;
  3426. procedure tfiledef.setsize;
  3427. begin
  3428. case filetyp of
  3429. ft_text:
  3430. savesize:=search_system_type('TEXTREC').typedef.size;
  3431. ft_typed:
  3432. begin
  3433. savesize:=search_system_type('FILEREC').typedef.size;
  3434. { allocate put/get buffer in iso mode }
  3435. if m_isolike_io in current_settings.modeswitches then
  3436. inc(savesize,typedfiledef.size);
  3437. end;
  3438. ft_untyped:
  3439. savesize:=search_system_type('FILEREC').typedef.size;
  3440. end;
  3441. end;
  3442. function tfiledef.alignment: shortint;
  3443. begin
  3444. case filetyp of
  3445. ft_text:
  3446. result:=search_system_type('TEXTREC').typedef.alignment;
  3447. ft_typed,
  3448. ft_untyped:
  3449. result:=search_system_type('FILEREC').typedef.alignment;
  3450. end;
  3451. end;
  3452. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  3453. begin
  3454. inherited ppuwrite(ppufile);
  3455. ppufile.putbyte(byte(filetyp));
  3456. if filetyp=ft_typed then
  3457. ppufile.putderef(typedfiledefderef);
  3458. writeentry(ppufile,ibfiledef);
  3459. end;
  3460. function tfiledef.GetTypeName : string;
  3461. begin
  3462. case filetyp of
  3463. ft_untyped:
  3464. GetTypeName:='File';
  3465. ft_typed:
  3466. GetTypeName:='File Of '+typedfiledef.typename;
  3467. ft_text:
  3468. GetTypeName:='Text'
  3469. end;
  3470. end;
  3471. function tfiledef.getmangledparaname : TSymStr;
  3472. begin
  3473. case filetyp of
  3474. ft_untyped:
  3475. getmangledparaname:='FILE';
  3476. ft_typed:
  3477. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  3478. ft_text:
  3479. getmangledparaname:='TEXT'
  3480. end;
  3481. end;
  3482. {****************************************************************************
  3483. TVARIANTDEF
  3484. ****************************************************************************}
  3485. constructor tvariantdef.create(v : tvarianttype);
  3486. begin
  3487. inherited create(variantdef,true);
  3488. varianttype:=v;
  3489. setsize;
  3490. end;
  3491. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  3492. begin
  3493. inherited ppuload(variantdef,ppufile);
  3494. varianttype:=tvarianttype(ppufile.getbyte);
  3495. setsize;
  3496. ppuload_platform(ppufile);
  3497. end;
  3498. function tvariantdef.getcopy : tstoreddef;
  3499. begin
  3500. result:=cvariantdef.create(varianttype);
  3501. end;
  3502. function tvariantdef.alignment: shortint;
  3503. begin
  3504. result:=search_system_type('TVARDATA').typedef.alignment;
  3505. end;
  3506. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  3507. begin
  3508. inherited ppuwrite(ppufile);
  3509. ppufile.putbyte(byte(varianttype));
  3510. writeentry(ppufile,ibvariantdef);
  3511. end;
  3512. function tvariantdef.getvardef : longint;
  3513. begin
  3514. Result:=varVariant;
  3515. end;
  3516. procedure tvariantdef.setsize;
  3517. begin
  3518. {$ifdef cpu64bitaddr}
  3519. savesize:=24;
  3520. {$else cpu64bitaddr}
  3521. savesize:=16;
  3522. {$endif cpu64bitaddr}
  3523. end;
  3524. function tvariantdef.GetTypeName : string;
  3525. begin
  3526. case varianttype of
  3527. vt_normalvariant:
  3528. GetTypeName:='Variant';
  3529. vt_olevariant:
  3530. GetTypeName:='OleVariant';
  3531. end;
  3532. end;
  3533. function tvariantdef.needs_inittable : boolean;
  3534. begin
  3535. needs_inittable:=true;
  3536. end;
  3537. function tvariantdef.is_publishable : tpublishproperty;
  3538. begin
  3539. is_publishable:=pp_publish;
  3540. end;
  3541. {****************************************************************************
  3542. TABSTRACtpointerdef
  3543. ****************************************************************************}
  3544. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  3545. begin
  3546. inherited create(dt,true);
  3547. pointeddef:=def;
  3548. pointeddefderef.reset;
  3549. if df_generic in pointeddef.defoptions then
  3550. include(defoptions,df_generic);
  3551. if df_specialization in pointeddef.defoptions then
  3552. include(defoptions,df_specialization);
  3553. end;
  3554. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3555. begin
  3556. inherited ppuload(dt,ppufile);
  3557. ppufile.getderef(pointeddefderef);
  3558. end;
  3559. procedure tabstractpointerdef.buildderef;
  3560. begin
  3561. inherited buildderef;
  3562. pointeddefderef.build(pointeddef);
  3563. end;
  3564. procedure tabstractpointerdef.deref;
  3565. begin
  3566. inherited deref;
  3567. pointeddef:=tdef(pointeddefderef.resolve);
  3568. end;
  3569. function tabstractpointerdef.size: asizeint;
  3570. begin
  3571. Result:=voidpointertype.size;
  3572. end;
  3573. function tabstractpointerdef.alignment: shortint;
  3574. begin
  3575. alignment:=size_2_align(voidpointertype.size);
  3576. end;
  3577. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3578. begin
  3579. inherited ppuwrite(ppufile);
  3580. ppufile.putderef(pointeddefderef);
  3581. end;
  3582. {****************************************************************************
  3583. tpointerdef
  3584. ****************************************************************************}
  3585. constructor tpointerdef.create(def:tdef);
  3586. begin
  3587. inherited create(pointerdef,def);
  3588. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  3589. if (df_specialization in tstoreddef(def).defoptions)
  3590. {$ifndef genericdef_for_nested}
  3591. { currently, nested procdefs of generic routines get df_specialization,
  3592. but no genericdef }
  3593. and assigned(tstoreddef(def).genericdef)
  3594. {$endif}
  3595. then
  3596. genericdef:=cpointerdef.getreusable(tstoreddef(def).genericdef);
  3597. end;
  3598. class function tpointerdef.getreusable(def: tdef): tpointerdef;
  3599. var
  3600. res: PHashSetItem;
  3601. oldsymtablestack: tsymtablestack;
  3602. begin
  3603. if not assigned(current_module) then
  3604. internalerror(2011071101);
  3605. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  3606. if not assigned(res^.Data) then
  3607. begin
  3608. { since these pointerdefs can be reused anywhere in the current
  3609. unit, add them to the global/staticsymtable (or local symtable
  3610. if they're a local def, because otherwise they'll be saved
  3611. to the ppu referencing a local symtable entry that doesn't
  3612. exist in the ppu) }
  3613. oldsymtablestack:=symtablestack;
  3614. { do not simply push/pop current_module.localsymtable, because
  3615. that can have side-effects (e.g., it removes helpers) }
  3616. symtablestack:=nil;
  3617. result:=cpointerdef.create(def);
  3618. setup_reusable_def(def,result,res,oldsymtablestack);
  3619. { res^.Data may still be nil -> don't overwrite result }
  3620. exit;
  3621. end;
  3622. result:=tpointerdef(res^.Data);
  3623. end;
  3624. class function tpointerdef.getreusable_no_free(def: tdef): tpointerdef;
  3625. begin
  3626. result:=getreusable(def);
  3627. if not result.is_registered then
  3628. include(result.defoptions,df_not_registered_no_free);
  3629. end;
  3630. function tpointerdef.size: asizeint;
  3631. begin
  3632. result:=sizeof(pint);
  3633. end;
  3634. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  3635. begin
  3636. inherited ppuload(pointerdef,ppufile);
  3637. has_pointer_math:=(ppufile.getbyte<>0);
  3638. ppuload_platform(ppufile);
  3639. end;
  3640. function tpointerdef.getcopy : tstoreddef;
  3641. begin
  3642. { don't use direct pointeddef if it is a forwarddef because in other case
  3643. one of them will be destroyed on forward type resolve and the second will
  3644. point to garbage }
  3645. if pointeddef.typ=forwarddef then
  3646. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  3647. else
  3648. result:=cpointerdef.create(pointeddef);
  3649. tpointerdef(result).has_pointer_math:=has_pointer_math;
  3650. tpointerdef(result).savesize:=savesize;
  3651. end;
  3652. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3653. begin
  3654. inherited ppuwrite(ppufile);
  3655. ppufile.putbyte(byte(has_pointer_math));
  3656. writeentry(ppufile,ibpointerdef);
  3657. end;
  3658. function tpointerdef.GetTypeName : string;
  3659. begin
  3660. { parameter types and the resultdef of a procvardef can contain a
  3661. pointer to this procvardef itself, resulting in endless recursion ->
  3662. use the typesym's name instead if it exists (if it doesn't, such as
  3663. for anynonymous procedure types in macpas/iso mode, then there cannot
  3664. be any recursive references to it either) }
  3665. if (pointeddef.typ<>procvardef) or
  3666. not assigned(pointeddef.typesym) then
  3667. GetTypeName:='^'+pointeddef.typename
  3668. else
  3669. GetTypeName:='^'+pointeddef.typesym.realname;
  3670. end;
  3671. function tpointerdef.pointer_arithmetic_int_type:tdef;
  3672. begin
  3673. result:=ptrsinttype;
  3674. end;
  3675. function tpointerdef.pointer_arithmetic_uint_type:tdef;
  3676. begin
  3677. result:=ptruinttype;
  3678. end;
  3679. function tpointerdef.pointer_subtraction_result_type:tdef;
  3680. begin
  3681. result:=ptrsinttype;
  3682. end;
  3683. function tpointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  3684. begin
  3685. result:=true;
  3686. end;
  3687. function tpointerdef.converted_pointer_to_array_range_type:tdef;
  3688. begin
  3689. result:=ptrsinttype;
  3690. end;
  3691. {****************************************************************************
  3692. TCLASSREFDEF
  3693. ****************************************************************************}
  3694. constructor tclassrefdef.create(def:tdef);
  3695. begin
  3696. while (def.typ=objectdef) and tobjectdef(def).is_unique_objpasdef do
  3697. def:=tobjectdef(def).childof;
  3698. inherited create(classrefdef,def);
  3699. if df_specialization in tstoreddef(def).defoptions then
  3700. genericdef:=cclassrefdef.create(tstoreddef(def).genericdef);
  3701. end;
  3702. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  3703. begin
  3704. inherited ppuload(classrefdef,ppufile);
  3705. ppuload_platform(ppufile);
  3706. end;
  3707. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  3708. begin
  3709. inherited ppuwrite(ppufile);
  3710. writeentry(ppufile,ibclassrefdef);
  3711. end;
  3712. function tclassrefdef.getcopy:tstoreddef;
  3713. begin
  3714. if pointeddef.typ=forwarddef then
  3715. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  3716. else
  3717. result:=cclassrefdef.create(pointeddef);
  3718. end;
  3719. function tclassrefdef.GetTypeName : string;
  3720. begin
  3721. GetTypeName:='Class Of '+pointeddef.typename;
  3722. end;
  3723. function tclassrefdef.is_publishable : tpublishproperty;
  3724. begin
  3725. is_publishable:=pp_publish;
  3726. end;
  3727. function tclassrefdef.rtti_mangledname(rt: trttitype): TSymStr;
  3728. begin
  3729. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  3730. result:=inherited rtti_mangledname(rt)
  3731. else
  3732. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  3733. end;
  3734. procedure tclassrefdef.register_created_object_type;
  3735. begin
  3736. tobjectdef(pointeddef).register_created_classref_type;
  3737. end;
  3738. {***************************************************************************
  3739. TSETDEF
  3740. ***************************************************************************}
  3741. constructor tsetdef.create(def: tdef; low, high: asizeint; doregister: boolean);
  3742. var
  3743. setallocbits: aint;
  3744. packedsavesize: aint;
  3745. actual_setalloc: ShortInt;
  3746. begin
  3747. inherited create(setdef,doregister);
  3748. elementdef:=def;
  3749. elementdefderef.reset;
  3750. setmax:=high;
  3751. actual_setalloc:=current_settings.setalloc;
  3752. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  3753. if actual_setalloc=0 then
  3754. actual_setalloc:=1;
  3755. {$endif}
  3756. if (actual_setalloc=0) then
  3757. begin
  3758. setbase:=0;
  3759. if (high<32) then
  3760. savesize:=Sizeof(longint)
  3761. else if (high<256) then
  3762. savesize:=32
  3763. else
  3764. savesize:=(high+7) div 8
  3765. end
  3766. else
  3767. begin
  3768. setallocbits:=actual_setalloc*8;
  3769. setbase:=low and not(setallocbits-1);
  3770. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  3771. savesize:=packedsavesize;
  3772. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  3773. if savesize=3 then
  3774. savesize:=4;
  3775. {$endif}
  3776. end;
  3777. end;
  3778. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  3779. begin
  3780. inherited ppuload(setdef,ppufile);
  3781. ppufile.getderef(elementdefderef);
  3782. savesize:=ppufile.getasizeint;
  3783. setbase:=ppufile.getasizeint;
  3784. setmax:=ppufile.getasizeint;
  3785. ppuload_platform(ppufile);
  3786. end;
  3787. function tsetdef.getcopy : tstoreddef;
  3788. begin
  3789. result:=csetdef.create(elementdef,setbase,setmax,true);
  3790. { the copy might have been created with a different setalloc setting }
  3791. tsetdef(result).savesize:=savesize;
  3792. end;
  3793. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  3794. begin
  3795. inherited ppuwrite(ppufile);
  3796. ppufile.putderef(elementdefderef);
  3797. ppufile.putasizeint(savesize);
  3798. ppufile.putasizeint(setbase);
  3799. ppufile.putasizeint(setmax);
  3800. writeentry(ppufile,ibsetdef);
  3801. end;
  3802. procedure tsetdef.buildderef;
  3803. begin
  3804. inherited buildderef;
  3805. elementdefderef.build(elementdef);
  3806. end;
  3807. procedure tsetdef.deref;
  3808. begin
  3809. inherited deref;
  3810. elementdef:=tdef(elementdefderef.resolve);
  3811. end;
  3812. function tsetdef.is_publishable : tpublishproperty;
  3813. begin
  3814. if savesize in [1,2,4] then
  3815. is_publishable:=pp_publish
  3816. else
  3817. is_publishable:=pp_error;
  3818. end;
  3819. function tsetdef.alignment: shortint;
  3820. begin
  3821. Result:=inherited;
  3822. if result>sizeof(aint) then
  3823. result:=sizeof(aint);
  3824. end;
  3825. function tsetdef.GetTypeName : string;
  3826. begin
  3827. if assigned(elementdef) then
  3828. GetTypeName:='Set Of '+elementdef.typename
  3829. else
  3830. GetTypeName:='Empty Set';
  3831. end;
  3832. {***************************************************************************
  3833. TFORMALDEF
  3834. ***************************************************************************}
  3835. constructor tformaldef.create(Atyped:boolean);
  3836. begin
  3837. inherited create(formaldef,true);
  3838. typed:=Atyped;
  3839. savesize:=0;
  3840. end;
  3841. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  3842. begin
  3843. inherited ppuload(formaldef,ppufile);
  3844. typed:=boolean(ppufile.getbyte);
  3845. savesize:=0;
  3846. ppuload_platform(ppufile);
  3847. end;
  3848. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  3849. begin
  3850. inherited ppuwrite(ppufile);
  3851. ppufile.putbyte(byte(typed));
  3852. writeentry(ppufile,ibformaldef);
  3853. end;
  3854. function tformaldef.GetTypeName : string;
  3855. begin
  3856. if typed then
  3857. GetTypeName:='<Typed formal type>'
  3858. else
  3859. GetTypeName:='<Formal type>';
  3860. end;
  3861. {***************************************************************************
  3862. TARRAYDEF
  3863. ***************************************************************************}
  3864. constructor tarraydef.create(l, h: asizeint; def: tdef);
  3865. begin
  3866. inherited create(arraydef,true);
  3867. lowrange:=l;
  3868. highrange:=h;
  3869. rangedef:=def;
  3870. rangedefderef.reset;
  3871. _elementdef:=nil;
  3872. _elementdefderef.reset;
  3873. arrayoptions:=[];
  3874. symtable:=tarraysymtable.create(self);
  3875. end;
  3876. constructor tarraydef.create_vector(l ,h: asizeint; def: tdef);
  3877. begin
  3878. self.create(l,h,def);
  3879. include(arrayoptions,ado_IsVector);
  3880. end;
  3881. constructor tarraydef.create_openarray;
  3882. begin
  3883. self.create(0,-1,sizesinttype);
  3884. include(arrayoptions,ado_OpenArray);
  3885. end;
  3886. class function tarraydef.getreusable_intern(def: tdef; elems: asizeint; const options: tarraydefoptions): tarraydef;
  3887. var
  3888. res: PHashSetItem;
  3889. oldsymtablestack: tsymtablestack;
  3890. arrdesc: packed record
  3891. def: tdef;
  3892. elecount: asizeint;
  3893. options: tarraydefoptions
  3894. end;
  3895. begin
  3896. if not assigned(current_module) then
  3897. internalerror(2011081301);
  3898. arrdesc.def:=def;
  3899. arrdesc.elecount:=elems;
  3900. arrdesc.options:=options;
  3901. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  3902. if not assigned(res^.Data) then
  3903. begin
  3904. { since these pointerdefs can be reused anywhere in the current
  3905. unit, add them to the global/staticsymtable (or local symtable
  3906. if they're a local def, because otherwise they'll be saved
  3907. to the ppu referencing a local symtable entry that doesn't
  3908. exist in the ppu) }
  3909. oldsymtablestack:=symtablestack;
  3910. { do not simply push/pop current_module.localsymtable, because
  3911. that can have side-effects (e.g., it removes helpers) }
  3912. symtablestack:=nil;
  3913. result:=carraydef.create(0,elems-1,sizesinttype);
  3914. result.elementdef:=def;
  3915. result.arrayoptions:=options;
  3916. setup_reusable_def(def,result,res,oldsymtablestack);
  3917. { res^.Data may still be nil -> don't overwrite result }
  3918. exit;
  3919. end;
  3920. result:=tarraydef(res^.Data);
  3921. end;
  3922. class function tarraydef.getreusable(def: tdef; elems: asizeint): tarraydef;
  3923. begin
  3924. result:=getreusable_intern(def,elems,[]);
  3925. end;
  3926. class function tarraydef.getreusable_vector(def: tdef; elems: asizeint): tarraydef;
  3927. begin
  3928. result:=getreusable_intern(def,elems,[ado_IsVector]);
  3929. end;
  3930. class function tarraydef.getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  3931. begin
  3932. result:=getreusable(def,elems);
  3933. if not result.is_registered then
  3934. include(result.defoptions,df_not_registered_no_free);
  3935. end;
  3936. class function tarraydef.getreusable_no_free_vector(def: tdef; elems: asizeint): tarraydef;
  3937. begin
  3938. result:=getreusable_vector(def,elems);
  3939. if not result.is_registered then
  3940. include(result.defoptions,df_not_registered_no_free);
  3941. end;
  3942. destructor tarraydef.destroy;
  3943. begin
  3944. symtable.free;
  3945. symtable:=nil;
  3946. inherited;
  3947. end;
  3948. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3949. begin
  3950. { divide by the element size and do -1 so the array will have a valid size,
  3951. further, the element size might be 0 e.g. for empty records, so use max(...,1)
  3952. to avoid a division by zero }
  3953. self.create(0,(high(asizeint) div max(def.pointeddef.size,1))-1,
  3954. def.converted_pointer_to_array_range_type);
  3955. arrayoptions:=[ado_IsConvertedPointer];
  3956. setelementdef(def.pointeddef);
  3957. end;
  3958. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3959. begin
  3960. inherited ppuload(arraydef,ppufile);
  3961. { the addresses are calculated later }
  3962. ppufile.getderef(_elementdefderef);
  3963. ppufile.getderef(rangedefderef);
  3964. lowrange:=ppufile.getasizeint;
  3965. highrange:=ppufile.getasizeint;
  3966. ppufile.getset(tppuset2(arrayoptions));
  3967. ppuload_platform(ppufile);
  3968. symtable:=tarraysymtable.create(self);
  3969. tarraysymtable(symtable).ppuload(ppufile)
  3970. end;
  3971. function tarraydef.getcopy : tstoreddef;
  3972. begin
  3973. result:=carraydef.create(lowrange,highrange,rangedef);
  3974. tarraydef(result).arrayoptions:=arrayoptions;
  3975. tarraydef(result)._elementdef:=_elementdef;
  3976. end;
  3977. procedure tarraydef.buildderef;
  3978. begin
  3979. inherited buildderef;
  3980. tarraysymtable(symtable).buildderef;
  3981. _elementdefderef.build(_elementdef);
  3982. rangedefderef.build(rangedef);
  3983. end;
  3984. procedure tarraydef.deref;
  3985. begin
  3986. inherited deref;
  3987. tarraysymtable(symtable).deref(false);
  3988. _elementdef:=tdef(_elementdefderef.resolve);
  3989. rangedef:=tdef(rangedefderef.resolve);
  3990. end;
  3991. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3992. begin
  3993. inherited ppuwrite(ppufile);
  3994. ppufile.putderef(_elementdefderef);
  3995. ppufile.putderef(rangedefderef);
  3996. ppufile.putasizeint(lowrange);
  3997. ppufile.putasizeint(highrange);
  3998. ppufile.putset(tppuset2(arrayoptions));
  3999. writeentry(ppufile,ibarraydef);
  4000. tarraysymtable(symtable).ppuwrite(ppufile);
  4001. end;
  4002. function tarraydef.elesize : asizeint;
  4003. begin
  4004. if (ado_IsBitPacked in arrayoptions) then
  4005. internalerror(2006080101);
  4006. if assigned(_elementdef) then
  4007. result:=_elementdef.size
  4008. else
  4009. result:=0;
  4010. end;
  4011. function tarraydef.elepackedbitsize : asizeint;
  4012. begin
  4013. if not(ado_IsBitPacked in arrayoptions) then
  4014. internalerror(2006080102);
  4015. if assigned(_elementdef) then
  4016. result:=_elementdef.packedbitsize
  4017. else
  4018. result:=0;
  4019. end;
  4020. function tarraydef.elecount : asizeuint;
  4021. var
  4022. qhigh,qlow : qword;
  4023. begin
  4024. if ado_IsDynamicArray in arrayoptions then
  4025. begin
  4026. result:=0;
  4027. exit;
  4028. end;
  4029. { check whether the range might be larger than high(asizeint). Has
  4030. to include 0..high(sizeint), since that's high(sizeint)+1 elements }
  4031. if (highrange>=0) and (lowrange<=0) then
  4032. begin
  4033. qhigh:=highrange;
  4034. if lowrange=low(asizeint) then
  4035. qlow:=high(asizeint) + 1
  4036. else
  4037. qlow:=qword(-lowrange);
  4038. { prevent overflow, return 0 to indicate overflow }
  4039. if qhigh+qlow>qword(high(asizeint)-1) then
  4040. result:=0
  4041. else
  4042. result:=qhigh+qlow+1;
  4043. end
  4044. else
  4045. result:=int64(highrange)-lowrange+1;
  4046. end;
  4047. function tarraydef.size : asizeint;
  4048. var
  4049. cachedelecount : asizeuint;
  4050. cachedelesize : asizeint;
  4051. begin
  4052. if ado_IsDynamicArray in arrayoptions then
  4053. begin
  4054. size:=voidpointertype.size;
  4055. exit;
  4056. end;
  4057. { Tarraydef.size may never be called for an open array! }
  4058. if ado_OpenArray in arrayoptions then
  4059. internalerror(99080501);
  4060. if not (ado_IsBitPacked in arrayoptions) then
  4061. cachedelesize:=elesize
  4062. else
  4063. cachedelesize := elepackedbitsize;
  4064. cachedelecount:=elecount;
  4065. if (cachedelesize = 0) then
  4066. begin
  4067. size := 0;
  4068. exit;
  4069. end;
  4070. if (cachedelecount = 0) then
  4071. begin
  4072. if ado_isconststring in arrayoptions then
  4073. size := 0
  4074. else
  4075. size := -1;
  4076. exit;
  4077. end;
  4078. { prevent overflow, return -1 to indicate overflow }
  4079. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  4080. if (cachedelecount > asizeuint(high(asizeint))) or
  4081. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  4082. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  4083. accessing the array, see ncgmem (PFV) }
  4084. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  4085. begin
  4086. result:=-1;
  4087. exit;
  4088. end;
  4089. result:=cachedelesize*asizeint(cachedelecount);
  4090. if (ado_IsBitPacked in arrayoptions) then
  4091. { can't just add 7 and divide by 8, because that may overflow }
  4092. result:=result div 8 + ord((result mod 8)<>0);
  4093. {$ifdef cpu16bitaddr}
  4094. if result>65535 then
  4095. begin
  4096. result:=-1;
  4097. exit;
  4098. end;
  4099. {$endif cpu16bitaddr}
  4100. end;
  4101. procedure tarraydef.setelementdef(def:tdef);
  4102. begin
  4103. _elementdef:=def;
  4104. if not(
  4105. (ado_IsDynamicArray in arrayoptions) or
  4106. (ado_IsConvertedPointer in arrayoptions) or
  4107. (ado_IsConstructor in arrayoptions) or
  4108. (ado_IsGeneric in arrayoptions) or
  4109. (highrange<lowrange)
  4110. ) and
  4111. (size=-1) then
  4112. Message(sym_e_segment_too_large);
  4113. end;
  4114. function tarraydef.alignment : shortint;
  4115. begin
  4116. if ado_IsVector in arrayoptions then
  4117. alignment:=size
  4118. { alignment of dyn. arrays doesn't depend on the element size }
  4119. else if ado_IsDynamicArray in arrayoptions then
  4120. alignment:=voidpointertype.alignment
  4121. { alignment is the target alignment for the used load size }
  4122. else if (ado_IsBitPacked in arrayoptions) and
  4123. (elementdef.typ in [enumdef,orddef]) then
  4124. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  4125. { alignment is the alignment of the elements }
  4126. else
  4127. alignment:=elementdef.alignment
  4128. end;
  4129. function tarraydef.needs_inittable : boolean;
  4130. begin
  4131. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  4132. end;
  4133. function tarraydef.needs_separate_initrtti : boolean;
  4134. begin
  4135. if ado_IsBitPacked in arrayoptions then
  4136. result:=false
  4137. else
  4138. result:=elementdef.needs_separate_initrtti;
  4139. end;
  4140. function tarraydef.GetTypeName : string;
  4141. begin
  4142. if (ado_IsConstString in arrayoptions) then
  4143. result:='Constant String'
  4144. else if (ado_isarrayofconst in arrayoptions) or
  4145. (ado_isConstructor in arrayoptions) then
  4146. begin
  4147. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  4148. GetTypeName:='Array Of Const'
  4149. else
  4150. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  4151. end
  4152. else if (ado_IsDynamicArray in arrayoptions) then
  4153. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  4154. else if (ado_OpenArray in arrayoptions) then
  4155. GetTypeName:='{Open} Array Of '+elementdef.typename
  4156. else
  4157. begin
  4158. result := '';
  4159. if (ado_IsBitPacked in arrayoptions) then
  4160. result:='BitPacked ';
  4161. if rangedef.typ=enumdef then
  4162. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  4163. else
  4164. result:=result+'Array['+tostr(lowrange)+'..'+
  4165. tostr(highrange)+'] Of '+elementdef.typename
  4166. end;
  4167. end;
  4168. function tarraydef.getmangledparaname : TSymStr;
  4169. begin
  4170. if ado_isarrayofconst in arrayoptions then
  4171. getmangledparaname:='array_of_const'
  4172. else
  4173. if ((highrange=-1) and (lowrange=0)) then
  4174. getmangledparaname:='array_of_'+elementdef.mangledparaname
  4175. else
  4176. internalerror(200204176);
  4177. end;
  4178. function tarraydef.is_publishable : tpublishproperty;
  4179. begin
  4180. if ado_IsDynamicArray in arrayoptions then
  4181. is_publishable:=pp_publish
  4182. else
  4183. is_publishable:=pp_error;
  4184. end;
  4185. function tarraydef.is_hwvector: boolean;
  4186. begin
  4187. result:=ado_IsVector in arrayoptions;
  4188. end;
  4189. {***************************************************************************
  4190. tabstractrecorddef
  4191. ***************************************************************************}
  4192. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp;doregister:boolean);
  4193. begin
  4194. inherited create(dt,doregister);
  4195. objname:=stringdup(upper(n));
  4196. objrealname:=stringdup(n);
  4197. objectoptions:=[];
  4198. if assigned(current_module.namespace) then
  4199. begin
  4200. import_lib:=stringdup(current_module.namespace^);
  4201. replace(import_lib^,'.','/');
  4202. end;
  4203. end;
  4204. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4205. var
  4206. ro: trtti_option;
  4207. begin
  4208. inherited ppuload(dt,ppufile);
  4209. objrealname:=ppufile.getpshortstring;
  4210. objname:=stringdup(upper(objrealname^));
  4211. import_lib:=ppufile.getpshortstring;
  4212. { only used for external C++ classes and Java classes/records }
  4213. if (import_lib^='') then
  4214. stringdispose(import_lib);
  4215. ppufile.getset(tppuset4(objectoptions));
  4216. rtti.clause:=trtti_clause(ppufile.getbyte);
  4217. for ro in trtti_option do
  4218. ppufile.getset(tppuset1(rtti.options[ro]));
  4219. end;
  4220. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  4221. var
  4222. ro: trtti_option;
  4223. begin
  4224. inherited ppuwrite(ppufile);
  4225. ppufile.putstring(objrealname^);
  4226. if assigned(import_lib) then
  4227. ppufile.putstring(import_lib^)
  4228. else
  4229. ppufile.putstring('');
  4230. ppufile.putset(tppuset4(objectoptions));
  4231. ppufile.putbyte(byte(rtti.clause));
  4232. for ro in trtti_option do
  4233. ppufile.putset(tppuset1(rtti.options[ro]));
  4234. end;
  4235. destructor tabstractrecorddef.destroy;
  4236. begin
  4237. stringdispose(objname);
  4238. stringdispose(objrealname);
  4239. stringdispose(import_lib);
  4240. tcinitcode.free;
  4241. inherited destroy;
  4242. end;
  4243. procedure tabstractrecorddef.buildderefimpl;
  4244. begin
  4245. inherited buildderefimpl;
  4246. if not (df_copied_def in defoptions) then
  4247. tstoredsymtable(symtable).buildderefimpl;
  4248. end;
  4249. procedure tabstractrecorddef.derefimpl;
  4250. begin
  4251. inherited derefimpl;
  4252. if not (df_copied_def in defoptions) then
  4253. tstoredsymtable(symtable).derefimpl(false);
  4254. end;
  4255. procedure tabstractrecorddef.check_forwards;
  4256. begin
  4257. { the defs of a copied def are defined for the original type only }
  4258. if not(df_copied_def in defoptions) then
  4259. tstoredsymtable(symtable).check_forwards;
  4260. end;
  4261. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  4262. var
  4263. i: longint;
  4264. sym: tsym;
  4265. begin
  4266. for i:=0 to symtable.SymList.Count-1 do
  4267. begin
  4268. sym:=tsym(symtable.SymList[i]);
  4269. if sym.typ=procsym then
  4270. begin
  4271. result:=tprocsym(sym).find_procdef_bytype(pt);
  4272. if assigned(result) then
  4273. exit;
  4274. end;
  4275. end;
  4276. result:=nil;
  4277. end;
  4278. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  4279. begin
  4280. if t=gs_record then
  4281. GetSymtable:=symtable
  4282. else
  4283. GetSymtable:=nil;
  4284. end;
  4285. function tabstractrecorddef.is_packed:boolean;
  4286. begin
  4287. result:=tabstractrecordsymtable(symtable).is_packed;
  4288. end;
  4289. function tabstractrecorddef.RttiName: string;
  4290. function generate_full_paramname(maxlength:longint):string;
  4291. const
  4292. commacount : array[boolean] of longint = (0,1);
  4293. var
  4294. fullparas,
  4295. paramname : ansistring;
  4296. module : tmodule;
  4297. sym : tsym;
  4298. def : tdef;
  4299. i : longint;
  4300. begin
  4301. { we want at least enough space for an ellipsis }
  4302. if maxlength<3 then
  4303. internalerror(2014121203);
  4304. fullparas:='';
  4305. for i:=0 to genericparas.count-1 do
  4306. begin
  4307. sym:=tsym(genericparas[i]);
  4308. module:=find_module_from_symtable(sym.owner);
  4309. if not assigned(module) then
  4310. internalerror(2014121202);
  4311. if not (sym.typ in [constsym,symconst.typesym]) then
  4312. internalerror(2020042501);
  4313. if sym.typ=constsym then
  4314. def:=tconstsym(sym).constdef
  4315. else
  4316. def:=ttypesym(sym).typedef;
  4317. paramname:=module.realmodulename^;
  4318. if def.typ in [objectdef,recorddef] then
  4319. paramname:=paramname+'.'+tabstractrecorddef(def).rttiname
  4320. else
  4321. paramname:=paramname+'.'+def.typename;
  4322. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  4323. begin
  4324. if i>0 then
  4325. fullparas:=fullparas+',...'
  4326. else
  4327. fullparas:=fullparas+'...';
  4328. break;
  4329. end;
  4330. { could we fit an ellipsis after this parameter if it should be too long? }
  4331. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  4332. begin
  4333. { then omit already this parameter }
  4334. if i>0 then
  4335. fullparas:=fullparas+',...'
  4336. else
  4337. fullparas:=fullparas+'...';
  4338. break;
  4339. end;
  4340. if i>0 then
  4341. fullparas:=fullparas+',';
  4342. fullparas:=fullparas+paramname;
  4343. end;
  4344. result:=fullparas;
  4345. end;
  4346. var
  4347. nongeneric,
  4348. basename : string;
  4349. i,
  4350. remlength,
  4351. paramcount,
  4352. crcidx : longint;
  4353. begin
  4354. if rttistring='' then
  4355. begin
  4356. if is_specialization then
  4357. begin
  4358. rttistring:=OwnerHierarchyName;
  4359. { there should be two $ characters, one before the CRC and one before the count }
  4360. crcidx:=-1;
  4361. for i:=length(objrealname^) downto 1 do
  4362. if objrealname^[i]='$' then
  4363. begin
  4364. crcidx:=i;
  4365. break;
  4366. end;
  4367. if crcidx<0 then
  4368. internalerror(2014121201);
  4369. basename:=copy(objrealname^,1,crcidx-1);
  4370. split_generic_name(basename,nongeneric,paramcount);
  4371. rttistring:=rttistring+nongeneric+'<';
  4372. remlength:=255-length(rttistring)-1;
  4373. if remlength<4 then
  4374. rttistring:=rttistring+'>'
  4375. else
  4376. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  4377. end
  4378. else
  4379. if is_generic then
  4380. begin
  4381. rttistring:=OwnerHierarchyName;
  4382. split_generic_name(objrealname^,nongeneric,paramcount);
  4383. rttistring:=rttistring+nongeneric+'<';
  4384. { we don't want any ',' if there is only one parameter }
  4385. for i:=0 to paramcount-0 do
  4386. rttistring:=rttistring+',';
  4387. rttistring:=rttistring+'>';
  4388. end
  4389. else
  4390. rttistring:=OwnerHierarchyName+objrealname^;
  4391. end;
  4392. result:=rttistring;
  4393. end;
  4394. function tabstractrecorddef.search_enumerator_get: tprocdef;
  4395. var
  4396. sym : tsym;
  4397. i : integer;
  4398. pd : tprocdef;
  4399. hashedid : THashedIDString;
  4400. begin
  4401. result:=nil;
  4402. hashedid.id:='GETENUMERATOR';
  4403. sym:=tsym(symtable.FindWithHash(hashedid));
  4404. if assigned(sym) and (sym.typ=procsym) then
  4405. begin
  4406. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4407. begin
  4408. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4409. if (pd.proctypeoption = potype_function) and
  4410. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  4411. (pd.visibility >= vis_public) then
  4412. begin
  4413. result:=pd;
  4414. exit;
  4415. end;
  4416. end;
  4417. end;
  4418. end;
  4419. function tabstractrecorddef.search_enumerator_move: tprocdef;
  4420. var
  4421. sym : tsym;
  4422. i : integer;
  4423. pd : tprocdef;
  4424. hashedid : THashedIDString;
  4425. begin
  4426. result:=nil;
  4427. // first search for po_enumerator_movenext method modifier
  4428. // then search for public function MoveNext: Boolean
  4429. for i:=0 to symtable.SymList.Count-1 do
  4430. begin
  4431. sym:=TSym(symtable.SymList[i]);
  4432. if (sym.typ=procsym) then
  4433. begin
  4434. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4435. if assigned(pd) then
  4436. begin
  4437. result:=pd;
  4438. exit;
  4439. end;
  4440. end;
  4441. end;
  4442. hashedid.id:='MOVENEXT';
  4443. sym:=tsym(symtable.FindWithHash(hashedid));
  4444. if assigned(sym) and (sym.typ=procsym) then
  4445. begin
  4446. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  4447. begin
  4448. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4449. if (pd.proctypeoption = potype_function) and
  4450. is_boolean(pd.returndef) and
  4451. (pd.minparacount = 0) and
  4452. (pd.visibility >= vis_public) then
  4453. begin
  4454. result:=pd;
  4455. exit;
  4456. end;
  4457. end;
  4458. end;
  4459. end;
  4460. function tabstractrecorddef.search_enumerator_current: tsym;
  4461. var
  4462. sym: tsym;
  4463. i: integer;
  4464. hashedid : THashedIDString;
  4465. begin
  4466. result:=nil;
  4467. // first search for ppo_enumerator_current property modifier
  4468. // then search for public property Current
  4469. for i:=0 to symtable.SymList.Count-1 do
  4470. begin
  4471. sym:=TSym(symtable.SymList[i]);
  4472. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4473. begin
  4474. result:=sym;
  4475. exit;
  4476. end;
  4477. end;
  4478. hashedid.id:='CURRENT';
  4479. sym:=tsym(symtable.FindWithHash(hashedid));
  4480. if assigned(sym) and (sym.typ=propertysym) and
  4481. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4482. begin
  4483. result:=sym;
  4484. exit;
  4485. end;
  4486. end;
  4487. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  4488. var
  4489. st: tsymtable;
  4490. enclosingdef: tdef;
  4491. begin
  4492. if typ=objectdef then
  4493. result:=tobjectdef(self).objextname^
  4494. else if assigned(typesym) then
  4495. result:=typesym.realname
  4496. { have to generate anonymous nested type in current unit/class/record }
  4497. else
  4498. internalerror(2011032601);
  4499. { in case of specializations, add some extras to prevent name conflicts
  4500. with nested classes }
  4501. if df_specialization in defoptions then
  4502. result:='$'+result+'$specialization$';
  4503. st:=owner;
  4504. while assigned(st) and
  4505. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  4506. begin
  4507. { nested classes are named as "OuterClass$InnerClass" }
  4508. enclosingdef:=tdef(st.defowner);
  4509. if enclosingdef.typ=procdef then
  4510. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  4511. else if enclosingdef.typ=objectdef then
  4512. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  4513. else if assigned(enclosingdef.typesym) then
  4514. result:=enclosingdef.typesym.realname+'$'+result
  4515. else
  4516. internalerror(2011060305);
  4517. st:=enclosingdef.owner;
  4518. end;
  4519. if with_package_name and
  4520. assigned(import_lib) then
  4521. result:=import_lib^+'/'+result;
  4522. end;
  4523. function tabstractrecorddef.contains_float_field: boolean;
  4524. var
  4525. i : longint;
  4526. begin
  4527. result:=true;
  4528. for i:=0 to symtable.symlist.count-1 do
  4529. begin
  4530. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4531. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4532. continue;
  4533. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4534. begin
  4535. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  4536. exit;
  4537. { search recursively }
  4538. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4539. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  4540. exit;
  4541. end;
  4542. end;
  4543. result:=false;
  4544. end;
  4545. function tabstractrecorddef.contains_cross_aword_field: boolean;
  4546. var
  4547. i : longint;
  4548. foffset, fsize: aword;
  4549. begin
  4550. result:=true;
  4551. for i:=0 to symtable.symlist.count-1 do
  4552. begin
  4553. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4554. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4555. continue;
  4556. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4557. begin
  4558. if is_packed then
  4559. begin
  4560. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset;
  4561. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.packedbitsize;
  4562. end
  4563. else
  4564. begin
  4565. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset*8;
  4566. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.size*8;
  4567. end;
  4568. if (fsize>0) and ((foffset div (sizeof(aword)*8)) <> ((foffset+fsize-1) div (sizeof(aword)*8))) then
  4569. exit;
  4570. { search recursively }
  4571. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4572. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_cross_aword_field) then
  4573. exit;
  4574. end;
  4575. end;
  4576. result:=false;
  4577. end;
  4578. procedure tabstractrecorddef.apply_rtti_directive(dir: trtti_directive);
  4579. begin
  4580. { records don't support the inherit clause but shouldn't
  4581. give an error either if used (for Delphi compatibility),
  4582. so we silently enforce the clause as explicit. }
  4583. rtti.clause:=rtc_explicit;
  4584. rtti.options:=dir.options;
  4585. end;
  4586. function tabstractrecorddef.is_visible_for_rtti(option: trtti_option; vis: tvisibility): boolean;
  4587. begin
  4588. case vis of
  4589. vis_private,
  4590. vis_strictprivate: result:=rv_private in rtti.options[option];
  4591. vis_protected,
  4592. vis_strictprotected: result:=rv_protected in rtti.options[option];
  4593. vis_public: result:=rv_public in rtti.options[option];
  4594. vis_published: result:=rv_published in rtti.options[option];
  4595. otherwise
  4596. result:=false;
  4597. end;
  4598. end;
  4599. function tabstractrecorddef.rtti_visibilities_for_option(option: trtti_option): tvisibilities;
  4600. begin
  4601. result:=[];
  4602. if rv_private in rtti.options[option] then
  4603. begin
  4604. include(result,vis_private);
  4605. include(result,vis_strictprivate);
  4606. end;
  4607. if rv_protected in rtti.options[option] then
  4608. begin
  4609. include(result,vis_protected);
  4610. include(result,vis_strictprotected);
  4611. end;
  4612. if rv_public in rtti.options[option] then
  4613. include(result,vis_public);
  4614. if rv_published in rtti.options[option] then
  4615. include(result,vis_published);
  4616. end;
  4617. function tabstractrecorddef.has_extended_rtti: boolean;
  4618. begin
  4619. result := (rtti.options[ro_fields]<>[]) or
  4620. (rtti.options[ro_methods]<>[]) or
  4621. (rtti.options[ro_properties]<>[]);
  4622. end;
  4623. {$ifdef DEBUG_NODE_XML}
  4624. procedure tabstractrecorddef.XMLPrintDefData(var T: Text; Sym: TSym);
  4625. procedure WriteSymOptions(SourceSym: TSym);
  4626. var
  4627. i: TSymOption;
  4628. first: Boolean;
  4629. begin
  4630. First := True;
  4631. for i := Low(TSymOption) to High(TSymOption) do
  4632. if i in SourceSym.symoptions then
  4633. begin
  4634. if First then
  4635. begin
  4636. Write(T, '" symoptions="', i);
  4637. First := False;
  4638. end
  4639. else
  4640. Write(T, ',', i)
  4641. end;
  4642. end;
  4643. var
  4644. List: TFPHashObjectList;
  4645. i: Integer;
  4646. begin
  4647. WriteLn(T, PrintNodeIndention, '<size>', size, '</size>');
  4648. if (alignment = structalignment) and (alignment = aggregatealignment) then
  4649. begin
  4650. { Straightforward and simple }
  4651. WriteLn(T, PrintNodeIndention, '<alignment>', alignment, '</alignment>');
  4652. end
  4653. else
  4654. begin
  4655. WriteLn(T, PrintNodeIndention, '<alignment>');
  4656. printnodeindent;
  4657. WriteLn(T, PrintNodeIndention, '<basic>', alignment, '</basic>');
  4658. if (structalignment <> alignment) then
  4659. WriteLn(T, PrintNodeIndention, '<struct>', structalignment, '</struct>');
  4660. if (aggregatealignment <> alignment) and (aggregatealignment <> structalignment) then
  4661. WriteLn(T, PrintNodeIndention, '<aggregate>', aggregatealignment, '</aggregate>');
  4662. printnodeunindent;
  4663. WriteLn(T, PrintNodeIndention, '</alignment>');
  4664. end;
  4665. { List the fields }
  4666. List := Symtable.SymList;
  4667. for i := 0 to List.Count - 1 do
  4668. case TSym(List[i]).typ of
  4669. { staticvarsym,localvarsym,paravarsym,fieldvarsym,
  4670. typesym,procsym,unitsym,}
  4671. constsym:
  4672. with TConstSym(List[i]) do
  4673. begin
  4674. Write(T, PrintNodeIndention, '<const name="', RealName, '" pos="', fileinfo.line, ',', fileinfo.column);
  4675. WriteSymOptions(TSym(List[i]));
  4676. WriteLn(T, '">');
  4677. PrintNodeIndent;
  4678. XMLPrintConstData(T);
  4679. PrintNodeUnindent;
  4680. WriteLn(T, PrintNodeIndention, '</const>');
  4681. end;
  4682. {
  4683. errorsym,syssym,labelsym,absolutevarsym,propertysym,
  4684. macrosym,namespacesym,undefinedsym,programparasym
  4685. }
  4686. fieldvarsym:
  4687. with TFieldVarSym(List[i]) do
  4688. begin
  4689. Write(T, PrintNodeIndention, '<field name="', RealName, '" pos="', fileinfo.line, ',', fileinfo.column);
  4690. WriteSymOptions(TSym(List[i]));
  4691. WriteLn(T, '">');
  4692. PrintNodeIndent;
  4693. XMLPrintFieldData(T);
  4694. PrintNodeUnindent;
  4695. WriteLn(T, PrintNodeIndention, '</field>');
  4696. end;
  4697. else
  4698. ;
  4699. end;
  4700. end;
  4701. {$endif DEBUG_NODE_XML}
  4702. {***************************************************************************
  4703. trecorddef
  4704. ***************************************************************************}
  4705. constructor trecorddef.create(const n:string; p:TSymtable);
  4706. begin
  4707. inherited create(n,recorddef,true);
  4708. symtable:=p;
  4709. { we can own the symtable only if nobody else owns a copy so far }
  4710. if symtable.refcount=1 then
  4711. symtable.defowner:=self;
  4712. isunion:=false;
  4713. cloneddefderef.reset;
  4714. end;
  4715. constructor trecorddef.create_internal(const n: string; packrecords, recordalignmin: shortint; where: tsymtable);
  4716. var
  4717. name : string;
  4718. pname : pshortstring;
  4719. oldsymtablestack: tsymtablestack;
  4720. ts: ttypesym;
  4721. begin
  4722. { construct name }
  4723. if n<>'' then
  4724. pname:=@n
  4725. else
  4726. begin
  4727. init_defid;
  4728. name:='$InternalRec'+unique_id_str;
  4729. pname:=@name;
  4730. end;
  4731. oldsymtablestack:=symtablestack;
  4732. { do not simply push/pop current_module.localsymtable, because
  4733. that can have side-effects (e.g., it removes helpers) }
  4734. symtablestack:=nil;
  4735. symtable:=trecordsymtable.create(pname^,packrecords,recordalignmin);
  4736. symtable.defowner:=self;
  4737. isunion:=false;
  4738. inherited create(pname^,recorddef,true);
  4739. where.insertdef(self);
  4740. { if we specified a name, then we'll probably want to look up the
  4741. type again by name too -> create typesym }
  4742. if n<>'' then
  4743. begin
  4744. ts:=ctypesym.create(n,self);
  4745. include(ts.symoptions,sp_internal);
  4746. { avoid hints about unused types (these may only be used for
  4747. typed constant data) }
  4748. ts.increfcount;
  4749. where.insertsym(ts);
  4750. end;
  4751. symtablestack:=oldsymtablestack;
  4752. { don't create RTTI for internal types, these are not exported }
  4753. defstates:=defstates+[ds_rtti_table_written,ds_init_table_written];
  4754. include(defoptions,df_internal);
  4755. end;
  4756. constructor trecorddef.create_global_internal(const n: string; packrecords, recordalignmin: shortint);
  4757. var
  4758. where : tsymtable;
  4759. begin
  4760. where:=current_module.localsymtable;
  4761. if not assigned(where) then
  4762. where:=current_module.globalsymtable;
  4763. create_internal(n,packrecords,recordalignmin,where);
  4764. end;
  4765. function trecorddef.add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  4766. var
  4767. sym: tfieldvarsym;
  4768. name: TIDString;
  4769. pname: ^TIDString;
  4770. begin
  4771. if optionalname='' then
  4772. begin
  4773. name:='$f'+tostr(trecordsymtable(symtable).symlist.count);
  4774. pname:=@name
  4775. end
  4776. else
  4777. pname:=@optionalname;
  4778. sym:=cfieldvarsym.create(pname^,vs_value,def,[]);
  4779. symtable.insertsym(sym);
  4780. trecordsymtable(symtable).addfield(sym,vis_hidden);
  4781. result:=sym;
  4782. end;
  4783. procedure trecorddef.add_fields_from_deflist(fieldtypes: tfplist);
  4784. var
  4785. i: longint;
  4786. begin
  4787. for i:=0 to fieldtypes.count-1 do
  4788. add_field_by_def('',tdef(fieldtypes[i]));
  4789. end;
  4790. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  4791. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  4792. var
  4793. i,j : longint;
  4794. begin
  4795. if ppufile.getbyte=1 then
  4796. begin
  4797. new(variantrecdesc);
  4798. ppufile.getderef(variantrecdesc^.variantselectorderef);
  4799. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  4800. for i:=0 to high(variantrecdesc^.branches) do
  4801. begin
  4802. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  4803. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4804. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  4805. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4806. end;
  4807. end
  4808. else
  4809. variantrecdesc:=nil;
  4810. end;
  4811. begin
  4812. inherited ppuload(recorddef,ppufile);
  4813. if df_copied_def in defoptions then
  4814. begin
  4815. ppufile.getderef(cloneddefderef);
  4816. ppuload_platform(ppufile);
  4817. end
  4818. else
  4819. begin
  4820. symtable:=trecordsymtable.create(objrealname^,0,0);
  4821. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  4822. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  4823. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  4824. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  4825. trecordsymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  4826. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  4827. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  4828. ppufile.getset(tppuset1(trecordsymtable(symtable).managementoperators));
  4829. { position of ppuload_platform call must correspond
  4830. to position of writeentry in ppuwrite method }
  4831. ppuload_platform(ppufile);
  4832. trecordsymtable(symtable).ppuload(ppufile);
  4833. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4834. but because iso mode supports no units, there is no need to store the variantrecdesc
  4835. in the ppu
  4836. }
  4837. // readvariantrecdesc(variantrecdesc);
  4838. { requires usefieldalignment to be set }
  4839. symtable.defowner:=self;
  4840. end;
  4841. isunion:=false;
  4842. end;
  4843. destructor trecorddef.destroy;
  4844. procedure free_variantrecdesc(var variantrecdesc : pvariantrecdesc);
  4845. var
  4846. i : longint;
  4847. begin
  4848. while assigned(variantrecdesc) do
  4849. begin
  4850. for i:=0 to high(variantrecdesc^.branches) do
  4851. begin
  4852. free_variantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4853. SetLength(variantrecdesc^.branches[i].values,0);
  4854. end;
  4855. SetLength(variantrecdesc^.branches,0);
  4856. dispose(variantrecdesc);
  4857. variantrecdesc:=nil;
  4858. end;
  4859. end;
  4860. begin
  4861. if assigned(variantrecdesc) then
  4862. free_variantrecdesc(variantrecdesc);
  4863. if assigned(symtable) then
  4864. begin
  4865. symtable.free;
  4866. symtable:=nil;
  4867. end;
  4868. inherited destroy;
  4869. end;
  4870. function trecorddef.getcopy : tstoreddef;
  4871. begin
  4872. result:=crecorddef.create(objrealname^,symtable.getcopy);
  4873. trecorddef(result).isunion:=isunion;
  4874. include(trecorddef(result).defoptions,df_copied_def);
  4875. if assigned(tcinitcode) then
  4876. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  4877. if assigned(import_lib) then
  4878. trecorddef(result).import_lib:=stringdup(import_lib^);
  4879. end;
  4880. function trecorddef.needs_inittable : boolean;
  4881. begin
  4882. { each record with managed field or with any management operator needs
  4883. init table }
  4884. needs_inittable:=(trecordsymtable(symtable).managementoperators<>[]) or
  4885. trecordsymtable(symtable).needs_init_final
  4886. end;
  4887. function trecorddef.needs_separate_initrtti : boolean;
  4888. begin
  4889. result:=true;
  4890. end;
  4891. function trecorddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  4892. begin
  4893. result:=trecordsymtable(symtable).has_non_trivial_init;
  4894. end;
  4895. procedure trecorddef.buildderef;
  4896. begin
  4897. inherited buildderef;
  4898. if df_copied_def in defoptions then
  4899. cloneddefderef.build(symtable.defowner)
  4900. else
  4901. tstoredsymtable(symtable).buildderef;
  4902. end;
  4903. procedure trecorddef.deref;
  4904. begin
  4905. inherited deref;
  4906. { now dereference the definitions }
  4907. if df_copied_def in defoptions then
  4908. begin
  4909. cloneddef:=trecorddef(cloneddefderef.resolve);
  4910. symtable:=cloneddef.symtable.getcopy;
  4911. end
  4912. else
  4913. tstoredsymtable(symtable).deref(false);
  4914. { internal types, only load from the system unit }
  4915. if assigned(owner) and
  4916. assigned(owner.name) and
  4917. (owner.name^='SYSTEM') then
  4918. begin
  4919. { TGUID }
  4920. if not assigned(rec_tguid) and
  4921. (upper(typename)='TGUID') then
  4922. rec_tguid:=self
  4923. { JMP_BUF }
  4924. else if not assigned(rec_jmp_buf) and
  4925. (upper(typename)='JMP_BUF') then
  4926. rec_jmp_buf:=self
  4927. else if not assigned(rec_exceptaddr) and
  4928. (upper(typename)='TEXCEPTADDR') then
  4929. rec_exceptaddr:=self;
  4930. end;
  4931. end;
  4932. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  4933. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  4934. var
  4935. i,j : longint;
  4936. begin
  4937. if assigned(variantrecdesc) then
  4938. begin
  4939. ppufile.putbyte(1);
  4940. ppufile.putderef(variantrecdesc^.variantselectorderef);
  4941. ppufile.putasizeint(length(variantrecdesc^.branches));
  4942. for i:=0 to high(variantrecdesc^.branches) do
  4943. begin
  4944. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  4945. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4946. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  4947. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4948. end;
  4949. end
  4950. else
  4951. ppufile.putbyte(0);
  4952. end;
  4953. begin
  4954. inherited ppuwrite(ppufile);
  4955. if df_copied_def in defoptions then
  4956. ppufile.putderef(cloneddefderef)
  4957. else
  4958. begin
  4959. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  4960. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  4961. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  4962. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  4963. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignmin));
  4964. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  4965. ppufile.putword(trecordsymtable(symtable).paddingsize);
  4966. ppufile.putset(tppuset1(trecordsymtable(symtable).managementoperators));
  4967. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4968. but because iso mode supports no units, there is no need to store the variantrecdesc
  4969. in the ppu
  4970. }
  4971. // writevariantrecdesc(variantrecdesc);
  4972. end;
  4973. writeentry(ppufile,ibrecorddef);
  4974. if not(df_copied_def in defoptions) then
  4975. trecordsymtable(symtable).ppuwrite(ppufile);
  4976. end;
  4977. function trecorddef.size:asizeint;
  4978. begin
  4979. result:=trecordsymtable(symtable).datasize;
  4980. end;
  4981. function trecorddef.alignment:shortint;
  4982. begin
  4983. alignment:=trecordsymtable(symtable).recordalignment;
  4984. end;
  4985. function trecorddef.padalignment:shortint;
  4986. begin
  4987. padalignment := trecordsymtable(symtable).padalignment;
  4988. end;
  4989. function trecorddef.GetTypeName : string;
  4990. begin
  4991. GetTypeName:='<record type>'
  4992. end;
  4993. {$ifdef DEBUG_NODE_XML}
  4994. function TRecordDef.XMLPrintType: ansistring;
  4995. begin
  4996. Result := '&lt;record&gt;';
  4997. end;
  4998. {$endif DEBUG_NODE_XML}
  4999. {***************************************************************************
  5000. TABSTRACTPROCDEF
  5001. ***************************************************************************}
  5002. constructor tabstractprocdef.create(dt:tdeftyp;level:byte;doregister:boolean);
  5003. begin
  5004. inherited create(dt,doregister);
  5005. parast:=tparasymtable.create(self,level);
  5006. paras:=nil;
  5007. minparacount:=0;
  5008. maxparacount:=0;
  5009. proctypeoption:=potype_none;
  5010. proccalloption:=pocall_none;
  5011. procoptions:=[];
  5012. returndef:=voidtype;
  5013. returndefderef.reset;
  5014. savesize:=sizeof(pint);
  5015. callerargareasize:=0;
  5016. calleeargareasize:=0;
  5017. has_paraloc_info:=callnoside;
  5018. funcretloc[callerside].init;
  5019. funcretloc[calleeside].init;
  5020. check_mark_as_nested;
  5021. end;
  5022. destructor tabstractprocdef.destroy;
  5023. begin
  5024. if assigned(paras) then
  5025. begin
  5026. {$ifdef MEMDEBUG}
  5027. memprocpara.start;
  5028. {$endif MEMDEBUG}
  5029. paras.free;
  5030. paras:=nil;
  5031. {$ifdef MEMDEBUG}
  5032. memprocpara.stop;
  5033. {$endif MEMDEBUG}
  5034. end;
  5035. if assigned(parast) then
  5036. begin
  5037. {$ifdef MEMDEBUG}
  5038. memprocparast.start;
  5039. {$endif MEMDEBUG}
  5040. parast.free;
  5041. parast:=nil;
  5042. {$ifdef MEMDEBUG}
  5043. memprocparast.stop;
  5044. {$endif MEMDEBUG}
  5045. end;
  5046. funcretloc[callerside].done;
  5047. funcretloc[calleeside].done;
  5048. inherited destroy;
  5049. end;
  5050. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  5051. begin
  5052. if (tsym(p).typ<>paravarsym) then
  5053. exit;
  5054. inc(plongint(arg)^);
  5055. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  5056. begin
  5057. if not assigned(tparavarsym(p).defaultconstsym) then
  5058. inc(minparacount);
  5059. inc(maxparacount);
  5060. end;
  5061. end;
  5062. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  5063. begin
  5064. if (tsym(p).typ<>paravarsym) then
  5065. exit;
  5066. paras.add(p);
  5067. end;
  5068. procedure tabstractprocdef.calcparas;
  5069. var
  5070. paracount : longint;
  5071. begin
  5072. { This can already be assigned when
  5073. we need to reresolve this unit (PFV) }
  5074. if assigned(paras) then
  5075. paras.free;
  5076. paras:=tparalist.create(false);
  5077. paracount:=0;
  5078. minparacount:=0;
  5079. maxparacount:=0;
  5080. parast.SymList.ForEachCall(@count_para,@paracount);
  5081. paras.capacity:=paracount;
  5082. { Insert parameters in table }
  5083. parast.SymList.ForEachCall(@insert_para,nil);
  5084. { Order parameters }
  5085. paras.sortparas;
  5086. end;
  5087. function tabstractprocdef.mangledprocparanames(oldlen : longint) : string;
  5088. var
  5089. hash : qword;
  5090. hp : TParavarsym;
  5091. hs : TSymStr;
  5092. newlen,
  5093. i : integer;
  5094. begin
  5095. result:='';
  5096. hp:=nil;
  5097. { add parameter types }
  5098. for i:=0 to paras.count-1 do
  5099. begin
  5100. hp:=tparavarsym(paras[i]);
  5101. if not(vo_is_hidden_para in hp.varoptions) then
  5102. result:=result+'$'+hp.vardef.mangledparaname;
  5103. end;
  5104. { add resultdef, add $$ as separator to make it unique from a
  5105. parameter separator }
  5106. if not is_void(returndef) then
  5107. result:=result+'$$'+returndef.mangledparaname;
  5108. newlen:=length(result)+oldlen;
  5109. { Replace with hash if the parameter line is very long }
  5110. if (newlen-oldlen>12) and
  5111. ((newlen>100) or (newlen-oldlen>64)) then
  5112. begin
  5113. hash:=InitFnv64;
  5114. for i:=0 to paras.count-1 do
  5115. begin
  5116. hp:=tparavarsym(paras[i]);
  5117. if not(vo_is_hidden_para in hp.varoptions) then
  5118. begin
  5119. hs:=hp.vardef.mangledparaname;
  5120. hash:=UpdateFnv64(hash,hs[1],length(hs));
  5121. end;
  5122. end;
  5123. if not is_void(returndef) then
  5124. begin
  5125. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  5126. hs:='$$'+returndef.mangledparaname;
  5127. hash:=UpdateFnv64(hash,hs[1],length(hs));
  5128. end;
  5129. result:='$h'+Base64Mangle(hash);
  5130. end;
  5131. end;
  5132. procedure tabstractprocdef.buildderef;
  5133. begin
  5134. { released procdef? }
  5135. if not assigned(parast) then
  5136. exit;
  5137. inherited buildderef;
  5138. returndefderef.build(returndef);
  5139. if po_explicitparaloc in procoptions then
  5140. funcretloc[callerside].buildderef;
  5141. { parast }
  5142. tparasymtable(parast).buildderef;
  5143. end;
  5144. procedure tabstractprocdef.deref;
  5145. begin
  5146. inherited deref;
  5147. returndef:=tdef(returndefderef.resolve);
  5148. if po_explicitparaloc in procoptions then
  5149. begin
  5150. funcretloc[callerside].deref;
  5151. has_paraloc_info:=callerside;
  5152. end
  5153. else
  5154. begin
  5155. { deref is called after loading from a ppu, but also after another
  5156. unit has been reloaded/recompiled and all references must be
  5157. re-resolved. Since the funcretloc contains a reference to a tdef,
  5158. reset it so that we won't try to access the stale def }
  5159. funcretloc[callerside].init;
  5160. has_paraloc_info:=callnoside;
  5161. end;
  5162. { parast }
  5163. tparasymtable(parast).deref(false);
  5164. { recalculated parameters }
  5165. calcparas;
  5166. end;
  5167. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  5168. begin
  5169. inherited ppuload(dt,ppufile);
  5170. parast:=nil;
  5171. Paras:=nil;
  5172. minparacount:=0;
  5173. maxparacount:=0;
  5174. ppufile.getderef(returndefderef);
  5175. proctypeoption:=tproctypeoption(ppufile.getbyte);
  5176. proccalloption:=tproccalloption(ppufile.getbyte);
  5177. ppufile.getset(tppuset8(procoptions));
  5178. funcretloc[callerside].init;
  5179. if po_explicitparaloc in procoptions then
  5180. funcretloc[callerside].ppuload(ppufile);
  5181. savesize:=sizeof(pint);
  5182. if (po_explicitparaloc in procoptions) then
  5183. has_paraloc_info:=callerside;
  5184. end;
  5185. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  5186. var
  5187. oldintfcrc : boolean;
  5188. begin
  5189. { released procdef? }
  5190. if not assigned(parast) then
  5191. exit;
  5192. inherited ppuwrite(ppufile);
  5193. ppufile.putderef(returndefderef);
  5194. oldintfcrc:=ppufile.do_interface_crc;
  5195. ppufile.do_interface_crc:=false;
  5196. ppufile.putbyte(ord(proctypeoption));
  5197. ppufile.putbyte(ord(proccalloption));
  5198. ppufile.putset(tppuset8(procoptions));
  5199. ppufile.do_interface_crc:=oldintfcrc;
  5200. if (po_explicitparaloc in procoptions) then
  5201. funcretloc[callerside].ppuwrite(ppufile);
  5202. end;
  5203. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  5204. var
  5205. hs,s : ansistring;
  5206. hp : TParavarsym;
  5207. hpc : tconstsym;
  5208. first : boolean;
  5209. i,j : integer;
  5210. begin
  5211. s:='';
  5212. first:=true;
  5213. for i:=0 to paras.count-1 do
  5214. begin
  5215. hp:=tparavarsym(paras[i]);
  5216. if not(vo_is_hidden_para in hp.varoptions) or
  5217. (pno_showhidden in pno) then
  5218. begin
  5219. if first then
  5220. begin
  5221. s:=s+'(';
  5222. first:=false;
  5223. end
  5224. else
  5225. s:=s+';';
  5226. if vo_is_hidden_para in hp.varoptions then
  5227. s:=s+'<';
  5228. case hp.varspez of
  5229. vs_var :
  5230. s:=s+'var ';
  5231. vs_const :
  5232. s:=s+'const ';
  5233. vs_out :
  5234. s:=s+'out ';
  5235. vs_constref :
  5236. s:=s+'constref ';
  5237. else
  5238. ;
  5239. end;
  5240. if (pno_paranames in pno) then
  5241. s:=s+hp.realname+':';
  5242. if hp.univpara then
  5243. s:=s+'univ ';
  5244. if assigned(hp.vardef.typesym) then
  5245. begin
  5246. hs:=hp.vardef.typesym.realname;
  5247. if hs[1]<>'$' then
  5248. s:=s+hp.vardef.OwnerHierarchyName+hs
  5249. else
  5250. s:=s+hp.vardef.GetTypeName;
  5251. end
  5252. else
  5253. s:=s+hp.vardef.GetTypeName;
  5254. { default value }
  5255. if assigned(hp.defaultconstsym) then
  5256. begin
  5257. hpc:=tconstsym(hp.defaultconstsym);
  5258. hs:='';
  5259. case hpc.consttyp of
  5260. constwresourcestring,
  5261. constwstring:
  5262. begin
  5263. if pcompilerwidestring(hpc.value.valueptr)^.len>0 then
  5264. begin
  5265. setlength(hs,pcompilerwidestring(hpc.value.valueptr)^.len);
  5266. for j:=0 to pcompilerwidestring(hpc.value.valueptr)^.len-1 do
  5267. begin
  5268. if (ord(pcompilerwidestring(hpc.value.valueptr)^.data[j])<127) and
  5269. not(byte(pcompilerwidestring(hpc.value.valueptr)^.data[j]) in [0,10,13]) then
  5270. hs[j+1]:=char(pcompilerwidestring(hpc.value.valueptr)^.data[j])
  5271. else
  5272. hs[j+1]:='.';
  5273. end;
  5274. end;
  5275. end;
  5276. conststring,
  5277. constresourcestring :
  5278. begin
  5279. if hpc.value.len>0 then
  5280. begin
  5281. setLength(hs,hpc.value.len);
  5282. { don't write past the end of hs if the constant
  5283. is > 255 chars }
  5284. move(hpc.value.valueptr^,hs[1],length(hs));
  5285. { make sure that constant strings with newline chars
  5286. don't create a linebreak in the assembler code,
  5287. since comments are line-based. Also remove nulls
  5288. because the comments are written as a pchar. }
  5289. ReplaceCase(hs,#0,'.');
  5290. ReplaceCase(hs,#10,'.');
  5291. ReplaceCase(hs,#13,'.');
  5292. end;
  5293. end;
  5294. constreal :
  5295. str(pbestreal(hpc.value.valueptr)^,hs);
  5296. constpointer :
  5297. hs:=tostr(hpc.value.valueordptr);
  5298. constord :
  5299. begin
  5300. if is_boolean(hpc.constdef) then
  5301. begin
  5302. if hpc.value.valueord<>0 then
  5303. hs:='TRUE'
  5304. else
  5305. hs:='FALSE';
  5306. end
  5307. else
  5308. hs:=tostr(hpc.value.valueord);
  5309. end;
  5310. constnil :
  5311. hs:='nil';
  5312. constset :
  5313. hs:='<set>';
  5314. constguid:
  5315. hs:=guid2string(pguid(hpc.value.valueptr)^);
  5316. constnone:
  5317. internalerror(2019050704);
  5318. end;
  5319. if hs<>'' then
  5320. s:=s+'=`'+hs+'`';
  5321. end;
  5322. if vo_is_hidden_para in hp.varoptions then
  5323. s:=s+'>';
  5324. end;
  5325. end;
  5326. if not first then
  5327. s:=s+')';
  5328. if (po_varargs in procoptions) then
  5329. s:=s+';VarArgs';
  5330. typename_paras:=s;
  5331. end;
  5332. function tabstractprocdef.is_methodpointer:boolean;
  5333. begin
  5334. result:=false;
  5335. end;
  5336. function tabstractprocdef.is_addressonly:boolean;
  5337. begin
  5338. result:=true;
  5339. end;
  5340. function tabstractprocdef.no_self_node: boolean;
  5341. begin
  5342. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  5343. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  5344. end;
  5345. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef;
  5346. var
  5347. j, nestinglevel: longint;
  5348. pvs, npvs: tparavarsym;
  5349. begin
  5350. nestinglevel:=parast.symtablelevel;
  5351. if newtyp=procdef then
  5352. begin
  5353. if (copytyp<>pc_bareproc) then
  5354. result:=cprocdef.create(nestinglevel,doregister)
  5355. else
  5356. result:=cprocdef.create(normal_function_level,doregister);
  5357. tprocdef(result).visibility:=vis_public;
  5358. end
  5359. else
  5360. begin
  5361. result:=cprocvardef.create(nestinglevel,true);
  5362. end;
  5363. tabstractprocdef(result).returndef:=returndef;
  5364. tabstractprocdef(result).returndefderef:=returndefderef;
  5365. pvs:=nil;
  5366. npvs:=nil;
  5367. if copytyp<>pc_normal_no_paras then
  5368. begin
  5369. for j:=0 to parast.symlist.count-1 do
  5370. begin
  5371. case tsym(parast.symlist[j]).typ of
  5372. paravarsym:
  5373. begin
  5374. pvs:=tparavarsym(parast.symlist[j]);
  5375. { in case of bare proc, don't copy self, vmt or framepointer
  5376. parameters }
  5377. if (copytyp in [pc_bareproc,pc_normal_no_hidden]) and
  5378. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret,vo_is_high_para]*pvs.varoptions)<>[]) then
  5379. continue;
  5380. if paraprefix='' then
  5381. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  5382. pvs.vardef,pvs.varoptions)
  5383. else if not(vo_is_high_para in pvs.varoptions) then
  5384. npvs:=cparavarsym.create(paraprefix+pvs.realname,pvs.paranr,pvs.varspez,
  5385. pvs.vardef,pvs.varoptions)
  5386. else
  5387. npvs:=cparavarsym.create('$high'+paraprefix+copy(pvs.name,5,length(pvs.name)),pvs.paranr,pvs.varspez,
  5388. pvs.vardef,pvs.varoptions);
  5389. npvs.defaultconstsym:=pvs.defaultconstsym;
  5390. tabstractprocdef(result).parast.insertsym(npvs);
  5391. end;
  5392. constsym:
  5393. begin
  5394. // ignore, reuse original constym. Should also be duplicated
  5395. // be safe though
  5396. end;
  5397. symconst.typesym:
  5398. begin
  5399. // reuse original, part of generic declaration
  5400. end
  5401. else
  5402. internalerror(201160604);
  5403. end;
  5404. end;
  5405. end;
  5406. tabstractprocdef(result).savesize:=savesize;
  5407. if (typ<>procvardef) and
  5408. (newtyp=procvardef) then
  5409. begin
  5410. { procvars can't be (class)constructures/destructors etc }
  5411. if proctypeoption=potype_constructor then
  5412. begin
  5413. tabstractprocdef(result).returndef:=tdef(owner.defowner);
  5414. if not(is_implicit_pointer_object_type(returndef) or
  5415. (returndef.typ<>objectdef)) then
  5416. tabstractprocdef(result).returndef:=cpointerdef.getreusable(tabstractprocdef(result).returndef);
  5417. tabstractprocdef(result).proctypeoption:=potype_function;
  5418. end
  5419. else if is_void(returndef) then
  5420. tabstractprocdef(result).proctypeoption:=potype_procedure
  5421. else
  5422. tabstractprocdef(result).proctypeoption:=potype_function;
  5423. end
  5424. else
  5425. tabstractprocdef(result).proctypeoption:=proctypeoption;
  5426. tabstractprocdef(result).proccalloption:=proccalloption;
  5427. tabstractprocdef(result).procoptions:=procoptions;
  5428. if (copytyp=pc_bareproc) then
  5429. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  5430. if newtyp=procvardef then
  5431. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  5432. if copytyp=pc_address_only then
  5433. include(tabstractprocdef(result).procoptions,po_addressonly);
  5434. tabstractprocdef(result).callerargareasize:=callerargareasize;
  5435. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  5436. tabstractprocdef(result).maxparacount:=maxparacount;
  5437. tabstractprocdef(result).minparacount:=minparacount;
  5438. if po_explicitparaloc in procoptions then
  5439. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  5440. { recalculate parameter info }
  5441. tabstractprocdef(result).has_paraloc_info:=callnoside;
  5442. {$ifdef m68k}
  5443. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  5444. {$endif}
  5445. if (typ=procdef) and
  5446. (newtyp=procvardef) and
  5447. (owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  5448. include(tprocvardef(result).procoptions,po_methodpointer);
  5449. end;
  5450. function tabstractprocdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  5451. begin
  5452. result:=is_addressonly;
  5453. end;
  5454. procedure tabstractprocdef.check_mark_as_nested;
  5455. begin
  5456. { nested procvars require that nested functions use the Delphi-style
  5457. nested procedure calling convention }
  5458. if (parast.symtablelevel>normal_function_level) and
  5459. (m_nested_procvars in current_settings.modeswitches) then
  5460. include(procoptions,po_delphi_nested_cc);
  5461. end;
  5462. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  5463. begin
  5464. if (side in [callerside,callbothsides]) and
  5465. not(has_paraloc_info in [callerside,callbothsides]) then
  5466. begin
  5467. if not is_c_variadic(self) then
  5468. callerargareasize:=paramanager.create_paraloc_info(self,callerside)
  5469. else
  5470. callerargareasize:=paramanager.create_varargs_paraloc_info(self,callerside,nil);
  5471. if has_paraloc_info in [calleeside,callbothsides] then
  5472. has_paraloc_info:=callbothsides
  5473. else
  5474. has_paraloc_info:=callerside;
  5475. end;
  5476. if (side in [calleeside,callbothsides]) and
  5477. not(has_paraloc_info in [calleeside,callbothsides]) then
  5478. begin
  5479. if not is_c_variadic(self) then
  5480. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside)
  5481. else
  5482. callerargareasize:=paramanager.create_varargs_paraloc_info(self,calleeside,nil);
  5483. if has_paraloc_info in [callerside,callbothsides] then
  5484. has_paraloc_info:=callbothsides
  5485. else
  5486. has_paraloc_info:=calleeside;
  5487. end;
  5488. end;
  5489. procedure tabstractprocdef.done_paraloc_info(side: tcallercallee);
  5490. var
  5491. i: longint;
  5492. begin
  5493. if (side in [callerside,callbothsides]) and
  5494. (has_paraloc_info in [callerside,callbothsides]) then
  5495. begin
  5496. funcretloc[callerside].done;
  5497. for i:=0 to paras.count-1 do
  5498. tparavarsym(paras[i]).paraloc[callerside].done;
  5499. if has_paraloc_info=callerside then
  5500. has_paraloc_info:=callnoside
  5501. else
  5502. has_paraloc_info:=calleeside;
  5503. end;
  5504. if (side in [calleeside,callbothsides]) and
  5505. (has_paraloc_info in [calleeside,callbothsides]) then
  5506. begin
  5507. funcretloc[calleeside].done;
  5508. for i:=0 to paras.count-1 do
  5509. tparavarsym(paras[i]).paraloc[calleeside].done;
  5510. if has_paraloc_info=calleeside then
  5511. has_paraloc_info:=callnoside
  5512. else
  5513. has_paraloc_info:=callerside;
  5514. end;
  5515. end;
  5516. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  5517. var
  5518. p: tparavarsym;
  5519. ploc: PCGParalocation;
  5520. i: longint;
  5521. begin
  5522. result:=false;
  5523. init_paraloc_info(side);
  5524. for i:=0 to parast.SymList.Count-1 do
  5525. if tsym(parast.SymList[i]).typ=paravarsym then
  5526. begin
  5527. p:=tparavarsym(parast.SymList[i]);
  5528. if not p.is_used then
  5529. continue;
  5530. { check if no parameter is located on the stack }
  5531. if (is_open_array(p.vardef) or
  5532. is_array_of_const(p.vardef)) and (p.varspez=vs_value) then
  5533. begin
  5534. result:=true;
  5535. exit;
  5536. end;
  5537. ploc:=p.paraloc[side].location;
  5538. while assigned(ploc) do
  5539. begin
  5540. if (ploc^.loc=LOC_REFERENCE) then
  5541. begin
  5542. result:=true;
  5543. exit
  5544. end;
  5545. ploc:=ploc^.next;
  5546. end;
  5547. end;
  5548. end;
  5549. function tabstractprocdef.is_pushleftright: boolean;
  5550. begin
  5551. result:=false;
  5552. end;
  5553. function tabstractprocdef.address_type: tdef;
  5554. begin
  5555. result:=voidcodepointertype;
  5556. end;
  5557. function tabstractprocdef.ofs_address_type:tdef;
  5558. begin
  5559. result:=address_type;
  5560. end;
  5561. procedure tabstractprocdef.declared_far;
  5562. begin
  5563. Message1(parser_w_proc_directive_ignored,'FAR');
  5564. end;
  5565. procedure tabstractprocdef.declared_near;
  5566. begin
  5567. Message1(parser_w_proc_directive_ignored,'NEAR');
  5568. end;
  5569. function tabstractprocdef.generate_safecall_wrapper: boolean;
  5570. begin
  5571. {$ifdef SUPPORT_SAFECALL}
  5572. result:=
  5573. (proccalloption=pocall_safecall) and
  5574. not(po_assembler in procoptions) and
  5575. (tf_safecall_exceptions in target_info.flags);
  5576. {$else SUPPORT_SAFECALL}
  5577. result:=false;
  5578. {$endif}
  5579. end;
  5580. function tabstractprocdef.is_generic_param(def:tdef): boolean;
  5581. begin
  5582. if def.typ=undefineddef then
  5583. result:=def.owner=self.parast
  5584. else if def.typ=objectdef then
  5585. result:=(def.owner=self.parast) and (tstoreddef(def).genconstraintdata<>nil)
  5586. else
  5587. result:=false;
  5588. end;
  5589. function tabstractprocdef.wpo_may_create_instance(optionalmethodpointer: tnode): boolean;
  5590. begin
  5591. result:=
  5592. (proctypeoption=potype_constructor) or
  5593. ((typ=procdef) and
  5594. ((not assigned(optionalmethodpointer) and
  5595. is_class(tdef(owner.defowner))) or
  5596. (assigned(optionalmethodpointer) and
  5597. ((optionalmethodpointer.resultdef.typ=classrefdef) or
  5598. (optionalmethodpointer.nodetype=typen)))) and
  5599. (tprocdef(self).procsym.Name='NEWINSTANCE'))
  5600. end;
  5601. {***************************************************************************
  5602. TPROCDEF
  5603. ***************************************************************************}
  5604. function tprocdef.getparentfpsym: tsym;
  5605. begin
  5606. if not assigned(_parentfpsym) then
  5607. begin
  5608. _parentfpsym:=tsym(parast.Find('parentfp'));
  5609. if not assigned(_parentfpsym) then
  5610. internalerror(200309281);
  5611. end;
  5612. result:=_parentfpsym;
  5613. end;
  5614. function tprocdef.Getis_implemented: boolean;
  5615. begin
  5616. result:=not assigned(implprocdefinfo) or implprocdefinfo^.is_implemented;
  5617. end;
  5618. procedure tprocdef.Setis_implemented(AValue: boolean);
  5619. begin
  5620. if not assigned(implprocdefinfo) then
  5621. internalerror(2020062101);
  5622. implprocdefinfo^.is_implemented:=AValue;
  5623. end;
  5624. function tprocdef.getwas_anonymous:boolean;
  5625. begin
  5626. result:=assigned(implprocdefinfo) and implprocdefinfo^.was_anonymous;
  5627. end;
  5628. procedure tprocdef.setwas_anonymous(avalue:boolean);
  5629. begin
  5630. if not assigned(implprocdefinfo) then
  5631. internalerror(2022020502);
  5632. implprocdefinfo^.was_anonymous:=avalue;
  5633. end;
  5634. function tprocdef.gethas_capturer:boolean;
  5635. begin
  5636. result:=assigned(implprocdefinfo) and implprocdefinfo^.has_capturer;
  5637. end;
  5638. procedure tprocdef.sethas_capturer(avalue:boolean);
  5639. begin
  5640. if not assigned(implprocdefinfo) then
  5641. internalerror(2022020503);
  5642. implprocdefinfo^.has_capturer:=avalue;
  5643. end;
  5644. function tprocdef.Getcapturedsyms:tfplist;
  5645. begin
  5646. if not assigned(implprocdefinfo) then
  5647. result:=nil
  5648. else
  5649. result:=implprocdefinfo^.capturedsyms;
  5650. end;
  5651. function tprocdef.store_localst: boolean;
  5652. begin
  5653. result:=has_inlininginfo or (df_generic in defoptions);
  5654. end;
  5655. function tprocdef.GetResultName: PShortString;
  5656. begin
  5657. if not assigned(implprocdefinfo) then
  5658. internalerror(2014010301);
  5659. result:=implprocdefinfo^.resultname;
  5660. end;
  5661. procedure tprocdef.SetResultName(AValue: PShortString);
  5662. begin
  5663. if not assigned(implprocdefinfo) then
  5664. internalerror(2014010302);
  5665. implprocdefinfo^.resultname:=AValue;
  5666. end;
  5667. function tprocdef.GetParentFPInitBlock: tnode;
  5668. begin
  5669. if not assigned(implprocdefinfo) then
  5670. internalerror(2014010303);
  5671. result:=implprocdefinfo^.parentfpinitblock;
  5672. end;
  5673. function tprocdef.GetParentFPStruct: tsym;
  5674. begin
  5675. if not assigned(implprocdefinfo) then
  5676. internalerror(2014010304);
  5677. result:=implprocdefinfo^.parentfpstruct;
  5678. end;
  5679. function tprocdef.GetParentFPStructPtrType: tdef;
  5680. begin
  5681. if not assigned(implprocdefinfo) then
  5682. internalerror(2014010305);
  5683. result:=implprocdefinfo^.parentfpstructptrtype;
  5684. end;
  5685. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  5686. begin
  5687. if not assigned(implprocdefinfo) then
  5688. internalerror(2014010306);
  5689. implprocdefinfo^.parentfpinitblock:=AValue;
  5690. end;
  5691. function tprocdef.Getprocendtai: tai;
  5692. begin
  5693. if not assigned(implprocdefinfo) then
  5694. internalerror(2014010307);
  5695. result:=implprocdefinfo^.procendtai;
  5696. end;
  5697. function tprocdef.Getprocstarttai: tai;
  5698. begin
  5699. if not assigned(implprocdefinfo) then
  5700. internalerror(2014010308);
  5701. result:=implprocdefinfo^.procstarttai;
  5702. end;
  5703. procedure tprocdef.Setprocendtai(AValue: tai);
  5704. begin
  5705. if not assigned(implprocdefinfo) then
  5706. internalerror(2014010309);
  5707. implprocdefinfo^.procendtai:=AValue;
  5708. end;
  5709. function tprocdef.Getskpara: pointer;
  5710. begin
  5711. if not assigned(implprocdefinfo) then
  5712. internalerror(2014010310);
  5713. result:=implprocdefinfo^.skpara;
  5714. end;
  5715. procedure tprocdef.Setskpara(AValue: pointer);
  5716. begin
  5717. if not assigned(implprocdefinfo) then
  5718. internalerror(2014010311);
  5719. implprocdefinfo^.skpara:=AValue;
  5720. end;
  5721. function tprocdef.Getpersonality: tprocdef;
  5722. begin
  5723. if not assigned(implprocdefinfo) then
  5724. internalerror(2016121704);
  5725. result:=implprocdefinfo^.personality;
  5726. end;
  5727. procedure tprocdef.Setpersonality(AValue: tprocdef);
  5728. begin
  5729. if not assigned(implprocdefinfo) then
  5730. internalerror(2016121705);
  5731. implprocdefinfo^.personality:=AValue;
  5732. end;
  5733. function tprocdef.Getforwarddef: boolean;
  5734. begin
  5735. if not assigned(implprocdefinfo) then
  5736. internalerror(2014010312);
  5737. result:=implprocdefinfo^.forwarddef;
  5738. end;
  5739. function tprocdef.Gethasforward: boolean;
  5740. begin
  5741. if not assigned(implprocdefinfo) then
  5742. internalerror(2014010313);
  5743. result:=implprocdefinfo^.hasforward;
  5744. end;
  5745. function tprocdef.Getinterfacedef: boolean;
  5746. begin
  5747. if not assigned(implprocdefinfo) then
  5748. internalerror(2014010314);
  5749. result:=implprocdefinfo^.interfacedef;
  5750. end;
  5751. procedure tprocdef.Setforwarddef(AValue: boolean);
  5752. begin
  5753. if not assigned(implprocdefinfo) then
  5754. internalerror(2014010315);
  5755. implprocdefinfo^.forwarddef:=AValue;
  5756. end;
  5757. procedure tprocdef.Sethasforward(AValue: boolean);
  5758. begin
  5759. if not assigned(implprocdefinfo) then
  5760. internalerror(2014010316);
  5761. implprocdefinfo^.hasforward:=AValue;
  5762. end;
  5763. function tprocdef.GetIsEmpty: boolean;
  5764. begin
  5765. result:=pio_empty in implprocoptions;
  5766. end;
  5767. procedure tprocdef.SetIsEmpty(AValue: boolean);
  5768. begin
  5769. if AValue then
  5770. include(implprocoptions,pio_empty)
  5771. else
  5772. exclude(implprocoptions,pio_empty);
  5773. end;
  5774. function tprocdef.GetHasInliningInfo: boolean;
  5775. begin
  5776. result:=pio_has_inlininginfo in implprocoptions;
  5777. end;
  5778. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  5779. begin
  5780. if AValue then
  5781. include(implprocoptions,pio_has_inlininginfo)
  5782. else
  5783. exclude(implprocoptions,pio_has_inlininginfo);
  5784. end;
  5785. procedure tprocdef.Setinterfacedef(AValue: boolean);
  5786. begin
  5787. if not assigned(implprocdefinfo) then
  5788. internalerror(2014010317);
  5789. implprocdefinfo^.interfacedef:=AValue;
  5790. end;
  5791. procedure tprocdef.Setprocstarttai(AValue: tai);
  5792. begin
  5793. if not assigned(implprocdefinfo) then
  5794. internalerror(2014010318);
  5795. implprocdefinfo^.procstarttai:=AValue;
  5796. end;
  5797. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  5798. begin
  5799. if not assigned(implprocdefinfo) then
  5800. internalerror(2014010319);
  5801. implprocdefinfo^.parentfpstruct:=AValue;
  5802. end;
  5803. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  5804. begin
  5805. if not assigned(implprocdefinfo) then
  5806. internalerror(2014010320);
  5807. implprocdefinfo^.parentfpstructptrtype:=AValue;
  5808. end;
  5809. constructor tprocdef.create(level:byte;doregister:boolean);
  5810. begin
  5811. inherited create(procdef,level,doregister);
  5812. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  5813. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  5814. {$ifdef symansistr}
  5815. _mangledname:='';
  5816. {$else symansistr}
  5817. _mangledname:=nil;
  5818. {$endif symansistr}
  5819. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  5820. seenmarker := nil;
  5821. {$endif}
  5822. fileinfo:=current_filepos;
  5823. extnumber:=$ffff;
  5824. aliasnames:=TCmdStrList.create;
  5825. funcretsym:=nil;
  5826. funcretsymderef.reset;
  5827. procsymderef.reset;
  5828. forwarddef:=true;
  5829. interfacedef:=false;
  5830. hasforward:=false;
  5831. struct := nil;
  5832. structderef.reset;
  5833. import_dll:=nil;
  5834. import_name:=nil;
  5835. import_nr:=0;
  5836. inlininginfo:=nil;
  5837. deprecatedmsg:=nil;
  5838. genericdecltokenbuf:=nil;
  5839. if cs_opt_fastmath in current_settings.optimizerswitches then
  5840. include(implprocoptions, pio_fastmath);
  5841. end;
  5842. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  5843. var
  5844. i,aliasnamescount,sizeleft : longint;
  5845. level : byte;
  5846. buf : array[0..255] of byte;
  5847. begin
  5848. inherited ppuload(procdef,ppufile);
  5849. {$ifdef symansistr}
  5850. if po_has_mangledname in procoptions then
  5851. _mangledname:=ppufile.getansistring
  5852. else
  5853. _mangledname:='';
  5854. {$else symansistr}
  5855. if po_has_mangledname in procoptions then
  5856. _mangledname:=ppufile.getpshortstring
  5857. else
  5858. _mangledname:=nil;
  5859. {$endif symansistr}
  5860. extnumber:=ppufile.getword;
  5861. level:=ppufile.getbyte;
  5862. ppufile.getderef(structderef);
  5863. ppufile.getderef(procsymderef);
  5864. ppufile.getposinfo(fileinfo);
  5865. visibility:=tvisibility(ppufile.getbyte);
  5866. ppufile.getset(tppuset2(symoptions));
  5867. if sp_has_deprecated_msg in symoptions then
  5868. deprecatedmsg:=ppufile.getpshortstring
  5869. else
  5870. deprecatedmsg:=nil;
  5871. { import stuff }
  5872. if po_has_importdll in procoptions then
  5873. import_dll:=ppufile.getpshortstring
  5874. else
  5875. import_dll:=nil;
  5876. if po_has_importname in procoptions then
  5877. import_name:=ppufile.getpshortstring
  5878. else
  5879. import_name:=nil;
  5880. import_nr:=ppufile.getword;
  5881. if (po_msgint in procoptions) then
  5882. messageinf.i:=ppufile.getlongint;
  5883. if (po_msgstr in procoptions) then
  5884. messageinf.str:=ppufile.getpshortstring;
  5885. if (po_dispid in procoptions) then
  5886. dispid:=ppufile.getlongint;
  5887. { inline stuff }
  5888. ppufile.getset(tppuset1(implprocoptions));
  5889. if has_inlininginfo then
  5890. begin
  5891. ppufile.getderef(funcretsymderef);
  5892. new(inlininginfo);
  5893. ppufile.getset(tppuset4(inlininginfo^.flags));
  5894. end
  5895. else
  5896. begin
  5897. inlininginfo:=nil;
  5898. funcretsym:=nil;
  5899. end;
  5900. aliasnames:=TCmdStrList.create;
  5901. { count alias names }
  5902. aliasnamescount:=ppufile.getbyte;
  5903. for i:=1 to aliasnamescount do
  5904. aliasnames.insert(ppufile.getstring);
  5905. { load the token stream containing the declaration }
  5906. sizeleft:=ppufile.getlongint;
  5907. if sizeleft>0 then
  5908. begin
  5909. init_genericdecl;
  5910. while sizeleft>0 do
  5911. begin
  5912. if sizeleft>sizeof(buf) then
  5913. i:=sizeof(buf)
  5914. else
  5915. i:=sizeleft;
  5916. ppufile.getdata(buf,i);
  5917. genericdecltokenbuf.write(buf,i);
  5918. dec(sizeleft,i);
  5919. end;
  5920. end;
  5921. ppuload_platform(ppufile);
  5922. { load para symtable }
  5923. parast:=tparasymtable.create(self,level);
  5924. tparasymtable(parast).ppuload(ppufile);
  5925. { load local symtable }
  5926. if store_localst then
  5927. begin
  5928. localst:=tlocalsymtable.create(self,level);
  5929. tlocalsymtable(localst).ppuload(ppufile);
  5930. end
  5931. else
  5932. localst:=nil;
  5933. { inline stuff }
  5934. if has_inlininginfo then
  5935. inlininginfo^.code:=ppuloadnodetree(ppufile);
  5936. { default values for no persistent data }
  5937. if (cs_link_deffile in current_settings.globalswitches) and
  5938. (tf_need_export in target_info.flags) and
  5939. (po_exports in procoptions) then
  5940. deffile.AddExport(mangledname);
  5941. { Disable po_has_inlining until the derefimpl is done }
  5942. has_inlininginfo:=false;
  5943. end;
  5944. destructor tprocdef.destroy;
  5945. begin
  5946. aliasnames.free;
  5947. aliasnames:=nil;
  5948. if assigned(localst) and
  5949. (localst.symtabletype<>staticsymtable) then
  5950. begin
  5951. {$ifdef MEMDEBUG}
  5952. memproclocalst.start;
  5953. {$endif MEMDEBUG}
  5954. localst.free;
  5955. localst:=nil;
  5956. {$ifdef MEMDEBUG}
  5957. memproclocalst.start;
  5958. {$endif MEMDEBUG}
  5959. end;
  5960. if assigned(inlininginfo) then
  5961. begin
  5962. {$ifdef MEMDEBUG}
  5963. memprocnodetree.start;
  5964. {$endif MEMDEBUG}
  5965. tnode(inlininginfo^.code).free;
  5966. {$ifdef MEMDEBUG}
  5967. memprocnodetree.start;
  5968. {$endif MEMDEBUG}
  5969. dispose(inlininginfo);
  5970. inlininginfo:=nil;
  5971. end;
  5972. freeimplprocdefinfo;
  5973. genericdecltokenbuf.free;
  5974. genericdecltokenbuf:=nil;
  5975. stringdispose(import_dll);
  5976. stringdispose(import_name);
  5977. stringdispose(deprecatedmsg);
  5978. if (po_msgstr in procoptions) then
  5979. stringdispose(messageinf.str);
  5980. {$ifndef symansistr}
  5981. if assigned(_mangledname) then
  5982. begin
  5983. {$ifdef MEMDEBUG}
  5984. memmanglednames.start;
  5985. {$endif MEMDEBUG}
  5986. stringdispose(_mangledname);
  5987. {$ifdef MEMDEBUG}
  5988. memmanglednames.stop;
  5989. {$endif MEMDEBUG}
  5990. end;
  5991. {$endif symansistr}
  5992. inherited destroy;
  5993. end;
  5994. procedure tprocdef.freeimplprocdefinfo;
  5995. begin
  5996. if assigned(implprocdefinfo) then
  5997. begin
  5998. stringdispose(implprocdefinfo^.resultname);
  5999. TFPList.FreeAndNilDisposing(implprocdefinfo^.capturedsyms,typeinfo(tcapturedsyminfo));
  6000. freemem(implprocdefinfo);
  6001. implprocdefinfo:=nil;
  6002. end;
  6003. end;
  6004. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  6005. var
  6006. oldintfcrc : boolean;
  6007. aliasnamescount,i,sizeleft : longint;
  6008. item : TCmdStrListItem;
  6009. buf : array[0..255] of byte;
  6010. begin
  6011. { released procdef? }
  6012. if not assigned(parast) then
  6013. exit;
  6014. inherited ppuwrite(ppufile);
  6015. {$ifdef symansistr}
  6016. if po_has_mangledname in procoptions then
  6017. ppufile.putansistring(_mangledname);
  6018. {$else symansistr}
  6019. if po_has_mangledname in procoptions then
  6020. ppufile.putstring(_mangledname^);
  6021. {$endif symansistr}
  6022. ppufile.putword(extnumber);
  6023. ppufile.putbyte(parast.symtablelevel);
  6024. ppufile.putderef(structderef);
  6025. ppufile.putderef(procsymderef);
  6026. ppufile.putposinfo(fileinfo);
  6027. ppufile.putbyte(byte(visibility));
  6028. ppufile.putset(tppuset2(symoptions));
  6029. if sp_has_deprecated_msg in symoptions then
  6030. ppufile.putstring(deprecatedmsg^);
  6031. { import }
  6032. if po_has_importdll in procoptions then
  6033. ppufile.putstring(import_dll^);
  6034. if po_has_importname in procoptions then
  6035. ppufile.putstring(import_name^);
  6036. ppufile.putword(import_nr);
  6037. if (po_msgint in procoptions) then
  6038. ppufile.putlongint(messageinf.i);
  6039. if (po_msgstr in procoptions) then
  6040. ppufile.putstring(messageinf.str^);
  6041. if (po_dispid in procoptions) then
  6042. ppufile.putlongint(dispid);
  6043. { inline stuff }
  6044. oldintfcrc:=ppufile.do_crc;
  6045. ppufile.do_crc:=false;
  6046. ppufile.putset(tppuset1(implprocoptions));
  6047. if has_inlininginfo then
  6048. begin
  6049. ppufile.putderef(funcretsymderef);
  6050. ppufile.putset(tppuset4(inlininginfo^.flags));
  6051. end;
  6052. { count alias names }
  6053. aliasnamescount:=0;
  6054. item:=TCmdStrListItem(aliasnames.first);
  6055. while assigned(item) do
  6056. begin
  6057. inc(aliasnamescount);
  6058. item:=TCmdStrListItem(item.next);
  6059. end;
  6060. if aliasnamescount>255 then
  6061. internalerror(200711021);
  6062. ppufile.putbyte(aliasnamescount);
  6063. item:=TCmdStrListItem(aliasnames.first);
  6064. while assigned(item) do
  6065. begin
  6066. ppufile.putstring(item.str);
  6067. item:=TCmdStrListItem(item.next);
  6068. end;
  6069. ppufile.do_crc:=oldintfcrc;
  6070. { generic tokens for the declaration }
  6071. if assigned(genericdecltokenbuf) and (genericdecltokenbuf.size>0) then
  6072. begin
  6073. sizeleft:=genericdecltokenbuf.size;
  6074. genericdecltokenbuf.seek(0);
  6075. ppufile.putlongint(sizeleft);
  6076. while sizeleft>0 do
  6077. begin
  6078. if sizeleft>sizeof(buf) then
  6079. i:=sizeof(buf)
  6080. else
  6081. i:=sizeleft;
  6082. genericdecltokenbuf.read(buf,i);
  6083. ppufile.putdata(buf,i);
  6084. dec(sizeleft,i);
  6085. end;
  6086. end
  6087. else
  6088. ppufile.putlongint(0);
  6089. { write this entry }
  6090. writeentry(ppufile,ibprocdef);
  6091. { Save the para symtable, this is taken from the interface }
  6092. tparasymtable(parast).ppuwrite(ppufile);
  6093. { save localsymtable for inline procedures or when local
  6094. browser info is requested, this has no influence on the crc }
  6095. if store_localst and not ppufile.crc_only then
  6096. begin
  6097. oldintfcrc:=ppufile.do_crc;
  6098. ppufile.do_crc:=false;
  6099. tlocalsymtable(localst).ppuwrite(ppufile);
  6100. ppufile.do_crc:=oldintfcrc;
  6101. end;
  6102. { node tree for inlining }
  6103. oldintfcrc:=ppufile.do_crc;
  6104. ppufile.do_crc:=false;
  6105. if has_inlininginfo then
  6106. ppuwritenodetree(ppufile,inlininginfo^.code);
  6107. ppufile.do_crc:=oldintfcrc;
  6108. end;
  6109. function tprocdef.fullprocname(showhidden:boolean):string;
  6110. var
  6111. pno: tprocnameoptions;
  6112. begin
  6113. pno:=[];
  6114. if showhidden then
  6115. include(pno,pno_showhidden);
  6116. result:=customprocname(pno);
  6117. end;
  6118. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  6119. var
  6120. s, hs, rn : ansistring;
  6121. t : ttoken;
  6122. module : tmodule;
  6123. syssym : tsyssym;
  6124. begin
  6125. {$ifdef EXTDEBUG}
  6126. include(pno,pno_showhidden);
  6127. {$endif EXTDEBUG}
  6128. s:='';
  6129. rn:='';
  6130. if proctypeoption=potype_operator then
  6131. begin
  6132. for t:=NOTOKEN to last_overloaded do
  6133. if procsym.realname='$'+overloaded_names[t] then
  6134. begin
  6135. s:='operator ';
  6136. if (pno_ownername in pno) and
  6137. assigned(struct) then
  6138. s:=s+struct.RttiName+'.';
  6139. s:=s+arraytokeninfo[t].str;
  6140. if not (pno_noparams in pno) then
  6141. s:=s+typename_paras(pno);
  6142. break;
  6143. end;
  6144. end
  6145. else
  6146. begin
  6147. if (po_classmethod in procoptions) and
  6148. not(pno_noclassmarker in pno) then
  6149. s:='class ';
  6150. case proctypeoption of
  6151. potype_constructor,
  6152. potype_class_constructor:
  6153. s:=s+'constructor ';
  6154. potype_class_destructor,
  6155. potype_destructor:
  6156. s:=s+'destructor ';
  6157. else
  6158. if (pno_proctypeoption in pno) and not (po_anonymous in procoptions) then
  6159. begin
  6160. if assigned(returndef) and
  6161. not(is_void(returndef)) then
  6162. s:=s+'function '
  6163. else
  6164. s:=s+'procedure ';
  6165. end;
  6166. end;
  6167. if (pno_ownername in pno) and
  6168. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  6169. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  6170. if (po_compilerproc in procoptions) and (extnumber<>$ffff) then
  6171. begin
  6172. syssym:=tsyssym.find_by_number(extnumber);
  6173. if not assigned(syssym) then
  6174. internalerror(2016060305);
  6175. rn:=syssym.realname;
  6176. end
  6177. else if po_anonymous in procoptions then
  6178. begin
  6179. s:=s+'anonymous ';
  6180. if assigned(returndef) and
  6181. not(is_void(returndef)) then
  6182. s:=s+'function'
  6183. else
  6184. s:=s+'procedure';
  6185. end
  6186. else if pno_prettynames in pno then
  6187. rn:=procsym.prettyname
  6188. else
  6189. rn:=procsym.RealName;
  6190. if (pno_noleadingdollar in pno) and
  6191. (rn[1]='$') then
  6192. delete(rn,1,1);
  6193. s:=s+rn;
  6194. if not (pno_noparams in pno) then
  6195. s:=s+typename_paras(pno);
  6196. end;
  6197. if not(proctypeoption in [potype_constructor,potype_destructor,
  6198. potype_class_constructor,potype_class_destructor]) and
  6199. assigned(returndef) and
  6200. not(is_void(returndef)) then
  6201. begin
  6202. if assigned(returndef.typesym) then
  6203. begin
  6204. module:=find_module_from_symtable(returndef.typesym.owner);
  6205. if module <> current_module then
  6206. s:=s+':'+module.realmodulename^+'.'
  6207. else
  6208. s:=s+':';
  6209. if pno_prettynames in pno then
  6210. hs:=returndef.typesym.prettyname
  6211. else
  6212. hs:=returndef.typesym.realname;
  6213. if hs[1]<>'$' then
  6214. s:=s+returndef.OwnerHierarchyName+hs
  6215. else
  6216. s:=s+returndef.GetTypeName;
  6217. end
  6218. else
  6219. s:=s+':'+returndef.GetTypeName;
  6220. end;
  6221. if not (po_anonymous in procoptions) then
  6222. if assigned(owner) and (owner.symtabletype=localsymtable) then
  6223. s:=s+' is nested'
  6224. else if po_is_block in procoptions then
  6225. s:=s+' is block';
  6226. s:=s+';';
  6227. if po_far in procoptions then
  6228. s:=s+' far;';
  6229. { forced calling convention? }
  6230. if (po_hascallingconvention in procoptions) then
  6231. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  6232. if (po_staticmethod in procoptions) and
  6233. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  6234. s:=s+' Static;';
  6235. if pno_mangledname in pno then
  6236. s:=s+' -- mangled name: '+mangledname;
  6237. customprocname:=s;
  6238. end;
  6239. function tprocdef.is_methodpointer:boolean;
  6240. begin
  6241. { don't check assigned(_class), that's also the case for nested
  6242. procedures inside methods }
  6243. result:=(owner.symtabletype in [recordsymtable,ObjectSymtable]) and not no_self_node;
  6244. end;
  6245. function tprocdef.is_addressonly:boolean;
  6246. begin
  6247. result:=assigned(owner) and
  6248. not is_methodpointer and
  6249. (not(m_nested_procvars in current_settings.modeswitches) or
  6250. not is_nested_pd(self)) and
  6251. (
  6252. not (po_anonymous in procoptions) or
  6253. not assigned(capturedsyms) or
  6254. (capturedsyms.count=0)
  6255. );
  6256. end;
  6257. procedure tprocdef.make_external;
  6258. begin
  6259. include(procoptions,po_external);
  6260. forwarddef:=false;
  6261. end;
  6262. procedure tprocdef.init_genericdecl;
  6263. begin
  6264. if assigned(genericdecltokenbuf) then
  6265. internalerror(2015061901);
  6266. genericdecltokenbuf:=tdynamicarray.create(256);
  6267. end;
  6268. function tprocdef.get_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean;
  6269. begin
  6270. result:=false;
  6271. if proctypeoption=potype_constructor then
  6272. begin
  6273. result:=true;
  6274. ressym:=tsym(parast.Find('self'));
  6275. resdef:=tabstractnormalvarsym(ressym).vardef;
  6276. { and TP-style constructors return a pointer to self }
  6277. if is_object(resdef) then
  6278. resdef:=cpointerdef.getreusable(resdef);
  6279. end
  6280. else if (proccalloption=pocall_safecall) and
  6281. (tf_safecall_exceptions in target_info.flags) then
  6282. begin
  6283. result:=true;
  6284. ressym:=tsym(localst.Find('safecallresult'));
  6285. resdef:=tabstractnormalvarsym(ressym).vardef;
  6286. end
  6287. else if not is_void(returndef) then
  6288. begin
  6289. result:=true;
  6290. ressym:=funcretsym;
  6291. resdef:=tabstractnormalvarsym(ressym).vardef;
  6292. end;
  6293. end;
  6294. function tprocdef.get_safecall_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean;
  6295. begin
  6296. result:=false;
  6297. if (proctypeoption<>potype_constructor) and
  6298. (proccalloption=pocall_safecall) and
  6299. (tf_safecall_exceptions in target_info.flags) then
  6300. begin
  6301. result:=true;
  6302. ressym:=tsym(localst.Find('safecallresult'));
  6303. resdef:=tabstractnormalvarsym(ressym).vardef;
  6304. end
  6305. end;
  6306. procedure tprocdef.add_captured_sym(sym:tsym;def:tdef;const filepos:tfileposinfo);
  6307. var
  6308. i : longint;
  6309. capturedsym : pcapturedsyminfo;
  6310. begin
  6311. if not assigned(implprocdefinfo) then
  6312. internalerror(2021052601);
  6313. if not assigned(implprocdefinfo^.capturedsyms) then
  6314. implprocdefinfo^.capturedsyms:=tfplist.create;
  6315. for i:=0 to implprocdefinfo^.capturedsyms.count-1 do
  6316. begin
  6317. capturedsym:=pcapturedsyminfo(implprocdefinfo^.capturedsyms[i]);
  6318. if (capturedsym^.sym=sym) and (capturedsym^.def=def) then
  6319. exit;
  6320. end;
  6321. new(capturedsym);
  6322. capturedsym^.sym:=sym;
  6323. capturedsym^.def:=def;
  6324. capturedsym^.fileinfo:=filepos;
  6325. implprocdefinfo^.capturedsyms.add(capturedsym);
  6326. end;
  6327. function tprocdef.has_alias_name(const s: TSymStr): boolean;
  6328. var
  6329. item : TCmdStrListItem;
  6330. begin
  6331. result:=true;
  6332. if mangledname=s then
  6333. exit;
  6334. item:=TCmdStrListItem(aliasnames.first);
  6335. while assigned(item) do
  6336. begin
  6337. if item.str=s then
  6338. exit;
  6339. item:=TCmdStrListItem(item.next);
  6340. end;
  6341. result:=false;
  6342. end;
  6343. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  6344. begin
  6345. case t of
  6346. gs_local :
  6347. GetSymtable:=localst;
  6348. gs_para :
  6349. GetSymtable:=parast;
  6350. else
  6351. GetSymtable:=nil;
  6352. end;
  6353. end;
  6354. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef;
  6355. var
  6356. j : longint;
  6357. begin
  6358. result:=inherited;
  6359. if newtyp=procvardef then
  6360. begin
  6361. { create new paralist }
  6362. tprocvardef(result).calcparas;
  6363. exit;
  6364. end;
  6365. { don't copy mangled name, can be different }
  6366. tprocdef(result).messageinf:=messageinf;
  6367. tprocdef(result).dispid:=dispid;
  6368. if po_msgstr in procoptions then
  6369. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  6370. tprocdef(result).symoptions:=symoptions;
  6371. if assigned(deprecatedmsg) then
  6372. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  6373. { will have to be associated with appropriate procsym }
  6374. tprocdef(result).procsym:=nil;
  6375. { don't create aliases for bare copies, nor copy the funcretsym as
  6376. the function result parameter will be inserted again if necessary
  6377. (e.g. if the calling convention is changed) }
  6378. if not(copytyp in [pc_bareproc,pc_normal_no_hidden]) then
  6379. begin
  6380. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  6381. if assigned(funcretsym) then
  6382. begin
  6383. if funcretsym.owner=parast then
  6384. begin
  6385. j:=parast.symlist.indexof(funcretsym);
  6386. if j<0 then
  6387. internalerror(2011040606);
  6388. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  6389. end
  6390. else if funcretsym.owner=localst then
  6391. begin
  6392. { nothing to do, will be inserted for the new procdef while
  6393. parsing its body (by pdecsub.insert_funcret_local) }
  6394. end
  6395. else
  6396. internalerror(2011040605);
  6397. end;
  6398. end;
  6399. { will have to be associated with a new struct }
  6400. tprocdef(result).struct:=nil;
  6401. if assigned(implprocdefinfo) then
  6402. begin
  6403. if assigned(resultname) then
  6404. tprocdef(result).resultname:=stringdup(resultname^);
  6405. tprocdef(result).synthetickind:=synthetickind;
  6406. end;
  6407. if assigned(import_dll) then
  6408. tprocdef(result).import_dll:=stringdup(import_dll^);
  6409. if assigned(import_name) then
  6410. tprocdef(result).import_name:=stringdup(import_name^);
  6411. tprocdef(result).import_nr:=import_nr;
  6412. tprocdef(result).extnumber:=$ffff;
  6413. tprocdef(result).visibility:=visibility;
  6414. { we need a separate implementation for the copied def }
  6415. tprocdef(result).forwarddef:=true;
  6416. tprocdef(result).interfacedef:=true;
  6417. { create new paralist }
  6418. tprocdef(result).calcparas;
  6419. end;
  6420. function tprocdef.getcopy: tstoreddef;
  6421. begin
  6422. result:=getcopyas(procdef,pc_normal,'',true);
  6423. end;
  6424. procedure tprocdef.buildderef;
  6425. begin
  6426. inherited buildderef;
  6427. structderef.build(struct);
  6428. { procsym that originaly defined this definition, should be in the
  6429. same symtable }
  6430. procsymderef.build(procsym);
  6431. end;
  6432. procedure tprocdef.buildderefimpl;
  6433. begin
  6434. inherited buildderefimpl;
  6435. { Localst is not available for main/unit init }
  6436. if store_localst and assigned(localst) then
  6437. begin
  6438. tlocalsymtable(localst).buildderef;
  6439. tlocalsymtable(localst).buildderefimpl;
  6440. end;
  6441. { inline tree }
  6442. if has_inlininginfo then
  6443. begin
  6444. funcretsymderef.build(funcretsym);
  6445. inlininginfo^.code.buildderefimpl;
  6446. end;
  6447. end;
  6448. procedure tprocdef.deref;
  6449. begin
  6450. inherited deref;
  6451. struct:=tabstractrecorddef(structderef.resolve);
  6452. { procsym that originaly defined this definition, should be in the
  6453. same symtable }
  6454. procsym:=tprocsym(procsymderef.resolve);
  6455. end;
  6456. procedure tprocdef.derefimpl;
  6457. begin
  6458. { Enable has_inlininginfo when the inlininginfo
  6459. structure is available. The has_inlininginfo was disabled
  6460. after the load, since the data was invalid }
  6461. if assigned(inlininginfo) then
  6462. has_inlininginfo:=true;
  6463. { Locals }
  6464. if store_localst and assigned(localst) then
  6465. begin
  6466. tlocalsymtable(localst).deref(false);
  6467. tlocalsymtable(localst).derefimpl(false);
  6468. end;
  6469. { Inline }
  6470. if has_inlininginfo then
  6471. begin
  6472. inlininginfo^.code.derefimpl;
  6473. { funcretsym, this is always located in the localst }
  6474. funcretsym:=tsym(funcretsymderef.resolve);
  6475. end
  6476. else
  6477. begin
  6478. { safety }
  6479. { Not safe! A unit may be reresolved after its interface has been
  6480. parsed but before its implementation has been parsed, and in that
  6481. case the funcretsym is still required!
  6482. funcretsym:=nil; }
  6483. end;
  6484. end;
  6485. function tprocdef.GetTypeName : string;
  6486. begin
  6487. GetTypeName := FullProcName(false);
  6488. end;
  6489. function tprocdef.mangledname : TSymStr;
  6490. begin
  6491. {$ifdef symansistr}
  6492. if _mangledname='' then
  6493. begin
  6494. result:=defaultmangledname;
  6495. _mangledname:=result;
  6496. end
  6497. else
  6498. result:=_mangledname;
  6499. {$else symansistr}
  6500. if not assigned(_mangledname) then
  6501. begin
  6502. result:=defaultmangledname;
  6503. _mangledname:=stringdup(mangledname);
  6504. end
  6505. else
  6506. result:=_mangledname^;
  6507. {$endif symansistr}
  6508. end;
  6509. function tprocdef.defaultmangledname: TSymStr;
  6510. var
  6511. n : TSymStr;
  6512. begin
  6513. n:=procsym.name;
  6514. { make sure that the mangled names of these overloadable methods types is
  6515. unique even if it's made lowercase (e.g. for section names) }
  6516. if proctypeoption in [potype_operator,potype_class_constructor,potype_class_destructor] then
  6517. n:='$'+n;
  6518. { we need to use the symtable where the procsym is inserted,
  6519. because that is visible to the world }
  6520. defaultmangledname:=make_mangledname('',procsym.owner,n);
  6521. defaultmangledname:=defaultmangledname+mangledprocparanames(Length(defaultmangledname))
  6522. end;
  6523. function tprocdef.cplusplusmangledname : TSymStr;
  6524. function getcppparaname(p : tdef) : TSymStr;
  6525. const
  6526. {$ifdef NAMEMANGLING_GCC2}
  6527. ordtype2str : array[tordtype] of string[2] = (
  6528. '',
  6529. 'Uc','Us','Ui','Us','',
  6530. 'Sc','s','i','x','',
  6531. 'b','b','b','b','b','b',
  6532. 'c','w','x');
  6533. {$else NAMEMANGLING_GCC2}
  6534. ordtype2str : array[tordtype] of string[1] = (
  6535. 'v',
  6536. 'h','t','j','y','',
  6537. 'a','s','i','x','',
  6538. 'b','b','b','b','b',
  6539. 'b','b','b','b',
  6540. 'c','w','x','C');
  6541. floattype2str : array[tfloattype] of string[1] = (
  6542. 'f','d','e','e',
  6543. 'd','d','g');
  6544. {$endif NAMEMANGLING_GCC2}
  6545. var
  6546. s : TSymStr;
  6547. begin
  6548. case p.typ of
  6549. orddef:
  6550. begin
  6551. s:=ordtype2str[torddef(p).ordtype];
  6552. if s='C' then
  6553. s:=ordtype2str[range_to_basetype(torddef(p).low,torddef(p).high)];
  6554. end;
  6555. pointerdef:
  6556. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  6557. {$ifndef NAMEMANGLING_GCC2}
  6558. floatdef:
  6559. s:=floattype2str[tfloatdef(p).floattype];
  6560. {$endif NAMEMANGLING_GCC2}
  6561. else
  6562. internalerror(2103001);
  6563. end;
  6564. getcppparaname:=s;
  6565. end;
  6566. var
  6567. s,s2 : TSymStr;
  6568. hp : TParavarsym;
  6569. i : integer;
  6570. begin
  6571. {$ifdef NAMEMANGLING_GCC2}
  6572. { outdated gcc 2.x name mangling scheme }
  6573. s := procsym.realname;
  6574. if procsym.owner.symtabletype=ObjectSymtable then
  6575. begin
  6576. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  6577. case proctypeoption of
  6578. potype_destructor:
  6579. s:='_$_'+tostr(length(s2))+s2;
  6580. potype_constructor:
  6581. s:='___'+tostr(length(s2))+s2;
  6582. else
  6583. s:='_'+s+'__'+tostr(length(s2))+s2;
  6584. end;
  6585. end
  6586. else s:=s+'__';
  6587. s:=s+'F';
  6588. { concat modifiers }
  6589. { !!!!! }
  6590. { now we handle the parameters }
  6591. if maxparacount>0 then
  6592. begin
  6593. for i:=0 to paras.count-1 do
  6594. begin
  6595. hp:=tparavarsym(paras[i]);
  6596. { no hidden parameters form part of a C++ mangled name:
  6597. a) self is not included
  6598. b) there are no "high" or other hidden parameters
  6599. }
  6600. if vo_is_hidden_para in hp.varoptions then
  6601. continue;
  6602. s2:=getcppparaname(hp.vardef);
  6603. if hp.varspez in [vs_var,vs_out] then
  6604. s2:='R'+s2;
  6605. s:=s+s2;
  6606. end;
  6607. end
  6608. else
  6609. s:=s+'v';
  6610. cplusplusmangledname:=s;
  6611. {$else NAMEMANGLING_GCC2}
  6612. { gcc 3.x and 4.x name mangling scheme }
  6613. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  6614. if procsym.owner.symtabletype=ObjectSymtable then
  6615. begin
  6616. s:='_ZN';
  6617. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  6618. s:=s+tostr(length(s2))+s2;
  6619. case proctypeoption of
  6620. potype_constructor:
  6621. s:=s+'C1';
  6622. potype_destructor:
  6623. s:=s+'D1';
  6624. else
  6625. s:=s+tostr(length(procsym.realname))+procsym.realname;
  6626. end;
  6627. s:=s+'E';
  6628. end
  6629. else
  6630. s:=procsym.realname;
  6631. { now we handle the parameters }
  6632. if maxparacount>0 then
  6633. begin
  6634. for i:=0 to paras.count-1 do
  6635. begin
  6636. hp:=tparavarsym(paras[i]);
  6637. { no hidden parameters form part of a C++ mangled name:
  6638. a) self is not included
  6639. b) there are no "high" or other hidden parameters
  6640. }
  6641. if vo_is_hidden_para in hp.varoptions then
  6642. continue;
  6643. s2:=getcppparaname(hp.vardef);
  6644. if hp.varspez in [vs_var,vs_out] then
  6645. s2:='R'+s2;
  6646. s:=s+s2;
  6647. end;
  6648. end
  6649. else
  6650. s:=s+'v';
  6651. cplusplusmangledname:=s;
  6652. {$endif NAMEMANGLING_GCC2}
  6653. end;
  6654. function tprocdef.objcmangledname : TSymStr;
  6655. var
  6656. manglednamelen: longint;
  6657. iscatmethod : boolean;
  6658. begin
  6659. if not (po_msgstr in procoptions) then
  6660. internalerror(2009030901);
  6661. { we may very well need longer strings to handle these... }
  6662. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  6663. length('+"[ ]"')+length(messageinf.str^);
  6664. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  6665. if (iscatmethod) then
  6666. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  6667. if manglednamelen>255 then
  6668. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  6669. if not(po_classmethod in procoptions) then
  6670. result:='"-['
  6671. else
  6672. result:='"+[';
  6673. { quotes are necessary because the +/- otherwise confuse the assembler
  6674. into expecting a number
  6675. }
  6676. if iscatmethod then
  6677. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  6678. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  6679. if iscatmethod then
  6680. result:=result+')';
  6681. result:=result+' '+messageinf.str^+']"';
  6682. end;
  6683. procedure tprocdef.setmangledname(const s : TSymStr);
  6684. begin
  6685. { This is not allowed anymore, the forward declaration
  6686. already needs to create the correct mangledname, no changes
  6687. afterwards are allowed (PFV) }
  6688. { Exception: interface definitions in mode macpas, since in that }
  6689. { case no reference to the old name can exist yet (JM) }
  6690. {$ifdef symansistr}
  6691. if _mangledname<>'' then
  6692. if ((m_mac in current_settings.modeswitches) and
  6693. (interfacedef)) then
  6694. _mangledname:=''
  6695. else
  6696. internalerror(200411171);
  6697. {$else symansistr}
  6698. if assigned(_mangledname) then
  6699. if ((m_mac in current_settings.modeswitches) and
  6700. (interfacedef)) then
  6701. stringdispose(_mangledname)
  6702. else
  6703. internalerror(200411171);
  6704. {$endif symansistr}
  6705. {$ifdef jvm}
  6706. { this routine can be called for compilerproces. can't set mangled
  6707. name since it must be calculated, but it uses import_name when set
  6708. -> set that one }
  6709. import_name:=stringdup(s);
  6710. include(procoptions,po_has_importname);
  6711. include(procoptions,po_has_mangledname);
  6712. {$else}
  6713. {$ifdef symansistr}
  6714. _mangledname:=s;
  6715. {$else symansistr}
  6716. _mangledname:=stringdup(s);
  6717. {$endif symansistr}
  6718. {$endif jvm}
  6719. include(procoptions,po_has_mangledname);
  6720. end;
  6721. function tprocdef.needsglobalasmsym: boolean;
  6722. begin
  6723. result:=
  6724. (cs_profile in current_settings.moduleswitches) or
  6725. { smart linking using a library requires to promote
  6726. all non-nested procedures to AB_GLOBAL
  6727. otherwise you get undefined symbol error at linking
  6728. for msdos target with -CX option for instance }
  6729. (create_smartlink_library and not is_nested_pd(self)) or
  6730. (po_global in procoptions);
  6731. end;
  6732. procedure tprocdef.setcompilerprocname;
  6733. begin
  6734. procsym.realname:='$'+lower(procsym.name);
  6735. end;
  6736. {***************************************************************************
  6737. TPROCVARDEF
  6738. ***************************************************************************}
  6739. constructor tprocvardef.create(level:byte;doregister:boolean);
  6740. begin
  6741. inherited create(procvardef,level,doregister);
  6742. end;
  6743. class function tprocvardef.getreusableprocaddr(def: tabstractprocdef; copytyp: tcacheableproccopytyp): tprocvardef;
  6744. var
  6745. res: PHashSetItem;
  6746. oldsymtablestack: tsymtablestack;
  6747. key: packed record
  6748. def: tabstractprocdef;
  6749. copytyp: tcacheableproccopytyp;
  6750. end;
  6751. begin
  6752. if not assigned(current_module) then
  6753. internalerror(2011081302);
  6754. key.def:=def;
  6755. key.copytyp:=copytyp;
  6756. res:=current_module.procaddrdefs.FindOrAdd(@key,sizeof(key));
  6757. if not assigned(res^.Data) then
  6758. begin
  6759. { since these pointerdefs can be reused anywhere in the current
  6760. unit, add them to the global/staticsymtable (or local symtable
  6761. if they're a local def, because otherwise they'll be saved
  6762. to the ppu referencing a local symtable entry that doesn't
  6763. exist in the ppu) }
  6764. oldsymtablestack:=symtablestack;
  6765. { do not simply push/pop current_module.localsymtable, because
  6766. that can have side-effects (e.g., it removes helpers) }
  6767. symtablestack:=nil;
  6768. result:=tprocvardef(def.getcopyas(procvardef,copytyp,'',true));
  6769. setup_reusable_def(def,result,res,oldsymtablestack);
  6770. { res^.Data may still be nil -> don't overwrite result }
  6771. exit;
  6772. end;
  6773. result:=tprocvardef(res^.Data);
  6774. end;
  6775. class function tprocvardef.getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  6776. begin
  6777. result:=getreusableprocaddr(def,pc_address_only);
  6778. if not result.is_registered then
  6779. include(result.defoptions,df_not_registered_no_free);
  6780. end;
  6781. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  6782. begin
  6783. inherited ppuload(procvardef,ppufile);
  6784. { load para symtable }
  6785. parast:=tparasymtable.create(self,ppufile.getbyte);
  6786. ppuload_platform(ppufile);
  6787. tparasymtable(parast).ppuload(ppufile);
  6788. end;
  6789. function tprocvardef.getcopy : tstoreddef;
  6790. var
  6791. i : tcallercallee;
  6792. j : longint;
  6793. begin
  6794. result:=cprocvardef.create(parast.symtablelevel,true);
  6795. tprocvardef(result).returndef:=returndef;
  6796. tprocvardef(result).returndefderef:=returndefderef;
  6797. tprocvardef(result).parast:=parast.getcopy;
  6798. tprocvardef(result).savesize:=savesize;
  6799. { create paralist copy }
  6800. calcparas;
  6801. tprocvardef(result).paras:=tparalist.create(false);
  6802. tprocvardef(result).paras.count:=paras.count;
  6803. for j:=0 to paras.count-1 do
  6804. tprocvardef(result).paras[j]:=paras[j];
  6805. tprocvardef(result).proctypeoption:=proctypeoption;
  6806. tprocvardef(result).proccalloption:=proccalloption;
  6807. tprocvardef(result).procoptions:=procoptions;
  6808. tprocvardef(result).callerargareasize:=callerargareasize;
  6809. tprocvardef(result).calleeargareasize:=calleeargareasize;
  6810. tprocvardef(result).maxparacount:=maxparacount;
  6811. tprocvardef(result).minparacount:=minparacount;
  6812. for i:=low(funcretloc) to high(funcretloc) do
  6813. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  6814. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  6815. {$ifdef m68k}
  6816. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  6817. {$endif}
  6818. end;
  6819. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  6820. begin
  6821. inherited ppuwrite(ppufile);
  6822. { Save the para symtable level (necessary to distinguish nested
  6823. procvars) }
  6824. ppufile.putbyte(parast.symtablelevel);
  6825. { Write this entry }
  6826. writeentry(ppufile,ibprocvardef);
  6827. { Save the para symtable, this is taken from the interface }
  6828. tparasymtable(parast).ppuwrite(ppufile);
  6829. end;
  6830. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  6831. begin
  6832. case t of
  6833. gs_para :
  6834. GetSymtable:=parast;
  6835. else
  6836. GetSymtable:=nil;
  6837. end;
  6838. end;
  6839. function tprocvardef.size : asizeint;
  6840. begin
  6841. { we return false for is_addressonly for a block (because it's not a
  6842. simple pointer to a function), but they are handled as implicit
  6843. pointers to a datastructure that contains everything ->
  6844. voidpointertype.size instead of voidcodepointertype.size }
  6845. if po_is_block in procoptions then
  6846. size:=voidpointertype.size
  6847. else if not is_addressonly then
  6848. begin
  6849. if is_nested_pd(self) then
  6850. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  6851. else
  6852. size:=voidcodepointertype.size+voidpointertype.size;
  6853. end
  6854. else
  6855. size:=voidcodepointertype.size;
  6856. end;
  6857. function tprocvardef.is_methodpointer:boolean;
  6858. begin
  6859. result:=(po_methodpointer in procoptions);
  6860. end;
  6861. function tprocvardef.is_addressonly:boolean;
  6862. begin
  6863. result:=((not(po_methodpointer in procoptions) or (po_staticmethod in procoptions)) and
  6864. not(po_is_block in procoptions) and
  6865. not is_nested_pd(self)) or
  6866. (po_addressonly in procoptions);
  6867. end;
  6868. function tprocvardef.getmangledparaname:TSymStr;
  6869. begin
  6870. if not(po_methodpointer in procoptions) then
  6871. if not is_nested_pd(self) then
  6872. result:='procvar'
  6873. else
  6874. { we need the manglednames here, because nestedprocvars can be anonymous, e.g.
  6875. having not a type name or not an unique one, see webtbs/tw27515.pp
  6876. Further, use $_ ... _$ delimiters to avoid ambiguous names, see webtbs/tw27515.pp }
  6877. result:='$_nestedprovar'+mangledprocparanames(0)+'_$'
  6878. else
  6879. result:='procvarofobj'
  6880. end;
  6881. function tprocvardef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef;
  6882. begin
  6883. result:=inherited;
  6884. tabstractprocdef(result).calcparas;
  6885. end;
  6886. function tprocvardef.is_publishable : tpublishproperty;
  6887. begin
  6888. if po_methodpointer in procoptions then
  6889. is_publishable:=pp_publish
  6890. else
  6891. is_publishable:=pp_error;
  6892. end;
  6893. function tprocvardef.GetTypeName : string;
  6894. var
  6895. s: string;
  6896. pno : tprocnameoptions;
  6897. begin
  6898. {$ifdef EXTDEBUG}
  6899. pno:=[pno_showhidden];
  6900. {$else EXTDEBUG}
  6901. pno:=[];
  6902. {$endif EXTDEBUG}
  6903. s:='<';
  6904. if po_is_block in procoptions then
  6905. s := s+'reference to'
  6906. else if po_classmethod in procoptions then
  6907. s := s+'class method type of'
  6908. else
  6909. if po_addressonly in procoptions then
  6910. s := s+'address of'
  6911. else
  6912. s := s+'procedure variable type of';
  6913. if assigned(returndef) and
  6914. (returndef<>voidtype) then
  6915. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  6916. else
  6917. s:=s+' procedure'+typename_paras(pno);
  6918. if po_methodpointer in procoptions then
  6919. s := s+' of object';
  6920. if is_nested_pd(self) then
  6921. s := s+' is nested';
  6922. if po_far in procoptions then
  6923. s := s+';far';
  6924. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  6925. end;
  6926. {***************************************************************************
  6927. TOBJECTDEF
  6928. ***************************************************************************}
  6929. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);
  6930. begin
  6931. inherited create(n,objectdef,doregister);
  6932. fcurrent_dispid:=0;
  6933. objecttype:=ot;
  6934. childof:=nil;
  6935. childofderef.reset;
  6936. vmt_fieldderef.reset;
  6937. hiddenclassdefref.reset;
  6938. extendeddefderef.reset;
  6939. cloneddefderef.reset;
  6940. if objecttype=odt_helper then
  6941. owner.includeoption(sto_has_helper);
  6942. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords,
  6943. current_settings.alignment.recordalignmin);
  6944. { create space for vmt !! }
  6945. vmtentries:=TFPList.Create;
  6946. set_parent(c);
  6947. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  6948. prepareguid;
  6949. { setup implemented interfaces }
  6950. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  6951. ImplementedInterfaces:=TFPObjectList.Create(true)
  6952. else
  6953. ImplementedInterfaces:=nil;
  6954. writing_class_record_dbginfo:=false;
  6955. end;
  6956. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  6957. var
  6958. i,
  6959. implintfcount : longint;
  6960. d, getterd : tderef;
  6961. ImplIntf : TImplementedInterface;
  6962. vmtentry : pvmtentry;
  6963. begin
  6964. inherited ppuload(objectdef,ppufile);
  6965. objecttype:=tobjecttyp(ppufile.getbyte);
  6966. helpertype:=thelpertype(ppufile.getbyte);
  6967. objextname:=ppufile.getpshortstring;
  6968. { only used for external Objective-C classes/protocols }
  6969. if (objextname^='') then
  6970. stringdispose(objextname);
  6971. symtable:=tObjectSymtable.create(self,objrealname^,0,0);
  6972. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  6973. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  6974. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  6975. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  6976. tObjectSymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  6977. ppufile.getderef(vmt_fieldderef);
  6978. ppufile.getderef(childofderef);
  6979. { load guid }
  6980. iidstr:=nil;
  6981. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6982. begin
  6983. new(iidguid);
  6984. ppufile.getguid(iidguid^);
  6985. iidstr:=ppufile.getpshortstring;
  6986. end;
  6987. abstractcnt:=ppufile.getlongint;
  6988. if objecttype=odt_helper then
  6989. ppufile.getderef(extendeddefderef);
  6990. vmtentries:=TFPList.Create;
  6991. vmtentries.count:=ppufile.getlongint;
  6992. for i:=0 to vmtentries.count-1 do
  6993. begin
  6994. ppufile.getderef(d);
  6995. new(vmtentry);
  6996. vmtentry^.procdef:=nil;
  6997. vmtentry^.procdefderef:=d;
  6998. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  6999. vmtentries[i]:=vmtentry;
  7000. end;
  7001. { load implemented interfaces }
  7002. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  7003. begin
  7004. ImplementedInterfaces:=TFPObjectList.Create(true);
  7005. implintfcount:=ppufile.getlongint;
  7006. for i:=0 to implintfcount-1 do
  7007. begin
  7008. ppufile.getderef(d);
  7009. ppufile.getderef(getterd);
  7010. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  7011. ImplIntf.IOffset:=ppufile.getlongint;
  7012. byte(ImplIntf.IType):=ppufile.getbyte;
  7013. ImplementedInterfaces.Add(ImplIntf);
  7014. end;
  7015. end
  7016. else
  7017. ImplementedInterfaces:=nil;
  7018. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7019. ppufile.getderef(hiddenclassdefref);
  7020. if df_copied_def in defoptions then
  7021. begin
  7022. ppufile.getderef(cloneddefderef);
  7023. ppuload_platform(ppufile);
  7024. end
  7025. else
  7026. begin
  7027. ppuload_platform(ppufile);
  7028. tObjectSymtable(symtable).ppuload(ppufile);
  7029. end;
  7030. { handles the predefined class tobject }
  7031. { the last TOBJECT which is loaded gets }
  7032. { it ! }
  7033. { but do this only from a unit that's }
  7034. { marked as system unit to avoid some }
  7035. { equally named user's type to override }
  7036. { the internal types! }
  7037. if mf_system_unit in current_module.moduleflags then
  7038. begin
  7039. if (childof=nil) and
  7040. (objecttype in [odt_class,odt_javaclass]) and
  7041. (objname^='TOBJECT') then
  7042. class_tobject:=self;
  7043. if (childof=nil) and
  7044. (objecttype=odt_interfacecom) then
  7045. if (objname^='IUNKNOWN') then
  7046. interface_iunknown:=self
  7047. else
  7048. if (objname^='IDISPATCH') then
  7049. interface_idispatch:=self;
  7050. if (objecttype=odt_javaclass) and
  7051. not(oo_is_formal in objectoptions) then
  7052. begin
  7053. if (objname^='JLOBJECT') then
  7054. java_jlobject:=self
  7055. else if (objname^='JLTHROWABLE') then
  7056. java_jlthrowable:=self
  7057. else if (objname^='FPCBASERECORDTYPE') then
  7058. java_fpcbaserecordtype:=self
  7059. else if (objname^='JLSTRING') then
  7060. java_jlstring:=self
  7061. else if (objname^='ANSISTRINGCLASS') then
  7062. java_ansistring:=self
  7063. else if (objname^='SHORTSTRINGCLASS') then
  7064. java_shortstring:=self
  7065. else if (objname^='JLENUM') then
  7066. java_jlenum:=self
  7067. else if (objname^='JUENUMSET') then
  7068. java_juenumset:=self
  7069. else if (objname^='FPCBITSET') then
  7070. java_jubitset:=self
  7071. else if (objname^='FPCBASEPROCVARTYPE') then
  7072. java_procvarbase:=self;
  7073. end;
  7074. end;
  7075. if (childof=nil) and
  7076. (objecttype=odt_objcclass) and
  7077. (objname^='PROTOCOL') then
  7078. objc_protocoltype:=self;
  7079. writing_class_record_dbginfo:=false;
  7080. end;
  7081. destructor tobjectdef.destroy;
  7082. begin
  7083. if assigned(symtable) then
  7084. begin
  7085. symtable.free;
  7086. symtable:=nil;
  7087. end;
  7088. stringdispose(objextname);
  7089. stringdispose(iidstr);
  7090. if assigned(ImplementedInterfaces) then
  7091. begin
  7092. ImplementedInterfaces.free;
  7093. ImplementedInterfaces:=nil;
  7094. end;
  7095. if assigned(iidguid) then
  7096. begin
  7097. dispose(iidguid);
  7098. iidguid:=nil;
  7099. end;
  7100. TFPList.FreeAndNilDisposing(vmtentries,TypeInfo(tvmtentry));
  7101. if assigned(vmcallstaticinfo) then
  7102. begin
  7103. freemem(vmcallstaticinfo);
  7104. vmcallstaticinfo:=nil;
  7105. end;
  7106. inherited destroy;
  7107. end;
  7108. function tobjectdef.getcopy : tstoreddef;
  7109. var
  7110. i : longint;
  7111. begin
  7112. result:=cobjectdef.create(objecttype,objrealname^,childof,true);
  7113. { the constructor allocates a symtable which we release to avoid memory leaks }
  7114. tobjectdef(result).symtable.free;
  7115. tobjectdef(result).symtable:=symtable.getcopy;
  7116. if assigned(objextname) then
  7117. tobjectdef(result).objextname:=stringdup(objextname^);
  7118. if assigned(import_lib) then
  7119. tobjectdef(result).import_lib:=stringdup(import_lib^);
  7120. tobjectdef(result).objectoptions:=objectoptions;
  7121. include(tobjectdef(result).defoptions,df_copied_def);
  7122. tobjectdef(result).extendeddef:=extendeddef;
  7123. if assigned(tcinitcode) then
  7124. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  7125. tobjectdef(result).vmt_field:=vmt_field;
  7126. if assigned(iidguid) then
  7127. begin
  7128. new(tobjectdef(result).iidguid);
  7129. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  7130. end;
  7131. if assigned(iidstr) then
  7132. tobjectdef(result).iidstr:=stringdup(iidstr^);
  7133. tobjectdef(result).abstractcnt:=abstractcnt;
  7134. if assigned(ImplementedInterfaces) then
  7135. begin
  7136. tobjectdef(result).ImplementedInterfaces.capacity:=tobjectdef(result).ImplementedInterfaces.count+ImplementedInterfaces.count;
  7137. for i:=0 to ImplementedInterfaces.count-1 do
  7138. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  7139. end;
  7140. tobjectdef(result).copyvmtentries(self);
  7141. end;
  7142. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  7143. var
  7144. i : longint;
  7145. vmtentry : pvmtentry;
  7146. ImplIntf : TImplementedInterface;
  7147. old_do_indirect_crc: boolean;
  7148. begin
  7149. { if class1 in unit A changes, and class2 in unit B inherits from it
  7150. (so unit B uses unit A), then unit B with class2 will be recompiled.
  7151. However, if there is also a class3 in unit C that only depends on
  7152. unit B, then unit C will not be recompiled because nothing changed
  7153. to the interface of unit B. Nevertheless, unit C can indirectly
  7154. depend on unit A via derefs, and these must be updated -> the
  7155. indirect crc keeps track of such changes. }
  7156. old_do_indirect_crc:=ppufile.do_indirect_crc;
  7157. ppufile.do_indirect_crc:=true;
  7158. inherited ppuwrite(ppufile);
  7159. ppufile.putbyte(byte(objecttype));
  7160. ppufile.putbyte(byte(helpertype));
  7161. if assigned(objextname) then
  7162. ppufile.putstring(objextname^)
  7163. else
  7164. ppufile.putstring('');
  7165. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  7166. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  7167. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  7168. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  7169. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignmin));
  7170. ppufile.putderef(vmt_fieldderef);
  7171. ppufile.putderef(childofderef);
  7172. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  7173. begin
  7174. ppufile.putguid(iidguid^);
  7175. ppufile.putstring(iidstr^);
  7176. end;
  7177. ppufile.putlongint(abstractcnt);
  7178. if objecttype=odt_helper then
  7179. ppufile.putderef(extendeddefderef);
  7180. ppufile.putlongint(vmtentries.count);
  7181. for i:=0 to vmtentries.count-1 do
  7182. begin
  7183. vmtentry:=pvmtentry(vmtentries[i]);
  7184. ppufile.putderef(vmtentry^.procdefderef);
  7185. ppufile.putbyte(byte(vmtentry^.visibility));
  7186. end;
  7187. if assigned(ImplementedInterfaces) then
  7188. begin
  7189. ppufile.putlongint(ImplementedInterfaces.Count);
  7190. for i:=0 to ImplementedInterfaces.Count-1 do
  7191. begin
  7192. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  7193. ppufile.putderef(ImplIntf.intfdefderef);
  7194. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  7195. ppufile.putlongint(ImplIntf.Ioffset);
  7196. ppufile.putbyte(byte(ImplIntf.IType));
  7197. end;
  7198. end;
  7199. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7200. ppufile.putderef(hiddenclassdefref);
  7201. if df_copied_def in defoptions then
  7202. ppufile.putderef(cloneddefderef);
  7203. writeentry(ppufile,ibobjectdef);
  7204. if not(df_copied_def in defoptions) then
  7205. tObjectSymtable(symtable).ppuwrite(ppufile);
  7206. ppufile.do_indirect_crc:=old_do_indirect_crc;
  7207. end;
  7208. function tobjectdef.GetTypeName:string;
  7209. begin
  7210. { in this case we will go in endless recursion, because then }
  7211. { there is no tsym associated yet with the def. It can occur }
  7212. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  7213. { instead of the actual type name }
  7214. if not assigned(typesym) then
  7215. result:='<Currently Parsed Class>'
  7216. else
  7217. result:=typesymbolprettyname;
  7218. end;
  7219. procedure tobjectdef.buildderef;
  7220. var
  7221. i : longint;
  7222. vmtentry : pvmtentry;
  7223. begin
  7224. inherited buildderef;
  7225. vmt_fieldderef.build(vmt_field);
  7226. childofderef.build(childof);
  7227. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7228. hiddenclassdefref.build(hiddenclassdef);
  7229. if df_copied_def in defoptions then
  7230. cloneddefderef.build(symtable.defowner)
  7231. else
  7232. tstoredsymtable(symtable).buildderef;
  7233. if objecttype=odt_helper then
  7234. extendeddefderef.build(extendeddef);
  7235. for i:=0 to vmtentries.count-1 do
  7236. begin
  7237. vmtentry:=pvmtentry(vmtentries[i]);
  7238. vmtentry^.procdefderef.build(vmtentry^.procdef);
  7239. end;
  7240. if assigned(ImplementedInterfaces) then
  7241. begin
  7242. for i:=0 to ImplementedInterfaces.count-1 do
  7243. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  7244. end;
  7245. end;
  7246. procedure tobjectdef.deref;
  7247. var
  7248. i : longint;
  7249. vmtentry : pvmtentry;
  7250. begin
  7251. inherited deref;
  7252. vmt_field:=tsym(vmt_fieldderef.resolve);
  7253. childof:=tobjectdef(childofderef.resolve);
  7254. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7255. hiddenclassdef:=tobjectdef(hiddenclassdefref.resolve);
  7256. if df_copied_def in defoptions then
  7257. begin
  7258. cloneddef:=tobjectdef(cloneddefderef.resolve);
  7259. symtable:=cloneddef.symtable.getcopy;
  7260. end
  7261. else
  7262. tstoredsymtable(symtable).deref(false);
  7263. if objecttype=odt_helper then
  7264. extendeddef:=tdef(extendeddefderef.resolve);
  7265. for i:=0 to vmtentries.count-1 do
  7266. begin
  7267. vmtentry:=pvmtentry(vmtentries[i]);
  7268. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  7269. end;
  7270. if assigned(ImplementedInterfaces) then
  7271. begin
  7272. for i:=0 to ImplementedInterfaces.count-1 do
  7273. TImplementedInterface(ImplementedInterfaces[i]).deref;
  7274. end;
  7275. end;
  7276. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  7277. var
  7278. pd: tprocdef absolute def;
  7279. st: tsymtable;
  7280. psym: tsym;
  7281. nname: TIDString;
  7282. begin
  7283. if (tdef(def).typ<>procdef) then
  7284. exit;
  7285. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  7286. owner = symtable in which objcclassdef is defined
  7287. }
  7288. st:=pd.owner.defowner.owner;
  7289. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  7290. { check for an existing procsym with our special name }
  7291. psym:=tsym(st.find(nname));
  7292. if not assigned(psym) then
  7293. begin
  7294. psym:=cprocsym.create(nname);
  7295. { avoid warning about this symbol being unused }
  7296. psym.IncRefCount;
  7297. { don't check for duplicates:
  7298. a) we checked above
  7299. b) in case we are in the implementation section of a unit, this
  7300. will also check for this symbol in the interface section
  7301. (since you normally cannot have symbols with the same name
  7302. both interface and implementation), and it's possible to
  7303. have class helpers for the same class in the interface and
  7304. in the implementation, and they cannot be merged since only
  7305. the once in the interface must be saved to the ppu/visible
  7306. from other units }
  7307. st.insertsym(psym,false);
  7308. end
  7309. else if (psym.typ<>procsym) then
  7310. internalerror(2009111501);
  7311. { add ourselves to this special procsym }
  7312. tprocsym(psym).procdeflist.add(def);
  7313. end;
  7314. procedure tobjectdef.derefimpl;
  7315. begin
  7316. inherited derefimpl;
  7317. { the procdefs are not owned by the class helper procsyms, so they
  7318. are not stored/restored either -> re-add them here }
  7319. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  7320. (oo_is_classhelper in objectoptions) then
  7321. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  7322. end;
  7323. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  7324. var
  7325. i : longint;
  7326. vmtentry : pvmtentry;
  7327. begin
  7328. if vmtentries.count<>0 then
  7329. internalerror(2019081401);
  7330. vmtentries.count:=objdef.vmtentries.count;
  7331. for i:=0 to objdef.vmtentries.count-1 do
  7332. begin
  7333. new(vmtentry);
  7334. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  7335. vmtentries[i]:=vmtentry;
  7336. end;
  7337. end;
  7338. function tobjectdef.getparentdef:tdef;
  7339. begin
  7340. { TODO: Remove getparentdef hack}
  7341. { With 2 forward declared classes with the child class before the
  7342. parent class the child class is written earlier to the ppu. Leaving it
  7343. possible to have a reference to the parent class for property overriding,
  7344. but the parent class still has the childof not resolved yet (PFV) }
  7345. if childof=nil then
  7346. childof:=tobjectdef(childofderef.resolve);
  7347. result:=childof;
  7348. end;
  7349. procedure tobjectdef.prepareguid;
  7350. begin
  7351. { set up guid }
  7352. if not assigned(iidguid) then
  7353. begin
  7354. new(iidguid);
  7355. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  7356. end;
  7357. { setup iidstring }
  7358. if not assigned(iidstr) then
  7359. iidstr:=stringdup(''); { default is empty string }
  7360. end;
  7361. procedure tobjectdef.set_parent( c : tobjectdef);
  7362. begin
  7363. if assigned(childof) then
  7364. exit;
  7365. childof:=c;
  7366. if not assigned(c) then
  7367. exit;
  7368. { inherit options and status }
  7369. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  7370. { initially has the same number of abstract methods as the parent }
  7371. abstractcnt:=c.abstractcnt;
  7372. { add the data of the anchestor class/object }
  7373. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  7374. begin
  7375. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  7376. { inherit recordalignment }
  7377. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  7378. { if both the parent and this record use C-alignment, also inherit
  7379. the current field alignment }
  7380. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  7381. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  7382. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  7383. { the padding is not inherited for Objective-C classes (maybe not
  7384. for cppclass either?) }
  7385. if objecttype=odt_objcclass then
  7386. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  7387. if (oo_has_vmt in objectoptions) and
  7388. (oo_has_vmt in c.objectoptions) then
  7389. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  7390. { if parent has a vmt field then the offset is the same for the child PM }
  7391. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  7392. begin
  7393. vmt_field:=c.vmt_field;
  7394. include(objectoptions,oo_has_vmt);
  7395. end;
  7396. end;
  7397. end;
  7398. procedure tobjectdef.insertvmt;
  7399. begin
  7400. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  7401. exit;
  7402. if (oo_has_vmt in objectoptions) then
  7403. internalerror(2020100816)
  7404. else
  7405. begin
  7406. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  7407. tObjectSymtable(symtable).fieldalignment);
  7408. if (tf_requires_proper_alignment in target_info.flags) then
  7409. begin
  7410. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  7411. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  7412. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  7413. end;
  7414. vmt_field:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  7415. hidesym(vmt_field);
  7416. tObjectSymtable(symtable).insertsym(vmt_field);
  7417. tObjectSymtable(symtable).addfield(tfieldvarsym(vmt_field),vis_hidden);
  7418. include(objectoptions,oo_has_vmt);
  7419. end;
  7420. end;
  7421. function tobjectdef.vmt_offset: asizeint;
  7422. begin
  7423. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  7424. result:=0
  7425. else if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  7426. result:=tfieldvarsym(vmt_field).fieldoffset
  7427. else
  7428. result:=tfieldvarsym(vmt_field).fieldoffset div 8;
  7429. end;
  7430. procedure tobjectdef.check_forwards;
  7431. begin
  7432. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  7433. inherited;
  7434. if (oo_is_forward in objectoptions) then
  7435. begin
  7436. { ok, in future, the forward can be resolved }
  7437. Message1(sym_e_class_forward_not_resolved,objrealname^);
  7438. exclude(objectoptions,oo_is_forward);
  7439. end;
  7440. end;
  7441. function tobjectdef.find_destructor: tprocdef;
  7442. var
  7443. objdef: tobjectdef;
  7444. begin
  7445. objdef:=self;
  7446. while assigned(objdef) do
  7447. begin
  7448. result:=objdef.find_procdef_bytype(potype_destructor);
  7449. if assigned(result) then
  7450. exit;
  7451. objdef:=objdef.childof;
  7452. end;
  7453. result:=nil;
  7454. end;
  7455. function tobjectdef.implements_any_interfaces: boolean;
  7456. begin
  7457. result := (ImplementedInterfaces.Count > 0) or
  7458. (assigned(childof) and childof.implements_any_interfaces);
  7459. end;
  7460. function tobjectdef.register_implemented_interface(intfdef:tobjectdef;useguid:boolean):timplementedinterface;
  7461. begin
  7462. { allocate the GUID only if the class implements at least one interface }
  7463. if useguid then
  7464. prepareguid;
  7465. result:=timplementedinterface.create(intfdef);
  7466. ImplementedInterfaces.Add(result);
  7467. end;
  7468. function tobjectdef.size : asizeint;
  7469. begin
  7470. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  7471. result:=voidpointertype.size
  7472. else
  7473. result:=tObjectSymtable(symtable).datasize;
  7474. end;
  7475. function tobjectdef.alignment:shortint;
  7476. begin
  7477. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  7478. alignment:=voidpointertype.alignment
  7479. else
  7480. alignment:=tObjectSymtable(symtable).recordalignment;
  7481. end;
  7482. function tobjectdef.vmtmethodoffset(index:longint):longint;
  7483. begin
  7484. { for offset of methods for classes, see rtl/inc/objpash.inc }
  7485. case objecttype of
  7486. odt_class:
  7487. { the +2*sizeof(pint) is size and -size }
  7488. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  7489. odt_helper,
  7490. odt_objcclass,
  7491. odt_objcprotocol:
  7492. vmtmethodoffset:=0;
  7493. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  7494. vmtmethodoffset:=index*voidcodepointertype.size;
  7495. odt_javaclass,
  7496. odt_interfacejava:
  7497. { invalid }
  7498. vmtmethodoffset:=-1;
  7499. else
  7500. { the +2*sizeof(pint) is size and -size }
  7501. {$ifdef WITHDMT}
  7502. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  7503. {$else WITHDMT}
  7504. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  7505. {$endif WITHDMT}
  7506. end;
  7507. end;
  7508. function tobjectdef.vmt_mangledname : TSymStr;
  7509. begin
  7510. if not(oo_has_vmt in objectoptions) then
  7511. Message1(parser_n_object_has_no_vmt,objrealname^);
  7512. if not is_unique_objpasdef then
  7513. vmt_mangledname:=make_mangledname('VMT',owner,objname^)
  7514. else
  7515. vmt_mangledname:=childof.vmt_mangledname;
  7516. end;
  7517. function tobjectdef.vmt_def: trecorddef;
  7518. var
  7519. where: tsymtable;
  7520. vmttypesym: tsymentry;
  7521. begin
  7522. if not is_unique_objpasdef then
  7523. begin
  7524. vmttypesym:=symtable.Find('vmtdef');
  7525. if not assigned(vmttypesym) or
  7526. (vmttypesym.typ<>symconst.typesym) or
  7527. (ttypesym(vmttypesym).typedef.typ<>recorddef) then
  7528. internalerror(2015052501);
  7529. result:=trecorddef(ttypesym(vmttypesym).typedef);
  7530. end
  7531. else
  7532. result:=childof.vmt_def;
  7533. end;
  7534. function tobjectdef.needs_inittable : boolean;
  7535. var
  7536. hp : tobjectdef;
  7537. begin
  7538. case objecttype of
  7539. odt_helper,
  7540. odt_class :
  7541. needs_inittable:=false;
  7542. odt_dispinterface,
  7543. odt_interfacecom:
  7544. needs_inittable:=true;
  7545. odt_interfacecorba:
  7546. begin
  7547. hp:=childof;
  7548. while assigned(hp) do
  7549. begin
  7550. if hp=interface_iunknown then
  7551. begin
  7552. needs_inittable:=true;
  7553. exit;
  7554. end;
  7555. hp:=hp.childof;
  7556. end;
  7557. needs_inittable:=false;
  7558. end;
  7559. odt_object:
  7560. needs_inittable:=
  7561. tObjectSymtable(symtable).needs_init_final or
  7562. (assigned(childof) and
  7563. childof.needs_inittable);
  7564. odt_cppclass,
  7565. odt_objcclass,
  7566. odt_objcprotocol,
  7567. odt_javaclass,
  7568. odt_interfacejava:
  7569. needs_inittable:=false;
  7570. else
  7571. internalerror(200108267);
  7572. end;
  7573. end;
  7574. function tobjectdef.needs_separate_initrtti : boolean;
  7575. begin
  7576. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  7577. end;
  7578. function tobjectdef.has_non_trivial_init_child(check_parent:boolean):boolean;
  7579. begin
  7580. if objecttype in [odt_class,odt_object] then
  7581. begin
  7582. result:=tobjectsymtable(symtable).has_non_trivial_init or
  7583. (check_parent and assigned(childof) and childof.has_non_trivial_init_child(true));
  7584. end
  7585. else
  7586. result:=false;
  7587. end;
  7588. function tobjectdef.rtti_mangledname(rt: trttitype): TSymStr;
  7589. begin
  7590. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  7591. begin
  7592. if not is_unique_objpasdef then
  7593. result:=inherited rtti_mangledname(rt)
  7594. else
  7595. result:=childof.rtti_mangledname(rt)
  7596. end
  7597. else
  7598. begin
  7599. { necessary in case of a dynamic array of nsobject, or
  7600. if an nsobject field appears in a record that needs
  7601. init/finalisation }
  7602. if rt=initrtti then
  7603. begin
  7604. result:=voidpointertype.rtti_mangledname(rt);
  7605. exit;
  7606. end;
  7607. if not(target_info.system in systems_objc_nfabi) then
  7608. begin
  7609. result:=target_asm.labelprefix;
  7610. case objecttype of
  7611. odt_objcclass:
  7612. begin
  7613. case rt of
  7614. objcclassrtti:
  7615. if not(oo_is_classhelper in objectoptions) then
  7616. result:=result+'_OBJC_CLASS_'
  7617. else
  7618. result:=result+'_OBJC_CATEGORY_';
  7619. objcmetartti:
  7620. if not(oo_is_classhelper in objectoptions) then
  7621. result:=result+'_OBJC_METACLASS_'
  7622. else
  7623. internalerror(2009111511);
  7624. else
  7625. internalerror(2009092302);
  7626. end;
  7627. end;
  7628. odt_objcprotocol:
  7629. result:=result+'_OBJC_PROTOCOL_';
  7630. else
  7631. ;
  7632. end;
  7633. end
  7634. else
  7635. begin
  7636. case objecttype of
  7637. odt_objcclass:
  7638. begin
  7639. if (oo_is_classhelper in objectoptions) and
  7640. (rt<>objcclassrtti) then
  7641. internalerror(2009111512);
  7642. case rt of
  7643. objcclassrtti:
  7644. if not(oo_is_classhelper in objectoptions) then
  7645. result:='_OBJC_CLASS_$_'
  7646. else
  7647. result:='_OBJC_$_CATEGORY_';
  7648. objcmetartti:
  7649. result:='_OBJC_METACLASS_$_';
  7650. objcclassrortti:
  7651. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  7652. objcmetarortti:
  7653. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  7654. else
  7655. internalerror(2009092303);
  7656. end;
  7657. end;
  7658. odt_objcprotocol:
  7659. begin
  7660. result:=lower(target_asm.labelprefix);
  7661. case rt of
  7662. objcclassrtti:
  7663. result:=result+'_OBJC_PROTOCOL_$_';
  7664. objcmetartti:
  7665. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  7666. else
  7667. internalerror(2009092501);
  7668. end;
  7669. end;
  7670. else
  7671. internalerror(2013113005);
  7672. end;
  7673. end;
  7674. result:=result+objextname^;
  7675. end;
  7676. end;
  7677. function tobjectdef.is_unique_objpasdef: boolean;
  7678. begin
  7679. result:=
  7680. (df_unique in defoptions) and
  7681. is_class_or_interface_or_dispinterface(self)
  7682. end;
  7683. function tobjectdef.members_need_inittable : boolean;
  7684. begin
  7685. members_need_inittable:=tObjectSymtable(symtable).needs_init_final or
  7686. has_non_trivial_init_child(true);
  7687. end;
  7688. function tobjectdef.is_publishable : tpublishproperty;
  7689. begin
  7690. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  7691. is_publishable:=pp_publish
  7692. else
  7693. is_publishable:=pp_error;
  7694. end;
  7695. function tobjectdef.get_next_dispid: longint;
  7696. begin
  7697. inc(fcurrent_dispid);
  7698. result:=fcurrent_dispid;
  7699. end;
  7700. function tobjectdef.search_enumerator_get: tprocdef;
  7701. begin
  7702. result:=inherited;
  7703. if not assigned(result) and assigned(childof) then
  7704. result:=childof.search_enumerator_get;
  7705. end;
  7706. function tobjectdef.search_enumerator_move: tprocdef;
  7707. begin
  7708. result:=inherited;
  7709. if not assigned(result) and assigned(childof) then
  7710. result:=childof.search_enumerator_move;
  7711. end;
  7712. function tobjectdef.search_enumerator_current: tsym;
  7713. begin
  7714. result:=inherited;
  7715. if not assigned(result) and assigned(childof) then
  7716. result:=childof.search_enumerator_current;
  7717. end;
  7718. procedure tobjectdef.register_created_classref_type;
  7719. begin
  7720. if not classref_created_in_current_module then
  7721. begin
  7722. classref_created_in_current_module:=true;
  7723. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  7724. end;
  7725. end;
  7726. procedure tobjectdef.register_created_object_type;
  7727. begin
  7728. if not created_in_current_module then
  7729. begin
  7730. created_in_current_module:=true;
  7731. current_module.wpoinfo.addcreatedobjtype(self);
  7732. end;
  7733. end;
  7734. procedure tobjectdef.register_maybe_created_object_type;
  7735. begin
  7736. { if we know it has been created for sure, no need
  7737. to also record that it maybe can be created in
  7738. this module
  7739. }
  7740. if not (created_in_current_module) and
  7741. not (maybe_created_in_current_module) then
  7742. begin
  7743. maybe_created_in_current_module:=true;
  7744. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  7745. end;
  7746. end;
  7747. procedure tobjectdef.register_vmt_call(index: longint);
  7748. begin
  7749. if (is_object(self) or is_class(self)) then
  7750. current_module.wpoinfo.addcalledvmtentry(self,index);
  7751. end;
  7752. procedure check_and_finish_msg(data: tobject; arg: pointer);
  7753. var
  7754. def: tdef absolute data;
  7755. pd: tprocdef absolute data;
  7756. i,
  7757. paracount: longint;
  7758. begin
  7759. if (def.typ=procdef) then
  7760. begin
  7761. { add all messages also under a dummy name to the symtable in
  7762. which the objcclass/protocol/category is declared, so they can
  7763. be called via id.<name>
  7764. }
  7765. create_class_helper_for_procdef(pd,nil);
  7766. { we have to wait until now to set the mangled name because it
  7767. depends on the (possibly external) class name, which is defined
  7768. at the very end. }
  7769. if not(po_msgstr in pd.procoptions) then
  7770. begin
  7771. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  7772. { recover to avoid internalerror later on }
  7773. include(pd.procoptions,po_msgstr);
  7774. pd.messageinf.str:=stringdup('MissingDeclaration');
  7775. end;
  7776. { Mangled name is already set in case this is a copy of
  7777. another type. }
  7778. if not(po_has_mangledname in pd.procoptions) then
  7779. begin
  7780. { check whether the number of formal parameters is correct,
  7781. and whether they have valid Objective-C types }
  7782. paracount:=0;
  7783. for i:=1 to length(pd.messageinf.str^) do
  7784. if pd.messageinf.str^[i]=':' then
  7785. inc(paracount);
  7786. for i:=0 to pd.paras.count-1 do
  7787. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  7788. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  7789. dec(paracount);
  7790. if (paracount<>0) then
  7791. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  7792. pd.setmangledname(pd.objcmangledname);
  7793. end
  7794. else
  7795. { all checks already done }
  7796. exit;
  7797. if not(oo_is_external in pd.struct.objectoptions) then
  7798. begin
  7799. if (po_varargs in pd.procoptions) then
  7800. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  7801. else
  7802. begin
  7803. { check for "array of const" parameters }
  7804. for i:=0 to pd.parast.symlist.count-1 do
  7805. begin
  7806. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  7807. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  7808. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  7809. end;
  7810. end;
  7811. end;
  7812. end;
  7813. end;
  7814. procedure mark_private_fields_used(data: tobject; arg: pointer);
  7815. var
  7816. sym: tsym absolute data;
  7817. begin
  7818. if (sym.typ=fieldvarsym) and
  7819. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  7820. sym.IncRefCount;
  7821. end;
  7822. procedure tobjectdef.finish_objc_data;
  7823. begin
  7824. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  7825. if (oo_is_external in objectoptions) then
  7826. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  7827. end;
  7828. procedure verify_objc_vardef(data: tobject; arg: pointer);
  7829. var
  7830. sym: tabstractvarsym absolute data;
  7831. res: pboolean absolute arg;
  7832. founderrordef: tdef;
  7833. begin
  7834. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  7835. exit;
  7836. if (sym.typ=paravarsym) and
  7837. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  7838. is_array_of_const(tparavarsym(sym).vardef)) then
  7839. exit;
  7840. if not objcchecktype(sym.vardef,founderrordef) then
  7841. begin
  7842. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7843. res^:=false;
  7844. end;
  7845. end;
  7846. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  7847. var
  7848. def: tdef absolute data;
  7849. res: pboolean absolute arg;
  7850. founderrordef: tdef;
  7851. begin
  7852. if (def.typ<>procdef) then
  7853. exit;
  7854. { check parameter types for validity }
  7855. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  7856. { check the result type for validity }
  7857. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  7858. begin
  7859. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7860. res^:=false;
  7861. end;
  7862. end;
  7863. function tobjectdef.check_objc_types: boolean;
  7864. begin
  7865. { done in separate step from finish_objc_data, because when
  7866. finish_objc_data is called, not all forwarddefs have been resolved
  7867. yet and we need to know all types here }
  7868. result:=true;
  7869. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  7870. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  7871. end;
  7872. procedure do_cpp_import_info(data: tobject; arg: pointer);
  7873. var
  7874. def: tdef absolute data;
  7875. pd: tprocdef absolute data;
  7876. begin
  7877. if (def.typ=procdef) then
  7878. begin
  7879. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  7880. if (oo_is_external in pd.struct.objectoptions) then
  7881. begin
  7882. { copied from psub.read_proc }
  7883. if assigned(tobjectdef(pd.struct).import_lib) then
  7884. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  7885. else
  7886. begin
  7887. { add import name to external list for DLL scanning }
  7888. if tf_has_dllscanner in target_info.flags then
  7889. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  7890. end;
  7891. end;
  7892. end;
  7893. end;
  7894. procedure tobjectdef.finish_cpp_data;
  7895. begin
  7896. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  7897. end;
  7898. procedure tobjectdef.apply_rtti_directive(dir: trtti_directive);
  7899. begin
  7900. rtti.clause:=dir.clause;
  7901. rtti.options:=dir.options;
  7902. if (dir.clause=rtc_inherit) and assigned(childof) and (childof.rtti.clause<>rtc_none) then
  7903. begin
  7904. rtti.options[ro_methods]:=rtti.options[ro_methods]+childof.rtti.options[ro_methods];
  7905. rtti.options[ro_fields]:=rtti.options[ro_fields]+childof.rtti.options[ro_fields];
  7906. rtti.options[ro_properties]:=rtti.options[ro_properties]+childof.rtti.options[ro_properties];
  7907. end;
  7908. end;
  7909. {$ifdef DEBUG_NODE_XML}
  7910. function TObjectDef.XMLPrintType: ansistring;
  7911. begin
  7912. if (oo_is_forward in objectoptions) then
  7913. Result := '&lt;class prototype&gt;'
  7914. else
  7915. Result := '&lt;class&gt;';
  7916. end;
  7917. procedure TObjectDef.XMLPrintDefInfo(var T: Text; Sym: TSym);
  7918. var
  7919. i: TObjectOption;
  7920. first: Boolean;
  7921. begin
  7922. inherited XMLPrintDefInfo(T, Sym);
  7923. First := True;
  7924. for i := Low(TObjectOption) to High(TObjectOption) do
  7925. if i in objectoptions then
  7926. begin
  7927. if First then
  7928. begin
  7929. Write(T, ' objectoptions="', i);
  7930. First := False;
  7931. end
  7932. else
  7933. Write(T, ',', i)
  7934. end;
  7935. if not first then
  7936. Write(T, '"');
  7937. end;
  7938. procedure TObjectDef.XMLPrintDefData(var T: Text; Sym: TSym);
  7939. begin
  7940. { There's nothing useful yet if the type is only forward-declared }
  7941. if not (oo_is_forward in objectoptions) then
  7942. begin
  7943. if Assigned(childof) then
  7944. WriteLn(T, printnodeindention, '<ancestor>', SanitiseXMLString(childof.typesym.RealName), '</ancestor>');
  7945. inherited XMLPrintDefData(T, Sym);
  7946. end;
  7947. end;
  7948. {$endif DEBUG_NODE_XML}
  7949. {****************************************************************************
  7950. TImplementedInterface
  7951. ****************************************************************************}
  7952. function TImplementedInterface.GetIOffset: longint;
  7953. begin
  7954. if (fIOffset=-1) and
  7955. (IType in [etFieldValue,etFieldValueClass]) then
  7956. result:=tfieldvarsym(ImplementsField).fieldoffset
  7957. else
  7958. result:=fIOffset;
  7959. end;
  7960. constructor TImplementedInterface.create(aintf: tobjectdef);
  7961. begin
  7962. inherited create;
  7963. intfdef:=aintf;
  7964. intfdefderef.reset;
  7965. IOffset:=-1;
  7966. IType:=etStandard;
  7967. NameMappings:=nil;
  7968. procdefs:=nil;
  7969. end;
  7970. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  7971. begin
  7972. inherited create;
  7973. intfdef:=nil;
  7974. intfdefderef:=intfd;
  7975. ImplementsGetterDeref:=getterd;
  7976. IOffset:=-1;
  7977. IType:=etStandard;
  7978. NameMappings:=nil;
  7979. procdefs:=nil;
  7980. end;
  7981. destructor TImplementedInterface.destroy;
  7982. var
  7983. i : longint;
  7984. mappedname : pshortstring;
  7985. begin
  7986. if assigned(NameMappings) then
  7987. begin
  7988. for i:=0 to NameMappings.Count-1 do
  7989. begin
  7990. mappedname:=pshortstring(NameMappings[i]);
  7991. stringdispose(mappedname);
  7992. end;
  7993. NameMappings.free;
  7994. NameMappings:=nil;
  7995. end;
  7996. if assigned(procdefs) then
  7997. begin
  7998. procdefs.free;
  7999. procdefs:=nil;
  8000. end;
  8001. inherited destroy;
  8002. end;
  8003. procedure TImplementedInterface.buildderef;
  8004. begin
  8005. intfdefderef.build(intfdef);
  8006. ImplementsGetterDeref.build(ImplementsGetter);
  8007. end;
  8008. procedure TImplementedInterface.deref;
  8009. begin
  8010. intfdef:=tobjectdef(intfdefderef.resolve);
  8011. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  8012. end;
  8013. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  8014. begin
  8015. if not assigned(NameMappings) then
  8016. NameMappings:=TFPHashList.Create;
  8017. NameMappings.Add(origname,stringdup(newname));
  8018. end;
  8019. function TImplementedInterface.GetMapping(const origname: string):string;
  8020. var
  8021. mappedname : pshortstring;
  8022. begin
  8023. result:='';
  8024. if not assigned(NameMappings) then
  8025. exit;
  8026. mappedname:=PShortstring(NameMappings.Find(origname));
  8027. if assigned(mappedname) then
  8028. result:=mappedname^;
  8029. end;
  8030. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  8031. begin
  8032. if not assigned(procdefs) then
  8033. procdefs:=TFPObjectList.Create(false);
  8034. { duplicate entries must be stored, because multiple }
  8035. { interfaces can declare methods with the same name }
  8036. { and all of these get their own VMT entry }
  8037. procdefs.Add(pd);
  8038. end;
  8039. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  8040. var
  8041. i : longint;
  8042. begin
  8043. result:=false;
  8044. { interfaces being implemented through delegation are not mergable (FK) }
  8045. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  8046. exit;
  8047. weight:=0;
  8048. { empty interface is mergeable }
  8049. if ProcDefs.Count=0 then
  8050. begin
  8051. result:=true;
  8052. exit;
  8053. end;
  8054. { The interface to merge must at least the number of
  8055. procedures of this interface }
  8056. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  8057. exit;
  8058. for i:=0 to ProcDefs.Count-1 do
  8059. begin
  8060. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  8061. exit;
  8062. end;
  8063. weight:=ProcDefs.Count;
  8064. result:=true;
  8065. end;
  8066. function TImplementedInterface.getcopy:TImplementedInterface;
  8067. var
  8068. i : longint;
  8069. begin
  8070. Result:=TImplementedInterface.Create(nil);
  8071. { 1) the procdefs list will be freed once for each copy
  8072. 2) since the procdefs list owns its elements, those will also be freed for each copy
  8073. Nope: procdefs are owned by their symtable, so no copy necessary
  8074. 3) idem for the name mappings
  8075. }
  8076. result.fIOffset:=fIOffset;
  8077. result.IntfDef:=IntfDef;
  8078. result.IntfDefDeref.reset;
  8079. result.IType:=IType;
  8080. result.VtblImplIntf:=VtblImplIntf;
  8081. if assigned(NameMappings) then
  8082. begin
  8083. result.NameMappings:=TFPHashList.create;
  8084. for i:=0 to NameMappings.Count-1 do
  8085. Result.NameMappings.Add(NameMappings.NameOfIndex(i),
  8086. stringdup(pshortstring(NameMappings.Items[i])^));
  8087. end;
  8088. if assigned(ProcDefs) then
  8089. begin
  8090. result.ProcDefs:=TFPObjectList.create(false);
  8091. { Note: this is probably wrong, because those procdefs are owned by
  8092. the old objectdef from which we copy, what would be the correct way
  8093. of doing this is to lookup the equivalent copy in the new owner
  8094. and reference this instead... But this is complicated so let's try
  8095. it this way until it blows up ok? }
  8096. for i:=0 to ProcDefs.Count-1 do
  8097. Result.ProcDefs.add(tprocdef(procdefs[i]).getcopy);
  8098. end;
  8099. result.ImplementsGetter:=ImplementsGetter;
  8100. result.ImplementsGetterDeref.reset;
  8101. result.ImplementsField:=ImplementsField;
  8102. end;
  8103. {****************************************************************************
  8104. TFORWARDDEF
  8105. ****************************************************************************}
  8106. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  8107. begin
  8108. inherited create(forwarddef,true);
  8109. tosymname:=stringdup(s);
  8110. forwardpos:=pos;
  8111. end;
  8112. function tforwarddef.GetTypeName:string;
  8113. begin
  8114. GetTypeName:='unresolved forward to '+tosymname^;
  8115. end;
  8116. destructor tforwarddef.destroy;
  8117. begin
  8118. stringdispose(tosymname);
  8119. inherited destroy;
  8120. end;
  8121. function tforwarddef.getcopy:tstoreddef;
  8122. begin
  8123. result:=cforwarddef.create(tosymname^, forwardpos);
  8124. end;
  8125. {****************************************************************************
  8126. TUNDEFINEDDEF
  8127. ****************************************************************************}
  8128. constructor tundefineddef.create(doregister:boolean);
  8129. begin
  8130. inherited create(undefineddef,doregister);
  8131. end;
  8132. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  8133. begin
  8134. inherited ppuload(undefineddef,ppufile);
  8135. ppuload_platform(ppufile);
  8136. end;
  8137. function tundefineddef.GetTypeName:string;
  8138. begin
  8139. GetTypeName:='<undefined type>';
  8140. end;
  8141. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  8142. begin
  8143. inherited ppuwrite(ppufile);
  8144. writeentry(ppufile,ibundefineddef);
  8145. end;
  8146. {****************************************************************************
  8147. TERRORDEF
  8148. ****************************************************************************}
  8149. constructor terrordef.create;
  8150. begin
  8151. inherited create(errordef,true);
  8152. { prevent consecutive faults }
  8153. savesize:=1;
  8154. end;
  8155. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  8156. begin
  8157. { Can't write errordefs to ppu }
  8158. internalerror(200411063);
  8159. end;
  8160. function terrordef.GetTypeName:string;
  8161. begin
  8162. GetTypeName:='<erroneous type>';
  8163. end;
  8164. function terrordef.getmangledparaname:TSymStr;
  8165. begin
  8166. getmangledparaname:='error';
  8167. end;
  8168. {****************************************************************************
  8169. Definition Helpers
  8170. ****************************************************************************}
  8171. function is_interfacecom(def: tdef): boolean;
  8172. begin
  8173. is_interfacecom:=
  8174. assigned(def) and
  8175. (def.typ=objectdef) and
  8176. (tobjectdef(def).objecttype=odt_interfacecom);
  8177. end;
  8178. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  8179. begin
  8180. is_interfacecom_or_dispinterface:=
  8181. assigned(def) and
  8182. (def.typ=objectdef) and
  8183. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  8184. end;
  8185. function is_any_interface_kind(def: tdef): boolean;
  8186. begin
  8187. result:=
  8188. assigned(def) and
  8189. (def.typ=objectdef) and
  8190. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  8191. is_objccategory(def));
  8192. end;
  8193. function is_interfacecorba(def: tdef): boolean;
  8194. begin
  8195. is_interfacecorba:=
  8196. assigned(def) and
  8197. (def.typ=objectdef) and
  8198. (tobjectdef(def).objecttype=odt_interfacecorba);
  8199. end;
  8200. function is_interface(def: tdef): boolean;
  8201. begin
  8202. is_interface:=
  8203. assigned(def) and
  8204. (def.typ=objectdef) and
  8205. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  8206. end;
  8207. function is_dispinterface(def: tdef): boolean;
  8208. begin
  8209. result:=
  8210. assigned(def) and
  8211. (def.typ=objectdef) and
  8212. (tobjectdef(def).objecttype=odt_dispinterface);
  8213. end;
  8214. function is_class(def: tdef): boolean;
  8215. begin
  8216. is_class:=
  8217. assigned(def) and
  8218. (def.typ=objectdef) and
  8219. (tobjectdef(def).objecttype=odt_class);
  8220. end;
  8221. function is_object(def: tdef): boolean;
  8222. begin
  8223. is_object:=
  8224. assigned(def) and
  8225. (def.typ=objectdef) and
  8226. (tobjectdef(def).objecttype=odt_object);
  8227. end;
  8228. function is_cppclass(def: tdef): boolean;
  8229. begin
  8230. is_cppclass:=
  8231. assigned(def) and
  8232. (def.typ=objectdef) and
  8233. (tobjectdef(def).objecttype=odt_cppclass);
  8234. end;
  8235. function is_objcclass(def: tdef): boolean;
  8236. begin
  8237. is_objcclass:=
  8238. assigned(def) and
  8239. (def.typ=objectdef) and
  8240. (tobjectdef(def).objecttype=odt_objcclass);
  8241. end;
  8242. function is_objectpascal_helper(def: tdef): boolean;
  8243. begin
  8244. result:=
  8245. assigned(def) and
  8246. (def.typ=objectdef) and
  8247. (tobjectdef(def).objecttype=odt_helper);
  8248. end;
  8249. function is_objcclassref(def: tdef): boolean;
  8250. begin
  8251. is_objcclassref:=
  8252. assigned(def) and
  8253. (def.typ=classrefdef) and
  8254. is_objcclass(tclassrefdef(def).pointeddef);
  8255. end;
  8256. function is_objcprotocol(def: tdef): boolean;
  8257. begin
  8258. result:=
  8259. assigned(def) and
  8260. (def.typ=objectdef) and
  8261. (tobjectdef(def).objecttype=odt_objcprotocol);
  8262. end;
  8263. function is_objccategory(def: tdef): boolean;
  8264. begin
  8265. result:=
  8266. assigned(def) and
  8267. (def.typ=objectdef) and
  8268. { if used as a forward type }
  8269. ((tobjectdef(def).objecttype=odt_objccategory) or
  8270. { if used as after it has been resolved }
  8271. ((tobjectdef(def).objecttype=odt_objcclass) and
  8272. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  8273. end;
  8274. function is_objc_class_or_protocol(def: tdef): boolean;
  8275. begin
  8276. result:=
  8277. assigned(def) and
  8278. (def.typ=objectdef) and
  8279. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  8280. end;
  8281. function is_objc_protocol_or_category(def: tdef): boolean;
  8282. begin
  8283. result:=
  8284. assigned(def) and
  8285. (def.typ=objectdef) and
  8286. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  8287. ((tobjectdef(def).objecttype = odt_objcclass) and
  8288. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  8289. end;
  8290. function is_classhelper(def: tdef): boolean;
  8291. begin
  8292. result:=
  8293. is_objectpascal_helper(def) or
  8294. is_objccategory(def);
  8295. end;
  8296. function is_class_or_interface(def: tdef): boolean;
  8297. begin
  8298. result:=
  8299. assigned(def) and
  8300. (def.typ=objectdef) and
  8301. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  8302. end;
  8303. function is_class_or_interface_or_objc(def: tdef): boolean;
  8304. begin
  8305. result:=
  8306. assigned(def) and
  8307. (def.typ=objectdef) and
  8308. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  8309. end;
  8310. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  8311. begin
  8312. result:=
  8313. assigned(def) and
  8314. (def.typ=objectdef) and
  8315. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  8316. end;
  8317. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  8318. begin
  8319. result:=
  8320. assigned(def) and
  8321. (def.typ=objectdef) and
  8322. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  8323. end;
  8324. function is_class_or_interface_or_object(def: tdef): boolean;
  8325. begin
  8326. result:=
  8327. assigned(def) and
  8328. (def.typ=objectdef) and
  8329. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  8330. end;
  8331. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  8332. begin
  8333. result:=
  8334. assigned(def) and
  8335. (def.typ=objectdef) and
  8336. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  8337. end;
  8338. function is_implicit_pointer_object_type(def: tdef): boolean;
  8339. begin
  8340. result:=
  8341. assigned(def) and
  8342. (((def.typ=objectdef) and
  8343. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  8344. ((target_info.system in systems_jvm) and
  8345. (def.typ=recorddef)));
  8346. end;
  8347. function is_implicit_array_pointer(def: tdef): boolean;
  8348. begin
  8349. result:=is_dynamic_array(def) or is_dynamicstring(def);
  8350. end;
  8351. function is_class_or_object(def: tdef): boolean;
  8352. begin
  8353. result:=
  8354. assigned(def) and
  8355. (def.typ=objectdef) and
  8356. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  8357. end;
  8358. function is_record(def: tdef): boolean;
  8359. begin
  8360. result:=
  8361. assigned(def) and
  8362. (def.typ=recorddef);
  8363. end;
  8364. function is_javaclass(def: tdef): boolean;
  8365. begin
  8366. result:=
  8367. assigned(def) and
  8368. (def.typ=objectdef) and
  8369. (tobjectdef(def).objecttype=odt_javaclass);
  8370. end;
  8371. function is_javaclassref(def: tdef): boolean;
  8372. begin
  8373. is_javaclassref:=
  8374. assigned(def) and
  8375. (def.typ=classrefdef) and
  8376. is_javaclass(tclassrefdef(def).pointeddef);
  8377. end;
  8378. function is_javainterface(def: tdef): boolean;
  8379. begin
  8380. result:=
  8381. assigned(def) and
  8382. (def.typ=objectdef) and
  8383. (tobjectdef(def).objecttype=odt_interfacejava);
  8384. end;
  8385. function is_java_class_or_interface(def: tdef): boolean;
  8386. begin
  8387. result:=
  8388. assigned(def) and
  8389. (def.typ=objectdef) and
  8390. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  8391. end;
  8392. procedure loadobjctypes;
  8393. begin
  8394. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  8395. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  8396. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  8397. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  8398. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  8399. end;
  8400. procedure maybeloadcocoatypes;
  8401. var
  8402. tsym: ttypesym;
  8403. cocoaunit: string[15];
  8404. begin
  8405. if assigned(objc_fastenumeration) then
  8406. exit;
  8407. if not(target_info.system in [system_arm_ios,system_i386_iphonesim,system_aarch64_ios,system_x86_64_iphonesim,system_aarch64_iphonesim]) then
  8408. cocoaunit:='COCOAALL'
  8409. else
  8410. cocoaunit:='IPHONEALL';
  8411. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  8412. if assigned(tsym) then
  8413. objc_fastenumeration:=tobjectdef(tsym.typedef)
  8414. else
  8415. objc_fastenumeration:=nil;
  8416. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  8417. if assigned(tsym) then
  8418. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  8419. else
  8420. objc_fastenumerationstate:=nil;
  8421. end;
  8422. function use_vectorfpu(def : tdef) : boolean;
  8423. begin
  8424. {$ifdef x86}
  8425. {$define use_vectorfpuimplemented}
  8426. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  8427. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  8428. {$endif x86}
  8429. {$ifdef arm}
  8430. {$define use_vectorfpuimplemented}
  8431. use_vectorfpu:=FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype];
  8432. {$endif arm}
  8433. {$ifdef aarch64}
  8434. {$define use_vectorfpuimplemented}
  8435. use_vectorfpu:=true;
  8436. {$endif aarch64}
  8437. {$ifndef use_vectorfpuimplemented}
  8438. use_vectorfpu:=false;
  8439. {$endif}
  8440. end;
  8441. procedure reset_all_default_types;
  8442. { Those class pointers have to be set to nil manually }
  8443. { after memory they point to have been released. }
  8444. { Necessary strictly for Textmode IDE. }
  8445. begin
  8446. voidpointertype:=nil; { pointer for Void-pointeddef }
  8447. charpointertype:=nil; { pointer for Char-pointeddef }
  8448. widecharpointertype:=nil; { pointer for WideChar-pointeddef }
  8449. voidcodepointertype:=nil; { pointer to code; corresponds to System.CodePointer }
  8450. voidstackpointertype:=nil; { the pointer type used for accessing parameters and local vars on the stack }
  8451. parentfpvoidpointertype:=nil; { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  8452. {$ifdef x86}
  8453. voidnearpointertype:=nil;
  8454. voidnearcspointertype:=nil;
  8455. voidneardspointertype:=nil;
  8456. voidnearsspointertype:=nil;
  8457. voidnearespointertype:=nil;
  8458. voidnearfspointertype:=nil;
  8459. voidneargspointertype:=nil;
  8460. {$ifdef i8086}
  8461. voidfarpointertype:=nil;
  8462. voidhugepointertype:=nil;
  8463. charnearpointertype:=nil;
  8464. charfarpointertype:=nil;
  8465. charhugepointertype:=nil;
  8466. bytefarpointertype:=nil; { used for Mem[] }
  8467. wordfarpointertype:=nil; { used for MemW[] }
  8468. longintfarpointertype:=nil; { used for MemL[] }
  8469. {$endif i8086}
  8470. {$endif x86}
  8471. {$ifdef wasm}
  8472. wasmvoidexternreftype:=nil;
  8473. {$endif wasm}
  8474. cundefinedtype:=nil;
  8475. cformaltype:=nil; { unique formal definition }
  8476. ctypedformaltype:=nil; { unique typed formal definition }
  8477. voidtype:=nil; { Void (procedure) }
  8478. cansichartype:=nil; { Char }
  8479. cwidechartype:=nil; { WideChar }
  8480. cchartype:=nil; { either cansichartype or cwidechartype. Do not free }
  8481. pasbool1type:=nil; { boolean type }
  8482. pasbool8type:=nil;
  8483. pasbool16type:=nil;
  8484. pasbool32type:=nil;
  8485. pasbool64type:=nil;
  8486. bool8type:=nil;
  8487. bool16type:=nil;
  8488. bool32type:=nil;
  8489. bool64type:=nil;
  8490. {$ifdef llvm}
  8491. llvmbool1type:=nil; { LLVM i1 type }
  8492. {$endif llvm}
  8493. u8inttype:=nil; { 8-Bit unsigned integer }
  8494. s8inttype:=nil; { 8-Bit signed integer }
  8495. u16inttype:=nil; { 16-Bit unsigned integer }
  8496. s16inttype:=nil; { 16-Bit signed integer }
  8497. u24inttype:=nil; { 24-Bit unsigned integer }
  8498. s24inttype:=nil; { 24-Bit signed integer }
  8499. u32inttype:=nil; { 32-Bit unsigned integer }
  8500. s32inttype:=nil; { 32-Bit signed integer }
  8501. u40inttype:=nil; { 40-Bit unsigned integer }
  8502. s40inttype:=nil; { 40-Bit signed integer }
  8503. u48inttype:=nil; { 48-Bit unsigned integer }
  8504. s48inttype:=nil; { 48-Bit signed integer }
  8505. u56inttype:=nil; { 56-Bit unsigned integer }
  8506. s56inttype:=nil; { 56-Bit signed integer }
  8507. u64inttype:=nil; { 64-bit unsigned integer }
  8508. s64inttype:=nil; { 64-bit signed integer }
  8509. u128inttype:=nil; { 128-bit unsigned integer }
  8510. s128inttype:=nil; { 128-bit signed integer }
  8511. s32floattype:=nil; { 32 bit floating point number }
  8512. s64floattype:=nil; { 64 bit floating point number }
  8513. s80floattype:=nil; { 80 bit floating point number }
  8514. sc80floattype:=nil; { 80 bit floating point number but stored like in C }
  8515. s64currencytype:=nil; { pointer to a currency type }
  8516. cshortstringtype:=nil; { pointer to type of short string const }
  8517. clongstringtype:=nil; { pointer to type of long string const }
  8518. cansistringtype:=nil; { pointer to type of ansi string const }
  8519. cwidestringtype:=nil; { pointer to type of wide string const }
  8520. cunicodestringtype:=nil;
  8521. openshortstringtype:=nil;
  8522. openchararraytype:=nil;
  8523. cfiletype:=nil;
  8524. methodpointertype:=nil; { typecasting of methodpointers to extract self }
  8525. nestedprocpointertype:=nil; { typecasting of nestedprocpointers to extract parentfp }
  8526. hresultdef:=nil;
  8527. typekindtype:=nil; { def of TTypeKind for correct handling of GetTypeKind parameters }
  8528. { we use only one variant def for every variant class }
  8529. cvarianttype:=nil;
  8530. colevarianttype:=nil;
  8531. { default integer type, normally s32inttype on 32 bit systems and s64bittype on 64 bit systems }
  8532. sinttype:=nil;
  8533. uinttype:=nil;
  8534. { integer types corresponding to OS_SINT/OS_INT }
  8535. ossinttype:=nil;
  8536. osuinttype:=nil;
  8537. { integer types corresponding to the ALU size, sizeof(aint) and the ALUSInt/ALUUInt types in the system unit }
  8538. alusinttype:=nil;
  8539. aluuinttype:=nil;
  8540. { integer types corresponding to SizeInt and SizeUInt for the target platform }
  8541. sizeuinttype:=nil;
  8542. sizesinttype:=nil;
  8543. { unsigned and signed ord type with the same size as a pointer }
  8544. ptruinttype:=nil;
  8545. ptrsinttype:=nil;
  8546. { unsigned and signed ord type with the same size as a codepointer }
  8547. codeptruinttype:=nil;
  8548. codeptrsinttype:=nil;
  8549. { several types to simulate more or less C++ objects for GDB }
  8550. vmttype:=nil;
  8551. vmtarraytype:=nil;
  8552. { type of classrefs, used for stabs }
  8553. pvmttype:=nil;
  8554. { return type of the setjmp function }
  8555. exceptionreasontype:=nil;
  8556. class_tobject:=nil;
  8557. class_tcustomattribute:=nil;
  8558. interface_iunknown:=nil;
  8559. interface_idispatch:=nil;
  8560. rec_tguid:=nil;
  8561. rec_jmp_buf:=nil;
  8562. rec_exceptaddr:=nil;
  8563. objc_metaclasstype:=nil;
  8564. objc_superclasstype:=nil;
  8565. objc_idtype:=nil;
  8566. objc_seltype:=nil;
  8567. objc_objecttype:=nil;
  8568. objc_protocoltype:=nil;
  8569. objc_fastenumeration:=nil;
  8570. objc_fastenumerationstate:=nil;
  8571. {$ifdef llvm}
  8572. { llvm types }
  8573. { a unique def to identify any kind of metadata }
  8574. llvm_metadatatype:=nil;
  8575. {$endif llvm}
  8576. { Java base types }
  8577. java_jlobject:=nil;
  8578. java_jlthrowable:=nil;
  8579. java_fpcbaserecordtype:=nil;
  8580. java_jlstring:=nil;
  8581. java_jlenum:=nil;
  8582. java_juenumset:=nil;
  8583. java_jubitset:=nil;
  8584. java_ansistring:=nil;
  8585. java_shortstring:=nil;
  8586. java_procvarbase:=nil;
  8587. { x86 vector types }
  8588. x86_m64type:=nil;
  8589. x86_m128type:=nil;
  8590. x86_m128dtype:=nil;
  8591. x86_m128itype:=nil;
  8592. x86_m256type:=nil;
  8593. x86_m256dtype:=nil;
  8594. x86_m256itype:=nil;
  8595. end;
  8596. end.