symdef.pas 304 KB

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