symdef.pas 259 KB

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