symdef.pas 242 KB

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