symdef.pas 304 KB

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