symdef.pas 263 KB

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