symdef.pas 285 KB

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