2
0

symdef.pas 294 KB

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