symdef.pas 321 KB

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