symdef.pas 286 KB

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