symdef.pas 245 KB

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