symdef.pas 263 KB

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