symdef.pas 294 KB

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