symdef.pas 281 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382
  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. recorddef:
  2063. begin
  2064. {$ifdef llvm}
  2065. is_intregable:=false;
  2066. {$else llvm}
  2067. recsize:=size;
  2068. is_intregable:=
  2069. ispowerof2(recsize,temp) and
  2070. ((recsize<=sizeof(aint)*2) and
  2071. not trecorddef(self).contains_cross_aword_field and
  2072. { records cannot go into registers on 16 bit targets for now }
  2073. (sizeof(aint)>2) and
  2074. (not trecorddef(self).contains_float_field) or
  2075. (recsize <= sizeof(aint))
  2076. ) and
  2077. not needs_inittable;
  2078. {$endif llvm}
  2079. end;
  2080. else
  2081. is_intregable:=false;
  2082. end;
  2083. end;
  2084. function tstoreddef.is_fpuregable : boolean;
  2085. begin
  2086. {$ifdef x86}
  2087. result:=use_vectorfpu(self);
  2088. {$else x86}
  2089. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  2090. {$endif x86}
  2091. end;
  2092. function tstoreddef.is_const_intregable : boolean;
  2093. begin
  2094. case typ of
  2095. stringdef:
  2096. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  2097. arraydef:
  2098. result:=is_dynamic_array(self);
  2099. objectdef:
  2100. result:=is_interface(self);
  2101. else
  2102. result:=false;
  2103. end;
  2104. end;
  2105. procedure tstoreddef.initgeneric;
  2106. begin
  2107. if assigned(generictokenbuf) then
  2108. internalerror(200512131);
  2109. generictokenbuf:=tdynamicarray.create(256);
  2110. end;
  2111. function tstoreddef.is_generic: boolean;
  2112. var
  2113. sym: tsym;
  2114. i: longint;
  2115. begin
  2116. result:=assigned(genericparas) and
  2117. (genericparas.count>0) and
  2118. (df_generic in defoptions);
  2119. if result then
  2120. { if any of the type parameters does *not* belong to as (meaning it was passed
  2121. in from outside) then we aren't a generic, but a specialization }
  2122. for i:=0 to genericparas.count-1 do
  2123. begin
  2124. sym:=tsym(genericparas[i]);
  2125. if sym.typ<>symconst.typesym then
  2126. internalerror(2014050903);
  2127. if sym.owner.defowner<>self then
  2128. exit(false);
  2129. end;
  2130. end;
  2131. function tstoreddef.is_specialization: boolean;
  2132. var
  2133. i : longint;
  2134. sym : tsym;
  2135. begin
  2136. result:=assigned(genericparas) and
  2137. (genericparas.count>0) and
  2138. (df_specialization in defoptions);
  2139. if result then
  2140. begin
  2141. { if at least one of the generic parameters is not owned by us (meaning it was
  2142. passed in from outside) then we have a specialization, otherwise we have a generic }
  2143. for i:=0 to genericparas.count-1 do
  2144. begin
  2145. sym:=tsym(genericparas[i]);
  2146. if sym.typ<>symconst.typesym then
  2147. internalerror(2014050904);
  2148. if sym.owner.defowner<>self then
  2149. exit(true);
  2150. end;
  2151. result:=false;
  2152. end;
  2153. end;
  2154. procedure tstoreddef.register_def;
  2155. var
  2156. gst : tgetsymtable;
  2157. st : tsymtable;
  2158. begin
  2159. if registered then
  2160. exit;
  2161. { Register in current_module }
  2162. if assigned(current_module) then
  2163. begin
  2164. exclude(defoptions,df_not_registered_no_free);
  2165. for gst:=low(tgetsymtable) to high(tgetsymtable) do
  2166. begin
  2167. st:=getsymtable(gst);
  2168. if assigned(st) then
  2169. tstoredsymtable(st).register_children;
  2170. end;
  2171. if defid<defid_not_registered then
  2172. defid:=deflist_index
  2173. else
  2174. begin
  2175. current_module.deflist.Add(self);
  2176. defid:=current_module.deflist.Count-1;
  2177. end;
  2178. maybe_put_in_symtable_stack;
  2179. end
  2180. else
  2181. DefId:=defid_registered_nost;
  2182. end;
  2183. procedure tstoreddef.maybe_put_in_symtable_stack;
  2184. var
  2185. insertstack: psymtablestackitem;
  2186. begin
  2187. if assigned(symtablestack) and
  2188. not assigned(self.owner) then
  2189. begin
  2190. insertstack:=symtablestack.stack;
  2191. { don't insert defs in exception symtables, as they are freed before
  2192. the module is compiled, so we can get crashes on high level targets
  2193. if they still need it while e.g. writing assembler code }
  2194. while assigned(insertstack) and
  2195. (insertstack^.symtable.symtabletype in [stt_exceptsymtable,withsymtable]) do
  2196. insertstack:=insertstack^.next;
  2197. if not assigned(insertstack) then
  2198. internalerror(200602044);
  2199. if insertstack^.symtable.sealed then
  2200. internalerror(2015022301);
  2201. insertstack^.symtable.insertdef(self);
  2202. end;
  2203. end;
  2204. {****************************************************************************
  2205. Tstringdef
  2206. ****************************************************************************}
  2207. constructor tstringdef.createshort(l: byte; doregister: boolean);
  2208. begin
  2209. inherited create(stringdef,doregister);
  2210. stringtype:=st_shortstring;
  2211. encoding:=0;
  2212. len:=l;
  2213. end;
  2214. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  2215. begin
  2216. inherited ppuload(stringdef,ppufile);
  2217. stringtype:=st_shortstring;
  2218. encoding:=0;
  2219. len:=ppufile.getbyte;
  2220. ppuload_platform(ppufile);
  2221. end;
  2222. constructor tstringdef.createlong(l: asizeint; doregister: boolean);
  2223. begin
  2224. inherited create(stringdef,doregister);
  2225. stringtype:=st_longstring;
  2226. encoding:=0;
  2227. len:=l;
  2228. end;
  2229. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  2230. begin
  2231. inherited ppuload(stringdef,ppufile);
  2232. stringtype:=st_longstring;
  2233. encoding:=0;
  2234. len:=ppufile.getasizeint;
  2235. ppuload_platform(ppufile);
  2236. end;
  2237. constructor tstringdef.createansi(aencoding: tstringencoding; doregister: boolean);
  2238. begin
  2239. inherited create(stringdef,doregister);
  2240. stringtype:=st_ansistring;
  2241. encoding:=aencoding;
  2242. len:=-1;
  2243. end;
  2244. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  2245. begin
  2246. inherited ppuload(stringdef,ppufile);
  2247. stringtype:=st_ansistring;
  2248. len:=ppufile.getasizeint;
  2249. encoding:=ppufile.getword;
  2250. ppuload_platform(ppufile);
  2251. end;
  2252. constructor tstringdef.createwide(doregister: boolean);
  2253. begin
  2254. inherited create(stringdef,doregister);
  2255. stringtype:=st_widestring;
  2256. if target_info.endian=endian_little then
  2257. encoding:=CP_UTF16LE
  2258. else
  2259. encoding:=CP_UTF16BE;
  2260. len:=-1;
  2261. end;
  2262. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2263. begin
  2264. inherited ppuload(stringdef,ppufile);
  2265. stringtype:=st_widestring;
  2266. if target_info.endian=endian_little then
  2267. encoding:=CP_UTF16LE
  2268. else
  2269. encoding:=CP_UTF16BE;
  2270. len:=ppufile.getasizeint;
  2271. ppuload_platform(ppufile);
  2272. end;
  2273. constructor tstringdef.createunicode(doregister: boolean);
  2274. begin
  2275. inherited create(stringdef,doregister);
  2276. stringtype:=st_unicodestring;
  2277. if target_info.endian=endian_little then
  2278. encoding:=CP_UTF16LE
  2279. else
  2280. encoding:=CP_UTF16BE;
  2281. len:=-1;
  2282. end;
  2283. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2284. begin
  2285. inherited ppuload(stringdef,ppufile);
  2286. stringtype:=st_unicodestring;
  2287. len:=ppufile.getasizeint;
  2288. encoding:=ppufile.getword;
  2289. ppuload_platform(ppufile);
  2290. end;
  2291. function tstringdef.getcopy : tstoreddef;
  2292. begin
  2293. result:=cstringdef.create(typ,true);
  2294. result.typ:=stringdef;
  2295. tstringdef(result).stringtype:=stringtype;
  2296. tstringdef(result).encoding:=encoding;
  2297. tstringdef(result).len:=len;
  2298. end;
  2299. function tstringdef.stringtypname:string;
  2300. const
  2301. typname:array[tstringtype] of string[10]=(
  2302. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2303. );
  2304. begin
  2305. stringtypname:=typname[stringtype];
  2306. end;
  2307. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2308. begin
  2309. inherited ppuwrite(ppufile);
  2310. if stringtype=st_shortstring then
  2311. begin
  2312. {$ifdef extdebug}
  2313. if len > 255 then internalerror(12122002);
  2314. {$endif}
  2315. ppufile.putbyte(byte(len))
  2316. end
  2317. else
  2318. ppufile.putasizeint(len);
  2319. if stringtype in [st_ansistring,st_unicodestring] then
  2320. ppufile.putword(encoding);
  2321. case stringtype of
  2322. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2323. st_longstring : writeentry(ppufile,iblongstringdef);
  2324. st_ansistring : writeentry(ppufile,ibansistringdef);
  2325. st_widestring : writeentry(ppufile,ibwidestringdef);
  2326. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2327. end;
  2328. end;
  2329. function tstringdef.needs_inittable : boolean;
  2330. begin
  2331. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2332. end;
  2333. function tstringdef.GetTypeName : string;
  2334. const
  2335. names : array[tstringtype] of string[15] = (
  2336. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2337. begin
  2338. GetTypeName:=names[stringtype];
  2339. end;
  2340. function tstringdef.getvardef : longint;
  2341. const
  2342. vardef : array[tstringtype] of longint = (
  2343. varUndefined,varUndefined,varString,varOleStr,varUString);
  2344. begin
  2345. result:=vardef[stringtype];
  2346. end;
  2347. function tstringdef.alignment : shortint;
  2348. begin
  2349. case stringtype of
  2350. st_unicodestring,
  2351. st_widestring,
  2352. st_ansistring:
  2353. alignment:=voidpointertype.alignment;
  2354. st_longstring,
  2355. st_shortstring:
  2356. { char to string accesses byte 0 and 1 with one word access }
  2357. if (tf_requires_proper_alignment in target_info.flags) or
  2358. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2359. (m_mac in current_settings.modeswitches) then
  2360. alignment:=size_2_align(2)
  2361. else
  2362. alignment:=size_2_align(1);
  2363. end;
  2364. end;
  2365. function tstringdef.getmangledparaname : TSymStr;
  2366. begin
  2367. getmangledparaname:='STRING';
  2368. end;
  2369. function tstringdef.is_publishable : boolean;
  2370. begin
  2371. is_publishable:=true;
  2372. end;
  2373. function tstringdef.size: asizeint;
  2374. begin
  2375. case stringtype of
  2376. st_shortstring:
  2377. Result:=len+1;
  2378. st_longstring,
  2379. st_ansistring,
  2380. st_widestring,
  2381. st_unicodestring:
  2382. Result:=voidpointertype.size;
  2383. end;
  2384. end;
  2385. {****************************************************************************
  2386. TENUMDEF
  2387. ****************************************************************************}
  2388. constructor tenumdef.create;
  2389. begin
  2390. inherited create(enumdef,true);
  2391. minval:=0;
  2392. maxval:=0;
  2393. calcsavesize(current_settings.packenum);
  2394. has_jumps:=false;
  2395. basedef:=nil;
  2396. basedefderef.reset;
  2397. symtable:=tenumsymtable.create(self);
  2398. end;
  2399. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2400. begin
  2401. inherited create(enumdef,true);
  2402. minval:=_min;
  2403. maxval:=_max;
  2404. basedef:=_basedef;
  2405. calcsavesize(current_settings.packenum);
  2406. has_jumps:=false;
  2407. symtable:=basedef.symtable.getcopy;
  2408. include(defoptions, df_copied_def);
  2409. end;
  2410. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2411. begin
  2412. inherited ppuload(enumdef,ppufile);
  2413. minval:=ppufile.getaint;
  2414. maxval:=ppufile.getaint;
  2415. savesize:=ppufile.getaint;
  2416. has_jumps:=false;
  2417. if df_copied_def in defoptions then
  2418. begin
  2419. symtable:=nil;
  2420. ppufile.getderef(basedefderef);
  2421. ppuload_platform(ppufile);
  2422. end
  2423. else
  2424. begin
  2425. ppuload_platform(ppufile);
  2426. // create with nil defowner first to prevent values changes on insert
  2427. symtable:=tenumsymtable.create(nil);
  2428. tenumsymtable(symtable).ppuload(ppufile);
  2429. symtable.defowner:=self;
  2430. end;
  2431. end;
  2432. destructor tenumdef.destroy;
  2433. begin
  2434. symtable.free;
  2435. symtable:=nil;
  2436. inherited destroy;
  2437. end;
  2438. function tenumdef.getcopy : tstoreddef;
  2439. begin
  2440. if assigned(basedef) then
  2441. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2442. else
  2443. begin
  2444. result:=cenumdef.create;
  2445. tenumdef(result).minval:=minval;
  2446. tenumdef(result).maxval:=maxval;
  2447. tenumdef(result).symtable.free;
  2448. tenumdef(result).symtable:=symtable.getcopy;
  2449. tenumdef(result).basedef:=self;
  2450. end;
  2451. tenumdef(result).has_jumps:=has_jumps;
  2452. tenumdef(result).basedefderef:=basedefderef;
  2453. include(tenumdef(result).defoptions,df_copied_def);
  2454. end;
  2455. procedure tenumdef.calcsavesize(packenum: shortint);
  2456. begin
  2457. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2458. if (packenum=8) or (int64(min)<low(longint)) or (int64(max)>high(cardinal)) then
  2459. savesize:=8
  2460. {$IFNDEF cpu64bitaddr} {$pop} {$ENDIF}
  2461. else
  2462. {$IFDEF cpu16bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2463. if (packenum=4) or (min<low(smallint)) or (max>high(word)) then
  2464. savesize:=4
  2465. {$IFDEF cpu16bitaddr} {$pop} {$ENDIF}
  2466. else
  2467. if (packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  2468. savesize:=2
  2469. else
  2470. savesize:=1;
  2471. end;
  2472. function tenumdef.packedbitsize: asizeint;
  2473. var
  2474. sizeval: tconstexprint;
  2475. power: longint;
  2476. begin
  2477. result := 0;
  2478. if (minval >= 0) and
  2479. (maxval <= 1) then
  2480. result := 1
  2481. else
  2482. begin
  2483. if (minval>=0) then
  2484. sizeval:=maxval
  2485. else
  2486. { don't count 0 twice, but take into account that range goes from -n-1..n }
  2487. sizeval:=(cutils.max(-minval,maxval+1)*2)-1;
  2488. { 256 must become 512 etc. }
  2489. nextpowerof2(sizeval+1,power);
  2490. result := power;
  2491. end;
  2492. end;
  2493. procedure tenumdef.setmax(_max:asizeint);
  2494. begin
  2495. maxval:=_max;
  2496. calcsavesize(current_settings.packenum);
  2497. end;
  2498. procedure tenumdef.setmin(_min:asizeint);
  2499. begin
  2500. minval:=_min;
  2501. calcsavesize(current_settings.packenum);
  2502. end;
  2503. function tenumdef.min:asizeint;
  2504. begin
  2505. min:=minval;
  2506. end;
  2507. function tenumdef.max:asizeint;
  2508. begin
  2509. max:=maxval;
  2510. end;
  2511. function tenumdef.getfirstsym: tsym;
  2512. var
  2513. i:integer;
  2514. begin
  2515. for i := 0 to symtable.SymList.Count - 1 do
  2516. begin
  2517. result:=tsym(symtable.SymList[i]);
  2518. if tenumsym(result).value=minval then
  2519. exit;
  2520. end;
  2521. result:=nil;
  2522. end;
  2523. function tenumdef.int2enumsym(l: asizeint): tsym;
  2524. var
  2525. i: longint;
  2526. sym: tsym;
  2527. bdef: tenumdef;
  2528. begin
  2529. result:=nil;
  2530. if (l<minval) or
  2531. (l>maxval) then
  2532. exit;
  2533. bdef:=getbasedef;
  2534. for i:=0 to bdef.symtable.symlist.count-1 do
  2535. begin
  2536. sym:=tsym(bdef.symtable.symlist[i]);
  2537. if (sym.typ=enumsym) and
  2538. (tenumsym(sym).value=l) then
  2539. begin
  2540. result:=sym;
  2541. exit;
  2542. end;
  2543. end;
  2544. end;
  2545. function tenumdef.getbasedef: tenumdef;
  2546. begin
  2547. if not assigned(basedef) then
  2548. result:=self
  2549. else
  2550. result:=basedef;
  2551. end;
  2552. procedure tenumdef.buildderef;
  2553. begin
  2554. inherited buildderef;
  2555. if df_copied_def in defoptions then
  2556. basedefderef.build(basedef)
  2557. else
  2558. tenumsymtable(symtable).buildderef;
  2559. end;
  2560. procedure tenumdef.deref;
  2561. begin
  2562. inherited deref;
  2563. if df_copied_def in defoptions then
  2564. begin
  2565. basedef:=tenumdef(basedefderef.resolve);
  2566. symtable:=basedef.symtable.getcopy;
  2567. end
  2568. else
  2569. tenumsymtable(symtable).deref(false);
  2570. end;
  2571. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2572. begin
  2573. inherited ppuwrite(ppufile);
  2574. ppufile.putaint(min);
  2575. ppufile.putaint(max);
  2576. ppufile.putaint(savesize);
  2577. if df_copied_def in defoptions then
  2578. ppufile.putderef(basedefderef);
  2579. writeentry(ppufile,ibenumdef);
  2580. if not (df_copied_def in defoptions) then
  2581. tenumsymtable(symtable).ppuwrite(ppufile);
  2582. end;
  2583. function tenumdef.is_publishable : boolean;
  2584. begin
  2585. is_publishable:=true;
  2586. end;
  2587. function tenumdef.GetTypeName : string;
  2588. begin
  2589. GetTypeName:='<enumeration type>';
  2590. end;
  2591. {****************************************************************************
  2592. TORDDEF
  2593. ****************************************************************************}
  2594. constructor torddef.create(t : tordtype;v,b : TConstExprInt; doregister: boolean);
  2595. begin
  2596. inherited create(orddef,doregister);
  2597. low:=v;
  2598. high:=b;
  2599. ordtype:=t;
  2600. setsize;
  2601. end;
  2602. constructor torddef.ppuload(ppufile:tcompilerppufile);
  2603. begin
  2604. inherited ppuload(orddef,ppufile);
  2605. ordtype:=tordtype(ppufile.getbyte);
  2606. low:=ppufile.getexprint;
  2607. high:=ppufile.getexprint;
  2608. setsize;
  2609. ppuload_platform(ppufile);
  2610. end;
  2611. function torddef.getcopy : tstoreddef;
  2612. begin
  2613. result:=corddef.create(ordtype,low,high,true);
  2614. result.typ:=orddef;
  2615. torddef(result).low:=low;
  2616. torddef(result).high:=high;
  2617. torddef(result).ordtype:=ordtype;
  2618. torddef(result).savesize:=savesize;
  2619. end;
  2620. function torddef.alignment:shortint;
  2621. begin
  2622. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  2623. (ordtype in [s64bit,u64bit]) then
  2624. result := 4
  2625. else
  2626. result := inherited alignment;
  2627. end;
  2628. procedure torddef.setsize;
  2629. const
  2630. sizetbl : array[tordtype] of longint = (
  2631. 0,
  2632. 1,2,4,8,16,
  2633. 1,2,4,8,16,
  2634. 1,1,2,4,8,
  2635. 1,2,4,8,
  2636. 1,2,8,system.high(longint)
  2637. );
  2638. begin
  2639. savesize:=sizetbl[ordtype];
  2640. if savesize=system.high(longint) then
  2641. savesize:=packedbitsize div 8;
  2642. end;
  2643. function torddef.packedbitsize: asizeint;
  2644. var
  2645. sizeval: tconstexprint;
  2646. power: longint;
  2647. begin
  2648. result := 0;
  2649. if ordtype = uvoid then
  2650. exit;
  2651. {$ifndef cpu64bitalu}
  2652. if (ordtype in [s64bit,u64bit]) then
  2653. {$else not cpu64bitalu}
  2654. if (ordtype = u64bit) or
  2655. ((ordtype = s64bit) and
  2656. ((low <= (system.low(int64) div 2)) or
  2657. (high > (system.high(int64) div 2)))) then
  2658. {$endif cpu64bitalu}
  2659. result := 64
  2660. else if (
  2661. (low >= 0) and
  2662. (high <= 1)
  2663. ) or (
  2664. ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]
  2665. ) then
  2666. result := 1
  2667. else
  2668. begin
  2669. if (low>=0) then
  2670. sizeval:=high
  2671. else
  2672. { don't count 0 twice, but take into account that range goes from -n-1..n }
  2673. sizeval:=(cutils.max(-low,high+1)*2)-1;
  2674. { 256 must become 512 etc. }
  2675. nextpowerof2(sizeval+1,power);
  2676. result := power;
  2677. end;
  2678. end;
  2679. function torddef.getvardef : longint;
  2680. const
  2681. basetype2vardef : array[tordtype] of longint = (
  2682. varUndefined,
  2683. varbyte,varword,varlongword,varqword,varUndefined,
  2684. varshortint,varsmallint,varinteger,varint64,varUndefined,
  2685. varboolean,varboolean,varboolean,varboolean,varboolean,
  2686. varboolean,varboolean,varUndefined,varUndefined,
  2687. varUndefined,varUndefined,varCurrency,varEmpty);
  2688. begin
  2689. result:=basetype2vardef[ordtype];
  2690. if result=varEmpty then
  2691. result:=basetype2vardef[range_to_basetype(low,high)];
  2692. end;
  2693. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  2694. begin
  2695. inherited ppuwrite(ppufile);
  2696. ppufile.putbyte(byte(ordtype));
  2697. ppufile.putexprint(low);
  2698. ppufile.putexprint(high);
  2699. writeentry(ppufile,iborddef);
  2700. end;
  2701. function torddef.is_publishable : boolean;
  2702. begin
  2703. is_publishable:=(ordtype<>uvoid);
  2704. end;
  2705. function torddef.GetTypeName : string;
  2706. const
  2707. names : array[tordtype] of string[20] = (
  2708. 'untyped',
  2709. 'Byte','Word','DWord','QWord','UInt128',
  2710. 'ShortInt','SmallInt','LongInt','Int64','Int128',
  2711. 'Boolean','Boolean8','Boolean16','Boolean32','Boolean64',
  2712. 'ByteBool','WordBool','LongBool','QWordBool',
  2713. 'Char','WideChar','Currency','CustomRange');
  2714. begin
  2715. GetTypeName:=names[ordtype];
  2716. end;
  2717. {****************************************************************************
  2718. TFLOATDEF
  2719. ****************************************************************************}
  2720. constructor tfloatdef.create(t: tfloattype; doregister: boolean);
  2721. begin
  2722. inherited create(floatdef,doregister);
  2723. floattype:=t;
  2724. setsize;
  2725. end;
  2726. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  2727. begin
  2728. inherited ppuload(floatdef,ppufile);
  2729. floattype:=tfloattype(ppufile.getbyte);
  2730. setsize;
  2731. ppuload_platform(ppufile);
  2732. end;
  2733. function tfloatdef.getcopy : tstoreddef;
  2734. begin
  2735. result:=cfloatdef.create(floattype,true);
  2736. result.typ:=floatdef;
  2737. tfloatdef(result).savesize:=savesize;
  2738. end;
  2739. function tfloatdef.alignment:shortint;
  2740. begin
  2741. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  2742. case floattype of
  2743. sc80real,
  2744. s80real: result:=16;
  2745. s64real,
  2746. s64currency,
  2747. s64comp : result:=4;
  2748. else
  2749. result := inherited alignment;
  2750. end
  2751. else
  2752. result := inherited alignment;
  2753. end;
  2754. function tfloatdef.structalignment: shortint;
  2755. begin
  2756. { aix is really annoying: the recommended scalar alignment for both
  2757. int64 and double is 64 bits, but in structs int64 has to be aligned
  2758. to 8 bytes and double to 4 bytes }
  2759. if (target_info.system in systems_aix) and
  2760. (floattype=s64real) then
  2761. result:=4
  2762. else
  2763. result:=alignment;
  2764. end;
  2765. procedure tfloatdef.setsize;
  2766. begin
  2767. case floattype of
  2768. s32real : savesize:=4;
  2769. s80real : savesize:=10;
  2770. sc80real:
  2771. if target_info.system in [system_i386_darwin,
  2772. system_i386_iphonesim,system_x86_64_darwin,
  2773. system_x86_64_iphonesim,
  2774. system_x86_64_linux,system_x86_64_freebsd,
  2775. system_x86_64_openbsd,system_x86_64_netbsd,
  2776. system_x86_64_solaris,system_x86_64_embedded,
  2777. system_x86_64_dragonfly] then
  2778. savesize:=16
  2779. else
  2780. savesize:=12;
  2781. s64real,
  2782. s64currency,
  2783. s64comp : savesize:=8;
  2784. else
  2785. savesize:=0;
  2786. end;
  2787. end;
  2788. function tfloatdef.getvardef : longint;
  2789. const
  2790. floattype2vardef : array[tfloattype] of longint = (
  2791. varSingle,varDouble,varUndefined,varUndefined,
  2792. varUndefined,varCurrency,varUndefined);
  2793. begin
  2794. if (upper(typename)='TDATETIME') and
  2795. assigned(owner) and
  2796. assigned(owner.name) and
  2797. (owner.name^='SYSTEM') then
  2798. result:=varDate
  2799. else
  2800. result:=floattype2vardef[floattype];
  2801. end;
  2802. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  2803. begin
  2804. inherited ppuwrite(ppufile);
  2805. ppufile.putbyte(byte(floattype));
  2806. writeentry(ppufile,ibfloatdef);
  2807. end;
  2808. function tfloatdef.is_publishable : boolean;
  2809. begin
  2810. is_publishable:=true;
  2811. end;
  2812. function tfloatdef.GetTypeName : string;
  2813. const
  2814. names : array[tfloattype] of string[20] = (
  2815. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  2816. begin
  2817. GetTypeName:=names[floattype];
  2818. end;
  2819. {****************************************************************************
  2820. TFILEDEF
  2821. ****************************************************************************}
  2822. constructor tfiledef.createtext;
  2823. begin
  2824. inherited create(filedef,true);
  2825. filetyp:=ft_text;
  2826. typedfiledef:=nil;
  2827. typedfiledefderef.reset;
  2828. end;
  2829. constructor tfiledef.createuntyped;
  2830. begin
  2831. inherited create(filedef,true);
  2832. filetyp:=ft_untyped;
  2833. typedfiledef:=nil;
  2834. end;
  2835. constructor tfiledef.createtyped(def:tdef);
  2836. begin
  2837. inherited create(filedef,true);
  2838. filetyp:=ft_typed;
  2839. typedfiledef:=def;
  2840. end;
  2841. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2842. begin
  2843. inherited ppuload(filedef,ppufile);
  2844. filetyp:=tfiletyp(ppufile.getbyte);
  2845. if filetyp=ft_typed then
  2846. ppufile.getderef(typedfiledefderef)
  2847. else
  2848. typedfiledef:=nil;
  2849. ppuload_platform(ppufile);
  2850. end;
  2851. function tfiledef.getcopy : tstoreddef;
  2852. begin
  2853. case filetyp of
  2854. ft_typed:
  2855. result:=cfiledef.createtyped(typedfiledef);
  2856. ft_untyped:
  2857. result:=cfiledef.createuntyped;
  2858. ft_text:
  2859. result:=cfiledef.createtext;
  2860. end;
  2861. end;
  2862. procedure tfiledef.buildderef;
  2863. begin
  2864. inherited buildderef;
  2865. if filetyp=ft_typed then
  2866. typedfiledefderef.build(typedfiledef);
  2867. end;
  2868. procedure tfiledef.deref;
  2869. begin
  2870. inherited deref;
  2871. if filetyp=ft_typed then
  2872. typedfiledef:=tdef(typedfiledefderef.resolve);
  2873. end;
  2874. function tfiledef.size:asizeint;
  2875. begin
  2876. if savesize=0 then
  2877. setsize;
  2878. size:=savesize;
  2879. end;
  2880. procedure tfiledef.setsize;
  2881. begin
  2882. case filetyp of
  2883. ft_text:
  2884. savesize:=search_system_type('TEXTREC').typedef.size;
  2885. ft_typed:
  2886. begin
  2887. savesize:=search_system_type('FILEREC').typedef.size;
  2888. { allocate put/get buffer in iso mode }
  2889. if m_isolike_io in current_settings.modeswitches then
  2890. inc(savesize,typedfiledef.size);
  2891. end;
  2892. ft_untyped:
  2893. savesize:=search_system_type('FILEREC').typedef.size;
  2894. end;
  2895. end;
  2896. function tfiledef.alignment: shortint;
  2897. begin
  2898. case filetyp of
  2899. ft_text:
  2900. result:=search_system_type('TEXTREC').typedef.alignment;
  2901. ft_typed,
  2902. ft_untyped:
  2903. result:=search_system_type('FILEREC').typedef.alignment;
  2904. end;
  2905. end;
  2906. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2907. begin
  2908. inherited ppuwrite(ppufile);
  2909. ppufile.putbyte(byte(filetyp));
  2910. if filetyp=ft_typed then
  2911. ppufile.putderef(typedfiledefderef);
  2912. writeentry(ppufile,ibfiledef);
  2913. end;
  2914. function tfiledef.GetTypeName : string;
  2915. begin
  2916. case filetyp of
  2917. ft_untyped:
  2918. GetTypeName:='File';
  2919. ft_typed:
  2920. GetTypeName:='File Of '+typedfiledef.typename;
  2921. ft_text:
  2922. GetTypeName:='Text'
  2923. end;
  2924. end;
  2925. function tfiledef.getmangledparaname : TSymStr;
  2926. begin
  2927. case filetyp of
  2928. ft_untyped:
  2929. getmangledparaname:='FILE';
  2930. ft_typed:
  2931. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2932. ft_text:
  2933. getmangledparaname:='TEXT'
  2934. end;
  2935. end;
  2936. {****************************************************************************
  2937. TVARIANTDEF
  2938. ****************************************************************************}
  2939. constructor tvariantdef.create(v : tvarianttype);
  2940. begin
  2941. inherited create(variantdef,true);
  2942. varianttype:=v;
  2943. setsize;
  2944. end;
  2945. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2946. begin
  2947. inherited ppuload(variantdef,ppufile);
  2948. varianttype:=tvarianttype(ppufile.getbyte);
  2949. setsize;
  2950. ppuload_platform(ppufile);
  2951. end;
  2952. function tvariantdef.getcopy : tstoreddef;
  2953. begin
  2954. result:=cvariantdef.create(varianttype);
  2955. end;
  2956. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2957. begin
  2958. inherited ppuwrite(ppufile);
  2959. ppufile.putbyte(byte(varianttype));
  2960. writeentry(ppufile,ibvariantdef);
  2961. end;
  2962. function tvariantdef.getvardef : longint;
  2963. begin
  2964. Result:=varVariant;
  2965. end;
  2966. procedure tvariantdef.setsize;
  2967. begin
  2968. {$ifdef cpu64bitaddr}
  2969. savesize:=24;
  2970. {$else cpu64bitaddr}
  2971. savesize:=16;
  2972. {$endif cpu64bitaddr}
  2973. end;
  2974. function tvariantdef.GetTypeName : string;
  2975. begin
  2976. case varianttype of
  2977. vt_normalvariant:
  2978. GetTypeName:='Variant';
  2979. vt_olevariant:
  2980. GetTypeName:='OleVariant';
  2981. end;
  2982. end;
  2983. function tvariantdef.needs_inittable : boolean;
  2984. begin
  2985. needs_inittable:=true;
  2986. end;
  2987. function tvariantdef.is_publishable : boolean;
  2988. begin
  2989. is_publishable:=true;
  2990. end;
  2991. {****************************************************************************
  2992. TABSTRACtpointerdef
  2993. ****************************************************************************}
  2994. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2995. begin
  2996. inherited create(dt,true);
  2997. pointeddef:=def;
  2998. pointeddefderef.reset;
  2999. if df_generic in pointeddef.defoptions then
  3000. include(defoptions,df_generic);
  3001. if df_specialization in pointeddef.defoptions then
  3002. include(defoptions,df_specialization);
  3003. end;
  3004. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3005. begin
  3006. inherited ppuload(dt,ppufile);
  3007. ppufile.getderef(pointeddefderef);
  3008. end;
  3009. procedure tabstractpointerdef.buildderef;
  3010. begin
  3011. inherited buildderef;
  3012. pointeddefderef.build(pointeddef);
  3013. end;
  3014. procedure tabstractpointerdef.deref;
  3015. begin
  3016. inherited deref;
  3017. pointeddef:=tdef(pointeddefderef.resolve);
  3018. end;
  3019. function tabstractpointerdef.size: asizeint;
  3020. begin
  3021. Result:=voidpointertype.size;
  3022. end;
  3023. function tabstractpointerdef.alignment: shortint;
  3024. begin
  3025. alignment:=size_2_align(voidpointertype.size);
  3026. end;
  3027. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3028. begin
  3029. inherited ppuwrite(ppufile);
  3030. ppufile.putderef(pointeddefderef);
  3031. end;
  3032. {****************************************************************************
  3033. tpointerdef
  3034. ****************************************************************************}
  3035. constructor tpointerdef.create(def:tdef);
  3036. begin
  3037. inherited create(pointerdef,def);
  3038. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  3039. if df_specialization in tstoreddef(def).defoptions then
  3040. genericdef:=cpointerdef.getreusable(tstoreddef(def).genericdef);
  3041. end;
  3042. class function tpointerdef.getreusable(def: tdef): tpointerdef;
  3043. var
  3044. res: PHashSetItem;
  3045. oldsymtablestack: tsymtablestack;
  3046. begin
  3047. if not assigned(current_module) then
  3048. internalerror(2011071101);
  3049. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  3050. if not assigned(res^.Data) then
  3051. begin
  3052. { since these pointerdefs can be reused anywhere in the current
  3053. unit, add them to the global/staticsymtable (or local symtable
  3054. if they're a local def, because otherwise they'll be saved
  3055. to the ppu referencing a local symtable entry that doesn't
  3056. exist in the ppu) }
  3057. oldsymtablestack:=symtablestack;
  3058. { do not simply push/pop current_module.localsymtable, because
  3059. that can have side-effects (e.g., it removes helpers) }
  3060. symtablestack:=nil;
  3061. result:=cpointerdef.create(def);
  3062. setup_reusable_def(def,result,res,oldsymtablestack);
  3063. { res^.Data may still be nil -> don't overwrite result }
  3064. exit;
  3065. end;
  3066. result:=tpointerdef(res^.Data);
  3067. end;
  3068. class function tpointerdef.getreusable_no_free(def: tdef): tpointerdef;
  3069. begin
  3070. result:=getreusable(def);
  3071. if not result.is_registered then
  3072. include(result.defoptions,df_not_registered_no_free);
  3073. end;
  3074. function tpointerdef.size: asizeint;
  3075. begin
  3076. result:=sizeof(pint);
  3077. end;
  3078. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  3079. begin
  3080. inherited ppuload(pointerdef,ppufile);
  3081. has_pointer_math:=(ppufile.getbyte<>0);
  3082. ppuload_platform(ppufile);
  3083. end;
  3084. function tpointerdef.getcopy : tstoreddef;
  3085. begin
  3086. { don't use direct pointeddef if it is a forwarddef because in other case
  3087. one of them will be destroyed on forward type resolve and the second will
  3088. point to garbage }
  3089. if pointeddef.typ=forwarddef then
  3090. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  3091. else
  3092. result:=cpointerdef.create(pointeddef);
  3093. tpointerdef(result).has_pointer_math:=has_pointer_math;
  3094. tpointerdef(result).savesize:=savesize;
  3095. end;
  3096. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3097. begin
  3098. inherited ppuwrite(ppufile);
  3099. ppufile.putbyte(byte(has_pointer_math));
  3100. writeentry(ppufile,ibpointerdef);
  3101. end;
  3102. function tpointerdef.GetTypeName : string;
  3103. begin
  3104. { parameter types and the resultdef of a procvardef can contain a
  3105. pointer to this procvardef itself, resulting in endless recursion ->
  3106. use the typesym's name instead if it exists (if it doesn't, such as
  3107. for anynonymous procedure types in macpas/iso mode, then there cannot
  3108. be any recursive references to it either) }
  3109. if (pointeddef.typ<>procvardef) or
  3110. not assigned(pointeddef.typesym) then
  3111. GetTypeName:='^'+pointeddef.typename
  3112. else
  3113. GetTypeName:='^'+pointeddef.typesym.realname;
  3114. end;
  3115. function tpointerdef.pointer_arithmetic_int_type:tdef;
  3116. begin
  3117. result:=ptrsinttype;
  3118. end;
  3119. function tpointerdef.pointer_arithmetic_uint_type:tdef;
  3120. begin
  3121. result:=ptruinttype;
  3122. end;
  3123. function tpointerdef.pointer_subtraction_result_type:tdef;
  3124. begin
  3125. result:=ptrsinttype;
  3126. end;
  3127. function tpointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  3128. begin
  3129. result:=true;
  3130. end;
  3131. function tpointerdef.converted_pointer_to_array_range_type:tdef;
  3132. begin
  3133. result:=ptrsinttype;
  3134. end;
  3135. {****************************************************************************
  3136. TCLASSREFDEF
  3137. ****************************************************************************}
  3138. constructor tclassrefdef.create(def:tdef);
  3139. begin
  3140. inherited create(classrefdef,def);
  3141. if df_specialization in tstoreddef(def).defoptions then
  3142. genericdef:=cclassrefdef.create(tstoreddef(def).genericdef);
  3143. end;
  3144. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  3145. begin
  3146. inherited ppuload(classrefdef,ppufile);
  3147. ppuload_platform(ppufile);
  3148. end;
  3149. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  3150. begin
  3151. inherited ppuwrite(ppufile);
  3152. writeentry(ppufile,ibclassrefdef);
  3153. end;
  3154. function tclassrefdef.getcopy:tstoreddef;
  3155. begin
  3156. if pointeddef.typ=forwarddef then
  3157. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  3158. else
  3159. result:=cclassrefdef.create(pointeddef);
  3160. end;
  3161. function tclassrefdef.GetTypeName : string;
  3162. begin
  3163. GetTypeName:='Class Of '+pointeddef.typename;
  3164. end;
  3165. function tclassrefdef.is_publishable : boolean;
  3166. begin
  3167. result:=true;
  3168. end;
  3169. function tclassrefdef.rtti_mangledname(rt: trttitype): TSymStr;
  3170. begin
  3171. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  3172. result:=inherited rtti_mangledname(rt)
  3173. else
  3174. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  3175. end;
  3176. procedure tclassrefdef.register_created_object_type;
  3177. begin
  3178. tobjectdef(pointeddef).register_created_classref_type;
  3179. end;
  3180. {***************************************************************************
  3181. TSETDEF
  3182. ***************************************************************************}
  3183. constructor tsetdef.create(def: tdef; low, high: asizeint; doregister: boolean);
  3184. var
  3185. setallocbits: aint;
  3186. packedsavesize: aint;
  3187. actual_setalloc: ShortInt;
  3188. begin
  3189. inherited create(setdef,doregister);
  3190. elementdef:=def;
  3191. elementdefderef.reset;
  3192. setmax:=high;
  3193. actual_setalloc:=current_settings.setalloc;
  3194. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  3195. if actual_setalloc=0 then
  3196. actual_setalloc:=1;
  3197. {$endif}
  3198. if (actual_setalloc=0) then
  3199. begin
  3200. setbase:=0;
  3201. if (high<32) then
  3202. savesize:=Sizeof(longint)
  3203. else if (high<256) then
  3204. savesize:=32
  3205. else
  3206. savesize:=(high+7) div 8
  3207. end
  3208. else
  3209. begin
  3210. setallocbits:=actual_setalloc*8;
  3211. setbase:=low and not(setallocbits-1);
  3212. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  3213. savesize:=packedsavesize;
  3214. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  3215. if savesize=3 then
  3216. savesize:=4;
  3217. {$endif}
  3218. end;
  3219. end;
  3220. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  3221. begin
  3222. inherited ppuload(setdef,ppufile);
  3223. ppufile.getderef(elementdefderef);
  3224. savesize:=ppufile.getasizeint;
  3225. setbase:=ppufile.getasizeint;
  3226. setmax:=ppufile.getasizeint;
  3227. ppuload_platform(ppufile);
  3228. end;
  3229. function tsetdef.getcopy : tstoreddef;
  3230. begin
  3231. result:=csetdef.create(elementdef,setbase,setmax,true);
  3232. { the copy might have been created with a different setalloc setting }
  3233. tsetdef(result).savesize:=savesize;
  3234. end;
  3235. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  3236. begin
  3237. inherited ppuwrite(ppufile);
  3238. ppufile.putderef(elementdefderef);
  3239. ppufile.putasizeint(savesize);
  3240. ppufile.putasizeint(setbase);
  3241. ppufile.putasizeint(setmax);
  3242. writeentry(ppufile,ibsetdef);
  3243. end;
  3244. procedure tsetdef.buildderef;
  3245. begin
  3246. inherited buildderef;
  3247. elementdefderef.build(elementdef);
  3248. end;
  3249. procedure tsetdef.deref;
  3250. begin
  3251. inherited deref;
  3252. elementdef:=tdef(elementdefderef.resolve);
  3253. end;
  3254. function tsetdef.is_publishable : boolean;
  3255. begin
  3256. is_publishable:=savesize in [1,2,4];
  3257. end;
  3258. function tsetdef.alignment: shortint;
  3259. begin
  3260. Result:=inherited;
  3261. if result>sizeof(aint) then
  3262. result:=sizeof(aint);
  3263. end;
  3264. function tsetdef.GetTypeName : string;
  3265. begin
  3266. if assigned(elementdef) then
  3267. GetTypeName:='Set Of '+elementdef.typename
  3268. else
  3269. GetTypeName:='Empty Set';
  3270. end;
  3271. {***************************************************************************
  3272. TFORMALDEF
  3273. ***************************************************************************}
  3274. constructor tformaldef.create(Atyped:boolean);
  3275. begin
  3276. inherited create(formaldef,true);
  3277. typed:=Atyped;
  3278. savesize:=0;
  3279. end;
  3280. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  3281. begin
  3282. inherited ppuload(formaldef,ppufile);
  3283. typed:=boolean(ppufile.getbyte);
  3284. savesize:=0;
  3285. ppuload_platform(ppufile);
  3286. end;
  3287. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  3288. begin
  3289. inherited ppuwrite(ppufile);
  3290. ppufile.putbyte(byte(typed));
  3291. writeentry(ppufile,ibformaldef);
  3292. end;
  3293. function tformaldef.GetTypeName : string;
  3294. begin
  3295. if typed then
  3296. GetTypeName:='<Typed formal type>'
  3297. else
  3298. GetTypeName:='<Formal type>';
  3299. end;
  3300. {***************************************************************************
  3301. TARRAYDEF
  3302. ***************************************************************************}
  3303. constructor tarraydef.create(l,h:asizeint;def:tdef);
  3304. begin
  3305. inherited create(arraydef,true);
  3306. lowrange:=l;
  3307. highrange:=h;
  3308. rangedef:=def;
  3309. rangedefderef.reset;
  3310. _elementdef:=nil;
  3311. _elementdefderef.reset;
  3312. arrayoptions:=[];
  3313. symtable:=tarraysymtable.create(self);
  3314. end;
  3315. constructor tarraydef.create_openarray;
  3316. begin
  3317. self.create(0,-1,sizesinttype)
  3318. end;
  3319. class function tarraydef.getreusable(def: tdef; elems: asizeint): tarraydef;
  3320. var
  3321. res: PHashSetItem;
  3322. oldsymtablestack: tsymtablestack;
  3323. arrdesc: packed record
  3324. def: tdef;
  3325. elecount: asizeint;
  3326. end;
  3327. begin
  3328. if not assigned(current_module) then
  3329. internalerror(2011081301);
  3330. arrdesc.def:=def;
  3331. arrdesc.elecount:=elems;
  3332. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  3333. if not assigned(res^.Data) then
  3334. begin
  3335. { since these pointerdefs can be reused anywhere in the current
  3336. unit, add them to the global/staticsymtable (or local symtable
  3337. if they're a local def, because otherwise they'll be saved
  3338. to the ppu referencing a local symtable entry that doesn't
  3339. exist in the ppu) }
  3340. oldsymtablestack:=symtablestack;
  3341. { do not simply push/pop current_module.localsymtable, because
  3342. that can have side-effects (e.g., it removes helpers) }
  3343. symtablestack:=nil;
  3344. result:=carraydef.create(0,elems-1,sizesinttype);
  3345. result.elementdef:=def;
  3346. setup_reusable_def(def,result,res,oldsymtablestack);
  3347. { res^.Data may still be nil -> don't overwrite result }
  3348. exit;
  3349. end;
  3350. result:=tarraydef(res^.Data);
  3351. end;
  3352. class function tarraydef.getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  3353. begin
  3354. result:=getreusable(def,elems);
  3355. if not result.is_registered then
  3356. include(result.defoptions,df_not_registered_no_free);
  3357. end;
  3358. destructor tarraydef.destroy;
  3359. begin
  3360. symtable.free;
  3361. symtable:=nil;
  3362. inherited;
  3363. end;
  3364. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3365. begin
  3366. { divide by the element size and do -1 so the array will have a valid size,
  3367. further, the element size might be 0 e.g. for empty records, so use max(...,1)
  3368. to avoid a division by zero }
  3369. self.create(0,(high(asizeint) div max(def.pointeddef.size,1))-1,
  3370. def.converted_pointer_to_array_range_type);
  3371. arrayoptions:=[ado_IsConvertedPointer];
  3372. setelementdef(def.pointeddef);
  3373. end;
  3374. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3375. begin
  3376. inherited ppuload(arraydef,ppufile);
  3377. { the addresses are calculated later }
  3378. ppufile.getderef(_elementdefderef);
  3379. ppufile.getderef(rangedefderef);
  3380. lowrange:=ppufile.getasizeint;
  3381. highrange:=ppufile.getasizeint;
  3382. ppufile.getsmallset(arrayoptions);
  3383. ppuload_platform(ppufile);
  3384. symtable:=tarraysymtable.create(self);
  3385. tarraysymtable(symtable).ppuload(ppufile)
  3386. end;
  3387. function tarraydef.getcopy : tstoreddef;
  3388. begin
  3389. result:=carraydef.create(lowrange,highrange,rangedef);
  3390. tarraydef(result).arrayoptions:=arrayoptions;
  3391. tarraydef(result)._elementdef:=_elementdef;
  3392. end;
  3393. procedure tarraydef.buildderef;
  3394. begin
  3395. inherited buildderef;
  3396. tarraysymtable(symtable).buildderef;
  3397. _elementdefderef.build(_elementdef);
  3398. rangedefderef.build(rangedef);
  3399. end;
  3400. procedure tarraydef.deref;
  3401. begin
  3402. inherited deref;
  3403. tarraysymtable(symtable).deref(false);
  3404. _elementdef:=tdef(_elementdefderef.resolve);
  3405. rangedef:=tdef(rangedefderef.resolve);
  3406. end;
  3407. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3408. begin
  3409. inherited ppuwrite(ppufile);
  3410. ppufile.putderef(_elementdefderef);
  3411. ppufile.putderef(rangedefderef);
  3412. ppufile.putasizeint(lowrange);
  3413. ppufile.putasizeint(highrange);
  3414. ppufile.putsmallset(arrayoptions);
  3415. writeentry(ppufile,ibarraydef);
  3416. tarraysymtable(symtable).ppuwrite(ppufile);
  3417. end;
  3418. function tarraydef.elesize : asizeint;
  3419. begin
  3420. if (ado_IsBitPacked in arrayoptions) then
  3421. internalerror(2006080101);
  3422. if assigned(_elementdef) then
  3423. result:=_elementdef.size
  3424. else
  3425. result:=0;
  3426. end;
  3427. function tarraydef.elepackedbitsize : asizeint;
  3428. begin
  3429. if not(ado_IsBitPacked in arrayoptions) then
  3430. internalerror(2006080102);
  3431. if assigned(_elementdef) then
  3432. result:=_elementdef.packedbitsize
  3433. else
  3434. result:=0;
  3435. end;
  3436. function tarraydef.elecount : asizeuint;
  3437. var
  3438. qhigh,qlow : qword;
  3439. begin
  3440. if ado_IsDynamicArray in arrayoptions then
  3441. begin
  3442. result:=0;
  3443. exit;
  3444. end;
  3445. if (highrange>0) and (lowrange<0) then
  3446. begin
  3447. qhigh:=highrange;
  3448. qlow:=qword(-lowrange);
  3449. { prevent overflow, return 0 to indicate overflow }
  3450. if qhigh+qlow>qword(high(asizeint)-1) then
  3451. result:=0
  3452. else
  3453. result:=qhigh+qlow+1;
  3454. end
  3455. else
  3456. result:=int64(highrange)-lowrange+1;
  3457. end;
  3458. function tarraydef.size : asizeint;
  3459. var
  3460. cachedelecount : asizeuint;
  3461. cachedelesize : asizeint;
  3462. begin
  3463. if ado_IsDynamicArray in arrayoptions then
  3464. begin
  3465. size:=voidpointertype.size;
  3466. exit;
  3467. end;
  3468. { Tarraydef.size may never be called for an open array! }
  3469. if highrange<lowrange then
  3470. internalerror(99080501);
  3471. if not (ado_IsBitPacked in arrayoptions) then
  3472. cachedelesize:=elesize
  3473. else
  3474. cachedelesize := elepackedbitsize;
  3475. cachedelecount:=elecount;
  3476. if (cachedelesize = 0) then
  3477. begin
  3478. size := 0;
  3479. exit;
  3480. end;
  3481. if (cachedelecount = 0) then
  3482. begin
  3483. size := -1;
  3484. exit;
  3485. end;
  3486. { prevent overflow, return -1 to indicate overflow }
  3487. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3488. if (cachedelecount > asizeuint(high(asizeint))) or
  3489. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3490. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3491. accessing the array, see ncgmem (PFV) }
  3492. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3493. begin
  3494. result:=-1;
  3495. exit;
  3496. end;
  3497. result:=cachedelesize*asizeint(cachedelecount);
  3498. if (ado_IsBitPacked in arrayoptions) then
  3499. { can't just add 7 and divide by 8, because that may overflow }
  3500. result:=result div 8 + ord((result mod 8)<>0);
  3501. {$ifdef cpu16bitaddr}
  3502. if result>65535 then
  3503. begin
  3504. result:=-1;
  3505. exit;
  3506. end;
  3507. {$endif cpu16bitaddr}
  3508. end;
  3509. procedure tarraydef.setelementdef(def:tdef);
  3510. begin
  3511. _elementdef:=def;
  3512. if not(
  3513. (ado_IsDynamicArray in arrayoptions) or
  3514. (ado_IsConvertedPointer in arrayoptions) or
  3515. (ado_IsConstructor in arrayoptions) or
  3516. (highrange<lowrange)
  3517. ) and
  3518. (size=-1) then
  3519. Message(sym_e_segment_too_large);
  3520. end;
  3521. function tarraydef.alignment : shortint;
  3522. begin
  3523. { alignment of dyn. arrays doesn't depend on the element size }
  3524. if (ado_IsDynamicArray in arrayoptions) then
  3525. alignment:=voidpointertype.alignment
  3526. { alignment is the target alignment for the used load size }
  3527. else if (ado_IsBitPacked in arrayoptions) and
  3528. (elementdef.typ in [enumdef,orddef]) then
  3529. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3530. { alignment is the alignment of the elements }
  3531. else
  3532. alignment:=elementdef.alignment
  3533. end;
  3534. function tarraydef.needs_inittable : boolean;
  3535. begin
  3536. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3537. end;
  3538. function tarraydef.needs_separate_initrtti : boolean;
  3539. begin
  3540. if ado_IsBitPacked in arrayoptions then
  3541. result:=false
  3542. else
  3543. result:=elementdef.needs_separate_initrtti;
  3544. end;
  3545. function tarraydef.GetTypeName : string;
  3546. begin
  3547. if (ado_IsConstString in arrayoptions) then
  3548. result:='Constant String'
  3549. else if (ado_isarrayofconst in arrayoptions) or
  3550. (ado_isConstructor in arrayoptions) then
  3551. begin
  3552. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3553. GetTypeName:='Array Of Const'
  3554. else
  3555. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3556. end
  3557. else if (ado_IsDynamicArray in arrayoptions) then
  3558. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3559. else if ((highrange=-1) and (lowrange=0)) then
  3560. GetTypeName:='{Open} Array Of '+elementdef.typename
  3561. else
  3562. begin
  3563. result := '';
  3564. if (ado_IsBitPacked in arrayoptions) then
  3565. result:='BitPacked ';
  3566. if rangedef.typ=enumdef then
  3567. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3568. else
  3569. result:=result+'Array['+tostr(lowrange)+'..'+
  3570. tostr(highrange)+'] Of '+elementdef.typename
  3571. end;
  3572. end;
  3573. function tarraydef.getmangledparaname : TSymStr;
  3574. begin
  3575. if ado_isarrayofconst in arrayoptions then
  3576. getmangledparaname:='array_of_const'
  3577. else
  3578. if ((highrange=-1) and (lowrange=0)) then
  3579. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3580. else
  3581. internalerror(200204176);
  3582. end;
  3583. function tarraydef.is_publishable : boolean;
  3584. begin
  3585. Result:=ado_IsDynamicArray in arrayoptions;
  3586. end;
  3587. {***************************************************************************
  3588. tabstractrecorddef
  3589. ***************************************************************************}
  3590. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp;doregister:boolean);
  3591. begin
  3592. inherited create(dt,doregister);
  3593. objname:=stringdup(upper(n));
  3594. objrealname:=stringdup(n);
  3595. objectoptions:=[];
  3596. if assigned(current_module.namespace) then
  3597. begin
  3598. import_lib:=stringdup(current_module.namespace^);
  3599. replace(import_lib^,'.','/');
  3600. end;
  3601. end;
  3602. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3603. begin
  3604. inherited ppuload(dt,ppufile);
  3605. objrealname:=ppufile.getpshortstring;
  3606. objname:=stringdup(upper(objrealname^));
  3607. import_lib:=ppufile.getpshortstring;
  3608. { only used for external C++ classes and Java classes/records }
  3609. if (import_lib^='') then
  3610. stringdispose(import_lib);
  3611. ppufile.getsmallset(objectoptions);
  3612. end;
  3613. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  3614. begin
  3615. inherited ppuwrite(ppufile);
  3616. ppufile.putstring(objrealname^);
  3617. if assigned(import_lib) then
  3618. ppufile.putstring(import_lib^)
  3619. else
  3620. ppufile.putstring('');
  3621. ppufile.putsmallset(objectoptions);
  3622. end;
  3623. destructor tabstractrecorddef.destroy;
  3624. begin
  3625. stringdispose(objname);
  3626. stringdispose(objrealname);
  3627. stringdispose(import_lib);
  3628. tcinitcode.free;
  3629. inherited destroy;
  3630. end;
  3631. procedure tabstractrecorddef.buildderefimpl;
  3632. begin
  3633. inherited buildderefimpl;
  3634. if not (df_copied_def in defoptions) then
  3635. tstoredsymtable(symtable).buildderefimpl;
  3636. end;
  3637. procedure tabstractrecorddef.derefimpl;
  3638. begin
  3639. inherited derefimpl;
  3640. if not (df_copied_def in defoptions) then
  3641. tstoredsymtable(symtable).derefimpl(false);
  3642. end;
  3643. procedure tabstractrecorddef.check_forwards;
  3644. begin
  3645. { the defs of a copied def are defined for the original type only }
  3646. if not(df_copied_def in defoptions) then
  3647. tstoredsymtable(symtable).check_forwards;
  3648. end;
  3649. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3650. var
  3651. i: longint;
  3652. sym: tsym;
  3653. begin
  3654. for i:=0 to symtable.SymList.Count-1 do
  3655. begin
  3656. sym:=tsym(symtable.SymList[i]);
  3657. if sym.typ=procsym then
  3658. begin
  3659. result:=tprocsym(sym).find_procdef_bytype(pt);
  3660. if assigned(result) then
  3661. exit;
  3662. end;
  3663. end;
  3664. result:=nil;
  3665. end;
  3666. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  3667. begin
  3668. if t=gs_record then
  3669. GetSymtable:=symtable
  3670. else
  3671. GetSymtable:=nil;
  3672. end;
  3673. function tabstractrecorddef.is_packed:boolean;
  3674. begin
  3675. result:=tabstractrecordsymtable(symtable).is_packed;
  3676. end;
  3677. function tabstractrecorddef.RttiName: string;
  3678. function generate_full_paramname(maxlength:longint):string;
  3679. const
  3680. commacount : array[boolean] of longint = (0,1);
  3681. var
  3682. fullparas,
  3683. paramname : ansistring;
  3684. module : tmodule;
  3685. sym : ttypesym;
  3686. i : longint;
  3687. begin
  3688. { we want at least enough space for an ellipsis }
  3689. if maxlength<3 then
  3690. internalerror(2014121203);
  3691. fullparas:='';
  3692. for i:=0 to genericparas.count-1 do
  3693. begin
  3694. sym:=ttypesym(genericparas[i]);
  3695. module:=find_module_from_symtable(sym.owner);
  3696. if not assigned(module) then
  3697. internalerror(2014121202);
  3698. paramname:=module.realmodulename^;
  3699. if sym.typedef.typ in [objectdef,recorddef] then
  3700. paramname:=paramname+'.'+tabstractrecorddef(sym.typedef).rttiname
  3701. else
  3702. paramname:=paramname+'.'+sym.typedef.typename;
  3703. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  3704. begin
  3705. if i>0 then
  3706. fullparas:=fullparas+',...'
  3707. else
  3708. fullparas:=fullparas+'...';
  3709. break;
  3710. end;
  3711. { could we fit an ellipsis after this parameter if it should be too long? }
  3712. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  3713. begin
  3714. { then omit already this parameter }
  3715. if i>0 then
  3716. fullparas:=fullparas+',...'
  3717. else
  3718. fullparas:=fullparas+'...';
  3719. break;
  3720. end;
  3721. if i>0 then
  3722. fullparas:=fullparas+',';
  3723. fullparas:=fullparas+paramname;
  3724. end;
  3725. result:=fullparas;
  3726. end;
  3727. var
  3728. nongeneric,
  3729. basename : string;
  3730. i,
  3731. remlength,
  3732. paramcount,
  3733. crcidx : longint;
  3734. begin
  3735. if rttistring='' then
  3736. begin
  3737. if is_specialization then
  3738. begin
  3739. rttistring:=OwnerHierarchyName;
  3740. { there should be two $ characters, one before the CRC and one before the count }
  3741. crcidx:=-1;
  3742. for i:=length(objrealname^) downto 1 do
  3743. if objrealname^[i]='$' then
  3744. begin
  3745. crcidx:=i;
  3746. break;
  3747. end;
  3748. if crcidx<0 then
  3749. internalerror(2014121201);
  3750. basename:=copy(objrealname^,1,crcidx-1);
  3751. split_generic_name(basename,nongeneric,paramcount);
  3752. rttistring:=rttistring+nongeneric+'<';
  3753. remlength:=255-length(rttistring)-1;
  3754. if remlength<4 then
  3755. rttistring:=rttistring+'>'
  3756. else
  3757. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  3758. end
  3759. else
  3760. if is_generic then
  3761. begin
  3762. rttistring:=OwnerHierarchyName;
  3763. split_generic_name(objrealname^,nongeneric,paramcount);
  3764. rttistring:=rttistring+nongeneric+'<';
  3765. { we don't want any ',' if there is only one parameter }
  3766. for i:=0 to paramcount-0 do
  3767. rttistring:=rttistring+',';
  3768. rttistring:=rttistring+'>';
  3769. end
  3770. else
  3771. rttistring:=OwnerHierarchyName+objrealname^;
  3772. end;
  3773. result:=rttistring;
  3774. end;
  3775. function tabstractrecorddef.search_enumerator_get: tprocdef;
  3776. var
  3777. sym : tsym;
  3778. i : integer;
  3779. pd : tprocdef;
  3780. hashedid : THashedIDString;
  3781. begin
  3782. result:=nil;
  3783. hashedid.id:='GETENUMERATOR';
  3784. sym:=tsym(symtable.FindWithHash(hashedid));
  3785. if assigned(sym) and (sym.typ=procsym) then
  3786. begin
  3787. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3788. begin
  3789. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3790. if (pd.proctypeoption = potype_function) and
  3791. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  3792. (pd.visibility >= vis_public) then
  3793. begin
  3794. result:=pd;
  3795. exit;
  3796. end;
  3797. end;
  3798. end;
  3799. end;
  3800. function tabstractrecorddef.search_enumerator_move: tprocdef;
  3801. var
  3802. sym : tsym;
  3803. i : integer;
  3804. pd : tprocdef;
  3805. hashedid : THashedIDString;
  3806. begin
  3807. result:=nil;
  3808. // first search for po_enumerator_movenext method modifier
  3809. // then search for public function MoveNext: Boolean
  3810. for i:=0 to symtable.SymList.Count-1 do
  3811. begin
  3812. sym:=TSym(symtable.SymList[i]);
  3813. if (sym.typ=procsym) then
  3814. begin
  3815. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3816. if assigned(pd) then
  3817. begin
  3818. result:=pd;
  3819. exit;
  3820. end;
  3821. end;
  3822. end;
  3823. hashedid.id:='MOVENEXT';
  3824. sym:=tsym(symtable.FindWithHash(hashedid));
  3825. if assigned(sym) and (sym.typ=procsym) then
  3826. begin
  3827. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  3828. begin
  3829. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3830. if (pd.proctypeoption = potype_function) and
  3831. is_boolean(pd.returndef) and
  3832. (pd.minparacount = 0) and
  3833. (pd.visibility >= vis_public) then
  3834. begin
  3835. result:=pd;
  3836. exit;
  3837. end;
  3838. end;
  3839. end;
  3840. end;
  3841. function tabstractrecorddef.search_enumerator_current: tsym;
  3842. var
  3843. sym: tsym;
  3844. i: integer;
  3845. hashedid : THashedIDString;
  3846. begin
  3847. result:=nil;
  3848. // first search for ppo_enumerator_current property modifier
  3849. // then search for public property Current
  3850. for i:=0 to symtable.SymList.Count-1 do
  3851. begin
  3852. sym:=TSym(symtable.SymList[i]);
  3853. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  3854. begin
  3855. result:=sym;
  3856. exit;
  3857. end;
  3858. end;
  3859. hashedid.id:='CURRENT';
  3860. sym:=tsym(symtable.FindWithHash(hashedid));
  3861. if assigned(sym) and (sym.typ=propertysym) and
  3862. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  3863. begin
  3864. result:=sym;
  3865. exit;
  3866. end;
  3867. end;
  3868. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  3869. var
  3870. st: tsymtable;
  3871. enclosingdef: tdef;
  3872. begin
  3873. if typ=objectdef then
  3874. result:=tobjectdef(self).objextname^
  3875. else if assigned(typesym) then
  3876. result:=typesym.realname
  3877. { have to generate anonymous nested type in current unit/class/record }
  3878. else
  3879. internalerror(2011032601);
  3880. { in case of specializations, add some extras to prevent name conflicts
  3881. with nested classes }
  3882. if df_specialization in defoptions then
  3883. result:='$'+result+'$specialization$';
  3884. st:=owner;
  3885. while assigned(st) and
  3886. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  3887. begin
  3888. { nested classes are named as "OuterClass$InnerClass" }
  3889. enclosingdef:=tdef(st.defowner);
  3890. if enclosingdef.typ=procdef then
  3891. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  3892. else if enclosingdef.typ=objectdef then
  3893. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  3894. else if assigned(enclosingdef.typesym) then
  3895. result:=enclosingdef.typesym.realname+'$'+result
  3896. else
  3897. internalerror(2011060305);
  3898. st:=enclosingdef.owner;
  3899. end;
  3900. if with_package_name and
  3901. assigned(import_lib) then
  3902. result:=import_lib^+'/'+result;
  3903. end;
  3904. function tabstractrecorddef.contains_float_field: boolean;
  3905. var
  3906. i : longint;
  3907. begin
  3908. result:=true;
  3909. for i:=0 to symtable.symlist.count-1 do
  3910. begin
  3911. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  3912. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  3913. continue;
  3914. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  3915. begin
  3916. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  3917. exit;
  3918. { search recursively }
  3919. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  3920. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  3921. exit;
  3922. end;
  3923. end;
  3924. result:=false;
  3925. end;
  3926. function tabstractrecorddef.contains_cross_aword_field: boolean;
  3927. var
  3928. i : longint;
  3929. foffset, fsize: aword;
  3930. begin
  3931. result:=true;
  3932. for i:=0 to symtable.symlist.count-1 do
  3933. begin
  3934. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  3935. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  3936. continue;
  3937. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  3938. begin
  3939. if is_packed then
  3940. begin
  3941. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset;
  3942. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.packedbitsize;
  3943. end
  3944. else
  3945. begin
  3946. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset*8;
  3947. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.size*8;
  3948. end;
  3949. if (fsize>0) and ((foffset div (sizeof(aword)*8)) <> ((foffset+fsize-1) div (sizeof(aword)*8))) then
  3950. exit;
  3951. { search recursively }
  3952. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  3953. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_cross_aword_field) then
  3954. exit;
  3955. end;
  3956. end;
  3957. result:=false;
  3958. end;
  3959. {***************************************************************************
  3960. trecorddef
  3961. ***************************************************************************}
  3962. constructor trecorddef.create(const n:string; p:TSymtable);
  3963. begin
  3964. inherited create(n,recorddef,true);
  3965. symtable:=p;
  3966. { we can own the symtable only if nobody else owns a copy so far }
  3967. if symtable.refcount=1 then
  3968. symtable.defowner:=self;
  3969. isunion:=false;
  3970. cloneddefderef.reset;
  3971. end;
  3972. constructor trecorddef.create_global_internal(n: string; packrecords, recordalignmin, maxCrecordalign: shortint);
  3973. var
  3974. oldsymtablestack: tsymtablestack;
  3975. ts: ttypesym;
  3976. definedname: boolean;
  3977. begin
  3978. { construct name }
  3979. definedname:=n<>'';
  3980. if not definedname then
  3981. n:='$InternalRec'+tostr(current_module.deflist.count);
  3982. oldsymtablestack:=symtablestack;
  3983. { do not simply push/pop current_module.localsymtable, because
  3984. that can have side-effects (e.g., it removes helpers) }
  3985. symtablestack:=nil;
  3986. symtable:=trecordsymtable.create(n,packrecords,recordalignmin,maxCrecordalign);
  3987. symtable.defowner:=self;
  3988. isunion:=false;
  3989. inherited create(n,recorddef,true);
  3990. { if we specified a name, then we'll probably want to look up the
  3991. type again by name too -> create typesym }
  3992. ts:=nil;
  3993. if definedname then
  3994. begin
  3995. ts:=ctypesym.create(n,self,true);
  3996. { avoid hints about unused types (these may only be used for
  3997. typed constant data) }
  3998. ts.increfcount;
  3999. end;
  4000. if assigned(current_module.localsymtable) then
  4001. begin
  4002. current_module.localsymtable.insertdef(self);
  4003. if definedname then
  4004. current_module.localsymtable.insert(ts);
  4005. end
  4006. else
  4007. begin
  4008. current_module.globalsymtable.insertdef(self);
  4009. if definedname then
  4010. current_module.globalsymtable.insert(ts);
  4011. end;
  4012. symtablestack:=oldsymtablestack;
  4013. { don't create RTTI for internal types, these are not exported }
  4014. defstates:=defstates+[ds_rtti_table_written,ds_init_table_written];
  4015. include(defoptions,df_internal);
  4016. end;
  4017. function trecorddef.add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  4018. var
  4019. sym: tfieldvarsym;
  4020. name: TIDString;
  4021. pname: ^TIDString;
  4022. begin
  4023. if optionalname='' then
  4024. begin
  4025. name:='$f'+tostr(trecordsymtable(symtable).symlist.count);
  4026. pname:=@name
  4027. end
  4028. else
  4029. pname:=@optionalname;
  4030. sym:=cfieldvarsym.create(pname^,vs_value,def,[],true);
  4031. symtable.insert(sym);
  4032. trecordsymtable(symtable).addfield(sym,vis_hidden);
  4033. result:=sym;
  4034. end;
  4035. procedure trecorddef.add_fields_from_deflist(fieldtypes: tfplist);
  4036. var
  4037. i: longint;
  4038. begin
  4039. for i:=0 to fieldtypes.count-1 do
  4040. add_field_by_def('',tdef(fieldtypes[i]));
  4041. end;
  4042. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  4043. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  4044. var
  4045. i,j : longint;
  4046. begin
  4047. if ppufile.getbyte=1 then
  4048. begin
  4049. new(variantrecdesc);
  4050. ppufile.getderef(variantrecdesc^.variantselectorderef);
  4051. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  4052. for i:=0 to high(variantrecdesc^.branches) do
  4053. begin
  4054. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  4055. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4056. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  4057. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4058. end;
  4059. end
  4060. else
  4061. variantrecdesc:=nil;
  4062. end;
  4063. begin
  4064. inherited ppuload(recorddef,ppufile);
  4065. if df_copied_def in defoptions then
  4066. begin
  4067. ppufile.getderef(cloneddefderef);
  4068. ppuload_platform(ppufile);
  4069. end
  4070. else
  4071. begin
  4072. symtable:=trecordsymtable.create(objrealname^,0,0,0);
  4073. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  4074. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  4075. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  4076. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  4077. trecordsymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  4078. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  4079. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  4080. ppufile.getsmallset(trecordsymtable(symtable).managementoperators);
  4081. { position of ppuload_platform call must correspond
  4082. to position of writeentry in ppuwrite method }
  4083. ppuload_platform(ppufile);
  4084. trecordsymtable(symtable).ppuload(ppufile);
  4085. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4086. but because iso mode supports no units, there is no need to store the variantrecdesc
  4087. in the ppu
  4088. }
  4089. // readvariantrecdesc(variantrecdesc);
  4090. { requires usefieldalignment to be set }
  4091. symtable.defowner:=self;
  4092. end;
  4093. isunion:=false;
  4094. end;
  4095. destructor trecorddef.destroy;
  4096. procedure free_variantrecdesc(var variantrecdesc : pvariantrecdesc);
  4097. var
  4098. i : longint;
  4099. begin
  4100. while assigned(variantrecdesc) do
  4101. begin
  4102. for i:=0 to high(variantrecdesc^.branches) do
  4103. begin
  4104. free_variantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4105. SetLength(variantrecdesc^.branches[i].values,0);
  4106. end;
  4107. SetLength(variantrecdesc^.branches,0);
  4108. dispose(variantrecdesc);
  4109. variantrecdesc:=nil;
  4110. end;
  4111. end;
  4112. begin
  4113. if assigned(variantrecdesc) then
  4114. free_variantrecdesc(variantrecdesc);
  4115. if assigned(symtable) then
  4116. begin
  4117. symtable.free;
  4118. symtable:=nil;
  4119. end;
  4120. inherited destroy;
  4121. end;
  4122. function trecorddef.getcopy : tstoreddef;
  4123. begin
  4124. result:=crecorddef.create(objrealname^,symtable.getcopy);
  4125. trecorddef(result).isunion:=isunion;
  4126. include(trecorddef(result).defoptions,df_copied_def);
  4127. if assigned(tcinitcode) then
  4128. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  4129. if assigned(import_lib) then
  4130. trecorddef(result).import_lib:=stringdup(import_lib^);
  4131. end;
  4132. function trecorddef.needs_inittable : boolean;
  4133. begin
  4134. { each record with managed field or with any management operator needs
  4135. init table }
  4136. needs_inittable:=(trecordsymtable(symtable).managementoperators<>[]) or
  4137. trecordsymtable(symtable).needs_init_final
  4138. end;
  4139. function trecorddef.needs_separate_initrtti : boolean;
  4140. begin
  4141. result:=true;
  4142. end;
  4143. function trecorddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  4144. begin
  4145. result:=trecordsymtable(symtable).has_non_trivial_init;
  4146. end;
  4147. procedure trecorddef.buildderef;
  4148. begin
  4149. inherited buildderef;
  4150. if df_copied_def in defoptions then
  4151. cloneddefderef.build(symtable.defowner)
  4152. else
  4153. tstoredsymtable(symtable).buildderef;
  4154. end;
  4155. procedure trecorddef.deref;
  4156. begin
  4157. inherited deref;
  4158. { now dereference the definitions }
  4159. if df_copied_def in defoptions then
  4160. begin
  4161. cloneddef:=trecorddef(cloneddefderef.resolve);
  4162. symtable:=cloneddef.symtable.getcopy;
  4163. end
  4164. else
  4165. tstoredsymtable(symtable).deref(false);
  4166. { internal types, only load from the system unit }
  4167. if assigned(owner) and
  4168. assigned(owner.name) and
  4169. (owner.name^='SYSTEM') then
  4170. begin
  4171. { TGUID }
  4172. if not assigned(rec_tguid) and
  4173. (upper(typename)='TGUID') then
  4174. rec_tguid:=self
  4175. { JMP_BUF }
  4176. else if not assigned(rec_jmp_buf) and
  4177. (upper(typename)='JMP_BUF') then
  4178. rec_jmp_buf:=self
  4179. else if not assigned(rec_exceptaddr) and
  4180. (upper(typename)='TEXCEPTADDR') then
  4181. rec_exceptaddr:=self;
  4182. end;
  4183. end;
  4184. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  4185. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  4186. var
  4187. i,j : longint;
  4188. begin
  4189. if assigned(variantrecdesc) then
  4190. begin
  4191. ppufile.putbyte(1);
  4192. ppufile.putderef(variantrecdesc^.variantselectorderef);
  4193. ppufile.putasizeint(length(variantrecdesc^.branches));
  4194. for i:=0 to high(variantrecdesc^.branches) do
  4195. begin
  4196. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  4197. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4198. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  4199. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4200. end;
  4201. end
  4202. else
  4203. ppufile.putbyte(0);
  4204. end;
  4205. begin
  4206. inherited ppuwrite(ppufile);
  4207. if df_copied_def in defoptions then
  4208. ppufile.putderef(cloneddefderef)
  4209. else
  4210. begin
  4211. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  4212. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  4213. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  4214. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  4215. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignmin));
  4216. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  4217. ppufile.putword(trecordsymtable(symtable).paddingsize);
  4218. ppufile.putsmallset(trecordsymtable(symtable).managementoperators);
  4219. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4220. but because iso mode supports no units, there is no need to store the variantrecdesc
  4221. in the ppu
  4222. }
  4223. // writevariantrecdesc(variantrecdesc);
  4224. end;
  4225. writeentry(ppufile,ibrecorddef);
  4226. if not(df_copied_def in defoptions) then
  4227. trecordsymtable(symtable).ppuwrite(ppufile);
  4228. end;
  4229. function trecorddef.size:asizeint;
  4230. begin
  4231. result:=trecordsymtable(symtable).datasize;
  4232. end;
  4233. function trecorddef.alignment:shortint;
  4234. begin
  4235. alignment:=trecordsymtable(symtable).recordalignment;
  4236. end;
  4237. function trecorddef.padalignment:shortint;
  4238. begin
  4239. padalignment := trecordsymtable(symtable).padalignment;
  4240. end;
  4241. function trecorddef.GetTypeName : string;
  4242. begin
  4243. GetTypeName:='<record type>'
  4244. end;
  4245. {***************************************************************************
  4246. TABSTRACTPROCDEF
  4247. ***************************************************************************}
  4248. constructor tabstractprocdef.create(dt:tdeftyp;level:byte;doregister:boolean);
  4249. begin
  4250. inherited create(dt,doregister);
  4251. parast:=tparasymtable.create(self,level);
  4252. paras:=nil;
  4253. minparacount:=0;
  4254. maxparacount:=0;
  4255. proctypeoption:=potype_none;
  4256. proccalloption:=pocall_none;
  4257. procoptions:=[];
  4258. returndef:=voidtype;
  4259. returndefderef.reset;
  4260. savesize:=sizeof(pint);
  4261. callerargareasize:=0;
  4262. calleeargareasize:=0;
  4263. has_paraloc_info:=callnoside;
  4264. funcretloc[callerside].init;
  4265. funcretloc[calleeside].init;
  4266. check_mark_as_nested;
  4267. end;
  4268. destructor tabstractprocdef.destroy;
  4269. begin
  4270. if assigned(paras) then
  4271. begin
  4272. {$ifdef MEMDEBUG}
  4273. memprocpara.start;
  4274. {$endif MEMDEBUG}
  4275. paras.free;
  4276. paras:=nil;
  4277. {$ifdef MEMDEBUG}
  4278. memprocpara.stop;
  4279. {$endif MEMDEBUG}
  4280. end;
  4281. if assigned(parast) then
  4282. begin
  4283. {$ifdef MEMDEBUG}
  4284. memprocparast.start;
  4285. {$endif MEMDEBUG}
  4286. parast.free;
  4287. parast:=nil;
  4288. {$ifdef MEMDEBUG}
  4289. memprocparast.stop;
  4290. {$endif MEMDEBUG}
  4291. end;
  4292. funcretloc[callerside].done;
  4293. funcretloc[calleeside].done;
  4294. inherited destroy;
  4295. end;
  4296. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  4297. begin
  4298. if (tsym(p).typ<>paravarsym) then
  4299. exit;
  4300. inc(plongint(arg)^);
  4301. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  4302. begin
  4303. if not assigned(tparavarsym(p).defaultconstsym) then
  4304. inc(minparacount);
  4305. inc(maxparacount);
  4306. end;
  4307. end;
  4308. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  4309. begin
  4310. if (tsym(p).typ<>paravarsym) then
  4311. exit;
  4312. paras.add(p);
  4313. end;
  4314. procedure tabstractprocdef.calcparas;
  4315. var
  4316. paracount : longint;
  4317. begin
  4318. { This can already be assigned when
  4319. we need to reresolve this unit (PFV) }
  4320. if assigned(paras) then
  4321. paras.free;
  4322. paras:=tparalist.create(false);
  4323. paracount:=0;
  4324. minparacount:=0;
  4325. maxparacount:=0;
  4326. parast.SymList.ForEachCall(@count_para,@paracount);
  4327. paras.capacity:=paracount;
  4328. { Insert parameters in table }
  4329. parast.SymList.ForEachCall(@insert_para,nil);
  4330. { Order parameters }
  4331. paras.sortparas;
  4332. end;
  4333. function tabstractprocdef.mangledprocparanames(oldlen : longint) : string;
  4334. var
  4335. crc : dword;
  4336. hp : TParavarsym;
  4337. hs : TSymStr;
  4338. newlen,
  4339. i : integer;
  4340. begin
  4341. result:='';
  4342. hp:=nil;
  4343. { add parameter types }
  4344. for i:=0 to paras.count-1 do
  4345. begin
  4346. hp:=tparavarsym(paras[i]);
  4347. if not(vo_is_hidden_para in hp.varoptions) then
  4348. result:=result+'$'+hp.vardef.mangledparaname;
  4349. end;
  4350. { add resultdef, add $$ as separator to make it unique from a
  4351. parameter separator }
  4352. if not is_void(returndef) then
  4353. result:=result+'$$'+returndef.mangledparaname;
  4354. newlen:=length(result)+oldlen;
  4355. { Replace with CRC if the parameter line is very long }
  4356. if (newlen-oldlen>12) and
  4357. ((newlen>100) or (newlen-oldlen>64)) then
  4358. begin
  4359. crc:=0;
  4360. for i:=0 to paras.count-1 do
  4361. begin
  4362. hp:=tparavarsym(paras[i]);
  4363. if not(vo_is_hidden_para in hp.varoptions) then
  4364. begin
  4365. hs:=hp.vardef.mangledparaname;
  4366. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4367. end;
  4368. end;
  4369. if not is_void(returndef) then
  4370. begin
  4371. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  4372. hs:='$$'+returndef.mangledparaname;
  4373. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4374. end;
  4375. result:='$crc'+hexstr(crc,8);
  4376. end;
  4377. end;
  4378. procedure tabstractprocdef.buildderef;
  4379. begin
  4380. { released procdef? }
  4381. if not assigned(parast) then
  4382. exit;
  4383. inherited buildderef;
  4384. returndefderef.build(returndef);
  4385. if po_explicitparaloc in procoptions then
  4386. funcretloc[callerside].buildderef;
  4387. { parast }
  4388. tparasymtable(parast).buildderef;
  4389. end;
  4390. procedure tabstractprocdef.deref;
  4391. begin
  4392. inherited deref;
  4393. returndef:=tdef(returndefderef.resolve);
  4394. if po_explicitparaloc in procoptions then
  4395. begin
  4396. funcretloc[callerside].deref;
  4397. has_paraloc_info:=callerside;
  4398. end
  4399. else
  4400. begin
  4401. { deref is called after loading from a ppu, but also after another
  4402. unit has been reloaded/recompiled and all references must be
  4403. re-resolved. Since the funcretloc contains a reference to a tdef,
  4404. reset it so that we won't try to access the stale def }
  4405. funcretloc[callerside].init;
  4406. has_paraloc_info:=callnoside;
  4407. end;
  4408. { parast }
  4409. tparasymtable(parast).deref(false);
  4410. { recalculated parameters }
  4411. calcparas;
  4412. end;
  4413. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4414. begin
  4415. inherited ppuload(dt,ppufile);
  4416. parast:=nil;
  4417. Paras:=nil;
  4418. minparacount:=0;
  4419. maxparacount:=0;
  4420. ppufile.getderef(returndefderef);
  4421. proctypeoption:=tproctypeoption(ppufile.getbyte);
  4422. proccalloption:=tproccalloption(ppufile.getbyte);
  4423. ppufile.getnormalset(procoptions);
  4424. funcretloc[callerside].init;
  4425. if po_explicitparaloc in procoptions then
  4426. funcretloc[callerside].ppuload(ppufile);
  4427. savesize:=sizeof(pint);
  4428. if (po_explicitparaloc in procoptions) then
  4429. has_paraloc_info:=callerside;
  4430. end;
  4431. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  4432. var
  4433. oldintfcrc : boolean;
  4434. begin
  4435. { released procdef? }
  4436. if not assigned(parast) then
  4437. exit;
  4438. inherited ppuwrite(ppufile);
  4439. ppufile.putderef(returndefderef);
  4440. oldintfcrc:=ppufile.do_interface_crc;
  4441. ppufile.do_interface_crc:=false;
  4442. ppufile.putbyte(ord(proctypeoption));
  4443. ppufile.putbyte(ord(proccalloption));
  4444. ppufile.putnormalset(procoptions);
  4445. ppufile.do_interface_crc:=oldintfcrc;
  4446. if (po_explicitparaloc in procoptions) then
  4447. funcretloc[callerside].ppuwrite(ppufile);
  4448. end;
  4449. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  4450. var
  4451. hs,s : ansistring;
  4452. hp : TParavarsym;
  4453. hpc : tconstsym;
  4454. first : boolean;
  4455. i,j : integer;
  4456. begin
  4457. s:='';
  4458. first:=true;
  4459. for i:=0 to paras.count-1 do
  4460. begin
  4461. hp:=tparavarsym(paras[i]);
  4462. if not(vo_is_hidden_para in hp.varoptions) or
  4463. (pno_showhidden in pno) then
  4464. begin
  4465. if first then
  4466. begin
  4467. s:=s+'(';
  4468. first:=false;
  4469. end
  4470. else
  4471. s:=s+';';
  4472. if vo_is_hidden_para in hp.varoptions then
  4473. s:=s+'<';
  4474. case hp.varspez of
  4475. vs_var :
  4476. s:=s+'var ';
  4477. vs_const :
  4478. s:=s+'const ';
  4479. vs_out :
  4480. s:=s+'out ';
  4481. vs_constref :
  4482. s:=s+'constref ';
  4483. else
  4484. ;
  4485. end;
  4486. if (pno_paranames in pno) then
  4487. s:=s+hp.realname+':';
  4488. if hp.univpara then
  4489. s:=s+'univ ';
  4490. if assigned(hp.vardef.typesym) then
  4491. begin
  4492. hs:=hp.vardef.typesym.realname;
  4493. if hs[1]<>'$' then
  4494. s:=s+hp.vardef.OwnerHierarchyName+hs
  4495. else
  4496. s:=s+hp.vardef.GetTypeName;
  4497. end
  4498. else
  4499. s:=s+hp.vardef.GetTypeName;
  4500. { default value }
  4501. if assigned(hp.defaultconstsym) then
  4502. begin
  4503. hpc:=tconstsym(hp.defaultconstsym);
  4504. hs:='';
  4505. case hpc.consttyp of
  4506. constwstring:
  4507. begin
  4508. if pcompilerwidestring(hpc.value.valueptr)^.len>0 then
  4509. begin
  4510. setlength(hs,pcompilerwidestring(hpc.value.valueptr)^.len);
  4511. for j:=0 to pcompilerwidestring(hpc.value.valueptr)^.len-1 do
  4512. begin
  4513. if (ord(pcompilerwidestring(hpc.value.valueptr)^.data[j])<127) and
  4514. not(byte(pcompilerwidestring(hpc.value.valueptr)^.data[j]) in [0,10,13]) then
  4515. hs[j+1]:=char(pcompilerwidestring(hpc.value.valueptr)^.data[j])
  4516. else
  4517. hs[j+1]:='.';
  4518. end;
  4519. end;
  4520. end;
  4521. conststring,
  4522. constresourcestring :
  4523. begin
  4524. if hpc.value.len>0 then
  4525. begin
  4526. setLength(hs,hpc.value.len);
  4527. { don't write past the end of hs if the constant
  4528. is > 255 chars }
  4529. move(hpc.value.valueptr^,hs[1],length(hs));
  4530. { make sure that constant strings with newline chars
  4531. don't create a linebreak in the assembler code,
  4532. since comments are line-based. Also remove nulls
  4533. because the comments are written as a pchar. }
  4534. ReplaceCase(hs,#0,'.');
  4535. ReplaceCase(hs,#10,'.');
  4536. ReplaceCase(hs,#13,'.');
  4537. end;
  4538. end;
  4539. constreal :
  4540. str(pbestreal(hpc.value.valueptr)^,hs);
  4541. constpointer :
  4542. hs:=tostr(hpc.value.valueordptr);
  4543. constord :
  4544. begin
  4545. if is_boolean(hpc.constdef) then
  4546. begin
  4547. if hpc.value.valueord<>0 then
  4548. hs:='TRUE'
  4549. else
  4550. hs:='FALSE';
  4551. end
  4552. else
  4553. hs:=tostr(hpc.value.valueord);
  4554. end;
  4555. constnil :
  4556. hs:='nil';
  4557. constset :
  4558. hs:='<set>';
  4559. constguid:
  4560. hs:=guid2string(pguid(hpc.value.valueptr)^);
  4561. constnone:
  4562. internalerror(2019050704);
  4563. end;
  4564. if hs<>'' then
  4565. s:=s+'=`'+hs+'`';
  4566. end;
  4567. if vo_is_hidden_para in hp.varoptions then
  4568. s:=s+'>';
  4569. end;
  4570. end;
  4571. if not first then
  4572. s:=s+')';
  4573. if (po_varargs in procoptions) then
  4574. s:=s+';VarArgs';
  4575. typename_paras:=s;
  4576. end;
  4577. function tabstractprocdef.is_methodpointer:boolean;
  4578. begin
  4579. result:=false;
  4580. end;
  4581. function tabstractprocdef.is_addressonly:boolean;
  4582. begin
  4583. result:=true;
  4584. end;
  4585. function tabstractprocdef.no_self_node: boolean;
  4586. begin
  4587. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  4588. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  4589. end;
  4590. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string): tstoreddef;
  4591. var
  4592. j, nestinglevel: longint;
  4593. pvs, npvs: tparavarsym;
  4594. begin
  4595. nestinglevel:=parast.symtablelevel;
  4596. if newtyp=procdef then
  4597. begin
  4598. if (copytyp<>pc_bareproc) then
  4599. result:=cprocdef.create(nestinglevel,true)
  4600. else
  4601. result:=cprocdef.create(normal_function_level,true);
  4602. tprocdef(result).visibility:=vis_public;
  4603. end
  4604. else
  4605. begin
  4606. result:=cprocvardef.create(nestinglevel);
  4607. end;
  4608. tabstractprocdef(result).returndef:=returndef;
  4609. tabstractprocdef(result).returndefderef:=returndefderef;
  4610. pvs:=nil;
  4611. npvs:=nil;
  4612. for j:=0 to parast.symlist.count-1 do
  4613. begin
  4614. case tsym(parast.symlist[j]).typ of
  4615. paravarsym:
  4616. begin
  4617. pvs:=tparavarsym(parast.symlist[j]);
  4618. { in case of bare proc, don't copy self, vmt or framepointer
  4619. parameters }
  4620. if (copytyp in [pc_bareproc,pc_normal_no_hidden]) and
  4621. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  4622. continue;
  4623. if paraprefix='' then
  4624. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  4625. pvs.vardef,pvs.varoptions)
  4626. else if not(vo_is_high_para in pvs.varoptions) then
  4627. npvs:=cparavarsym.create(paraprefix+pvs.realname,pvs.paranr,pvs.varspez,
  4628. pvs.vardef,pvs.varoptions)
  4629. else
  4630. npvs:=cparavarsym.create('$high'+paraprefix+copy(pvs.name,5,length(pvs.name)),pvs.paranr,pvs.varspez,
  4631. pvs.vardef,pvs.varoptions);
  4632. npvs.defaultconstsym:=pvs.defaultconstsym;
  4633. tabstractprocdef(result).parast.insert(npvs);
  4634. end;
  4635. constsym:
  4636. begin
  4637. // ignore, reuse original constym. Should also be duplicated
  4638. // be safe though
  4639. end;
  4640. symconst.typesym:
  4641. begin
  4642. // reuse original, part of generic declaration
  4643. end
  4644. else
  4645. internalerror(201160604);
  4646. end;
  4647. end;
  4648. tabstractprocdef(result).savesize:=savesize;
  4649. if (typ<>procvardef) and
  4650. (newtyp=procvardef) then
  4651. begin
  4652. { procvars can't be (class)constructures/destructors etc }
  4653. if proctypeoption=potype_constructor then
  4654. begin
  4655. tabstractprocdef(result).returndef:=tdef(owner.defowner);
  4656. if not(is_implicit_pointer_object_type(returndef) or
  4657. (returndef.typ<>objectdef)) then
  4658. tabstractprocdef(result).returndef:=cpointerdef.getreusable(tabstractprocdef(result).returndef);
  4659. tabstractprocdef(result).proctypeoption:=potype_function;
  4660. end
  4661. else if is_void(returndef) then
  4662. tabstractprocdef(result).proctypeoption:=potype_procedure
  4663. else
  4664. tabstractprocdef(result).proctypeoption:=potype_function;
  4665. end
  4666. else
  4667. tabstractprocdef(result).proctypeoption:=proctypeoption;
  4668. tabstractprocdef(result).proccalloption:=proccalloption;
  4669. tabstractprocdef(result).procoptions:=procoptions;
  4670. if (copytyp=pc_bareproc) then
  4671. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  4672. if newtyp=procvardef then
  4673. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  4674. if copytyp=pc_address_only then
  4675. include(tabstractprocdef(result).procoptions,po_addressonly);
  4676. tabstractprocdef(result).callerargareasize:=callerargareasize;
  4677. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  4678. tabstractprocdef(result).maxparacount:=maxparacount;
  4679. tabstractprocdef(result).minparacount:=minparacount;
  4680. if po_explicitparaloc in procoptions then
  4681. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  4682. { recalculate parameter info }
  4683. tabstractprocdef(result).has_paraloc_info:=callnoside;
  4684. {$ifdef m68k}
  4685. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  4686. {$endif}
  4687. if (typ=procdef) and
  4688. (newtyp=procvardef) and
  4689. (owner.symtabletype=ObjectSymtable) then
  4690. include(tprocvardef(result).procoptions,po_methodpointer);
  4691. end;
  4692. function tabstractprocdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  4693. begin
  4694. result:=is_addressonly;
  4695. end;
  4696. procedure tabstractprocdef.check_mark_as_nested;
  4697. begin
  4698. { nested procvars require that nested functions use the Delphi-style
  4699. nested procedure calling convention }
  4700. if (parast.symtablelevel>normal_function_level) and
  4701. (m_nested_procvars in current_settings.modeswitches) then
  4702. include(procoptions,po_delphi_nested_cc);
  4703. end;
  4704. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  4705. begin
  4706. if (side in [callerside,callbothsides]) and
  4707. not(has_paraloc_info in [callerside,callbothsides]) then
  4708. begin
  4709. if not is_c_variadic(self) then
  4710. callerargareasize:=paramanager.create_paraloc_info(self,callerside)
  4711. else
  4712. callerargareasize:=paramanager.create_varargs_paraloc_info(self,callerside,nil);
  4713. if has_paraloc_info in [calleeside,callbothsides] then
  4714. has_paraloc_info:=callbothsides
  4715. else
  4716. has_paraloc_info:=callerside;
  4717. end;
  4718. if (side in [calleeside,callbothsides]) and
  4719. not(has_paraloc_info in [calleeside,callbothsides]) then
  4720. begin
  4721. if not is_c_variadic(self) then
  4722. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside)
  4723. else
  4724. callerargareasize:=paramanager.create_varargs_paraloc_info(self,calleeside,nil);
  4725. if has_paraloc_info in [callerside,callbothsides] then
  4726. has_paraloc_info:=callbothsides
  4727. else
  4728. has_paraloc_info:=calleeside;
  4729. end;
  4730. end;
  4731. procedure tabstractprocdef.done_paraloc_info(side: tcallercallee);
  4732. var
  4733. i: longint;
  4734. begin
  4735. if (side in [callerside,callbothsides]) and
  4736. (has_paraloc_info in [callerside,callbothsides]) then
  4737. begin
  4738. funcretloc[callerside].done;
  4739. for i:=0 to paras.count-1 do
  4740. tparavarsym(paras[i]).paraloc[callerside].done;
  4741. if has_paraloc_info=callerside then
  4742. has_paraloc_info:=callnoside
  4743. else
  4744. has_paraloc_info:=calleeside;
  4745. end;
  4746. if (side in [calleeside,callbothsides]) and
  4747. (has_paraloc_info in [calleeside,callbothsides]) then
  4748. begin
  4749. funcretloc[calleeside].done;
  4750. for i:=0 to paras.count-1 do
  4751. tparavarsym(paras[i]).paraloc[calleeside].done;
  4752. if has_paraloc_info=calleeside then
  4753. has_paraloc_info:=callnoside
  4754. else
  4755. has_paraloc_info:=callerside;
  4756. end;
  4757. end;
  4758. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  4759. var
  4760. p: tparavarsym;
  4761. ploc: PCGParalocation;
  4762. i: longint;
  4763. begin
  4764. result:=false;
  4765. init_paraloc_info(side);
  4766. for i:=0 to parast.SymList.Count-1 do
  4767. if tsym(parast.SymList[i]).typ=paravarsym then
  4768. begin
  4769. p:=tparavarsym(parast.SymList[i]);
  4770. { check if no parameter is located on the stack }
  4771. if (is_open_array(p.vardef) or
  4772. is_array_of_const(p.vardef)) and (p.varspez=vs_value) then
  4773. begin
  4774. result:=true;
  4775. exit;
  4776. end;
  4777. ploc:=p.paraloc[side].location;
  4778. while assigned(ploc) do
  4779. begin
  4780. if (ploc^.loc=LOC_REFERENCE) then
  4781. begin
  4782. result:=true;
  4783. exit
  4784. end;
  4785. ploc:=ploc^.next;
  4786. end;
  4787. end;
  4788. end;
  4789. function tabstractprocdef.is_pushleftright: boolean;
  4790. begin
  4791. result:=false;
  4792. end;
  4793. function tabstractprocdef.address_type: tdef;
  4794. begin
  4795. result:=voidcodepointertype;
  4796. end;
  4797. function tabstractprocdef.ofs_address_type:tdef;
  4798. begin
  4799. result:=address_type;
  4800. end;
  4801. procedure tabstractprocdef.declared_far;
  4802. begin
  4803. Message1(parser_w_proc_directive_ignored,'FAR');
  4804. end;
  4805. procedure tabstractprocdef.declared_near;
  4806. begin
  4807. Message1(parser_w_proc_directive_ignored,'NEAR');
  4808. end;
  4809. {***************************************************************************
  4810. TPROCDEF
  4811. ***************************************************************************}
  4812. function tprocdef.GetResultName: PShortString;
  4813. begin
  4814. if not assigned(implprocdefinfo) then
  4815. internalerror(2014010301);
  4816. result:=implprocdefinfo^.resultname;
  4817. end;
  4818. procedure tprocdef.SetResultName(AValue: PShortString);
  4819. begin
  4820. if not assigned(implprocdefinfo) then
  4821. internalerror(2014010302);
  4822. implprocdefinfo^.resultname:=AValue;
  4823. end;
  4824. function tprocdef.GetParentFPInitBlock: tnode;
  4825. begin
  4826. if not assigned(implprocdefinfo) then
  4827. internalerror(2014010303);
  4828. result:=implprocdefinfo^.parentfpinitblock;
  4829. end;
  4830. function tprocdef.GetParentFPStruct: tsym;
  4831. begin
  4832. if not assigned(implprocdefinfo) then
  4833. internalerror(2014010304);
  4834. result:=implprocdefinfo^.parentfpstruct;
  4835. end;
  4836. function tprocdef.GetParentFPStructPtrType: tdef;
  4837. begin
  4838. if not assigned(implprocdefinfo) then
  4839. internalerror(2014010305);
  4840. result:=implprocdefinfo^.parentfpstructptrtype;
  4841. end;
  4842. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  4843. begin
  4844. if not assigned(implprocdefinfo) then
  4845. internalerror(2014010306);
  4846. implprocdefinfo^.parentfpinitblock:=AValue;
  4847. end;
  4848. function tprocdef.Getprocendtai: tai;
  4849. begin
  4850. if not assigned(implprocdefinfo) then
  4851. internalerror(2014010307);
  4852. result:=implprocdefinfo^.procendtai;
  4853. end;
  4854. function tprocdef.Getprocstarttai: tai;
  4855. begin
  4856. if not assigned(implprocdefinfo) then
  4857. internalerror(2014010308);
  4858. result:=implprocdefinfo^.procstarttai;
  4859. end;
  4860. procedure tprocdef.Setprocendtai(AValue: tai);
  4861. begin
  4862. if not assigned(implprocdefinfo) then
  4863. internalerror(2014010309);
  4864. implprocdefinfo^.procendtai:=AValue;
  4865. end;
  4866. function tprocdef.Getskpara: pointer;
  4867. begin
  4868. if not assigned(implprocdefinfo) then
  4869. internalerror(2014010310);
  4870. result:=implprocdefinfo^.skpara;
  4871. end;
  4872. procedure tprocdef.Setskpara(AValue: pointer);
  4873. begin
  4874. if not assigned(implprocdefinfo) then
  4875. internalerror(2014010311);
  4876. implprocdefinfo^.skpara:=AValue;
  4877. end;
  4878. function tprocdef.Getpersonality: tprocdef;
  4879. begin
  4880. if not assigned(implprocdefinfo) then
  4881. internalerror(2016121701);
  4882. result:=implprocdefinfo^.personality;
  4883. end;
  4884. procedure tprocdef.Setpersonality(AValue: tprocdef);
  4885. begin
  4886. if not assigned(implprocdefinfo) then
  4887. internalerror(2016121702);
  4888. implprocdefinfo^.personality:=AValue;
  4889. end;
  4890. function tprocdef.Getforwarddef: boolean;
  4891. begin
  4892. if not assigned(implprocdefinfo) then
  4893. internalerror(2014010312);
  4894. result:=implprocdefinfo^.forwarddef;
  4895. end;
  4896. function tprocdef.Gethasforward: boolean;
  4897. begin
  4898. if not assigned(implprocdefinfo) then
  4899. internalerror(2014010313);
  4900. result:=implprocdefinfo^.hasforward;
  4901. end;
  4902. function tprocdef.Getinterfacedef: boolean;
  4903. begin
  4904. if not assigned(implprocdefinfo) then
  4905. internalerror(2014010314);
  4906. result:=implprocdefinfo^.interfacedef;
  4907. end;
  4908. procedure tprocdef.Setforwarddef(AValue: boolean);
  4909. begin
  4910. if not assigned(implprocdefinfo) then
  4911. internalerror(2014010315);
  4912. implprocdefinfo^.forwarddef:=AValue;
  4913. end;
  4914. procedure tprocdef.Sethasforward(AValue: boolean);
  4915. begin
  4916. if not assigned(implprocdefinfo) then
  4917. internalerror(2014010316);
  4918. implprocdefinfo^.hasforward:=AValue;
  4919. end;
  4920. function tprocdef.GetIsEmpty: boolean;
  4921. begin
  4922. result:=pio_empty in implprocoptions;
  4923. end;
  4924. procedure tprocdef.SetIsEmpty(AValue: boolean);
  4925. begin
  4926. if AValue then
  4927. include(implprocoptions,pio_empty)
  4928. else
  4929. include(implprocoptions,pio_empty);
  4930. end;
  4931. function tprocdef.GetHasInliningInfo: boolean;
  4932. begin
  4933. result:=pio_has_inlininginfo in implprocoptions;
  4934. end;
  4935. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  4936. begin
  4937. if AValue then
  4938. include(implprocoptions,pio_has_inlininginfo)
  4939. else
  4940. exclude(implprocoptions,pio_has_inlininginfo);
  4941. end;
  4942. procedure tprocdef.Setinterfacedef(AValue: boolean);
  4943. begin
  4944. if not assigned(implprocdefinfo) then
  4945. internalerror(2014010317);
  4946. implprocdefinfo^.interfacedef:=AValue;
  4947. end;
  4948. procedure tprocdef.Setprocstarttai(AValue: tai);
  4949. begin
  4950. if not assigned(implprocdefinfo) then
  4951. internalerror(2014010318);
  4952. implprocdefinfo^.procstarttai:=AValue;
  4953. end;
  4954. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  4955. begin
  4956. if not assigned(implprocdefinfo) then
  4957. internalerror(2014010319);
  4958. implprocdefinfo^.parentfpstruct:=AValue;
  4959. end;
  4960. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  4961. begin
  4962. if not assigned(implprocdefinfo) then
  4963. internalerror(2014010320);
  4964. implprocdefinfo^.parentfpstructptrtype:=AValue;
  4965. end;
  4966. constructor tprocdef.create(level:byte;doregister:boolean);
  4967. begin
  4968. inherited create(procdef,level,doregister);
  4969. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  4970. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  4971. {$ifdef symansistr}
  4972. _mangledname:='';
  4973. {$else symansistr}
  4974. _mangledname:=nil;
  4975. {$endif symansistr}
  4976. fileinfo:=current_filepos;
  4977. extnumber:=$ffff;
  4978. aliasnames:=TCmdStrList.create;
  4979. funcretsym:=nil;
  4980. funcretsymderef.reset;
  4981. procsymderef.reset;
  4982. forwarddef:=true;
  4983. interfacedef:=false;
  4984. hasforward:=false;
  4985. struct := nil;
  4986. structderef.reset;
  4987. import_dll:=nil;
  4988. import_name:=nil;
  4989. import_nr:=0;
  4990. inlininginfo:=nil;
  4991. deprecatedmsg:=nil;
  4992. end;
  4993. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  4994. var
  4995. i,aliasnamescount,sizeleft : longint;
  4996. level : byte;
  4997. buf : array[0..255] of byte;
  4998. begin
  4999. inherited ppuload(procdef,ppufile);
  5000. {$ifdef symansistr}
  5001. if po_has_mangledname in procoptions then
  5002. _mangledname:=ppufile.getansistring
  5003. else
  5004. _mangledname:='';
  5005. {$else symansistr}
  5006. if po_has_mangledname in procoptions then
  5007. _mangledname:=ppufile.getpshortstring
  5008. else
  5009. _mangledname:=nil;
  5010. {$endif symansistr}
  5011. extnumber:=ppufile.getword;
  5012. level:=ppufile.getbyte;
  5013. ppufile.getderef(structderef);
  5014. ppufile.getderef(procsymderef);
  5015. ppufile.getposinfo(fileinfo);
  5016. visibility:=tvisibility(ppufile.getbyte);
  5017. ppufile.getsmallset(symoptions);
  5018. if sp_has_deprecated_msg in symoptions then
  5019. deprecatedmsg:=ppufile.getpshortstring
  5020. else
  5021. deprecatedmsg:=nil;
  5022. { import stuff }
  5023. if po_has_importdll in procoptions then
  5024. import_dll:=ppufile.getpshortstring
  5025. else
  5026. import_dll:=nil;
  5027. if po_has_importname in procoptions then
  5028. import_name:=ppufile.getpshortstring
  5029. else
  5030. import_name:=nil;
  5031. import_nr:=ppufile.getword;
  5032. if (po_msgint in procoptions) then
  5033. messageinf.i:=ppufile.getlongint;
  5034. if (po_msgstr in procoptions) then
  5035. messageinf.str:=ppufile.getpshortstring;
  5036. if (po_dispid in procoptions) then
  5037. dispid:=ppufile.getlongint;
  5038. { inline stuff }
  5039. ppufile.getsmallset(implprocoptions);
  5040. if has_inlininginfo then
  5041. begin
  5042. ppufile.getderef(funcretsymderef);
  5043. new(inlininginfo);
  5044. ppufile.getsmallset(inlininginfo^.flags);
  5045. end
  5046. else
  5047. begin
  5048. inlininginfo:=nil;
  5049. funcretsym:=nil;
  5050. end;
  5051. aliasnames:=TCmdStrList.create;
  5052. { count alias names }
  5053. aliasnamescount:=ppufile.getbyte;
  5054. for i:=1 to aliasnamescount do
  5055. aliasnames.insert(ppufile.getstring);
  5056. { load the token stream containing the declaration }
  5057. sizeleft:=ppufile.getlongint;
  5058. if sizeleft>0 then
  5059. begin
  5060. init_genericdecl;
  5061. while sizeleft>0 do
  5062. begin
  5063. if sizeleft>sizeof(buf) then
  5064. i:=sizeof(buf)
  5065. else
  5066. i:=sizeleft;
  5067. ppufile.getdata(buf,i);
  5068. genericdecltokenbuf.write(buf,i);
  5069. dec(sizeleft,i);
  5070. end;
  5071. end;
  5072. ppuload_platform(ppufile);
  5073. { load para symtable }
  5074. parast:=tparasymtable.create(self,level);
  5075. tparasymtable(parast).ppuload(ppufile);
  5076. { load local symtable }
  5077. if has_inlininginfo then
  5078. begin
  5079. localst:=tlocalsymtable.create(self,level);
  5080. tlocalsymtable(localst).ppuload(ppufile);
  5081. end
  5082. else
  5083. localst:=nil;
  5084. { inline stuff }
  5085. if has_inlininginfo then
  5086. inlininginfo^.code:=ppuloadnodetree(ppufile);
  5087. { default values for no persistent data }
  5088. if (cs_link_deffile in current_settings.globalswitches) and
  5089. (tf_need_export in target_info.flags) and
  5090. (po_exports in procoptions) then
  5091. deffile.AddExport(mangledname);
  5092. { Disable po_has_inlining until the derefimpl is done }
  5093. has_inlininginfo:=false;
  5094. end;
  5095. destructor tprocdef.destroy;
  5096. begin
  5097. aliasnames.free;
  5098. aliasnames:=nil;
  5099. if assigned(localst) and
  5100. (localst.symtabletype<>staticsymtable) then
  5101. begin
  5102. {$ifdef MEMDEBUG}
  5103. memproclocalst.start;
  5104. {$endif MEMDEBUG}
  5105. localst.free;
  5106. localst:=nil;
  5107. {$ifdef MEMDEBUG}
  5108. memproclocalst.start;
  5109. {$endif MEMDEBUG}
  5110. end;
  5111. if assigned(inlininginfo) then
  5112. begin
  5113. {$ifdef MEMDEBUG}
  5114. memprocnodetree.start;
  5115. {$endif MEMDEBUG}
  5116. tnode(inlininginfo^.code).free;
  5117. {$ifdef MEMDEBUG}
  5118. memprocnodetree.start;
  5119. {$endif MEMDEBUG}
  5120. dispose(inlininginfo);
  5121. inlininginfo:=nil;
  5122. end;
  5123. freeimplprocdefinfo;
  5124. genericdecltokenbuf.free;
  5125. genericdecltokenbuf:=nil;
  5126. stringdispose(import_dll);
  5127. stringdispose(import_name);
  5128. stringdispose(deprecatedmsg);
  5129. if (po_msgstr in procoptions) then
  5130. stringdispose(messageinf.str);
  5131. {$ifndef symansistr}
  5132. if assigned(_mangledname) then
  5133. begin
  5134. {$ifdef MEMDEBUG}
  5135. memmanglednames.start;
  5136. {$endif MEMDEBUG}
  5137. stringdispose(_mangledname);
  5138. {$ifdef MEMDEBUG}
  5139. memmanglednames.stop;
  5140. {$endif MEMDEBUG}
  5141. end;
  5142. {$endif symansistr}
  5143. inherited destroy;
  5144. end;
  5145. procedure tprocdef.freeimplprocdefinfo;
  5146. begin
  5147. if assigned(implprocdefinfo) then
  5148. begin
  5149. stringdispose(implprocdefinfo^.resultname);
  5150. freemem(implprocdefinfo);
  5151. implprocdefinfo:=nil;
  5152. end;
  5153. end;
  5154. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  5155. var
  5156. oldintfcrc : boolean;
  5157. aliasnamescount,i,sizeleft : longint;
  5158. item : TCmdStrListItem;
  5159. buf : array[0..255] of byte;
  5160. begin
  5161. { released procdef? }
  5162. if not assigned(parast) then
  5163. exit;
  5164. inherited ppuwrite(ppufile);
  5165. {$ifdef symansistr}
  5166. if po_has_mangledname in procoptions then
  5167. ppufile.putansistring(_mangledname);
  5168. {$else symansistr}
  5169. if po_has_mangledname in procoptions then
  5170. ppufile.putstring(_mangledname^);
  5171. {$endif symansistr}
  5172. ppufile.putword(extnumber);
  5173. ppufile.putbyte(parast.symtablelevel);
  5174. ppufile.putderef(structderef);
  5175. ppufile.putderef(procsymderef);
  5176. ppufile.putposinfo(fileinfo);
  5177. ppufile.putbyte(byte(visibility));
  5178. ppufile.putsmallset(symoptions);
  5179. if sp_has_deprecated_msg in symoptions then
  5180. ppufile.putstring(deprecatedmsg^);
  5181. { import }
  5182. if po_has_importdll in procoptions then
  5183. ppufile.putstring(import_dll^);
  5184. if po_has_importname in procoptions then
  5185. ppufile.putstring(import_name^);
  5186. ppufile.putword(import_nr);
  5187. if (po_msgint in procoptions) then
  5188. ppufile.putlongint(messageinf.i);
  5189. if (po_msgstr in procoptions) then
  5190. ppufile.putstring(messageinf.str^);
  5191. if (po_dispid in procoptions) then
  5192. ppufile.putlongint(dispid);
  5193. { inline stuff }
  5194. oldintfcrc:=ppufile.do_crc;
  5195. ppufile.do_crc:=false;
  5196. ppufile.putsmallset(implprocoptions);
  5197. if has_inlininginfo then
  5198. begin
  5199. ppufile.putderef(funcretsymderef);
  5200. ppufile.putsmallset(inlininginfo^.flags);
  5201. end;
  5202. { count alias names }
  5203. aliasnamescount:=0;
  5204. item:=TCmdStrListItem(aliasnames.first);
  5205. while assigned(item) do
  5206. begin
  5207. inc(aliasnamescount);
  5208. item:=TCmdStrListItem(item.next);
  5209. end;
  5210. if aliasnamescount>255 then
  5211. internalerror(200711021);
  5212. ppufile.putbyte(aliasnamescount);
  5213. item:=TCmdStrListItem(aliasnames.first);
  5214. while assigned(item) do
  5215. begin
  5216. ppufile.putstring(item.str);
  5217. item:=TCmdStrListItem(item.next);
  5218. end;
  5219. ppufile.do_crc:=oldintfcrc;
  5220. { generic tokens for the declaration }
  5221. if assigned(genericdecltokenbuf) and (genericdecltokenbuf.size>0) then
  5222. begin
  5223. sizeleft:=genericdecltokenbuf.size;
  5224. genericdecltokenbuf.seek(0);
  5225. ppufile.putlongint(sizeleft);
  5226. while sizeleft>0 do
  5227. begin
  5228. if sizeleft>sizeof(buf) then
  5229. i:=sizeof(buf)
  5230. else
  5231. i:=sizeleft;
  5232. genericdecltokenbuf.read(buf,i);
  5233. ppufile.putdata(buf,i);
  5234. dec(sizeleft,i);
  5235. end;
  5236. end
  5237. else
  5238. ppufile.putlongint(0);
  5239. { write this entry }
  5240. writeentry(ppufile,ibprocdef);
  5241. { Save the para symtable, this is taken from the interface }
  5242. tparasymtable(parast).ppuwrite(ppufile);
  5243. { save localsymtable for inline procedures or when local
  5244. browser info is requested, this has no influence on the crc }
  5245. if has_inlininginfo then
  5246. begin
  5247. oldintfcrc:=ppufile.do_crc;
  5248. ppufile.do_crc:=false;
  5249. tlocalsymtable(localst).ppuwrite(ppufile);
  5250. ppufile.do_crc:=oldintfcrc;
  5251. end;
  5252. { node tree for inlining }
  5253. oldintfcrc:=ppufile.do_crc;
  5254. ppufile.do_crc:=false;
  5255. if has_inlininginfo then
  5256. ppuwritenodetree(ppufile,inlininginfo^.code);
  5257. ppufile.do_crc:=oldintfcrc;
  5258. end;
  5259. function tprocdef.fullprocname(showhidden:boolean):string;
  5260. var
  5261. pno: tprocnameoptions;
  5262. begin
  5263. pno:=[];
  5264. if showhidden then
  5265. include(pno,pno_showhidden);
  5266. result:=customprocname(pno);
  5267. end;
  5268. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  5269. var
  5270. s, rn : ansistring;
  5271. t : ttoken;
  5272. syssym : tsyssym;
  5273. begin
  5274. {$ifdef EXTDEBUG}
  5275. include(pno,pno_showhidden);
  5276. {$endif EXTDEBUG}
  5277. s:='';
  5278. if proctypeoption=potype_operator then
  5279. begin
  5280. for t:=NOTOKEN to last_overloaded do
  5281. if procsym.realname='$'+overloaded_names[t] then
  5282. begin
  5283. s:='operator ';
  5284. if (pno_ownername in pno) and
  5285. assigned(struct) then
  5286. s:=s+struct.RttiName+'.';
  5287. s:=s+arraytokeninfo[t].str;
  5288. if not (pno_noparams in pno) then
  5289. s:=s+typename_paras(pno);
  5290. break;
  5291. end;
  5292. end
  5293. else
  5294. begin
  5295. if (po_classmethod in procoptions) and
  5296. not(pno_noclassmarker in pno) then
  5297. s:='class ';
  5298. case proctypeoption of
  5299. potype_constructor,
  5300. potype_class_constructor:
  5301. s:=s+'constructor ';
  5302. potype_class_destructor,
  5303. potype_destructor:
  5304. s:=s+'destructor ';
  5305. else
  5306. if (pno_proctypeoption in pno) then
  5307. begin
  5308. if assigned(returndef) and
  5309. not(is_void(returndef)) then
  5310. s:=s+'function '
  5311. else
  5312. s:=s+'procedure ';
  5313. end;
  5314. end;
  5315. if (pno_ownername in pno) and
  5316. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  5317. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  5318. if (po_compilerproc in procoptions) and (extnumber<>$ffff) then
  5319. begin
  5320. syssym:=tsyssym.find_by_number(extnumber);
  5321. if not assigned(syssym) then
  5322. internalerror(2016060305);
  5323. rn:=syssym.realname;
  5324. end
  5325. else
  5326. rn:=procsym.realname;
  5327. if (pno_noleadingdollar in pno) and
  5328. (rn[1]='$') then
  5329. delete(rn,1,1);
  5330. s:=s+rn;
  5331. if not (pno_noparams in pno) then
  5332. s:=s+typename_paras(pno);
  5333. end;
  5334. if not(proctypeoption in [potype_constructor,potype_destructor,
  5335. potype_class_constructor,potype_class_destructor]) and
  5336. assigned(returndef) and
  5337. not(is_void(returndef)) then
  5338. s:=s+':'+returndef.GetTypeName;
  5339. if assigned(owner) and (owner.symtabletype=localsymtable) then
  5340. s:=s+' is nested'
  5341. else if po_is_block in procoptions then
  5342. s:=s+' is block';
  5343. s:=s+';';
  5344. if po_far in procoptions then
  5345. s:=s+' far;';
  5346. { forced calling convention? }
  5347. if (po_hascallingconvention in procoptions) then
  5348. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  5349. if (po_staticmethod in procoptions) and
  5350. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  5351. s:=s+' Static;';
  5352. if pno_mangledname in pno then
  5353. s:=s+' -- mangled name: '+mangledname;
  5354. customprocname:=s;
  5355. end;
  5356. function tprocdef.is_methodpointer:boolean;
  5357. begin
  5358. { don't check assigned(_class), that's also the case for nested
  5359. procedures inside methods }
  5360. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  5361. end;
  5362. function tprocdef.is_addressonly:boolean;
  5363. begin
  5364. result:=assigned(owner) and
  5365. not is_methodpointer and
  5366. (not(m_nested_procvars in current_settings.modeswitches) or
  5367. not is_nested_pd(self));
  5368. end;
  5369. procedure tprocdef.make_external;
  5370. begin
  5371. include(procoptions,po_external);
  5372. forwarddef:=false;
  5373. end;
  5374. procedure tprocdef.init_genericdecl;
  5375. begin
  5376. if assigned(genericdecltokenbuf) then
  5377. internalerror(2015061901);
  5378. genericdecltokenbuf:=tdynamicarray.create(256);
  5379. end;
  5380. function tprocdef.getfuncretsyminfo(out ressym: tsym; out resdef: tdef): boolean;
  5381. begin
  5382. result:=false;
  5383. if proctypeoption=potype_constructor then
  5384. begin
  5385. result:=true;
  5386. ressym:=tsym(parast.Find('self'));
  5387. resdef:=tabstractnormalvarsym(ressym).vardef;
  5388. { and TP-style constructors return a pointer to self }
  5389. if is_object(resdef) then
  5390. resdef:=cpointerdef.getreusable(resdef);
  5391. end
  5392. else if not is_void(returndef) then
  5393. begin
  5394. result:=true;
  5395. ressym:=funcretsym;
  5396. resdef:=tabstractnormalvarsym(ressym).vardef;
  5397. end;
  5398. end;
  5399. function tprocdef.has_alias_name(const s: TSymStr): boolean;
  5400. var
  5401. item : TCmdStrListItem;
  5402. begin
  5403. result:=true;
  5404. if mangledname=s then
  5405. exit;
  5406. item:=TCmdStrListItem(aliasnames.first);
  5407. while assigned(item) do
  5408. begin
  5409. if item.str=s then
  5410. exit;
  5411. item:=TCmdStrListItem(item.next);
  5412. end;
  5413. result:=false;
  5414. end;
  5415. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  5416. begin
  5417. case t of
  5418. gs_local :
  5419. GetSymtable:=localst;
  5420. gs_para :
  5421. GetSymtable:=parast;
  5422. else
  5423. GetSymtable:=nil;
  5424. end;
  5425. end;
  5426. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string): tstoreddef;
  5427. var
  5428. j : longint;
  5429. begin
  5430. result:=inherited;
  5431. if newtyp=procvardef then
  5432. begin
  5433. { create new paralist }
  5434. tprocvardef(result).calcparas;
  5435. exit;
  5436. end;
  5437. { don't copy mangled name, can be different }
  5438. tprocdef(result).messageinf:=messageinf;
  5439. tprocdef(result).dispid:=dispid;
  5440. if po_msgstr in procoptions then
  5441. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  5442. tprocdef(result).symoptions:=symoptions;
  5443. if assigned(deprecatedmsg) then
  5444. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  5445. { will have to be associated with appropriate procsym }
  5446. tprocdef(result).procsym:=nil;
  5447. { don't create aliases for bare copies, nor copy the funcretsym as
  5448. the function result parameter will be inserted again if necessary
  5449. (e.g. if the calling convention is changed) }
  5450. if not(copytyp in [pc_bareproc,pc_normal_no_hidden]) then
  5451. begin
  5452. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  5453. if assigned(funcretsym) then
  5454. begin
  5455. if funcretsym.owner=parast then
  5456. begin
  5457. j:=parast.symlist.indexof(funcretsym);
  5458. if j<0 then
  5459. internalerror(2011040606);
  5460. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  5461. end
  5462. else if funcretsym.owner=localst then
  5463. begin
  5464. { nothing to do, will be inserted for the new procdef while
  5465. parsing its body (by pdecsub.insert_funcret_local) }
  5466. end
  5467. else
  5468. internalerror(2011040605);
  5469. end;
  5470. end;
  5471. { will have to be associated with a new struct }
  5472. tprocdef(result).struct:=nil;
  5473. if assigned(implprocdefinfo) then
  5474. begin
  5475. if assigned(resultname) then
  5476. tprocdef(result).resultname:=stringdup(resultname^);
  5477. tprocdef(result).synthetickind:=synthetickind;
  5478. end;
  5479. if assigned(import_dll) then
  5480. tprocdef(result).import_dll:=stringdup(import_dll^);
  5481. if assigned(import_name) then
  5482. tprocdef(result).import_name:=stringdup(import_name^);
  5483. tprocdef(result).import_nr:=import_nr;
  5484. tprocdef(result).extnumber:=$ffff;
  5485. tprocdef(result).visibility:=visibility;
  5486. { we need a separate implementation for the copied def }
  5487. tprocdef(result).forwarddef:=true;
  5488. tprocdef(result).interfacedef:=true;
  5489. { create new paralist }
  5490. tprocdef(result).calcparas;
  5491. end;
  5492. function tprocdef.getcopy: tstoreddef;
  5493. begin
  5494. result:=getcopyas(procdef,pc_normal,'');
  5495. end;
  5496. procedure tprocdef.buildderef;
  5497. begin
  5498. inherited buildderef;
  5499. structderef.build(struct);
  5500. { procsym that originaly defined this definition, should be in the
  5501. same symtable }
  5502. procsymderef.build(procsym);
  5503. end;
  5504. procedure tprocdef.buildderefimpl;
  5505. begin
  5506. inherited buildderefimpl;
  5507. { inline tree }
  5508. if has_inlininginfo then
  5509. begin
  5510. { Localst is not available for main/unit init }
  5511. if assigned(localst) then
  5512. begin
  5513. tlocalsymtable(localst).buildderef;
  5514. tlocalsymtable(localst).buildderefimpl;
  5515. end;
  5516. funcretsymderef.build(funcretsym);
  5517. inlininginfo^.code.buildderefimpl;
  5518. end;
  5519. end;
  5520. procedure tprocdef.deref;
  5521. begin
  5522. inherited deref;
  5523. struct:=tabstractrecorddef(structderef.resolve);
  5524. { procsym that originaly defined this definition, should be in the
  5525. same symtable }
  5526. procsym:=tprocsym(procsymderef.resolve);
  5527. end;
  5528. procedure tprocdef.derefimpl;
  5529. begin
  5530. { Enable has_inlininginfo when the inlininginfo
  5531. structure is available. The has_inlininginfo was disabled
  5532. after the load, since the data was invalid }
  5533. if assigned(inlininginfo) then
  5534. has_inlininginfo:=true;
  5535. { Inline }
  5536. if has_inlininginfo then
  5537. begin
  5538. { Locals }
  5539. if assigned(localst) then
  5540. begin
  5541. tlocalsymtable(localst).deref(false);
  5542. tlocalsymtable(localst).derefimpl(false);
  5543. end;
  5544. inlininginfo^.code.derefimpl;
  5545. { funcretsym, this is always located in the localst }
  5546. funcretsym:=tsym(funcretsymderef.resolve);
  5547. end
  5548. else
  5549. begin
  5550. { safety }
  5551. { Not safe! A unit may be reresolved after its interface has been
  5552. parsed but before its implementation has been parsed, and in that
  5553. case the funcretsym is still required!
  5554. funcretsym:=nil; }
  5555. end;
  5556. end;
  5557. function tprocdef.GetTypeName : string;
  5558. begin
  5559. GetTypeName := FullProcName(false);
  5560. end;
  5561. function tprocdef.mangledname : TSymStr;
  5562. begin
  5563. {$ifdef symansistr}
  5564. if _mangledname='' then
  5565. begin
  5566. result:=defaultmangledname;
  5567. _mangledname:=result;
  5568. end
  5569. else
  5570. result:=_mangledname;
  5571. {$else symansistr}
  5572. if not assigned(_mangledname) then
  5573. begin
  5574. result:=defaultmangledname;
  5575. _mangledname:=stringdup(mangledname);
  5576. end
  5577. else
  5578. result:=_mangledname^;
  5579. {$endif symansistr}
  5580. end;
  5581. function tprocdef.defaultmangledname: TSymStr;
  5582. begin
  5583. { we need to use the symtable where the procsym is inserted,
  5584. because that is visible to the world }
  5585. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  5586. defaultmangledname:=defaultmangledname+mangledprocparanames(Length(defaultmangledname))
  5587. end;
  5588. function tprocdef.cplusplusmangledname : TSymStr;
  5589. function getcppparaname(p : tdef) : TSymStr;
  5590. const
  5591. {$ifdef NAMEMANGLING_GCC2}
  5592. ordtype2str : array[tordtype] of string[2] = (
  5593. '',
  5594. 'Uc','Us','Ui','Us','',
  5595. 'Sc','s','i','x','',
  5596. 'b','b','b','b','b','b',
  5597. 'c','w','x');
  5598. {$else NAMEMANGLING_GCC2}
  5599. ordtype2str : array[tordtype] of string[1] = (
  5600. 'v',
  5601. 'h','t','j','y','',
  5602. 'a','s','i','x','',
  5603. 'b','b','b','b','b',
  5604. 'b','b','b','b',
  5605. 'c','w','x','C');
  5606. floattype2str : array[tfloattype] of string[1] = (
  5607. 'f','d','e','e',
  5608. 'd','d','g');
  5609. {$endif NAMEMANGLING_GCC2}
  5610. var
  5611. s : TSymStr;
  5612. begin
  5613. case p.typ of
  5614. orddef:
  5615. begin
  5616. s:=ordtype2str[torddef(p).ordtype];
  5617. if s='C' then
  5618. s:=ordtype2str[range_to_basetype(torddef(p).low,torddef(p).high)];
  5619. end;
  5620. pointerdef:
  5621. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  5622. {$ifndef NAMEMANGLING_GCC2}
  5623. floatdef:
  5624. s:=floattype2str[tfloatdef(p).floattype];
  5625. {$endif NAMEMANGLING_GCC2}
  5626. else
  5627. internalerror(2103001);
  5628. end;
  5629. getcppparaname:=s;
  5630. end;
  5631. var
  5632. s,s2 : TSymStr;
  5633. hp : TParavarsym;
  5634. i : integer;
  5635. begin
  5636. {$ifdef NAMEMANGLING_GCC2}
  5637. { outdated gcc 2.x name mangling scheme }
  5638. s := procsym.realname;
  5639. if procsym.owner.symtabletype=ObjectSymtable then
  5640. begin
  5641. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  5642. case proctypeoption of
  5643. potype_destructor:
  5644. s:='_$_'+tostr(length(s2))+s2;
  5645. potype_constructor:
  5646. s:='___'+tostr(length(s2))+s2;
  5647. else
  5648. s:='_'+s+'__'+tostr(length(s2))+s2;
  5649. end;
  5650. end
  5651. else s:=s+'__';
  5652. s:=s+'F';
  5653. { concat modifiers }
  5654. { !!!!! }
  5655. { now we handle the parameters }
  5656. if maxparacount>0 then
  5657. begin
  5658. for i:=0 to paras.count-1 do
  5659. begin
  5660. hp:=tparavarsym(paras[i]);
  5661. { no hidden parameters form part of a C++ mangled name:
  5662. a) self is not included
  5663. b) there are no "high" or other hidden parameters
  5664. }
  5665. if vo_is_hidden_para in hp.varoptions then
  5666. continue;
  5667. s2:=getcppparaname(hp.vardef);
  5668. if hp.varspez in [vs_var,vs_out] then
  5669. s2:='R'+s2;
  5670. s:=s+s2;
  5671. end;
  5672. end
  5673. else
  5674. s:=s+'v';
  5675. cplusplusmangledname:=s;
  5676. {$else NAMEMANGLING_GCC2}
  5677. { gcc 3.x and 4.x name mangling scheme }
  5678. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  5679. if procsym.owner.symtabletype=ObjectSymtable then
  5680. begin
  5681. s:='_ZN';
  5682. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  5683. s:=s+tostr(length(s2))+s2;
  5684. case proctypeoption of
  5685. potype_constructor:
  5686. s:=s+'C1';
  5687. potype_destructor:
  5688. s:=s+'D1';
  5689. else
  5690. s:=s+tostr(length(procsym.realname))+procsym.realname;
  5691. end;
  5692. s:=s+'E';
  5693. end
  5694. else
  5695. s:=procsym.realname;
  5696. { now we handle the parameters }
  5697. if maxparacount>0 then
  5698. begin
  5699. for i:=0 to paras.count-1 do
  5700. begin
  5701. hp:=tparavarsym(paras[i]);
  5702. { no hidden parameters form part of a C++ mangled name:
  5703. a) self is not included
  5704. b) there are no "high" or other hidden parameters
  5705. }
  5706. if vo_is_hidden_para in hp.varoptions then
  5707. continue;
  5708. s2:=getcppparaname(hp.vardef);
  5709. if hp.varspez in [vs_var,vs_out] then
  5710. s2:='R'+s2;
  5711. s:=s+s2;
  5712. end;
  5713. end
  5714. else
  5715. s:=s+'v';
  5716. cplusplusmangledname:=s;
  5717. {$endif NAMEMANGLING_GCC2}
  5718. end;
  5719. function tprocdef.objcmangledname : TSymStr;
  5720. var
  5721. manglednamelen: longint;
  5722. iscatmethod : boolean;
  5723. begin
  5724. if not (po_msgstr in procoptions) then
  5725. internalerror(2009030901);
  5726. { we may very well need longer strings to handle these... }
  5727. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  5728. length('+"[ ]"')+length(messageinf.str^);
  5729. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  5730. if (iscatmethod) then
  5731. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  5732. if manglednamelen>255 then
  5733. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  5734. if not(po_classmethod in procoptions) then
  5735. result:='"-['
  5736. else
  5737. result:='"+[';
  5738. { quotes are necessary because the +/- otherwise confuse the assembler
  5739. into expecting a number
  5740. }
  5741. if iscatmethod then
  5742. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  5743. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  5744. if iscatmethod then
  5745. result:=result+')';
  5746. result:=result+' '+messageinf.str^+']"';
  5747. end;
  5748. procedure tprocdef.setmangledname(const s : TSymStr);
  5749. begin
  5750. { This is not allowed anymore, the forward declaration
  5751. already needs to create the correct mangledname, no changes
  5752. afterwards are allowed (PFV) }
  5753. { Exception: interface definitions in mode macpas, since in that }
  5754. { case no reference to the old name can exist yet (JM) }
  5755. {$ifdef symansistr}
  5756. if _mangledname<>'' then
  5757. if ((m_mac in current_settings.modeswitches) and
  5758. (interfacedef)) then
  5759. _mangledname:=''
  5760. else
  5761. internalerror(200411171);
  5762. {$else symansistr}
  5763. if assigned(_mangledname) then
  5764. if ((m_mac in current_settings.modeswitches) and
  5765. (interfacedef)) then
  5766. stringdispose(_mangledname)
  5767. else
  5768. internalerror(200411171);
  5769. {$endif symansistr}
  5770. {$ifdef jvm}
  5771. { this routine can be called for compilerproces. can't set mangled
  5772. name since it must be calculated, but it uses import_name when set
  5773. -> set that one }
  5774. import_name:=stringdup(s);
  5775. include(procoptions,po_has_importname);
  5776. include(procoptions,po_has_mangledname);
  5777. {$else}
  5778. {$ifdef symansistr}
  5779. _mangledname:=s;
  5780. {$else symansistr}
  5781. _mangledname:=stringdup(s);
  5782. {$endif symansistr}
  5783. {$endif jvm}
  5784. include(procoptions,po_has_mangledname);
  5785. end;
  5786. procedure tprocdef.setcompilerprocname;
  5787. begin
  5788. procsym.realname:='$'+lower(procsym.name);
  5789. end;
  5790. {***************************************************************************
  5791. TPROCVARDEF
  5792. ***************************************************************************}
  5793. constructor tprocvardef.create(level:byte);
  5794. begin
  5795. inherited create(procvardef,level,true);
  5796. end;
  5797. class function tprocvardef.getreusableprocaddr(def: tabstractprocdef): tprocvardef;
  5798. var
  5799. res: PHashSetItem;
  5800. oldsymtablestack: tsymtablestack;
  5801. begin
  5802. if not assigned(current_module) then
  5803. internalerror(2011081301);
  5804. res:=current_module.procaddrdefs.FindOrAdd(@def,sizeof(def));
  5805. if not assigned(res^.Data) then
  5806. begin
  5807. { since these pointerdefs can be reused anywhere in the current
  5808. unit, add them to the global/staticsymtable (or local symtable
  5809. if they're a local def, because otherwise they'll be saved
  5810. to the ppu referencing a local symtable entry that doesn't
  5811. exist in the ppu) }
  5812. oldsymtablestack:=symtablestack;
  5813. { do not simply push/pop current_module.localsymtable, because
  5814. that can have side-effects (e.g., it removes helpers) }
  5815. symtablestack:=nil;
  5816. result:=tprocvardef(def.getcopyas(procvardef,pc_address_only,''));
  5817. setup_reusable_def(def,result,res,oldsymtablestack);
  5818. { res^.Data may still be nil -> don't overwrite result }
  5819. exit;
  5820. end;
  5821. result:=tprocvardef(res^.Data);
  5822. end;
  5823. class function tprocvardef.getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  5824. begin
  5825. result:=getreusableprocaddr(def);
  5826. if not result.is_registered then
  5827. include(result.defoptions,df_not_registered_no_free);
  5828. end;
  5829. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  5830. begin
  5831. inherited ppuload(procvardef,ppufile);
  5832. { load para symtable }
  5833. parast:=tparasymtable.create(self,ppufile.getbyte);
  5834. ppuload_platform(ppufile);
  5835. tparasymtable(parast).ppuload(ppufile);
  5836. end;
  5837. function tprocvardef.getcopy : tstoreddef;
  5838. var
  5839. i : tcallercallee;
  5840. j : longint;
  5841. begin
  5842. result:=cprocvardef.create(parast.symtablelevel);
  5843. tprocvardef(result).returndef:=returndef;
  5844. tprocvardef(result).returndefderef:=returndefderef;
  5845. tprocvardef(result).parast:=parast.getcopy;
  5846. tprocvardef(result).savesize:=savesize;
  5847. { create paralist copy }
  5848. calcparas;
  5849. tprocvardef(result).paras:=tparalist.create(false);
  5850. tprocvardef(result).paras.count:=paras.count;
  5851. for j:=0 to paras.count-1 do
  5852. tprocvardef(result).paras[j]:=paras[j];
  5853. tprocvardef(result).proctypeoption:=proctypeoption;
  5854. tprocvardef(result).proccalloption:=proccalloption;
  5855. tprocvardef(result).procoptions:=procoptions;
  5856. tprocvardef(result).callerargareasize:=callerargareasize;
  5857. tprocvardef(result).calleeargareasize:=calleeargareasize;
  5858. tprocvardef(result).maxparacount:=maxparacount;
  5859. tprocvardef(result).minparacount:=minparacount;
  5860. for i:=low(funcretloc) to high(funcretloc) do
  5861. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  5862. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  5863. {$ifdef m68k}
  5864. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  5865. {$endif}
  5866. end;
  5867. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  5868. begin
  5869. inherited ppuwrite(ppufile);
  5870. { Save the para symtable level (necessary to distinguish nested
  5871. procvars) }
  5872. ppufile.putbyte(parast.symtablelevel);
  5873. { Write this entry }
  5874. writeentry(ppufile,ibprocvardef);
  5875. { Save the para symtable, this is taken from the interface }
  5876. tparasymtable(parast).ppuwrite(ppufile);
  5877. end;
  5878. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  5879. begin
  5880. case t of
  5881. gs_para :
  5882. GetSymtable:=parast;
  5883. else
  5884. GetSymtable:=nil;
  5885. end;
  5886. end;
  5887. function tprocvardef.size : asizeint;
  5888. begin
  5889. { we return false for is_addressonly for a block (because it's not a
  5890. simple pointer to a function), but they are handled as implicit
  5891. pointers to a datastructure that contains everything ->
  5892. voidpointertype.size instead of voidcodepointertype.size }
  5893. if po_is_block in procoptions then
  5894. size:=voidpointertype.size
  5895. else if not is_addressonly then
  5896. begin
  5897. if is_nested_pd(self) then
  5898. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  5899. else
  5900. size:=voidcodepointertype.size+voidpointertype.size;
  5901. end
  5902. else
  5903. size:=voidcodepointertype.size;
  5904. end;
  5905. function tprocvardef.is_methodpointer:boolean;
  5906. begin
  5907. result:=(po_methodpointer in procoptions);
  5908. end;
  5909. function tprocvardef.is_addressonly:boolean;
  5910. begin
  5911. result:=((not(po_methodpointer in procoptions) or (po_staticmethod in procoptions)) and
  5912. not(po_is_block in procoptions) and
  5913. not is_nested_pd(self)) or
  5914. (po_addressonly in procoptions);
  5915. end;
  5916. function tprocvardef.getmangledparaname:TSymStr;
  5917. begin
  5918. if not(po_methodpointer in procoptions) then
  5919. if not is_nested_pd(self) then
  5920. result:='procvar'
  5921. else
  5922. { we need the manglednames here, because nestedprocvars can be anonymous, e.g.
  5923. having not a type name or not an unique one, see webtbs/tw27515.pp
  5924. Further, use $_ ... _$ delimiters to avoid ambiguous names, see webtbs/tw27515.pp }
  5925. result:='$_nestedprovar'+mangledprocparanames(0)+'_$'
  5926. else
  5927. result:='procvarofobj'
  5928. end;
  5929. function tprocvardef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string): tstoreddef;
  5930. begin
  5931. result:=inherited;
  5932. tabstractprocdef(result).calcparas;
  5933. end;
  5934. function tprocvardef.is_publishable : boolean;
  5935. begin
  5936. is_publishable:=(po_methodpointer in procoptions);
  5937. end;
  5938. function tprocvardef.GetTypeName : string;
  5939. var
  5940. s: string;
  5941. pno : tprocnameoptions;
  5942. begin
  5943. {$ifdef EXTDEBUG}
  5944. pno:=[pno_showhidden];
  5945. {$else EXTDEBUG}
  5946. pno:=[];
  5947. {$endif EXTDEBUG}
  5948. s:='<';
  5949. if po_is_block in procoptions then
  5950. s := s+'reference to'
  5951. else if po_classmethod in procoptions then
  5952. s := s+'class method type of'
  5953. else
  5954. if po_addressonly in procoptions then
  5955. s := s+'address of'
  5956. else
  5957. s := s+'procedure variable type of';
  5958. if assigned(returndef) and
  5959. (returndef<>voidtype) then
  5960. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5961. else
  5962. s:=s+' procedure'+typename_paras(pno);
  5963. if po_methodpointer in procoptions then
  5964. s := s+' of object';
  5965. if is_nested_pd(self) then
  5966. s := s+' is nested';
  5967. if po_far in procoptions then
  5968. s := s+';far';
  5969. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5970. end;
  5971. {***************************************************************************
  5972. TOBJECTDEF
  5973. ***************************************************************************}
  5974. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);
  5975. begin
  5976. inherited create(n,objectdef,doregister);
  5977. fcurrent_dispid:=0;
  5978. objecttype:=ot;
  5979. childof:=nil;
  5980. childofderef.reset;
  5981. vmt_fieldderef.reset;
  5982. extendeddefderef.reset;
  5983. cloneddefderef.reset;
  5984. if objecttype=odt_helper then
  5985. owner.includeoption(sto_has_helper);
  5986. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords,
  5987. current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  5988. { create space for vmt !! }
  5989. vmtentries:=TFPList.Create;
  5990. set_parent(c);
  5991. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5992. prepareguid;
  5993. { setup implemented interfaces }
  5994. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5995. ImplementedInterfaces:=TFPObjectList.Create(true)
  5996. else
  5997. ImplementedInterfaces:=nil;
  5998. writing_class_record_dbginfo:=false;
  5999. end;
  6000. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  6001. var
  6002. i,
  6003. implintfcount : longint;
  6004. d, getterd : tderef;
  6005. ImplIntf : TImplementedInterface;
  6006. vmtentry : pvmtentry;
  6007. begin
  6008. inherited ppuload(objectdef,ppufile);
  6009. objecttype:=tobjecttyp(ppufile.getbyte);
  6010. helpertype:=thelpertype(ppufile.getbyte);
  6011. objextname:=ppufile.getpshortstring;
  6012. { only used for external Objective-C classes/protocols }
  6013. if (objextname^='') then
  6014. stringdispose(objextname);
  6015. symtable:=tObjectSymtable.create(self,objrealname^,0,0,0);
  6016. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  6017. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  6018. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  6019. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  6020. tObjectSymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  6021. ppufile.getderef(vmt_fieldderef);
  6022. ppufile.getderef(childofderef);
  6023. { load guid }
  6024. iidstr:=nil;
  6025. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6026. begin
  6027. new(iidguid);
  6028. ppufile.getguid(iidguid^);
  6029. iidstr:=ppufile.getpshortstring;
  6030. end;
  6031. abstractcnt:=ppufile.getlongint;
  6032. if objecttype=odt_helper then
  6033. ppufile.getderef(extendeddefderef);
  6034. vmtentries:=TFPList.Create;
  6035. vmtentries.count:=ppufile.getlongint;
  6036. for i:=0 to vmtentries.count-1 do
  6037. begin
  6038. ppufile.getderef(d);
  6039. new(vmtentry);
  6040. vmtentry^.procdef:=nil;
  6041. vmtentry^.procdefderef:=d;
  6042. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  6043. vmtentries[i]:=vmtentry;
  6044. end;
  6045. { load implemented interfaces }
  6046. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6047. begin
  6048. ImplementedInterfaces:=TFPObjectList.Create(true);
  6049. implintfcount:=ppufile.getlongint;
  6050. for i:=0 to implintfcount-1 do
  6051. begin
  6052. ppufile.getderef(d);
  6053. ppufile.getderef(getterd);
  6054. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  6055. ImplIntf.IOffset:=ppufile.getlongint;
  6056. byte(ImplIntf.IType):=ppufile.getbyte;
  6057. ImplementedInterfaces.Add(ImplIntf);
  6058. end;
  6059. end
  6060. else
  6061. ImplementedInterfaces:=nil;
  6062. if df_copied_def in defoptions then
  6063. begin
  6064. ppufile.getderef(cloneddefderef);
  6065. ppuload_platform(ppufile);
  6066. end
  6067. else
  6068. begin
  6069. ppuload_platform(ppufile);
  6070. tObjectSymtable(symtable).ppuload(ppufile);
  6071. end;
  6072. { handles the predefined class tobject }
  6073. { the last TOBJECT which is loaded gets }
  6074. { it ! }
  6075. if (childof=nil) and
  6076. (objecttype in [odt_class,odt_javaclass]) and
  6077. (objname^='TOBJECT') then
  6078. class_tobject:=self;
  6079. if (childof=nil) and
  6080. (objecttype=odt_interfacecom) then
  6081. if (objname^='IUNKNOWN') then
  6082. interface_iunknown:=self
  6083. else
  6084. if (objname^='IDISPATCH') then
  6085. interface_idispatch:=self;
  6086. if (childof=nil) and
  6087. (objecttype=odt_objcclass) and
  6088. (objname^='PROTOCOL') then
  6089. objc_protocoltype:=self;
  6090. if (objecttype=odt_javaclass) and
  6091. not(oo_is_formal in objectoptions) then
  6092. begin
  6093. if (objname^='JLOBJECT') then
  6094. java_jlobject:=self
  6095. else if (objname^='JLTHROWABLE') then
  6096. java_jlthrowable:=self
  6097. else if (objname^='FPCBASERECORDTYPE') then
  6098. java_fpcbaserecordtype:=self
  6099. else if (objname^='JLSTRING') then
  6100. java_jlstring:=self
  6101. else if (objname^='ANSISTRINGCLASS') then
  6102. java_ansistring:=self
  6103. else if (objname^='SHORTSTRINGCLASS') then
  6104. java_shortstring:=self
  6105. else if (objname^='JLENUM') then
  6106. java_jlenum:=self
  6107. else if (objname^='JUENUMSET') then
  6108. java_juenumset:=self
  6109. else if (objname^='FPCBITSET') then
  6110. java_jubitset:=self
  6111. else if (objname^='FPCBASEPROCVARTYPE') then
  6112. java_procvarbase:=self;
  6113. end;
  6114. writing_class_record_dbginfo:=false;
  6115. end;
  6116. destructor tobjectdef.destroy;
  6117. begin
  6118. if assigned(symtable) then
  6119. begin
  6120. symtable.free;
  6121. symtable:=nil;
  6122. end;
  6123. stringdispose(objextname);
  6124. stringdispose(iidstr);
  6125. if assigned(ImplementedInterfaces) then
  6126. begin
  6127. ImplementedInterfaces.free;
  6128. ImplementedInterfaces:=nil;
  6129. end;
  6130. if assigned(iidguid) then
  6131. begin
  6132. dispose(iidguid);
  6133. iidguid:=nil;
  6134. end;
  6135. if assigned(vmtentries) then
  6136. begin
  6137. resetvmtentries;
  6138. vmtentries.free;
  6139. vmtentries:=nil;
  6140. end;
  6141. if assigned(vmcallstaticinfo) then
  6142. begin
  6143. freemem(vmcallstaticinfo);
  6144. vmcallstaticinfo:=nil;
  6145. end;
  6146. inherited destroy;
  6147. end;
  6148. function tobjectdef.getcopy : tstoreddef;
  6149. var
  6150. i : longint;
  6151. begin
  6152. result:=cobjectdef.create(objecttype,objrealname^,childof,true);
  6153. { the constructor allocates a symtable which we release to avoid memory leaks }
  6154. tobjectdef(result).symtable.free;
  6155. tobjectdef(result).symtable:=symtable.getcopy;
  6156. if assigned(objextname) then
  6157. tobjectdef(result).objextname:=stringdup(objextname^);
  6158. if assigned(import_lib) then
  6159. tobjectdef(result).import_lib:=stringdup(import_lib^);
  6160. tobjectdef(result).objectoptions:=objectoptions;
  6161. include(tobjectdef(result).defoptions,df_copied_def);
  6162. tobjectdef(result).extendeddef:=extendeddef;
  6163. if assigned(tcinitcode) then
  6164. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  6165. tobjectdef(result).vmt_field:=vmt_field;
  6166. if assigned(iidguid) then
  6167. begin
  6168. new(tobjectdef(result).iidguid);
  6169. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  6170. end;
  6171. if assigned(iidstr) then
  6172. tobjectdef(result).iidstr:=stringdup(iidstr^);
  6173. tobjectdef(result).abstractcnt:=abstractcnt;
  6174. if assigned(ImplementedInterfaces) then
  6175. begin
  6176. for i:=0 to ImplementedInterfaces.count-1 do
  6177. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  6178. end;
  6179. if assigned(vmtentries) then
  6180. begin
  6181. tobjectdef(result).vmtentries:=TFPList.Create;
  6182. tobjectdef(result).copyvmtentries(self);
  6183. end;
  6184. end;
  6185. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  6186. var
  6187. i : longint;
  6188. vmtentry : pvmtentry;
  6189. ImplIntf : TImplementedInterface;
  6190. old_do_indirect_crc: boolean;
  6191. begin
  6192. { if class1 in unit A changes, and class2 in unit B inherits from it
  6193. (so unit B uses unit A), then unit B with class2 will be recompiled.
  6194. However, if there is also a class3 in unit C that only depends on
  6195. unit B, then unit C will not be recompiled because nothing changed
  6196. to the interface of unit B. Nevertheless, unit C can indirectly
  6197. depend on unit A via derefs, and these must be updated -> the
  6198. indirect crc keeps track of such changes. }
  6199. old_do_indirect_crc:=ppufile.do_indirect_crc;
  6200. ppufile.do_indirect_crc:=true;
  6201. inherited ppuwrite(ppufile);
  6202. ppufile.putbyte(byte(objecttype));
  6203. ppufile.putbyte(byte(helpertype));
  6204. if assigned(objextname) then
  6205. ppufile.putstring(objextname^)
  6206. else
  6207. ppufile.putstring('');
  6208. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  6209. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  6210. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  6211. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  6212. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignmin));
  6213. ppufile.putderef(vmt_fieldderef);
  6214. ppufile.putderef(childofderef);
  6215. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6216. begin
  6217. ppufile.putguid(iidguid^);
  6218. ppufile.putstring(iidstr^);
  6219. end;
  6220. ppufile.putlongint(abstractcnt);
  6221. if objecttype=odt_helper then
  6222. ppufile.putderef(extendeddefderef);
  6223. ppufile.putlongint(vmtentries.count);
  6224. for i:=0 to vmtentries.count-1 do
  6225. begin
  6226. vmtentry:=pvmtentry(vmtentries[i]);
  6227. ppufile.putderef(vmtentry^.procdefderef);
  6228. ppufile.putbyte(byte(vmtentry^.visibility));
  6229. end;
  6230. if assigned(ImplementedInterfaces) then
  6231. begin
  6232. ppufile.putlongint(ImplementedInterfaces.Count);
  6233. for i:=0 to ImplementedInterfaces.Count-1 do
  6234. begin
  6235. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  6236. ppufile.putderef(ImplIntf.intfdefderef);
  6237. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  6238. ppufile.putlongint(ImplIntf.Ioffset);
  6239. ppufile.putbyte(byte(ImplIntf.IType));
  6240. end;
  6241. end;
  6242. if df_copied_def in defoptions then
  6243. ppufile.putderef(cloneddefderef);
  6244. writeentry(ppufile,ibobjectdef);
  6245. if not(df_copied_def in defoptions) then
  6246. tObjectSymtable(symtable).ppuwrite(ppufile);
  6247. ppufile.do_indirect_crc:=old_do_indirect_crc;
  6248. end;
  6249. function tobjectdef.GetTypeName:string;
  6250. begin
  6251. { in this case we will go in endless recursion, because then }
  6252. { there is no tsym associated yet with the def. It can occur }
  6253. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  6254. { instead of the actual type name }
  6255. if not assigned(typesym) then
  6256. result:='<Currently Parsed Class>'
  6257. else
  6258. result:=typesymbolprettyname;
  6259. end;
  6260. procedure tobjectdef.buildderef;
  6261. var
  6262. i : longint;
  6263. vmtentry : pvmtentry;
  6264. begin
  6265. inherited buildderef;
  6266. vmt_fieldderef.build(vmt_field);
  6267. childofderef.build(childof);
  6268. if df_copied_def in defoptions then
  6269. cloneddefderef.build(symtable.defowner)
  6270. else
  6271. tstoredsymtable(symtable).buildderef;
  6272. if objecttype=odt_helper then
  6273. extendeddefderef.build(extendeddef);
  6274. for i:=0 to vmtentries.count-1 do
  6275. begin
  6276. vmtentry:=pvmtentry(vmtentries[i]);
  6277. vmtentry^.procdefderef.build(vmtentry^.procdef);
  6278. end;
  6279. if assigned(ImplementedInterfaces) then
  6280. begin
  6281. for i:=0 to ImplementedInterfaces.count-1 do
  6282. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  6283. end;
  6284. end;
  6285. procedure tobjectdef.deref;
  6286. var
  6287. i : longint;
  6288. vmtentry : pvmtentry;
  6289. begin
  6290. inherited deref;
  6291. vmt_field:=tsym(vmt_fieldderef.resolve);
  6292. childof:=tobjectdef(childofderef.resolve);
  6293. if df_copied_def in defoptions then
  6294. begin
  6295. cloneddef:=tobjectdef(cloneddefderef.resolve);
  6296. symtable:=cloneddef.symtable.getcopy;
  6297. end
  6298. else
  6299. tstoredsymtable(symtable).deref(false);
  6300. if objecttype=odt_helper then
  6301. extendeddef:=tdef(extendeddefderef.resolve);
  6302. for i:=0 to vmtentries.count-1 do
  6303. begin
  6304. vmtentry:=pvmtentry(vmtentries[i]);
  6305. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  6306. end;
  6307. if assigned(ImplementedInterfaces) then
  6308. begin
  6309. for i:=0 to ImplementedInterfaces.count-1 do
  6310. TImplementedInterface(ImplementedInterfaces[i]).deref;
  6311. end;
  6312. end;
  6313. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  6314. var
  6315. pd: tprocdef absolute def;
  6316. st: tsymtable;
  6317. psym: tsym;
  6318. nname: TIDString;
  6319. begin
  6320. if (tdef(def).typ<>procdef) then
  6321. exit;
  6322. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  6323. owner = symtable in which objcclassdef is defined
  6324. }
  6325. st:=pd.owner.defowner.owner;
  6326. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  6327. { check for an existing procsym with our special name }
  6328. psym:=tsym(st.find(nname));
  6329. if not assigned(psym) then
  6330. begin
  6331. psym:=cprocsym.create(nname);
  6332. { avoid warning about this symbol being unused }
  6333. psym.IncRefCount;
  6334. { don't check for duplicates:
  6335. a) we checked above
  6336. b) in case we are in the implementation section of a unit, this
  6337. will also check for this symbol in the interface section
  6338. (since you normally cannot have symbols with the same name
  6339. both interface and implementation), and it's possible to
  6340. have class helpers for the same class in the interface and
  6341. in the implementation, and they cannot be merged since only
  6342. the once in the interface must be saved to the ppu/visible
  6343. from other units }
  6344. st.insert(psym,false);
  6345. end
  6346. else if (psym.typ<>procsym) then
  6347. internalerror(2009111501);
  6348. { add ourselves to this special procsym }
  6349. tprocsym(psym).procdeflist.add(def);
  6350. end;
  6351. procedure tobjectdef.derefimpl;
  6352. begin
  6353. inherited derefimpl;
  6354. { the procdefs are not owned by the class helper procsyms, so they
  6355. are not stored/restored either -> re-add them here }
  6356. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  6357. (oo_is_classhelper in objectoptions) then
  6358. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  6359. end;
  6360. procedure tobjectdef.resetvmtentries;
  6361. var
  6362. i : longint;
  6363. begin
  6364. for i:=0 to vmtentries.Count-1 do
  6365. Dispose(pvmtentry(vmtentries[i]));
  6366. vmtentries.clear;
  6367. end;
  6368. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  6369. var
  6370. i : longint;
  6371. vmtentry : pvmtentry;
  6372. begin
  6373. resetvmtentries;
  6374. vmtentries.count:=objdef.vmtentries.count;
  6375. for i:=0 to objdef.vmtentries.count-1 do
  6376. begin
  6377. new(vmtentry);
  6378. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  6379. vmtentries[i]:=vmtentry;
  6380. end;
  6381. end;
  6382. function tobjectdef.getparentdef:tdef;
  6383. begin
  6384. { TODO: Remove getparentdef hack}
  6385. { With 2 forward declared classes with the child class before the
  6386. parent class the child class is written earlier to the ppu. Leaving it
  6387. possible to have a reference to the parent class for property overriding,
  6388. but the parent class still has the childof not resolved yet (PFV) }
  6389. if childof=nil then
  6390. childof:=tobjectdef(childofderef.resolve);
  6391. result:=childof;
  6392. end;
  6393. procedure tobjectdef.prepareguid;
  6394. begin
  6395. { set up guid }
  6396. if not assigned(iidguid) then
  6397. begin
  6398. new(iidguid);
  6399. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  6400. end;
  6401. { setup iidstring }
  6402. if not assigned(iidstr) then
  6403. iidstr:=stringdup(''); { default is empty string }
  6404. end;
  6405. procedure tobjectdef.set_parent( c : tobjectdef);
  6406. begin
  6407. if assigned(childof) then
  6408. exit;
  6409. childof:=c;
  6410. if not assigned(c) then
  6411. exit;
  6412. { inherit options and status }
  6413. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  6414. { initially has the same number of abstract methods as the parent }
  6415. abstractcnt:=c.abstractcnt;
  6416. { add the data of the anchestor class/object }
  6417. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  6418. begin
  6419. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  6420. { inherit recordalignment }
  6421. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  6422. { if both the parent and this record use C-alignment, also inherit
  6423. the current field alignment }
  6424. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  6425. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  6426. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  6427. { the padding is not inherited for Objective-C classes (maybe not
  6428. for cppclass either?) }
  6429. if objecttype=odt_objcclass then
  6430. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  6431. if (oo_has_vmt in objectoptions) and
  6432. (oo_has_vmt in c.objectoptions) then
  6433. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  6434. { if parent has a vmt field then the offset is the same for the child PM }
  6435. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  6436. begin
  6437. vmt_field:=c.vmt_field;
  6438. include(objectoptions,oo_has_vmt);
  6439. end;
  6440. end;
  6441. end;
  6442. procedure tobjectdef.insertvmt;
  6443. begin
  6444. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6445. exit;
  6446. if (oo_has_vmt in objectoptions) then
  6447. internalerror(12345)
  6448. else
  6449. begin
  6450. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  6451. tObjectSymtable(symtable).fieldalignment);
  6452. if (tf_requires_proper_alignment in target_info.flags) then
  6453. begin
  6454. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  6455. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  6456. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  6457. end;
  6458. vmt_field:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[],true);
  6459. hidesym(vmt_field);
  6460. tObjectSymtable(symtable).insert(vmt_field);
  6461. tObjectSymtable(symtable).addfield(tfieldvarsym(vmt_field),vis_hidden);
  6462. include(objectoptions,oo_has_vmt);
  6463. end;
  6464. end;
  6465. function tobjectdef.vmt_offset: asizeint;
  6466. begin
  6467. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6468. result:=0
  6469. else if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  6470. result:=tfieldvarsym(vmt_field).fieldoffset
  6471. else
  6472. result:=tfieldvarsym(vmt_field).fieldoffset div 8;
  6473. end;
  6474. procedure tobjectdef.check_forwards;
  6475. begin
  6476. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  6477. inherited;
  6478. if (oo_is_forward in objectoptions) then
  6479. begin
  6480. { ok, in future, the forward can be resolved }
  6481. Message1(sym_e_class_forward_not_resolved,objrealname^);
  6482. exclude(objectoptions,oo_is_forward);
  6483. end;
  6484. end;
  6485. function tobjectdef.find_destructor: tprocdef;
  6486. var
  6487. objdef: tobjectdef;
  6488. begin
  6489. objdef:=self;
  6490. while assigned(objdef) do
  6491. begin
  6492. result:=objdef.find_procdef_bytype(potype_destructor);
  6493. if assigned(result) then
  6494. exit;
  6495. objdef:=objdef.childof;
  6496. end;
  6497. result:=nil;
  6498. end;
  6499. function tobjectdef.implements_any_interfaces: boolean;
  6500. begin
  6501. result := (ImplementedInterfaces.Count > 0) or
  6502. (assigned(childof) and childof.implements_any_interfaces);
  6503. end;
  6504. function tobjectdef.size : asizeint;
  6505. begin
  6506. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6507. result:=voidpointertype.size
  6508. else
  6509. result:=tObjectSymtable(symtable).datasize;
  6510. end;
  6511. function tobjectdef.alignment:shortint;
  6512. begin
  6513. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6514. alignment:=voidpointertype.alignment
  6515. else
  6516. alignment:=tObjectSymtable(symtable).recordalignment;
  6517. end;
  6518. function tobjectdef.vmtmethodoffset(index:longint):longint;
  6519. begin
  6520. { for offset of methods for classes, see rtl/inc/objpash.inc }
  6521. case objecttype of
  6522. odt_class:
  6523. { the +2*sizeof(pint) is size and -size }
  6524. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  6525. odt_helper,
  6526. odt_objcclass,
  6527. odt_objcprotocol:
  6528. vmtmethodoffset:=0;
  6529. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  6530. vmtmethodoffset:=index*voidcodepointertype.size;
  6531. odt_javaclass,
  6532. odt_interfacejava:
  6533. { invalid }
  6534. vmtmethodoffset:=-1;
  6535. else
  6536. { the +2*sizeof(pint) is size and -size }
  6537. {$ifdef WITHDMT}
  6538. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  6539. {$else WITHDMT}
  6540. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  6541. {$endif WITHDMT}
  6542. end;
  6543. end;
  6544. function tobjectdef.vmt_mangledname : TSymStr;
  6545. begin
  6546. if not(oo_has_vmt in objectoptions) then
  6547. Message1(parser_n_object_has_no_vmt,objrealname^);
  6548. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  6549. end;
  6550. function tobjectdef.vmt_def: trecorddef;
  6551. var
  6552. vmttypesym: tsym;
  6553. begin
  6554. vmttypesym:=tsym(get_top_level_symtable.Find('vmtdef$'+mangledparaname));
  6555. if not assigned(vmttypesym) or
  6556. (vmttypesym.typ<>symconst.typesym) or
  6557. (ttypesym(vmttypesym).typedef.typ<>recorddef) then
  6558. internalerror(2015052501);
  6559. result:=trecorddef(ttypesym(vmttypesym).typedef);
  6560. end;
  6561. function tobjectdef.needs_inittable : boolean;
  6562. var
  6563. hp : tobjectdef;
  6564. begin
  6565. case objecttype of
  6566. odt_helper,
  6567. odt_class :
  6568. needs_inittable:=false;
  6569. odt_dispinterface,
  6570. odt_interfacecom:
  6571. needs_inittable:=true;
  6572. odt_interfacecorba:
  6573. begin
  6574. hp:=childof;
  6575. while assigned(hp) do
  6576. begin
  6577. if hp=interface_iunknown then
  6578. begin
  6579. needs_inittable:=true;
  6580. exit;
  6581. end;
  6582. hp:=hp.childof;
  6583. end;
  6584. needs_inittable:=false;
  6585. end;
  6586. odt_object:
  6587. needs_inittable:=
  6588. tObjectSymtable(symtable).needs_init_final or
  6589. (assigned(childof) and
  6590. childof.needs_inittable);
  6591. odt_cppclass,
  6592. odt_objcclass,
  6593. odt_objcprotocol,
  6594. odt_javaclass,
  6595. odt_interfacejava:
  6596. needs_inittable:=false;
  6597. else
  6598. internalerror(200108267);
  6599. end;
  6600. end;
  6601. function tobjectdef.needs_separate_initrtti : boolean;
  6602. begin
  6603. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6604. end;
  6605. function tobjectdef.has_non_trivial_init_child(check_parent:boolean):boolean;
  6606. begin
  6607. if objecttype in [odt_class,odt_object] then
  6608. begin
  6609. result:=tobjectsymtable(symtable).has_non_trivial_init or
  6610. (check_parent and assigned(childof) and childof.has_non_trivial_init_child(true));
  6611. end
  6612. else
  6613. result:=false;
  6614. end;
  6615. function tobjectdef.rtti_mangledname(rt: trttitype): TSymStr;
  6616. begin
  6617. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  6618. result:=inherited rtti_mangledname(rt)
  6619. else
  6620. begin
  6621. { necessary in case of a dynamic array of nsobject, or
  6622. if an nsobject field appears in a record that needs
  6623. init/finalisation }
  6624. if rt=initrtti then
  6625. begin
  6626. result:=voidpointertype.rtti_mangledname(rt);
  6627. exit;
  6628. end;
  6629. if not(target_info.system in systems_objc_nfabi) then
  6630. begin
  6631. result:=target_asm.labelprefix;
  6632. case objecttype of
  6633. odt_objcclass:
  6634. begin
  6635. case rt of
  6636. objcclassrtti:
  6637. if not(oo_is_classhelper in objectoptions) then
  6638. result:=result+'_OBJC_CLASS_'
  6639. else
  6640. result:=result+'_OBJC_CATEGORY_';
  6641. objcmetartti:
  6642. if not(oo_is_classhelper in objectoptions) then
  6643. result:=result+'_OBJC_METACLASS_'
  6644. else
  6645. internalerror(2009111511);
  6646. else
  6647. internalerror(2009092302);
  6648. end;
  6649. end;
  6650. odt_objcprotocol:
  6651. result:=result+'_OBJC_PROTOCOL_';
  6652. else
  6653. ;
  6654. end;
  6655. end
  6656. else
  6657. begin
  6658. case objecttype of
  6659. odt_objcclass:
  6660. begin
  6661. if (oo_is_classhelper in objectoptions) and
  6662. (rt<>objcclassrtti) then
  6663. internalerror(2009111512);
  6664. case rt of
  6665. objcclassrtti:
  6666. if not(oo_is_classhelper in objectoptions) then
  6667. result:='_OBJC_CLASS_$_'
  6668. else
  6669. result:='_OBJC_$_CATEGORY_';
  6670. objcmetartti:
  6671. result:='_OBJC_METACLASS_$_';
  6672. objcclassrortti:
  6673. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  6674. objcmetarortti:
  6675. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  6676. else
  6677. internalerror(2009092303);
  6678. end;
  6679. end;
  6680. odt_objcprotocol:
  6681. begin
  6682. result:=lower(target_asm.labelprefix);
  6683. case rt of
  6684. objcclassrtti:
  6685. result:=result+'_OBJC_PROTOCOL_$_';
  6686. objcmetartti:
  6687. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  6688. else
  6689. internalerror(2009092501);
  6690. end;
  6691. end;
  6692. else
  6693. internalerror(2013113005);
  6694. end;
  6695. end;
  6696. result:=result+objextname^;
  6697. end;
  6698. end;
  6699. function tobjectdef.members_need_inittable : boolean;
  6700. begin
  6701. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  6702. end;
  6703. function tobjectdef.is_publishable : boolean;
  6704. begin
  6705. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  6706. end;
  6707. function tobjectdef.get_next_dispid: longint;
  6708. begin
  6709. inc(fcurrent_dispid);
  6710. result:=fcurrent_dispid;
  6711. end;
  6712. function tobjectdef.search_enumerator_get: tprocdef;
  6713. begin
  6714. result:=inherited;
  6715. if not assigned(result) and assigned(childof) then
  6716. result:=childof.search_enumerator_get;
  6717. end;
  6718. function tobjectdef.search_enumerator_move: tprocdef;
  6719. begin
  6720. result:=inherited;
  6721. if not assigned(result) and assigned(childof) then
  6722. result:=childof.search_enumerator_move;
  6723. end;
  6724. function tobjectdef.search_enumerator_current: tsym;
  6725. begin
  6726. result:=inherited;
  6727. if not assigned(result) and assigned(childof) then
  6728. result:=childof.search_enumerator_current;
  6729. end;
  6730. procedure tobjectdef.register_created_classref_type;
  6731. begin
  6732. if not classref_created_in_current_module then
  6733. begin
  6734. classref_created_in_current_module:=true;
  6735. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  6736. end;
  6737. end;
  6738. procedure tobjectdef.register_created_object_type;
  6739. begin
  6740. if not created_in_current_module then
  6741. begin
  6742. created_in_current_module:=true;
  6743. current_module.wpoinfo.addcreatedobjtype(self);
  6744. end;
  6745. end;
  6746. procedure tobjectdef.register_maybe_created_object_type;
  6747. begin
  6748. { if we know it has been created for sure, no need
  6749. to also record that it maybe can be created in
  6750. this module
  6751. }
  6752. if not (created_in_current_module) and
  6753. not (maybe_created_in_current_module) then
  6754. begin
  6755. maybe_created_in_current_module:=true;
  6756. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  6757. end;
  6758. end;
  6759. procedure tobjectdef.register_vmt_call(index: longint);
  6760. begin
  6761. if (is_object(self) or is_class(self)) then
  6762. current_module.wpoinfo.addcalledvmtentry(self,index);
  6763. end;
  6764. procedure check_and_finish_msg(data: tobject; arg: pointer);
  6765. var
  6766. def: tdef absolute data;
  6767. pd: tprocdef absolute data;
  6768. i,
  6769. paracount: longint;
  6770. begin
  6771. if (def.typ=procdef) then
  6772. begin
  6773. { add all messages also under a dummy name to the symtable in
  6774. which the objcclass/protocol/category is declared, so they can
  6775. be called via id.<name>
  6776. }
  6777. create_class_helper_for_procdef(pd,nil);
  6778. { we have to wait until now to set the mangled name because it
  6779. depends on the (possibly external) class name, which is defined
  6780. at the very end. }
  6781. if not(po_msgstr in pd.procoptions) then
  6782. begin
  6783. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  6784. { recover to avoid internalerror later on }
  6785. include(pd.procoptions,po_msgstr);
  6786. pd.messageinf.str:=stringdup('MissingDeclaration');
  6787. end;
  6788. { Mangled name is already set in case this is a copy of
  6789. another type. }
  6790. if not(po_has_mangledname in pd.procoptions) then
  6791. begin
  6792. { check whether the number of formal parameters is correct,
  6793. and whether they have valid Objective-C types }
  6794. paracount:=0;
  6795. for i:=1 to length(pd.messageinf.str^) do
  6796. if pd.messageinf.str^[i]=':' then
  6797. inc(paracount);
  6798. for i:=0 to pd.paras.count-1 do
  6799. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  6800. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  6801. dec(paracount);
  6802. if (paracount<>0) then
  6803. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  6804. pd.setmangledname(pd.objcmangledname);
  6805. end
  6806. else
  6807. { all checks already done }
  6808. exit;
  6809. if not(oo_is_external in pd.struct.objectoptions) then
  6810. begin
  6811. if (po_varargs in pd.procoptions) then
  6812. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  6813. else
  6814. begin
  6815. { check for "array of const" parameters }
  6816. for i:=0 to pd.parast.symlist.count-1 do
  6817. begin
  6818. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  6819. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  6820. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  6821. end;
  6822. end;
  6823. end;
  6824. end;
  6825. end;
  6826. procedure mark_private_fields_used(data: tobject; arg: pointer);
  6827. var
  6828. sym: tsym absolute data;
  6829. begin
  6830. if (sym.typ=fieldvarsym) and
  6831. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  6832. sym.IncRefCount;
  6833. end;
  6834. procedure tobjectdef.finish_objc_data;
  6835. begin
  6836. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  6837. if (oo_is_external in objectoptions) then
  6838. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  6839. end;
  6840. procedure verify_objc_vardef(data: tobject; arg: pointer);
  6841. var
  6842. sym: tabstractvarsym absolute data;
  6843. res: pboolean absolute arg;
  6844. founderrordef: tdef;
  6845. begin
  6846. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  6847. exit;
  6848. if (sym.typ=paravarsym) and
  6849. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  6850. is_array_of_const(tparavarsym(sym).vardef)) then
  6851. exit;
  6852. if not objcchecktype(sym.vardef,founderrordef) then
  6853. begin
  6854. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6855. res^:=false;
  6856. end;
  6857. end;
  6858. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  6859. var
  6860. def: tdef absolute data;
  6861. res: pboolean absolute arg;
  6862. founderrordef: tdef;
  6863. begin
  6864. if (def.typ<>procdef) then
  6865. exit;
  6866. { check parameter types for validity }
  6867. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  6868. { check the result type for validity }
  6869. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  6870. begin
  6871. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6872. res^:=false;
  6873. end;
  6874. end;
  6875. function tobjectdef.check_objc_types: boolean;
  6876. begin
  6877. { done in separate step from finish_objc_data, because when
  6878. finish_objc_data is called, not all forwarddefs have been resolved
  6879. yet and we need to know all types here }
  6880. result:=true;
  6881. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  6882. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  6883. end;
  6884. procedure do_cpp_import_info(data: tobject; arg: pointer);
  6885. var
  6886. def: tdef absolute data;
  6887. pd: tprocdef absolute data;
  6888. begin
  6889. if (def.typ=procdef) then
  6890. begin
  6891. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  6892. if (oo_is_external in pd.struct.objectoptions) then
  6893. begin
  6894. { copied from psub.read_proc }
  6895. if assigned(tobjectdef(pd.struct).import_lib) then
  6896. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  6897. else
  6898. begin
  6899. { add import name to external list for DLL scanning }
  6900. if tf_has_dllscanner in target_info.flags then
  6901. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  6902. end;
  6903. end;
  6904. end;
  6905. end;
  6906. procedure tobjectdef.finish_cpp_data;
  6907. begin
  6908. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  6909. end;
  6910. {****************************************************************************
  6911. TImplementedInterface
  6912. ****************************************************************************}
  6913. function TImplementedInterface.GetIOffset: longint;
  6914. begin
  6915. if (fIOffset=-1) and
  6916. (IType in [etFieldValue,etFieldValueClass]) then
  6917. result:=tfieldvarsym(ImplementsField).fieldoffset
  6918. else
  6919. result:=fIOffset;
  6920. end;
  6921. constructor TImplementedInterface.create(aintf: tobjectdef);
  6922. begin
  6923. inherited create;
  6924. intfdef:=aintf;
  6925. intfdefderef.reset;
  6926. IOffset:=-1;
  6927. IType:=etStandard;
  6928. NameMappings:=nil;
  6929. procdefs:=nil;
  6930. end;
  6931. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  6932. begin
  6933. inherited create;
  6934. intfdef:=nil;
  6935. intfdefderef:=intfd;
  6936. ImplementsGetterDeref:=getterd;
  6937. IOffset:=-1;
  6938. IType:=etStandard;
  6939. NameMappings:=nil;
  6940. procdefs:=nil;
  6941. end;
  6942. destructor TImplementedInterface.destroy;
  6943. var
  6944. i : longint;
  6945. mappedname : pshortstring;
  6946. begin
  6947. if assigned(NameMappings) then
  6948. begin
  6949. for i:=0 to NameMappings.Count-1 do
  6950. begin
  6951. mappedname:=pshortstring(NameMappings[i]);
  6952. stringdispose(mappedname);
  6953. end;
  6954. NameMappings.free;
  6955. NameMappings:=nil;
  6956. end;
  6957. if assigned(procdefs) then
  6958. begin
  6959. procdefs.free;
  6960. procdefs:=nil;
  6961. end;
  6962. inherited destroy;
  6963. end;
  6964. procedure TImplementedInterface.buildderef;
  6965. begin
  6966. intfdefderef.build(intfdef);
  6967. ImplementsGetterDeref.build(ImplementsGetter);
  6968. end;
  6969. procedure TImplementedInterface.deref;
  6970. begin
  6971. intfdef:=tobjectdef(intfdefderef.resolve);
  6972. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6973. end;
  6974. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6975. begin
  6976. if not assigned(NameMappings) then
  6977. NameMappings:=TFPHashList.Create;
  6978. NameMappings.Add(origname,stringdup(newname));
  6979. end;
  6980. function TImplementedInterface.GetMapping(const origname: string):string;
  6981. var
  6982. mappedname : pshortstring;
  6983. begin
  6984. result:='';
  6985. if not assigned(NameMappings) then
  6986. exit;
  6987. mappedname:=PShortstring(NameMappings.Find(origname));
  6988. if assigned(mappedname) then
  6989. result:=mappedname^;
  6990. end;
  6991. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6992. begin
  6993. if not assigned(procdefs) then
  6994. procdefs:=TFPObjectList.Create(false);
  6995. { duplicate entries must be stored, because multiple }
  6996. { interfaces can declare methods with the same name }
  6997. { and all of these get their own VMT entry }
  6998. procdefs.Add(pd);
  6999. end;
  7000. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  7001. var
  7002. i : longint;
  7003. begin
  7004. result:=false;
  7005. { interfaces being implemented through delegation are not mergable (FK) }
  7006. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  7007. exit;
  7008. weight:=0;
  7009. { empty interface is mergeable }
  7010. if ProcDefs.Count=0 then
  7011. begin
  7012. result:=true;
  7013. exit;
  7014. end;
  7015. { The interface to merge must at least the number of
  7016. procedures of this interface }
  7017. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  7018. exit;
  7019. for i:=0 to ProcDefs.Count-1 do
  7020. begin
  7021. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  7022. exit;
  7023. end;
  7024. weight:=ProcDefs.Count;
  7025. result:=true;
  7026. end;
  7027. function TImplementedInterface.getcopy:TImplementedInterface;
  7028. begin
  7029. Result:=TImplementedInterface.Create(nil);
  7030. { 1) the procdefs list will be freed once for each copy
  7031. 2) since the procdefs list owns its elements, those will also be freed for each copy
  7032. 3) idem for the name mappings
  7033. }
  7034. { warning: this is completely wrong on so many levels...
  7035. Move(pointer(self)^,pointer(result)^,InstanceSize);
  7036. We need to make clean copies of the different fields
  7037. this is not implemented yet, and thus we generate an internal
  7038. error instead PM 2011-06-14 }
  7039. internalerror(2011061401);
  7040. end;
  7041. {****************************************************************************
  7042. TFORWARDDEF
  7043. ****************************************************************************}
  7044. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  7045. begin
  7046. inherited create(forwarddef,true);
  7047. tosymname:=stringdup(s);
  7048. forwardpos:=pos;
  7049. end;
  7050. function tforwarddef.GetTypeName:string;
  7051. begin
  7052. GetTypeName:='unresolved forward to '+tosymname^;
  7053. end;
  7054. destructor tforwarddef.destroy;
  7055. begin
  7056. stringdispose(tosymname);
  7057. inherited destroy;
  7058. end;
  7059. function tforwarddef.getcopy:tstoreddef;
  7060. begin
  7061. result:=cforwarddef.create(tosymname^, forwardpos);
  7062. end;
  7063. {****************************************************************************
  7064. TUNDEFINEDDEF
  7065. ****************************************************************************}
  7066. constructor tundefineddef.create(doregister:boolean);
  7067. begin
  7068. inherited create(undefineddef,doregister);
  7069. end;
  7070. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  7071. begin
  7072. inherited ppuload(undefineddef,ppufile);
  7073. ppuload_platform(ppufile);
  7074. end;
  7075. function tundefineddef.GetTypeName:string;
  7076. begin
  7077. GetTypeName:='<undefined type>';
  7078. end;
  7079. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  7080. begin
  7081. inherited ppuwrite(ppufile);
  7082. writeentry(ppufile,ibundefineddef);
  7083. end;
  7084. {****************************************************************************
  7085. TERRORDEF
  7086. ****************************************************************************}
  7087. constructor terrordef.create;
  7088. begin
  7089. inherited create(errordef,true);
  7090. { prevent consecutive faults }
  7091. savesize:=1;
  7092. end;
  7093. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  7094. begin
  7095. { Can't write errordefs to ppu }
  7096. internalerror(200411063);
  7097. end;
  7098. function terrordef.GetTypeName:string;
  7099. begin
  7100. GetTypeName:='<erroneous type>';
  7101. end;
  7102. function terrordef.getmangledparaname:TSymStr;
  7103. begin
  7104. getmangledparaname:='error';
  7105. end;
  7106. {****************************************************************************
  7107. Definition Helpers
  7108. ****************************************************************************}
  7109. function is_interfacecom(def: tdef): boolean;
  7110. begin
  7111. is_interfacecom:=
  7112. assigned(def) and
  7113. (def.typ=objectdef) and
  7114. (tobjectdef(def).objecttype=odt_interfacecom);
  7115. end;
  7116. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  7117. begin
  7118. is_interfacecom_or_dispinterface:=
  7119. assigned(def) and
  7120. (def.typ=objectdef) and
  7121. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  7122. end;
  7123. function is_any_interface_kind(def: tdef): boolean;
  7124. begin
  7125. result:=
  7126. assigned(def) and
  7127. (def.typ=objectdef) and
  7128. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  7129. is_objccategory(def));
  7130. end;
  7131. function is_interfacecorba(def: tdef): boolean;
  7132. begin
  7133. is_interfacecorba:=
  7134. assigned(def) and
  7135. (def.typ=objectdef) and
  7136. (tobjectdef(def).objecttype=odt_interfacecorba);
  7137. end;
  7138. function is_interface(def: tdef): boolean;
  7139. begin
  7140. is_interface:=
  7141. assigned(def) and
  7142. (def.typ=objectdef) and
  7143. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  7144. end;
  7145. function is_dispinterface(def: tdef): boolean;
  7146. begin
  7147. result:=
  7148. assigned(def) and
  7149. (def.typ=objectdef) and
  7150. (tobjectdef(def).objecttype=odt_dispinterface);
  7151. end;
  7152. function is_class(def: tdef): boolean;
  7153. begin
  7154. is_class:=
  7155. assigned(def) and
  7156. (def.typ=objectdef) and
  7157. (tobjectdef(def).objecttype=odt_class);
  7158. end;
  7159. function is_object(def: tdef): boolean;
  7160. begin
  7161. is_object:=
  7162. assigned(def) and
  7163. (def.typ=objectdef) and
  7164. (tobjectdef(def).objecttype=odt_object);
  7165. end;
  7166. function is_cppclass(def: tdef): boolean;
  7167. begin
  7168. is_cppclass:=
  7169. assigned(def) and
  7170. (def.typ=objectdef) and
  7171. (tobjectdef(def).objecttype=odt_cppclass);
  7172. end;
  7173. function is_objcclass(def: tdef): boolean;
  7174. begin
  7175. is_objcclass:=
  7176. assigned(def) and
  7177. (def.typ=objectdef) and
  7178. (tobjectdef(def).objecttype=odt_objcclass);
  7179. end;
  7180. function is_objectpascal_helper(def: tdef): boolean;
  7181. begin
  7182. result:=
  7183. assigned(def) and
  7184. (def.typ=objectdef) and
  7185. (tobjectdef(def).objecttype=odt_helper);
  7186. end;
  7187. function is_objcclassref(def: tdef): boolean;
  7188. begin
  7189. is_objcclassref:=
  7190. assigned(def) and
  7191. (def.typ=classrefdef) and
  7192. is_objcclass(tclassrefdef(def).pointeddef);
  7193. end;
  7194. function is_objcprotocol(def: tdef): boolean;
  7195. begin
  7196. result:=
  7197. assigned(def) and
  7198. (def.typ=objectdef) and
  7199. (tobjectdef(def).objecttype=odt_objcprotocol);
  7200. end;
  7201. function is_objccategory(def: tdef): boolean;
  7202. begin
  7203. result:=
  7204. assigned(def) and
  7205. (def.typ=objectdef) and
  7206. { if used as a forward type }
  7207. ((tobjectdef(def).objecttype=odt_objccategory) or
  7208. { if used as after it has been resolved }
  7209. ((tobjectdef(def).objecttype=odt_objcclass) and
  7210. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  7211. end;
  7212. function is_objc_class_or_protocol(def: tdef): boolean;
  7213. begin
  7214. result:=
  7215. assigned(def) and
  7216. (def.typ=objectdef) and
  7217. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  7218. end;
  7219. function is_objc_protocol_or_category(def: tdef): boolean;
  7220. begin
  7221. result:=
  7222. assigned(def) and
  7223. (def.typ=objectdef) and
  7224. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  7225. ((tobjectdef(def).objecttype = odt_objcclass) and
  7226. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  7227. end;
  7228. function is_classhelper(def: tdef): boolean;
  7229. begin
  7230. result:=
  7231. is_objectpascal_helper(def) or
  7232. is_objccategory(def);
  7233. end;
  7234. function is_class_or_interface(def: tdef): boolean;
  7235. begin
  7236. result:=
  7237. assigned(def) and
  7238. (def.typ=objectdef) and
  7239. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  7240. end;
  7241. function is_class_or_interface_or_objc(def: tdef): boolean;
  7242. begin
  7243. result:=
  7244. assigned(def) and
  7245. (def.typ=objectdef) and
  7246. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  7247. end;
  7248. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  7249. begin
  7250. result:=
  7251. assigned(def) and
  7252. (def.typ=objectdef) and
  7253. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  7254. end;
  7255. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  7256. begin
  7257. result:=
  7258. assigned(def) and
  7259. (def.typ=objectdef) and
  7260. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  7261. end;
  7262. function is_class_or_interface_or_object(def: tdef): boolean;
  7263. begin
  7264. result:=
  7265. assigned(def) and
  7266. (def.typ=objectdef) and
  7267. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  7268. end;
  7269. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  7270. begin
  7271. result:=
  7272. assigned(def) and
  7273. (def.typ=objectdef) and
  7274. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  7275. end;
  7276. function is_implicit_pointer_object_type(def: tdef): boolean;
  7277. begin
  7278. result:=
  7279. assigned(def) and
  7280. (((def.typ=objectdef) and
  7281. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  7282. ((target_info.system in systems_jvm) and
  7283. (def.typ=recorddef)));
  7284. end;
  7285. function is_implicit_array_pointer(def: tdef): boolean;
  7286. begin
  7287. result:=is_dynamic_array(def) or is_dynamicstring(def);
  7288. end;
  7289. function is_class_or_object(def: tdef): boolean;
  7290. begin
  7291. result:=
  7292. assigned(def) and
  7293. (def.typ=objectdef) and
  7294. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  7295. end;
  7296. function is_record(def: tdef): boolean;
  7297. begin
  7298. result:=
  7299. assigned(def) and
  7300. (def.typ=recorddef);
  7301. end;
  7302. function is_javaclass(def: tdef): boolean;
  7303. begin
  7304. result:=
  7305. assigned(def) and
  7306. (def.typ=objectdef) and
  7307. (tobjectdef(def).objecttype=odt_javaclass);
  7308. end;
  7309. function is_javaclassref(def: tdef): boolean;
  7310. begin
  7311. is_javaclassref:=
  7312. assigned(def) and
  7313. (def.typ=classrefdef) and
  7314. is_javaclass(tclassrefdef(def).pointeddef);
  7315. end;
  7316. function is_javainterface(def: tdef): boolean;
  7317. begin
  7318. result:=
  7319. assigned(def) and
  7320. (def.typ=objectdef) and
  7321. (tobjectdef(def).objecttype=odt_interfacejava);
  7322. end;
  7323. function is_java_class_or_interface(def: tdef): boolean;
  7324. begin
  7325. result:=
  7326. assigned(def) and
  7327. (def.typ=objectdef) and
  7328. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  7329. end;
  7330. procedure loadobjctypes;
  7331. begin
  7332. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  7333. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  7334. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  7335. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  7336. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  7337. end;
  7338. procedure maybeloadcocoatypes;
  7339. var
  7340. tsym: ttypesym;
  7341. cocoaunit: string[15];
  7342. begin
  7343. if assigned(objc_fastenumeration) then
  7344. exit;
  7345. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim,system_aarch64_darwin,system_x86_64_iphonesim]) then
  7346. cocoaunit:='COCOAALL'
  7347. else
  7348. cocoaunit:='IPHONEALL';
  7349. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  7350. if assigned(tsym) then
  7351. objc_fastenumeration:=tobjectdef(tsym.typedef)
  7352. else
  7353. objc_fastenumeration:=nil;
  7354. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  7355. if assigned(tsym) then
  7356. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  7357. else
  7358. objc_fastenumerationstate:=nil;
  7359. end;
  7360. function use_vectorfpu(def : tdef) : boolean;
  7361. begin
  7362. {$ifdef x86}
  7363. {$define use_vectorfpuimplemented}
  7364. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  7365. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  7366. {$endif x86}
  7367. {$ifdef arm}
  7368. {$define use_vectorfpuimplemented}
  7369. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  7370. {$endif arm}
  7371. {$ifdef aarch64}
  7372. {$define use_vectorfpuimplemented}
  7373. use_vectorfpu:=true;
  7374. {$endif aarch64}
  7375. {$ifndef use_vectorfpuimplemented}
  7376. use_vectorfpu:=false;
  7377. {$endif}
  7378. end;
  7379. end.