symdef.pas 245 KB

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