symdef.pas 296 KB

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