symdef.pas 257 KB

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