symdef.pas 320 KB

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