symdef.pas 331 KB

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