symdef.pas 246 KB

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