symdef.pas 256 KB

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