symdef.pas 246 KB

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