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