symdef.pas 316 KB

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