symdef.pas 244 KB

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