symdef.pas 281 KB

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