symdef.pas 291 KB

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