symdef.pas 304 KB

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