symdef.pas 267 KB

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