symdef.pas 263 KB

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