symdef.pas 323 KB

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