symdef.pas 253 KB

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