symdef.pas 308 KB

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