class.c 290 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532
  1. /* Functions related to building classes and their related objects.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. Contributed by Michael Tiemann (tiemann@cygnus.com)
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* High-level class interface. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "options.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tm.h"
  31. #include "tree.h"
  32. #include "stringpool.h"
  33. #include "stor-layout.h"
  34. #include "attribs.h"
  35. #include "hash-table.h"
  36. #include "cp-tree.h"
  37. #include "flags.h"
  38. #include "toplev.h"
  39. #include "target.h"
  40. #include "convert.h"
  41. #include "hash-map.h"
  42. #include "is-a.h"
  43. #include "plugin-api.h"
  44. #include "hard-reg-set.h"
  45. #include "input.h"
  46. #include "function.h"
  47. #include "ipa-ref.h"
  48. #include "cgraph.h"
  49. #include "dumpfile.h"
  50. #include "splay-tree.h"
  51. #include "gimplify.h"
  52. #include "wide-int.h"
  53. /* The number of nested classes being processed. If we are not in the
  54. scope of any class, this is zero. */
  55. int current_class_depth;
  56. /* In order to deal with nested classes, we keep a stack of classes.
  57. The topmost entry is the innermost class, and is the entry at index
  58. CURRENT_CLASS_DEPTH */
  59. typedef struct class_stack_node {
  60. /* The name of the class. */
  61. tree name;
  62. /* The _TYPE node for the class. */
  63. tree type;
  64. /* The access specifier pending for new declarations in the scope of
  65. this class. */
  66. tree access;
  67. /* If were defining TYPE, the names used in this class. */
  68. splay_tree names_used;
  69. /* Nonzero if this class is no longer open, because of a call to
  70. push_to_top_level. */
  71. size_t hidden;
  72. }* class_stack_node_t;
  73. typedef struct vtbl_init_data_s
  74. {
  75. /* The base for which we're building initializers. */
  76. tree binfo;
  77. /* The type of the most-derived type. */
  78. tree derived;
  79. /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
  80. unless ctor_vtbl_p is true. */
  81. tree rtti_binfo;
  82. /* The negative-index vtable initializers built up so far. These
  83. are in order from least negative index to most negative index. */
  84. vec<constructor_elt, va_gc> *inits;
  85. /* The binfo for the virtual base for which we're building
  86. vcall offset initializers. */
  87. tree vbase;
  88. /* The functions in vbase for which we have already provided vcall
  89. offsets. */
  90. vec<tree, va_gc> *fns;
  91. /* The vtable index of the next vcall or vbase offset. */
  92. tree index;
  93. /* Nonzero if we are building the initializer for the primary
  94. vtable. */
  95. int primary_vtbl_p;
  96. /* Nonzero if we are building the initializer for a construction
  97. vtable. */
  98. int ctor_vtbl_p;
  99. /* True when adding vcall offset entries to the vtable. False when
  100. merely computing the indices. */
  101. bool generate_vcall_entries;
  102. } vtbl_init_data;
  103. /* The type of a function passed to walk_subobject_offsets. */
  104. typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
  105. /* The stack itself. This is a dynamically resized array. The
  106. number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
  107. static int current_class_stack_size;
  108. static class_stack_node_t current_class_stack;
  109. /* The size of the largest empty class seen in this translation unit. */
  110. static GTY (()) tree sizeof_biggest_empty_class;
  111. /* An array of all local classes present in this translation unit, in
  112. declaration order. */
  113. vec<tree, va_gc> *local_classes;
  114. static tree get_vfield_name (tree);
  115. static void finish_struct_anon (tree);
  116. static tree get_vtable_name (tree);
  117. static void get_basefndecls (tree, tree, vec<tree> *);
  118. static int build_primary_vtable (tree, tree);
  119. static int build_secondary_vtable (tree);
  120. static void finish_vtbls (tree);
  121. static void modify_vtable_entry (tree, tree, tree, tree, tree *);
  122. static void finish_struct_bits (tree);
  123. static int alter_access (tree, tree, tree);
  124. static void handle_using_decl (tree, tree);
  125. static tree dfs_modify_vtables (tree, void *);
  126. static tree modify_all_vtables (tree, tree);
  127. static void determine_primary_bases (tree);
  128. static void finish_struct_methods (tree);
  129. static void maybe_warn_about_overly_private_class (tree);
  130. static int method_name_cmp (const void *, const void *);
  131. static int resort_method_name_cmp (const void *, const void *);
  132. static void add_implicitly_declared_members (tree, tree*, int, int);
  133. static tree fixed_type_or_null (tree, int *, int *);
  134. static tree build_simple_base_path (tree expr, tree binfo);
  135. static tree build_vtbl_ref_1 (tree, tree);
  136. static void build_vtbl_initializer (tree, tree, tree, tree, int *,
  137. vec<constructor_elt, va_gc> **);
  138. static int count_fields (tree);
  139. static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
  140. static void insert_into_classtype_sorted_fields (tree, tree, int);
  141. static bool check_bitfield_decl (tree);
  142. static void check_field_decl (tree, tree, int *, int *, int *);
  143. static void check_field_decls (tree, tree *, int *, int *);
  144. static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
  145. static void build_base_fields (record_layout_info, splay_tree, tree *);
  146. static void check_methods (tree);
  147. static void remove_zero_width_bit_fields (tree);
  148. static bool accessible_nvdtor_p (tree);
  149. static void check_bases (tree, int *, int *);
  150. static void check_bases_and_members (tree);
  151. static tree create_vtable_ptr (tree, tree *);
  152. static void include_empty_classes (record_layout_info);
  153. static void layout_class_type (tree, tree *);
  154. static void propagate_binfo_offsets (tree, tree);
  155. static void layout_virtual_bases (record_layout_info, splay_tree);
  156. static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
  157. static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
  158. static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
  159. static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
  160. static void add_vcall_offset (tree, tree, vtbl_init_data *);
  161. static void layout_vtable_decl (tree, int);
  162. static tree dfs_find_final_overrider_pre (tree, void *);
  163. static tree dfs_find_final_overrider_post (tree, void *);
  164. static tree find_final_overrider (tree, tree, tree);
  165. static int make_new_vtable (tree, tree);
  166. static tree get_primary_binfo (tree);
  167. static int maybe_indent_hierarchy (FILE *, int, int);
  168. static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
  169. static void dump_class_hierarchy (tree);
  170. static void dump_class_hierarchy_1 (FILE *, int, tree);
  171. static void dump_array (FILE *, tree);
  172. static void dump_vtable (tree, tree, tree);
  173. static void dump_vtt (tree, tree);
  174. static void dump_thunk (FILE *, int, tree);
  175. static tree build_vtable (tree, tree, tree);
  176. static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
  177. static void layout_nonempty_base_or_field (record_layout_info,
  178. tree, tree, splay_tree);
  179. static tree end_of_class (tree, int);
  180. static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
  181. static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
  182. vec<constructor_elt, va_gc> **);
  183. static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
  184. vec<constructor_elt, va_gc> **);
  185. static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
  186. static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
  187. static void clone_constructors_and_destructors (tree);
  188. static tree build_clone (tree, tree);
  189. static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
  190. static void build_ctor_vtbl_group (tree, tree);
  191. static void build_vtt (tree);
  192. static tree binfo_ctor_vtable (tree);
  193. static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
  194. tree *);
  195. static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
  196. static tree dfs_fixup_binfo_vtbls (tree, void *);
  197. static int record_subobject_offset (tree, tree, splay_tree);
  198. static int check_subobject_offset (tree, tree, splay_tree);
  199. static int walk_subobject_offsets (tree, subobject_offset_fn,
  200. tree, splay_tree, tree, int);
  201. static void record_subobject_offsets (tree, tree, splay_tree, bool);
  202. static int layout_conflict_p (tree, tree, splay_tree, int);
  203. static int splay_tree_compare_integer_csts (splay_tree_key k1,
  204. splay_tree_key k2);
  205. static void warn_about_ambiguous_bases (tree);
  206. static bool type_requires_array_cookie (tree);
  207. static bool base_derived_from (tree, tree);
  208. static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
  209. static tree end_of_base (tree);
  210. static tree get_vcall_index (tree, tree);
  211. /* Variables shared between class.c and call.c. */
  212. int n_vtables = 0;
  213. int n_vtable_entries = 0;
  214. int n_vtable_searches = 0;
  215. int n_vtable_elems = 0;
  216. int n_convert_harshness = 0;
  217. int n_compute_conversion_costs = 0;
  218. int n_inner_fields_searched = 0;
  219. /* Convert to or from a base subobject. EXPR is an expression of type
  220. `A' or `A*', an expression of type `B' or `B*' is returned. To
  221. convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
  222. the B base instance within A. To convert base A to derived B, CODE
  223. is MINUS_EXPR and BINFO is the binfo for the A instance within B.
  224. In this latter case, A must not be a morally virtual base of B.
  225. NONNULL is true if EXPR is known to be non-NULL (this is only
  226. needed when EXPR is of pointer type). CV qualifiers are preserved
  227. from EXPR. */
  228. tree
  229. build_base_path (enum tree_code code,
  230. tree expr,
  231. tree binfo,
  232. int nonnull,
  233. tsubst_flags_t complain)
  234. {
  235. tree v_binfo = NULL_TREE;
  236. tree d_binfo = NULL_TREE;
  237. tree probe;
  238. tree offset;
  239. tree target_type;
  240. tree null_test = NULL;
  241. tree ptr_target_type;
  242. int fixed_type_p;
  243. int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
  244. bool has_empty = false;
  245. bool virtual_access;
  246. bool rvalue = false;
  247. if (expr == error_mark_node || binfo == error_mark_node || !binfo)
  248. return error_mark_node;
  249. for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
  250. {
  251. d_binfo = probe;
  252. if (is_empty_class (BINFO_TYPE (probe)))
  253. has_empty = true;
  254. if (!v_binfo && BINFO_VIRTUAL_P (probe))
  255. v_binfo = probe;
  256. }
  257. probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
  258. if (want_pointer)
  259. probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
  260. if (code == PLUS_EXPR
  261. && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
  262. {
  263. /* This can happen when adjust_result_of_qualified_name_lookup can't
  264. find a unique base binfo in a call to a member function. We
  265. couldn't give the diagnostic then since we might have been calling
  266. a static member function, so we do it now. */
  267. if (complain & tf_error)
  268. {
  269. tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
  270. ba_unique, NULL, complain);
  271. gcc_assert (base == error_mark_node);
  272. }
  273. return error_mark_node;
  274. }
  275. gcc_assert ((code == MINUS_EXPR
  276. && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
  277. || code == PLUS_EXPR);
  278. if (binfo == d_binfo)
  279. /* Nothing to do. */
  280. return expr;
  281. if (code == MINUS_EXPR && v_binfo)
  282. {
  283. if (complain & tf_error)
  284. {
  285. if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
  286. {
  287. if (want_pointer)
  288. error ("cannot convert from pointer to base class %qT to "
  289. "pointer to derived class %qT because the base is "
  290. "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
  291. else
  292. error ("cannot convert from base class %qT to derived "
  293. "class %qT because the base is virtual",
  294. BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
  295. }
  296. else
  297. {
  298. if (want_pointer)
  299. error ("cannot convert from pointer to base class %qT to "
  300. "pointer to derived class %qT via virtual base %qT",
  301. BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
  302. BINFO_TYPE (v_binfo));
  303. else
  304. error ("cannot convert from base class %qT to derived "
  305. "class %qT via virtual base %qT", BINFO_TYPE (binfo),
  306. BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
  307. }
  308. }
  309. return error_mark_node;
  310. }
  311. if (!want_pointer)
  312. {
  313. rvalue = !real_lvalue_p (expr);
  314. /* This must happen before the call to save_expr. */
  315. expr = cp_build_addr_expr (expr, complain);
  316. }
  317. else
  318. expr = mark_rvalue_use (expr);
  319. offset = BINFO_OFFSET (binfo);
  320. fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
  321. target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
  322. /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
  323. cv-unqualified. Extract the cv-qualifiers from EXPR so that the
  324. expression returned matches the input. */
  325. target_type = cp_build_qualified_type
  326. (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
  327. ptr_target_type = build_pointer_type (target_type);
  328. /* Do we need to look in the vtable for the real offset? */
  329. virtual_access = (v_binfo && fixed_type_p <= 0);
  330. /* Don't bother with the calculations inside sizeof; they'll ICE if the
  331. source type is incomplete and the pointer value doesn't matter. In a
  332. template (even in instantiate_non_dependent_expr), we don't have vtables
  333. set up properly yet, and the value doesn't matter there either; we're
  334. just interested in the result of overload resolution. */
  335. if (cp_unevaluated_operand != 0
  336. || in_template_function ())
  337. {
  338. expr = build_nop (ptr_target_type, expr);
  339. goto indout;
  340. }
  341. /* If we're in an NSDMI, we don't have the full constructor context yet
  342. that we need for converting to a virtual base, so just build a stub
  343. CONVERT_EXPR and expand it later in bot_replace. */
  344. if (virtual_access && fixed_type_p < 0
  345. && current_scope () != current_function_decl)
  346. {
  347. expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
  348. CONVERT_EXPR_VBASE_PATH (expr) = true;
  349. goto indout;
  350. }
  351. /* Do we need to check for a null pointer? */
  352. if (want_pointer && !nonnull)
  353. {
  354. /* If we know the conversion will not actually change the value
  355. of EXPR, then we can avoid testing the expression for NULL.
  356. We have to avoid generating a COMPONENT_REF for a base class
  357. field, because other parts of the compiler know that such
  358. expressions are always non-NULL. */
  359. if (!virtual_access && integer_zerop (offset))
  360. return build_nop (ptr_target_type, expr);
  361. null_test = error_mark_node;
  362. }
  363. /* Protect against multiple evaluation if necessary. */
  364. if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
  365. expr = save_expr (expr);
  366. /* Now that we've saved expr, build the real null test. */
  367. if (null_test)
  368. {
  369. tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
  370. null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  371. expr, zero);
  372. }
  373. /* If this is a simple base reference, express it as a COMPONENT_REF. */
  374. if (code == PLUS_EXPR && !virtual_access
  375. /* We don't build base fields for empty bases, and they aren't very
  376. interesting to the optimizers anyway. */
  377. && !has_empty)
  378. {
  379. expr = cp_build_indirect_ref (expr, RO_NULL, complain);
  380. expr = build_simple_base_path (expr, binfo);
  381. if (rvalue)
  382. expr = move (expr);
  383. if (want_pointer)
  384. expr = build_address (expr);
  385. target_type = TREE_TYPE (expr);
  386. goto out;
  387. }
  388. if (virtual_access)
  389. {
  390. /* Going via virtual base V_BINFO. We need the static offset
  391. from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
  392. V_BINFO. That offset is an entry in D_BINFO's vtable. */
  393. tree v_offset;
  394. if (fixed_type_p < 0 && in_base_initializer)
  395. {
  396. /* In a base member initializer, we cannot rely on the
  397. vtable being set up. We have to indirect via the
  398. vtt_parm. */
  399. tree t;
  400. t = TREE_TYPE (TYPE_VFIELD (current_class_type));
  401. t = build_pointer_type (t);
  402. v_offset = convert (t, current_vtt_parm);
  403. v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
  404. }
  405. else
  406. {
  407. tree t = expr;
  408. if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
  409. {
  410. t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
  411. probe, expr);
  412. if (t == NULL_TREE)
  413. t = expr;
  414. }
  415. v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
  416. complain),
  417. TREE_TYPE (TREE_TYPE (expr)));
  418. }
  419. if (v_offset == error_mark_node)
  420. return error_mark_node;
  421. v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
  422. v_offset = build1 (NOP_EXPR,
  423. build_pointer_type (ptrdiff_type_node),
  424. v_offset);
  425. v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
  426. TREE_CONSTANT (v_offset) = 1;
  427. offset = convert_to_integer (ptrdiff_type_node,
  428. size_diffop_loc (input_location, offset,
  429. BINFO_OFFSET (v_binfo)));
  430. if (!integer_zerop (offset))
  431. v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
  432. if (fixed_type_p < 0)
  433. /* Negative fixed_type_p means this is a constructor or destructor;
  434. virtual base layout is fixed in in-charge [cd]tors, but not in
  435. base [cd]tors. */
  436. offset = build3 (COND_EXPR, ptrdiff_type_node,
  437. build2 (EQ_EXPR, boolean_type_node,
  438. current_in_charge_parm, integer_zero_node),
  439. v_offset,
  440. convert_to_integer (ptrdiff_type_node,
  441. BINFO_OFFSET (binfo)));
  442. else
  443. offset = v_offset;
  444. }
  445. if (want_pointer)
  446. target_type = ptr_target_type;
  447. expr = build1 (NOP_EXPR, ptr_target_type, expr);
  448. if (!integer_zerop (offset))
  449. {
  450. offset = fold_convert (sizetype, offset);
  451. if (code == MINUS_EXPR)
  452. offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
  453. expr = fold_build_pointer_plus (expr, offset);
  454. }
  455. else
  456. null_test = NULL;
  457. indout:
  458. if (!want_pointer)
  459. {
  460. expr = cp_build_indirect_ref (expr, RO_NULL, complain);
  461. if (rvalue)
  462. expr = move (expr);
  463. }
  464. out:
  465. if (null_test)
  466. expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
  467. build_zero_cst (target_type));
  468. return expr;
  469. }
  470. /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
  471. Perform a derived-to-base conversion by recursively building up a
  472. sequence of COMPONENT_REFs to the appropriate base fields. */
  473. static tree
  474. build_simple_base_path (tree expr, tree binfo)
  475. {
  476. tree type = BINFO_TYPE (binfo);
  477. tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
  478. tree field;
  479. if (d_binfo == NULL_TREE)
  480. {
  481. tree temp;
  482. gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
  483. /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
  484. into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
  485. an lvalue in the front end; only _DECLs and _REFs are lvalues
  486. in the back end. */
  487. temp = unary_complex_lvalue (ADDR_EXPR, expr);
  488. if (temp)
  489. expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
  490. return expr;
  491. }
  492. /* Recurse. */
  493. expr = build_simple_base_path (expr, d_binfo);
  494. for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
  495. field; field = DECL_CHAIN (field))
  496. /* Is this the base field created by build_base_field? */
  497. if (TREE_CODE (field) == FIELD_DECL
  498. && DECL_FIELD_IS_BASE (field)
  499. && TREE_TYPE (field) == type
  500. /* If we're looking for a field in the most-derived class,
  501. also check the field offset; we can have two base fields
  502. of the same type if one is an indirect virtual base and one
  503. is a direct non-virtual base. */
  504. && (BINFO_INHERITANCE_CHAIN (d_binfo)
  505. || tree_int_cst_equal (byte_position (field),
  506. BINFO_OFFSET (binfo))))
  507. {
  508. /* We don't use build_class_member_access_expr here, as that
  509. has unnecessary checks, and more importantly results in
  510. recursive calls to dfs_walk_once. */
  511. int type_quals = cp_type_quals (TREE_TYPE (expr));
  512. expr = build3 (COMPONENT_REF,
  513. cp_build_qualified_type (type, type_quals),
  514. expr, field, NULL_TREE);
  515. expr = fold_if_not_in_template (expr);
  516. /* Mark the expression const or volatile, as appropriate.
  517. Even though we've dealt with the type above, we still have
  518. to mark the expression itself. */
  519. if (type_quals & TYPE_QUAL_CONST)
  520. TREE_READONLY (expr) = 1;
  521. if (type_quals & TYPE_QUAL_VOLATILE)
  522. TREE_THIS_VOLATILE (expr) = 1;
  523. return expr;
  524. }
  525. /* Didn't find the base field?!? */
  526. gcc_unreachable ();
  527. }
  528. /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
  529. type is a class type or a pointer to a class type. In the former
  530. case, TYPE is also a class type; in the latter it is another
  531. pointer type. If CHECK_ACCESS is true, an error message is emitted
  532. if TYPE is inaccessible. If OBJECT has pointer type, the value is
  533. assumed to be non-NULL. */
  534. tree
  535. convert_to_base (tree object, tree type, bool check_access, bool nonnull,
  536. tsubst_flags_t complain)
  537. {
  538. tree binfo;
  539. tree object_type;
  540. if (TYPE_PTR_P (TREE_TYPE (object)))
  541. {
  542. object_type = TREE_TYPE (TREE_TYPE (object));
  543. type = TREE_TYPE (type);
  544. }
  545. else
  546. object_type = TREE_TYPE (object);
  547. binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
  548. NULL, complain);
  549. if (!binfo || binfo == error_mark_node)
  550. return error_mark_node;
  551. return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
  552. }
  553. /* EXPR is an expression with unqualified class type. BASE is a base
  554. binfo of that class type. Returns EXPR, converted to the BASE
  555. type. This function assumes that EXPR is the most derived class;
  556. therefore virtual bases can be found at their static offsets. */
  557. tree
  558. convert_to_base_statically (tree expr, tree base)
  559. {
  560. tree expr_type;
  561. expr_type = TREE_TYPE (expr);
  562. if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
  563. {
  564. /* If this is a non-empty base, use a COMPONENT_REF. */
  565. if (!is_empty_class (BINFO_TYPE (base)))
  566. return build_simple_base_path (expr, base);
  567. /* We use fold_build2 and fold_convert below to simplify the trees
  568. provided to the optimizers. It is not safe to call these functions
  569. when processing a template because they do not handle C++-specific
  570. trees. */
  571. gcc_assert (!processing_template_decl);
  572. expr = cp_build_addr_expr (expr, tf_warning_or_error);
  573. if (!integer_zerop (BINFO_OFFSET (base)))
  574. expr = fold_build_pointer_plus_loc (input_location,
  575. expr, BINFO_OFFSET (base));
  576. expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
  577. expr = build_fold_indirect_ref_loc (input_location, expr);
  578. }
  579. return expr;
  580. }
  581. tree
  582. build_vfield_ref (tree datum, tree type)
  583. {
  584. tree vfield, vcontext;
  585. if (datum == error_mark_node
  586. /* Can happen in case of duplicate base types (c++/59082). */
  587. || !TYPE_VFIELD (type))
  588. return error_mark_node;
  589. /* First, convert to the requested type. */
  590. if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
  591. datum = convert_to_base (datum, type, /*check_access=*/false,
  592. /*nonnull=*/true, tf_warning_or_error);
  593. /* Second, the requested type may not be the owner of its own vptr.
  594. If not, convert to the base class that owns it. We cannot use
  595. convert_to_base here, because VCONTEXT may appear more than once
  596. in the inheritance hierarchy of TYPE, and thus direct conversion
  597. between the types may be ambiguous. Following the path back up
  598. one step at a time via primary bases avoids the problem. */
  599. vfield = TYPE_VFIELD (type);
  600. vcontext = DECL_CONTEXT (vfield);
  601. while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
  602. {
  603. datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
  604. type = TREE_TYPE (datum);
  605. }
  606. return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
  607. }
  608. /* Given an object INSTANCE, return an expression which yields the
  609. vtable element corresponding to INDEX. There are many special
  610. cases for INSTANCE which we take care of here, mainly to avoid
  611. creating extra tree nodes when we don't have to. */
  612. static tree
  613. build_vtbl_ref_1 (tree instance, tree idx)
  614. {
  615. tree aref;
  616. tree vtbl = NULL_TREE;
  617. /* Try to figure out what a reference refers to, and
  618. access its virtual function table directly. */
  619. int cdtorp = 0;
  620. tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
  621. tree basetype = non_reference (TREE_TYPE (instance));
  622. if (fixed_type && !cdtorp)
  623. {
  624. tree binfo = lookup_base (fixed_type, basetype,
  625. ba_unique, NULL, tf_none);
  626. if (binfo && binfo != error_mark_node)
  627. vtbl = unshare_expr (BINFO_VTABLE (binfo));
  628. }
  629. if (!vtbl)
  630. vtbl = build_vfield_ref (instance, basetype);
  631. aref = build_array_ref (input_location, vtbl, idx);
  632. TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
  633. return aref;
  634. }
  635. tree
  636. build_vtbl_ref (tree instance, tree idx)
  637. {
  638. tree aref = build_vtbl_ref_1 (instance, idx);
  639. return aref;
  640. }
  641. /* Given a stable object pointer INSTANCE_PTR, return an expression which
  642. yields a function pointer corresponding to vtable element INDEX. */
  643. tree
  644. build_vfn_ref (tree instance_ptr, tree idx)
  645. {
  646. tree aref;
  647. aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
  648. tf_warning_or_error),
  649. idx);
  650. /* When using function descriptors, the address of the
  651. vtable entry is treated as a function pointer. */
  652. if (TARGET_VTABLE_USES_DESCRIPTORS)
  653. aref = build1 (NOP_EXPR, TREE_TYPE (aref),
  654. cp_build_addr_expr (aref, tf_warning_or_error));
  655. /* Remember this as a method reference, for later devirtualization. */
  656. aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
  657. return aref;
  658. }
  659. /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
  660. for the given TYPE. */
  661. static tree
  662. get_vtable_name (tree type)
  663. {
  664. return mangle_vtbl_for_type (type);
  665. }
  666. /* DECL is an entity associated with TYPE, like a virtual table or an
  667. implicitly generated constructor. Determine whether or not DECL
  668. should have external or internal linkage at the object file
  669. level. This routine does not deal with COMDAT linkage and other
  670. similar complexities; it simply sets TREE_PUBLIC if it possible for
  671. entities in other translation units to contain copies of DECL, in
  672. the abstract. */
  673. void
  674. set_linkage_according_to_type (tree /*type*/, tree decl)
  675. {
  676. TREE_PUBLIC (decl) = 1;
  677. determine_visibility (decl);
  678. }
  679. /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
  680. (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
  681. Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
  682. static tree
  683. build_vtable (tree class_type, tree name, tree vtable_type)
  684. {
  685. tree decl;
  686. decl = build_lang_decl (VAR_DECL, name, vtable_type);
  687. /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
  688. now to avoid confusion in mangle_decl. */
  689. SET_DECL_ASSEMBLER_NAME (decl, name);
  690. DECL_CONTEXT (decl) = class_type;
  691. DECL_ARTIFICIAL (decl) = 1;
  692. TREE_STATIC (decl) = 1;
  693. TREE_READONLY (decl) = 1;
  694. DECL_VIRTUAL_P (decl) = 1;
  695. DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
  696. DECL_USER_ALIGN (decl) = true;
  697. DECL_VTABLE_OR_VTT_P (decl) = 1;
  698. set_linkage_according_to_type (class_type, decl);
  699. /* The vtable has not been defined -- yet. */
  700. DECL_EXTERNAL (decl) = 1;
  701. DECL_NOT_REALLY_EXTERN (decl) = 1;
  702. /* Mark the VAR_DECL node representing the vtable itself as a
  703. "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
  704. is rather important that such things be ignored because any
  705. effort to actually generate DWARF for them will run into
  706. trouble when/if we encounter code like:
  707. #pragma interface
  708. struct S { virtual void member (); };
  709. because the artificial declaration of the vtable itself (as
  710. manufactured by the g++ front end) will say that the vtable is
  711. a static member of `S' but only *after* the debug output for
  712. the definition of `S' has already been output. This causes
  713. grief because the DWARF entry for the definition of the vtable
  714. will try to refer back to an earlier *declaration* of the
  715. vtable as a static member of `S' and there won't be one. We
  716. might be able to arrange to have the "vtable static member"
  717. attached to the member list for `S' before the debug info for
  718. `S' get written (which would solve the problem) but that would
  719. require more intrusive changes to the g++ front end. */
  720. DECL_IGNORED_P (decl) = 1;
  721. return decl;
  722. }
  723. /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
  724. or even complete. If this does not exist, create it. If COMPLETE is
  725. nonzero, then complete the definition of it -- that will render it
  726. impossible to actually build the vtable, but is useful to get at those
  727. which are known to exist in the runtime. */
  728. tree
  729. get_vtable_decl (tree type, int complete)
  730. {
  731. tree decl;
  732. if (CLASSTYPE_VTABLES (type))
  733. return CLASSTYPE_VTABLES (type);
  734. decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
  735. CLASSTYPE_VTABLES (type) = decl;
  736. if (complete)
  737. {
  738. DECL_EXTERNAL (decl) = 1;
  739. cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
  740. }
  741. return decl;
  742. }
  743. /* Build the primary virtual function table for TYPE. If BINFO is
  744. non-NULL, build the vtable starting with the initial approximation
  745. that it is the same as the one which is the head of the association
  746. list. Returns a nonzero value if a new vtable is actually
  747. created. */
  748. static int
  749. build_primary_vtable (tree binfo, tree type)
  750. {
  751. tree decl;
  752. tree virtuals;
  753. decl = get_vtable_decl (type, /*complete=*/0);
  754. if (binfo)
  755. {
  756. if (BINFO_NEW_VTABLE_MARKED (binfo))
  757. /* We have already created a vtable for this base, so there's
  758. no need to do it again. */
  759. return 0;
  760. virtuals = copy_list (BINFO_VIRTUALS (binfo));
  761. TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
  762. DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
  763. DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
  764. }
  765. else
  766. {
  767. gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
  768. virtuals = NULL_TREE;
  769. }
  770. if (GATHER_STATISTICS)
  771. {
  772. n_vtables += 1;
  773. n_vtable_elems += list_length (virtuals);
  774. }
  775. /* Initialize the association list for this type, based
  776. on our first approximation. */
  777. BINFO_VTABLE (TYPE_BINFO (type)) = decl;
  778. BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
  779. SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
  780. return 1;
  781. }
  782. /* Give BINFO a new virtual function table which is initialized
  783. with a skeleton-copy of its original initialization. The only
  784. entry that changes is the `delta' entry, so we can really
  785. share a lot of structure.
  786. FOR_TYPE is the most derived type which caused this table to
  787. be needed.
  788. Returns nonzero if we haven't met BINFO before.
  789. The order in which vtables are built (by calling this function) for
  790. an object must remain the same, otherwise a binary incompatibility
  791. can result. */
  792. static int
  793. build_secondary_vtable (tree binfo)
  794. {
  795. if (BINFO_NEW_VTABLE_MARKED (binfo))
  796. /* We already created a vtable for this base. There's no need to
  797. do it again. */
  798. return 0;
  799. /* Remember that we've created a vtable for this BINFO, so that we
  800. don't try to do so again. */
  801. SET_BINFO_NEW_VTABLE_MARKED (binfo);
  802. /* Make fresh virtual list, so we can smash it later. */
  803. BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
  804. /* Secondary vtables are laid out as part of the same structure as
  805. the primary vtable. */
  806. BINFO_VTABLE (binfo) = NULL_TREE;
  807. return 1;
  808. }
  809. /* Create a new vtable for BINFO which is the hierarchy dominated by
  810. T. Return nonzero if we actually created a new vtable. */
  811. static int
  812. make_new_vtable (tree t, tree binfo)
  813. {
  814. if (binfo == TYPE_BINFO (t))
  815. /* In this case, it is *type*'s vtable we are modifying. We start
  816. with the approximation that its vtable is that of the
  817. immediate base class. */
  818. return build_primary_vtable (binfo, t);
  819. else
  820. /* This is our very own copy of `basetype' to play with. Later,
  821. we will fill in all the virtual functions that override the
  822. virtual functions in these base classes which are not defined
  823. by the current type. */
  824. return build_secondary_vtable (binfo);
  825. }
  826. /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
  827. (which is in the hierarchy dominated by T) list FNDECL as its
  828. BV_FN. DELTA is the required constant adjustment from the `this'
  829. pointer where the vtable entry appears to the `this' required when
  830. the function is actually called. */
  831. static void
  832. modify_vtable_entry (tree t,
  833. tree binfo,
  834. tree fndecl,
  835. tree delta,
  836. tree *virtuals)
  837. {
  838. tree v;
  839. v = *virtuals;
  840. if (fndecl != BV_FN (v)
  841. || !tree_int_cst_equal (delta, BV_DELTA (v)))
  842. {
  843. /* We need a new vtable for BINFO. */
  844. if (make_new_vtable (t, binfo))
  845. {
  846. /* If we really did make a new vtable, we also made a copy
  847. of the BINFO_VIRTUALS list. Now, we have to find the
  848. corresponding entry in that list. */
  849. *virtuals = BINFO_VIRTUALS (binfo);
  850. while (BV_FN (*virtuals) != BV_FN (v))
  851. *virtuals = TREE_CHAIN (*virtuals);
  852. v = *virtuals;
  853. }
  854. BV_DELTA (v) = delta;
  855. BV_VCALL_INDEX (v) = NULL_TREE;
  856. BV_FN (v) = fndecl;
  857. }
  858. }
  859. /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
  860. the USING_DECL naming METHOD. Returns true if the method could be
  861. added to the method vec. */
  862. bool
  863. add_method (tree type, tree method, tree using_decl)
  864. {
  865. unsigned slot;
  866. tree overload;
  867. bool template_conv_p = false;
  868. bool conv_p;
  869. vec<tree, va_gc> *method_vec;
  870. bool complete_p;
  871. bool insert_p = false;
  872. tree current_fns;
  873. tree fns;
  874. if (method == error_mark_node)
  875. return false;
  876. complete_p = COMPLETE_TYPE_P (type);
  877. conv_p = DECL_CONV_FN_P (method);
  878. if (conv_p)
  879. template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
  880. && DECL_TEMPLATE_CONV_FN_P (method));
  881. method_vec = CLASSTYPE_METHOD_VEC (type);
  882. if (!method_vec)
  883. {
  884. /* Make a new method vector. We start with 8 entries. We must
  885. allocate at least two (for constructors and destructors), and
  886. we're going to end up with an assignment operator at some
  887. point as well. */
  888. vec_alloc (method_vec, 8);
  889. /* Create slots for constructors and destructors. */
  890. method_vec->quick_push (NULL_TREE);
  891. method_vec->quick_push (NULL_TREE);
  892. CLASSTYPE_METHOD_VEC (type) = method_vec;
  893. }
  894. /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
  895. grok_special_member_properties (method);
  896. /* Constructors and destructors go in special slots. */
  897. if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
  898. slot = CLASSTYPE_CONSTRUCTOR_SLOT;
  899. else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
  900. {
  901. slot = CLASSTYPE_DESTRUCTOR_SLOT;
  902. if (TYPE_FOR_JAVA (type))
  903. {
  904. if (!DECL_ARTIFICIAL (method))
  905. error ("Java class %qT cannot have a destructor", type);
  906. else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  907. error ("Java class %qT cannot have an implicit non-trivial "
  908. "destructor",
  909. type);
  910. }
  911. }
  912. else
  913. {
  914. tree m;
  915. insert_p = true;
  916. /* See if we already have an entry with this name. */
  917. for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
  918. vec_safe_iterate (method_vec, slot, &m);
  919. ++slot)
  920. {
  921. m = OVL_CURRENT (m);
  922. if (template_conv_p)
  923. {
  924. if (TREE_CODE (m) == TEMPLATE_DECL
  925. && DECL_TEMPLATE_CONV_FN_P (m))
  926. insert_p = false;
  927. break;
  928. }
  929. if (conv_p && !DECL_CONV_FN_P (m))
  930. break;
  931. if (DECL_NAME (m) == DECL_NAME (method))
  932. {
  933. insert_p = false;
  934. break;
  935. }
  936. if (complete_p
  937. && !DECL_CONV_FN_P (m)
  938. && DECL_NAME (m) > DECL_NAME (method))
  939. break;
  940. }
  941. }
  942. current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
  943. /* Check to see if we've already got this method. */
  944. for (fns = current_fns; fns; fns = OVL_NEXT (fns))
  945. {
  946. tree fn = OVL_CURRENT (fns);
  947. tree fn_type;
  948. tree method_type;
  949. tree parms1;
  950. tree parms2;
  951. if (TREE_CODE (fn) != TREE_CODE (method))
  952. continue;
  953. /* [over.load] Member function declarations with the
  954. same name and the same parameter types cannot be
  955. overloaded if any of them is a static member
  956. function declaration.
  957. [over.load] Member function declarations with the same name and
  958. the same parameter-type-list as well as member function template
  959. declarations with the same name, the same parameter-type-list, and
  960. the same template parameter lists cannot be overloaded if any of
  961. them, but not all, have a ref-qualifier.
  962. [namespace.udecl] When a using-declaration brings names
  963. from a base class into a derived class scope, member
  964. functions in the derived class override and/or hide member
  965. functions with the same name and parameter types in a base
  966. class (rather than conflicting). */
  967. fn_type = TREE_TYPE (fn);
  968. method_type = TREE_TYPE (method);
  969. parms1 = TYPE_ARG_TYPES (fn_type);
  970. parms2 = TYPE_ARG_TYPES (method_type);
  971. /* Compare the quals on the 'this' parm. Don't compare
  972. the whole types, as used functions are treated as
  973. coming from the using class in overload resolution. */
  974. if (! DECL_STATIC_FUNCTION_P (fn)
  975. && ! DECL_STATIC_FUNCTION_P (method)
  976. /* Either both or neither need to be ref-qualified for
  977. differing quals to allow overloading. */
  978. && (FUNCTION_REF_QUALIFIED (fn_type)
  979. == FUNCTION_REF_QUALIFIED (method_type))
  980. && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
  981. || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
  982. continue;
  983. /* For templates, the return type and template parameters
  984. must be identical. */
  985. if (TREE_CODE (fn) == TEMPLATE_DECL
  986. && (!same_type_p (TREE_TYPE (fn_type),
  987. TREE_TYPE (method_type))
  988. || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
  989. DECL_TEMPLATE_PARMS (method))))
  990. continue;
  991. if (! DECL_STATIC_FUNCTION_P (fn))
  992. parms1 = TREE_CHAIN (parms1);
  993. if (! DECL_STATIC_FUNCTION_P (method))
  994. parms2 = TREE_CHAIN (parms2);
  995. if (compparms (parms1, parms2)
  996. && (!DECL_CONV_FN_P (fn)
  997. || same_type_p (TREE_TYPE (fn_type),
  998. TREE_TYPE (method_type))))
  999. {
  1000. /* For function versions, their parms and types match
  1001. but they are not duplicates. Record function versions
  1002. as and when they are found. extern "C" functions are
  1003. not treated as versions. */
  1004. if (TREE_CODE (fn) == FUNCTION_DECL
  1005. && TREE_CODE (method) == FUNCTION_DECL
  1006. && !DECL_EXTERN_C_P (fn)
  1007. && !DECL_EXTERN_C_P (method)
  1008. && targetm.target_option.function_versions (fn, method))
  1009. {
  1010. /* Mark functions as versions if necessary. Modify the mangled
  1011. decl name if necessary. */
  1012. if (!DECL_FUNCTION_VERSIONED (fn))
  1013. {
  1014. DECL_FUNCTION_VERSIONED (fn) = 1;
  1015. if (DECL_ASSEMBLER_NAME_SET_P (fn))
  1016. mangle_decl (fn);
  1017. }
  1018. if (!DECL_FUNCTION_VERSIONED (method))
  1019. {
  1020. DECL_FUNCTION_VERSIONED (method) = 1;
  1021. if (DECL_ASSEMBLER_NAME_SET_P (method))
  1022. mangle_decl (method);
  1023. }
  1024. cgraph_node::record_function_versions (fn, method);
  1025. continue;
  1026. }
  1027. if (DECL_INHERITED_CTOR_BASE (method))
  1028. {
  1029. if (DECL_INHERITED_CTOR_BASE (fn))
  1030. {
  1031. error_at (DECL_SOURCE_LOCATION (method),
  1032. "%q#D inherited from %qT", method,
  1033. DECL_INHERITED_CTOR_BASE (method));
  1034. error_at (DECL_SOURCE_LOCATION (fn),
  1035. "conflicts with version inherited from %qT",
  1036. DECL_INHERITED_CTOR_BASE (fn));
  1037. }
  1038. /* Otherwise defer to the other function. */
  1039. return false;
  1040. }
  1041. if (using_decl)
  1042. {
  1043. if (DECL_CONTEXT (fn) == type)
  1044. /* Defer to the local function. */
  1045. return false;
  1046. }
  1047. else
  1048. {
  1049. error ("%q+#D cannot be overloaded", method);
  1050. error ("with %q+#D", fn);
  1051. }
  1052. /* We don't call duplicate_decls here to merge the
  1053. declarations because that will confuse things if the
  1054. methods have inline definitions. In particular, we
  1055. will crash while processing the definitions. */
  1056. return false;
  1057. }
  1058. }
  1059. /* A class should never have more than one destructor. */
  1060. if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
  1061. return false;
  1062. /* Add the new binding. */
  1063. if (using_decl)
  1064. {
  1065. overload = ovl_cons (method, current_fns);
  1066. OVL_USED (overload) = true;
  1067. }
  1068. else
  1069. overload = build_overload (method, current_fns);
  1070. if (conv_p)
  1071. TYPE_HAS_CONVERSION (type) = 1;
  1072. else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
  1073. push_class_level_binding (DECL_NAME (method), overload);
  1074. if (insert_p)
  1075. {
  1076. bool reallocated;
  1077. /* We only expect to add few methods in the COMPLETE_P case, so
  1078. just make room for one more method in that case. */
  1079. if (complete_p)
  1080. reallocated = vec_safe_reserve_exact (method_vec, 1);
  1081. else
  1082. reallocated = vec_safe_reserve (method_vec, 1);
  1083. if (reallocated)
  1084. CLASSTYPE_METHOD_VEC (type) = method_vec;
  1085. if (slot == method_vec->length ())
  1086. method_vec->quick_push (overload);
  1087. else
  1088. method_vec->quick_insert (slot, overload);
  1089. }
  1090. else
  1091. /* Replace the current slot. */
  1092. (*method_vec)[slot] = overload;
  1093. return true;
  1094. }
  1095. /* Subroutines of finish_struct. */
  1096. /* Change the access of FDECL to ACCESS in T. Return 1 if change was
  1097. legit, otherwise return 0. */
  1098. static int
  1099. alter_access (tree t, tree fdecl, tree access)
  1100. {
  1101. tree elem;
  1102. if (!DECL_LANG_SPECIFIC (fdecl))
  1103. retrofit_lang_decl (fdecl);
  1104. gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
  1105. elem = purpose_member (t, DECL_ACCESS (fdecl));
  1106. if (elem)
  1107. {
  1108. if (TREE_VALUE (elem) != access)
  1109. {
  1110. if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
  1111. error ("conflicting access specifications for method"
  1112. " %q+D, ignored", TREE_TYPE (fdecl));
  1113. else
  1114. error ("conflicting access specifications for field %qE, ignored",
  1115. DECL_NAME (fdecl));
  1116. }
  1117. else
  1118. {
  1119. /* They're changing the access to the same thing they changed
  1120. it to before. That's OK. */
  1121. ;
  1122. }
  1123. }
  1124. else
  1125. {
  1126. perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
  1127. tf_warning_or_error);
  1128. DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
  1129. return 1;
  1130. }
  1131. return 0;
  1132. }
  1133. /* Process the USING_DECL, which is a member of T. */
  1134. static void
  1135. handle_using_decl (tree using_decl, tree t)
  1136. {
  1137. tree decl = USING_DECL_DECLS (using_decl);
  1138. tree name = DECL_NAME (using_decl);
  1139. tree access
  1140. = TREE_PRIVATE (using_decl) ? access_private_node
  1141. : TREE_PROTECTED (using_decl) ? access_protected_node
  1142. : access_public_node;
  1143. tree flist = NULL_TREE;
  1144. tree old_value;
  1145. gcc_assert (!processing_template_decl && decl);
  1146. old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
  1147. tf_warning_or_error);
  1148. if (old_value)
  1149. {
  1150. if (is_overloaded_fn (old_value))
  1151. old_value = OVL_CURRENT (old_value);
  1152. if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
  1153. /* OK */;
  1154. else
  1155. old_value = NULL_TREE;
  1156. }
  1157. cp_emit_debug_info_for_using (decl, t);
  1158. if (is_overloaded_fn (decl))
  1159. flist = decl;
  1160. if (! old_value)
  1161. ;
  1162. else if (is_overloaded_fn (old_value))
  1163. {
  1164. if (flist)
  1165. /* It's OK to use functions from a base when there are functions with
  1166. the same name already present in the current class. */;
  1167. else
  1168. {
  1169. error ("%q+D invalid in %q#T", using_decl, t);
  1170. error (" because of local method %q+#D with same name",
  1171. OVL_CURRENT (old_value));
  1172. return;
  1173. }
  1174. }
  1175. else if (!DECL_ARTIFICIAL (old_value))
  1176. {
  1177. error ("%q+D invalid in %q#T", using_decl, t);
  1178. error (" because of local member %q+#D with same name", old_value);
  1179. return;
  1180. }
  1181. /* Make type T see field decl FDECL with access ACCESS. */
  1182. if (flist)
  1183. for (; flist; flist = OVL_NEXT (flist))
  1184. {
  1185. add_method (t, OVL_CURRENT (flist), using_decl);
  1186. alter_access (t, OVL_CURRENT (flist), access);
  1187. }
  1188. else
  1189. alter_access (t, decl, access);
  1190. }
  1191. /* Data structure for find_abi_tags_r, below. */
  1192. struct abi_tag_data
  1193. {
  1194. tree t; // The type that we're checking for missing tags.
  1195. tree subob; // The subobject of T that we're getting tags from.
  1196. tree tags; // error_mark_node for diagnostics, or a list of missing tags.
  1197. };
  1198. /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
  1199. in the context of P. TAG can be either an identifier (the DECL_NAME of
  1200. a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
  1201. static void
  1202. check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
  1203. {
  1204. if (!IDENTIFIER_MARKED (id))
  1205. {
  1206. if (p->tags != error_mark_node)
  1207. {
  1208. /* We're collecting tags from template arguments or from
  1209. the type of a variable or function return type. */
  1210. p->tags = tree_cons (NULL_TREE, tag, p->tags);
  1211. /* Don't inherit this tag multiple times. */
  1212. IDENTIFIER_MARKED (id) = true;
  1213. if (TYPE_P (p->t))
  1214. {
  1215. /* Tags inherited from type template arguments are only used
  1216. to avoid warnings. */
  1217. ABI_TAG_IMPLICIT (p->tags) = true;
  1218. return;
  1219. }
  1220. /* For functions and variables we want to warn, too. */
  1221. }
  1222. /* Otherwise we're diagnosing missing tags. */
  1223. if (TREE_CODE (p->t) == FUNCTION_DECL)
  1224. {
  1225. if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
  1226. "that %qT (used in its return type) has",
  1227. p->t, tag, *tp))
  1228. inform (location_of (*tp), "%qT declared here", *tp);
  1229. }
  1230. else if (TREE_CODE (p->t) == VAR_DECL)
  1231. {
  1232. if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
  1233. "that %qT (used in its type) has", p->t, tag, *tp))
  1234. inform (location_of (*tp), "%qT declared here", *tp);
  1235. }
  1236. else if (TYPE_P (p->subob))
  1237. {
  1238. if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
  1239. "that base %qT has", p->t, tag, p->subob))
  1240. inform (location_of (p->subob), "%qT declared here",
  1241. p->subob);
  1242. }
  1243. else
  1244. {
  1245. if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
  1246. "that %qT (used in the type of %qD) has",
  1247. p->t, tag, *tp, p->subob))
  1248. {
  1249. inform (location_of (p->subob), "%qD declared here",
  1250. p->subob);
  1251. inform (location_of (*tp), "%qT declared here", *tp);
  1252. }
  1253. }
  1254. }
  1255. }
  1256. /* Find all the ABI tags in the attribute list ATTR and either call
  1257. check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
  1258. static void
  1259. mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
  1260. {
  1261. if (!attr)
  1262. return;
  1263. for (; (attr = lookup_attribute ("abi_tag", attr));
  1264. attr = TREE_CHAIN (attr))
  1265. for (tree list = TREE_VALUE (attr); list;
  1266. list = TREE_CHAIN (list))
  1267. {
  1268. tree tag = TREE_VALUE (list);
  1269. tree id = get_identifier (TREE_STRING_POINTER (tag));
  1270. if (tp)
  1271. check_tag (tag, id, tp, p);
  1272. else
  1273. IDENTIFIER_MARKED (id) = val;
  1274. }
  1275. }
  1276. /* Find all the ABI tags on T and its enclosing scopes and either call
  1277. check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
  1278. static void
  1279. mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
  1280. {
  1281. while (t != global_namespace)
  1282. {
  1283. tree attr;
  1284. if (TYPE_P (t))
  1285. {
  1286. attr = TYPE_ATTRIBUTES (t);
  1287. t = CP_TYPE_CONTEXT (t);
  1288. }
  1289. else
  1290. {
  1291. attr = DECL_ATTRIBUTES (t);
  1292. t = CP_DECL_CONTEXT (t);
  1293. }
  1294. mark_or_check_attr_tags (attr, tp, p, val);
  1295. }
  1296. }
  1297. /* walk_tree callback for check_abi_tags: if the type at *TP involves any
  1298. types with ABI tags, add the corresponding identifiers to the VEC in
  1299. *DATA and set IDENTIFIER_MARKED. */
  1300. static tree
  1301. find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
  1302. {
  1303. if (!OVERLOAD_TYPE_P (*tp))
  1304. return NULL_TREE;
  1305. /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
  1306. anyway, but let's make sure of it. */
  1307. *walk_subtrees = false;
  1308. abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
  1309. mark_or_check_tags (*tp, tp, p, false);
  1310. return NULL_TREE;
  1311. }
  1312. /* walk_tree callback for mark_abi_tags: if *TP is a class, set
  1313. IDENTIFIER_MARKED on its ABI tags. */
  1314. static tree
  1315. mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
  1316. {
  1317. if (!OVERLOAD_TYPE_P (*tp))
  1318. return NULL_TREE;
  1319. /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
  1320. anyway, but let's make sure of it. */
  1321. *walk_subtrees = false;
  1322. bool *valp = static_cast<bool*>(data);
  1323. mark_or_check_tags (*tp, NULL, NULL, *valp);
  1324. return NULL_TREE;
  1325. }
  1326. /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
  1327. scopes. */
  1328. static void
  1329. mark_abi_tags (tree t, bool val)
  1330. {
  1331. mark_or_check_tags (t, NULL, NULL, val);
  1332. if (DECL_P (t))
  1333. {
  1334. if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
  1335. && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
  1336. {
  1337. /* Template arguments are part of the signature. */
  1338. tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
  1339. for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
  1340. {
  1341. tree arg = TREE_VEC_ELT (level, j);
  1342. cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
  1343. }
  1344. }
  1345. if (TREE_CODE (t) == FUNCTION_DECL)
  1346. /* A function's parameter types are part of the signature, so
  1347. we don't need to inherit any tags that are also in them. */
  1348. for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
  1349. arg = TREE_CHAIN (arg))
  1350. cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
  1351. mark_abi_tags_r, &val);
  1352. }
  1353. }
  1354. /* Check that T has all the ABI tags that subobject SUBOB has, or
  1355. warn if not. If T is a (variable or function) declaration, also
  1356. add any missing tags. */
  1357. static void
  1358. check_abi_tags (tree t, tree subob)
  1359. {
  1360. bool inherit = DECL_P (t);
  1361. if (!inherit && !warn_abi_tag)
  1362. return;
  1363. tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
  1364. if (!TREE_PUBLIC (decl))
  1365. /* No need to worry about things local to this TU. */
  1366. return;
  1367. mark_abi_tags (t, true);
  1368. tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
  1369. struct abi_tag_data data = { t, subob, error_mark_node };
  1370. if (inherit)
  1371. data.tags = NULL_TREE;
  1372. cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
  1373. if (inherit && data.tags)
  1374. {
  1375. tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
  1376. if (attr)
  1377. TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
  1378. else
  1379. DECL_ATTRIBUTES (t)
  1380. = tree_cons (get_identifier ("abi_tag"), data.tags,
  1381. DECL_ATTRIBUTES (t));
  1382. }
  1383. mark_abi_tags (t, false);
  1384. }
  1385. /* Check that DECL has all the ABI tags that are used in parts of its type
  1386. that are not reflected in its mangled name. */
  1387. void
  1388. check_abi_tags (tree decl)
  1389. {
  1390. if (TREE_CODE (decl) == VAR_DECL)
  1391. check_abi_tags (decl, TREE_TYPE (decl));
  1392. else if (TREE_CODE (decl) == FUNCTION_DECL
  1393. && !mangle_return_type_p (decl))
  1394. check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
  1395. }
  1396. void
  1397. inherit_targ_abi_tags (tree t)
  1398. {
  1399. if (!CLASS_TYPE_P (t)
  1400. || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
  1401. return;
  1402. mark_abi_tags (t, true);
  1403. tree args = CLASSTYPE_TI_ARGS (t);
  1404. struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
  1405. for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
  1406. {
  1407. tree level = TMPL_ARGS_LEVEL (args, i+1);
  1408. for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
  1409. {
  1410. tree arg = TREE_VEC_ELT (level, j);
  1411. data.subob = arg;
  1412. cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
  1413. }
  1414. }
  1415. // If we found some tags on our template arguments, add them to our
  1416. // abi_tag attribute.
  1417. if (data.tags)
  1418. {
  1419. tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
  1420. if (attr)
  1421. TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
  1422. else
  1423. TYPE_ATTRIBUTES (t)
  1424. = tree_cons (get_identifier ("abi_tag"), data.tags,
  1425. TYPE_ATTRIBUTES (t));
  1426. }
  1427. mark_abi_tags (t, false);
  1428. }
  1429. /* Return true, iff class T has a non-virtual destructor that is
  1430. accessible from outside the class heirarchy (i.e. is public, or
  1431. there's a suitable friend. */
  1432. static bool
  1433. accessible_nvdtor_p (tree t)
  1434. {
  1435. tree dtor = CLASSTYPE_DESTRUCTORS (t);
  1436. /* An implicitly declared destructor is always public. And,
  1437. if it were virtual, we would have created it by now. */
  1438. if (!dtor)
  1439. return true;
  1440. if (DECL_VINDEX (dtor))
  1441. return false; /* Virtual */
  1442. if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
  1443. return true; /* Public */
  1444. if (CLASSTYPE_FRIEND_CLASSES (t)
  1445. || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
  1446. return true; /* Has friends */
  1447. return false;
  1448. }
  1449. /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
  1450. and NO_CONST_ASN_REF_P. Also set flag bits in T based on
  1451. properties of the bases. */
  1452. static void
  1453. check_bases (tree t,
  1454. int* cant_have_const_ctor_p,
  1455. int* no_const_asn_ref_p)
  1456. {
  1457. int i;
  1458. bool seen_non_virtual_nearly_empty_base_p = 0;
  1459. int seen_tm_mask = 0;
  1460. tree base_binfo;
  1461. tree binfo;
  1462. tree field = NULL_TREE;
  1463. if (!CLASSTYPE_NON_STD_LAYOUT (t))
  1464. for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
  1465. if (TREE_CODE (field) == FIELD_DECL)
  1466. break;
  1467. for (binfo = TYPE_BINFO (t), i = 0;
  1468. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  1469. {
  1470. tree basetype = TREE_TYPE (base_binfo);
  1471. gcc_assert (COMPLETE_TYPE_P (basetype));
  1472. if (CLASSTYPE_FINAL (basetype))
  1473. error ("cannot derive from %<final%> base %qT in derived type %qT",
  1474. basetype, t);
  1475. /* If any base class is non-literal, so is the derived class. */
  1476. if (!CLASSTYPE_LITERAL_P (basetype))
  1477. CLASSTYPE_LITERAL_P (t) = false;
  1478. /* If the base class doesn't have copy constructors or
  1479. assignment operators that take const references, then the
  1480. derived class cannot have such a member automatically
  1481. generated. */
  1482. if (TYPE_HAS_COPY_CTOR (basetype)
  1483. && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
  1484. *cant_have_const_ctor_p = 1;
  1485. if (TYPE_HAS_COPY_ASSIGN (basetype)
  1486. && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
  1487. *no_const_asn_ref_p = 1;
  1488. if (BINFO_VIRTUAL_P (base_binfo))
  1489. /* A virtual base does not effect nearly emptiness. */
  1490. ;
  1491. else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
  1492. {
  1493. if (seen_non_virtual_nearly_empty_base_p)
  1494. /* And if there is more than one nearly empty base, then the
  1495. derived class is not nearly empty either. */
  1496. CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
  1497. else
  1498. /* Remember we've seen one. */
  1499. seen_non_virtual_nearly_empty_base_p = 1;
  1500. }
  1501. else if (!is_empty_class (basetype))
  1502. /* If the base class is not empty or nearly empty, then this
  1503. class cannot be nearly empty. */
  1504. CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
  1505. /* A lot of properties from the bases also apply to the derived
  1506. class. */
  1507. TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
  1508. TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
  1509. |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
  1510. TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
  1511. |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
  1512. || !TYPE_HAS_COPY_ASSIGN (basetype));
  1513. TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
  1514. || !TYPE_HAS_COPY_CTOR (basetype));
  1515. TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
  1516. |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
  1517. TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
  1518. TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
  1519. CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
  1520. |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
  1521. TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
  1522. || TYPE_HAS_COMPLEX_DFLT (basetype));
  1523. SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
  1524. (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
  1525. | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
  1526. SET_CLASSTYPE_REF_FIELDS_NEED_INIT
  1527. (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
  1528. | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
  1529. /* A standard-layout class is a class that:
  1530. ...
  1531. * has no non-standard-layout base classes, */
  1532. CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
  1533. if (!CLASSTYPE_NON_STD_LAYOUT (t))
  1534. {
  1535. tree basefield;
  1536. /* ...has no base classes of the same type as the first non-static
  1537. data member... */
  1538. if (field && DECL_CONTEXT (field) == t
  1539. && (same_type_ignoring_top_level_qualifiers_p
  1540. (TREE_TYPE (field), basetype)))
  1541. CLASSTYPE_NON_STD_LAYOUT (t) = 1;
  1542. else
  1543. /* ...either has no non-static data members in the most-derived
  1544. class and at most one base class with non-static data
  1545. members, or has no base classes with non-static data
  1546. members */
  1547. for (basefield = TYPE_FIELDS (basetype); basefield;
  1548. basefield = DECL_CHAIN (basefield))
  1549. if (TREE_CODE (basefield) == FIELD_DECL)
  1550. {
  1551. if (field)
  1552. CLASSTYPE_NON_STD_LAYOUT (t) = 1;
  1553. else
  1554. field = basefield;
  1555. break;
  1556. }
  1557. }
  1558. /* Don't bother collecting tm attributes if transactional memory
  1559. support is not enabled. */
  1560. if (flag_tm)
  1561. {
  1562. tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
  1563. if (tm_attr)
  1564. seen_tm_mask |= tm_attr_to_mask (tm_attr);
  1565. }
  1566. check_abi_tags (t, basetype);
  1567. }
  1568. /* If one of the base classes had TM attributes, and the current class
  1569. doesn't define its own, then the current class inherits one. */
  1570. if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
  1571. {
  1572. tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
  1573. TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
  1574. }
  1575. }
  1576. /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
  1577. those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
  1578. that have had a nearly-empty virtual primary base stolen by some
  1579. other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
  1580. T. */
  1581. static void
  1582. determine_primary_bases (tree t)
  1583. {
  1584. unsigned i;
  1585. tree primary = NULL_TREE;
  1586. tree type_binfo = TYPE_BINFO (t);
  1587. tree base_binfo;
  1588. /* Determine the primary bases of our bases. */
  1589. for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
  1590. base_binfo = TREE_CHAIN (base_binfo))
  1591. {
  1592. tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
  1593. /* See if we're the non-virtual primary of our inheritance
  1594. chain. */
  1595. if (!BINFO_VIRTUAL_P (base_binfo))
  1596. {
  1597. tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
  1598. tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
  1599. if (parent_primary
  1600. && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
  1601. BINFO_TYPE (parent_primary)))
  1602. /* We are the primary binfo. */
  1603. BINFO_PRIMARY_P (base_binfo) = 1;
  1604. }
  1605. /* Determine if we have a virtual primary base, and mark it so.
  1606. */
  1607. if (primary && BINFO_VIRTUAL_P (primary))
  1608. {
  1609. tree this_primary = copied_binfo (primary, base_binfo);
  1610. if (BINFO_PRIMARY_P (this_primary))
  1611. /* Someone already claimed this base. */
  1612. BINFO_LOST_PRIMARY_P (base_binfo) = 1;
  1613. else
  1614. {
  1615. tree delta;
  1616. BINFO_PRIMARY_P (this_primary) = 1;
  1617. BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
  1618. /* A virtual binfo might have been copied from within
  1619. another hierarchy. As we're about to use it as a
  1620. primary base, make sure the offsets match. */
  1621. delta = size_diffop_loc (input_location,
  1622. convert (ssizetype,
  1623. BINFO_OFFSET (base_binfo)),
  1624. convert (ssizetype,
  1625. BINFO_OFFSET (this_primary)));
  1626. propagate_binfo_offsets (this_primary, delta);
  1627. }
  1628. }
  1629. }
  1630. /* First look for a dynamic direct non-virtual base. */
  1631. for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
  1632. {
  1633. tree basetype = BINFO_TYPE (base_binfo);
  1634. if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
  1635. {
  1636. primary = base_binfo;
  1637. goto found;
  1638. }
  1639. }
  1640. /* A "nearly-empty" virtual base class can be the primary base
  1641. class, if no non-virtual polymorphic base can be found. Look for
  1642. a nearly-empty virtual dynamic base that is not already a primary
  1643. base of something in the hierarchy. If there is no such base,
  1644. just pick the first nearly-empty virtual base. */
  1645. for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
  1646. base_binfo = TREE_CHAIN (base_binfo))
  1647. if (BINFO_VIRTUAL_P (base_binfo)
  1648. && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
  1649. {
  1650. if (!BINFO_PRIMARY_P (base_binfo))
  1651. {
  1652. /* Found one that is not primary. */
  1653. primary = base_binfo;
  1654. goto found;
  1655. }
  1656. else if (!primary)
  1657. /* Remember the first candidate. */
  1658. primary = base_binfo;
  1659. }
  1660. found:
  1661. /* If we've got a primary base, use it. */
  1662. if (primary)
  1663. {
  1664. tree basetype = BINFO_TYPE (primary);
  1665. CLASSTYPE_PRIMARY_BINFO (t) = primary;
  1666. if (BINFO_PRIMARY_P (primary))
  1667. /* We are stealing a primary base. */
  1668. BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
  1669. BINFO_PRIMARY_P (primary) = 1;
  1670. if (BINFO_VIRTUAL_P (primary))
  1671. {
  1672. tree delta;
  1673. BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
  1674. /* A virtual binfo might have been copied from within
  1675. another hierarchy. As we're about to use it as a primary
  1676. base, make sure the offsets match. */
  1677. delta = size_diffop_loc (input_location, ssize_int (0),
  1678. convert (ssizetype, BINFO_OFFSET (primary)));
  1679. propagate_binfo_offsets (primary, delta);
  1680. }
  1681. primary = TYPE_BINFO (basetype);
  1682. TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
  1683. BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
  1684. BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
  1685. }
  1686. }
  1687. /* Update the variant types of T. */
  1688. void
  1689. fixup_type_variants (tree t)
  1690. {
  1691. tree variants;
  1692. if (!t)
  1693. return;
  1694. for (variants = TYPE_NEXT_VARIANT (t);
  1695. variants;
  1696. variants = TYPE_NEXT_VARIANT (variants))
  1697. {
  1698. /* These fields are in the _TYPE part of the node, not in
  1699. the TYPE_LANG_SPECIFIC component, so they are not shared. */
  1700. TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
  1701. TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
  1702. TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
  1703. = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
  1704. TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
  1705. TYPE_BINFO (variants) = TYPE_BINFO (t);
  1706. /* Copy whatever these are holding today. */
  1707. TYPE_VFIELD (variants) = TYPE_VFIELD (t);
  1708. TYPE_METHODS (variants) = TYPE_METHODS (t);
  1709. TYPE_FIELDS (variants) = TYPE_FIELDS (t);
  1710. }
  1711. }
  1712. /* Early variant fixups: we apply attributes at the beginning of the class
  1713. definition, and we need to fix up any variants that have already been
  1714. made via elaborated-type-specifier so that check_qualified_type works. */
  1715. void
  1716. fixup_attribute_variants (tree t)
  1717. {
  1718. tree variants;
  1719. if (!t)
  1720. return;
  1721. for (variants = TYPE_NEXT_VARIANT (t);
  1722. variants;
  1723. variants = TYPE_NEXT_VARIANT (variants))
  1724. {
  1725. /* These are the two fields that check_qualified_type looks at and
  1726. are affected by attributes. */
  1727. TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
  1728. TYPE_ALIGN (variants) = TYPE_ALIGN (t);
  1729. }
  1730. }
  1731. /* Set memoizing fields and bits of T (and its variants) for later
  1732. use. */
  1733. static void
  1734. finish_struct_bits (tree t)
  1735. {
  1736. /* Fix up variants (if any). */
  1737. fixup_type_variants (t);
  1738. if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
  1739. /* For a class w/o baseclasses, 'finish_struct' has set
  1740. CLASSTYPE_PURE_VIRTUALS correctly (by definition).
  1741. Similarly for a class whose base classes do not have vtables.
  1742. When neither of these is true, we might have removed abstract
  1743. virtuals (by providing a definition), added some (by declaring
  1744. new ones), or redeclared ones from a base class. We need to
  1745. recalculate what's really an abstract virtual at this point (by
  1746. looking in the vtables). */
  1747. get_pure_virtuals (t);
  1748. /* If this type has a copy constructor or a destructor, force its
  1749. mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
  1750. nonzero. This will cause it to be passed by invisible reference
  1751. and prevent it from being returned in a register. */
  1752. if (type_has_nontrivial_copy_init (t)
  1753. || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
  1754. {
  1755. tree variants;
  1756. DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
  1757. for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
  1758. {
  1759. SET_TYPE_MODE (variants, BLKmode);
  1760. TREE_ADDRESSABLE (variants) = 1;
  1761. }
  1762. }
  1763. }
  1764. /* Issue warnings about T having private constructors, but no friends,
  1765. and so forth.
  1766. HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
  1767. static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
  1768. non-private static member functions. */
  1769. static void
  1770. maybe_warn_about_overly_private_class (tree t)
  1771. {
  1772. int has_member_fn = 0;
  1773. int has_nonprivate_method = 0;
  1774. tree fn;
  1775. if (!warn_ctor_dtor_privacy
  1776. /* If the class has friends, those entities might create and
  1777. access instances, so we should not warn. */
  1778. || (CLASSTYPE_FRIEND_CLASSES (t)
  1779. || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
  1780. /* We will have warned when the template was declared; there's
  1781. no need to warn on every instantiation. */
  1782. || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
  1783. /* There's no reason to even consider warning about this
  1784. class. */
  1785. return;
  1786. /* We only issue one warning, if more than one applies, because
  1787. otherwise, on code like:
  1788. class A {
  1789. // Oops - forgot `public:'
  1790. A();
  1791. A(const A&);
  1792. ~A();
  1793. };
  1794. we warn several times about essentially the same problem. */
  1795. /* Check to see if all (non-constructor, non-destructor) member
  1796. functions are private. (Since there are no friends or
  1797. non-private statics, we can't ever call any of the private member
  1798. functions.) */
  1799. for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
  1800. /* We're not interested in compiler-generated methods; they don't
  1801. provide any way to call private members. */
  1802. if (!DECL_ARTIFICIAL (fn))
  1803. {
  1804. if (!TREE_PRIVATE (fn))
  1805. {
  1806. if (DECL_STATIC_FUNCTION_P (fn))
  1807. /* A non-private static member function is just like a
  1808. friend; it can create and invoke private member
  1809. functions, and be accessed without a class
  1810. instance. */
  1811. return;
  1812. has_nonprivate_method = 1;
  1813. /* Keep searching for a static member function. */
  1814. }
  1815. else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
  1816. has_member_fn = 1;
  1817. }
  1818. if (!has_nonprivate_method && has_member_fn)
  1819. {
  1820. /* There are no non-private methods, and there's at least one
  1821. private member function that isn't a constructor or
  1822. destructor. (If all the private members are
  1823. constructors/destructors we want to use the code below that
  1824. issues error messages specifically referring to
  1825. constructors/destructors.) */
  1826. unsigned i;
  1827. tree binfo = TYPE_BINFO (t);
  1828. for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
  1829. if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
  1830. {
  1831. has_nonprivate_method = 1;
  1832. break;
  1833. }
  1834. if (!has_nonprivate_method)
  1835. {
  1836. warning (OPT_Wctor_dtor_privacy,
  1837. "all member functions in class %qT are private", t);
  1838. return;
  1839. }
  1840. }
  1841. /* Even if some of the member functions are non-private, the class
  1842. won't be useful for much if all the constructors or destructors
  1843. are private: such an object can never be created or destroyed. */
  1844. fn = CLASSTYPE_DESTRUCTORS (t);
  1845. if (fn && TREE_PRIVATE (fn))
  1846. {
  1847. warning (OPT_Wctor_dtor_privacy,
  1848. "%q#T only defines a private destructor and has no friends",
  1849. t);
  1850. return;
  1851. }
  1852. /* Warn about classes that have private constructors and no friends. */
  1853. if (TYPE_HAS_USER_CONSTRUCTOR (t)
  1854. /* Implicitly generated constructors are always public. */
  1855. && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
  1856. || !CLASSTYPE_LAZY_COPY_CTOR (t)))
  1857. {
  1858. int nonprivate_ctor = 0;
  1859. /* If a non-template class does not define a copy
  1860. constructor, one is defined for it, enabling it to avoid
  1861. this warning. For a template class, this does not
  1862. happen, and so we would normally get a warning on:
  1863. template <class T> class C { private: C(); };
  1864. To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
  1865. complete non-template or fully instantiated classes have this
  1866. flag set. */
  1867. if (!TYPE_HAS_COPY_CTOR (t))
  1868. nonprivate_ctor = 1;
  1869. else
  1870. for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
  1871. {
  1872. tree ctor = OVL_CURRENT (fn);
  1873. /* Ideally, we wouldn't count copy constructors (or, in
  1874. fact, any constructor that takes an argument of the
  1875. class type as a parameter) because such things cannot
  1876. be used to construct an instance of the class unless
  1877. you already have one. But, for now at least, we're
  1878. more generous. */
  1879. if (! TREE_PRIVATE (ctor))
  1880. {
  1881. nonprivate_ctor = 1;
  1882. break;
  1883. }
  1884. }
  1885. if (nonprivate_ctor == 0)
  1886. {
  1887. warning (OPT_Wctor_dtor_privacy,
  1888. "%q#T only defines private constructors and has no friends",
  1889. t);
  1890. return;
  1891. }
  1892. }
  1893. }
  1894. static struct {
  1895. gt_pointer_operator new_value;
  1896. void *cookie;
  1897. } resort_data;
  1898. /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
  1899. static int
  1900. method_name_cmp (const void* m1_p, const void* m2_p)
  1901. {
  1902. const tree *const m1 = (const tree *) m1_p;
  1903. const tree *const m2 = (const tree *) m2_p;
  1904. if (*m1 == NULL_TREE && *m2 == NULL_TREE)
  1905. return 0;
  1906. if (*m1 == NULL_TREE)
  1907. return -1;
  1908. if (*m2 == NULL_TREE)
  1909. return 1;
  1910. if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
  1911. return -1;
  1912. return 1;
  1913. }
  1914. /* This routine compares two fields like method_name_cmp but using the
  1915. pointer operator in resort_field_decl_data. */
  1916. static int
  1917. resort_method_name_cmp (const void* m1_p, const void* m2_p)
  1918. {
  1919. const tree *const m1 = (const tree *) m1_p;
  1920. const tree *const m2 = (const tree *) m2_p;
  1921. if (*m1 == NULL_TREE && *m2 == NULL_TREE)
  1922. return 0;
  1923. if (*m1 == NULL_TREE)
  1924. return -1;
  1925. if (*m2 == NULL_TREE)
  1926. return 1;
  1927. {
  1928. tree d1 = DECL_NAME (OVL_CURRENT (*m1));
  1929. tree d2 = DECL_NAME (OVL_CURRENT (*m2));
  1930. resort_data.new_value (&d1, resort_data.cookie);
  1931. resort_data.new_value (&d2, resort_data.cookie);
  1932. if (d1 < d2)
  1933. return -1;
  1934. }
  1935. return 1;
  1936. }
  1937. /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
  1938. void
  1939. resort_type_method_vec (void* obj,
  1940. void* /*orig_obj*/,
  1941. gt_pointer_operator new_value,
  1942. void* cookie)
  1943. {
  1944. vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
  1945. int len = vec_safe_length (method_vec);
  1946. size_t slot;
  1947. tree fn;
  1948. /* The type conversion ops have to live at the front of the vec, so we
  1949. can't sort them. */
  1950. for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
  1951. vec_safe_iterate (method_vec, slot, &fn);
  1952. ++slot)
  1953. if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
  1954. break;
  1955. if (len - slot > 1)
  1956. {
  1957. resort_data.new_value = new_value;
  1958. resort_data.cookie = cookie;
  1959. qsort (method_vec->address () + slot, len - slot, sizeof (tree),
  1960. resort_method_name_cmp);
  1961. }
  1962. }
  1963. /* Warn about duplicate methods in fn_fields.
  1964. Sort methods that are not special (i.e., constructors, destructors,
  1965. and type conversion operators) so that we can find them faster in
  1966. search. */
  1967. static void
  1968. finish_struct_methods (tree t)
  1969. {
  1970. tree fn_fields;
  1971. vec<tree, va_gc> *method_vec;
  1972. int slot, len;
  1973. method_vec = CLASSTYPE_METHOD_VEC (t);
  1974. if (!method_vec)
  1975. return;
  1976. len = method_vec->length ();
  1977. /* Clear DECL_IN_AGGR_P for all functions. */
  1978. for (fn_fields = TYPE_METHODS (t); fn_fields;
  1979. fn_fields = DECL_CHAIN (fn_fields))
  1980. DECL_IN_AGGR_P (fn_fields) = 0;
  1981. /* Issue warnings about private constructors and such. If there are
  1982. no methods, then some public defaults are generated. */
  1983. maybe_warn_about_overly_private_class (t);
  1984. /* The type conversion ops have to live at the front of the vec, so we
  1985. can't sort them. */
  1986. for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
  1987. method_vec->iterate (slot, &fn_fields);
  1988. ++slot)
  1989. if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
  1990. break;
  1991. if (len - slot > 1)
  1992. qsort (method_vec->address () + slot,
  1993. len-slot, sizeof (tree), method_name_cmp);
  1994. }
  1995. /* Make BINFO's vtable have N entries, including RTTI entries,
  1996. vbase and vcall offsets, etc. Set its type and call the back end
  1997. to lay it out. */
  1998. static void
  1999. layout_vtable_decl (tree binfo, int n)
  2000. {
  2001. tree atype;
  2002. tree vtable;
  2003. atype = build_array_of_n_type (vtable_entry_type, n);
  2004. layout_type (atype);
  2005. /* We may have to grow the vtable. */
  2006. vtable = get_vtbl_decl_for_binfo (binfo);
  2007. if (!same_type_p (TREE_TYPE (vtable), atype))
  2008. {
  2009. TREE_TYPE (vtable) = atype;
  2010. DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
  2011. layout_decl (vtable, 0);
  2012. }
  2013. }
  2014. /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
  2015. have the same signature. */
  2016. int
  2017. same_signature_p (const_tree fndecl, const_tree base_fndecl)
  2018. {
  2019. /* One destructor overrides another if they are the same kind of
  2020. destructor. */
  2021. if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
  2022. && special_function_p (base_fndecl) == special_function_p (fndecl))
  2023. return 1;
  2024. /* But a non-destructor never overrides a destructor, nor vice
  2025. versa, nor do different kinds of destructors override
  2026. one-another. For example, a complete object destructor does not
  2027. override a deleting destructor. */
  2028. if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
  2029. return 0;
  2030. if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
  2031. || (DECL_CONV_FN_P (fndecl)
  2032. && DECL_CONV_FN_P (base_fndecl)
  2033. && same_type_p (DECL_CONV_FN_TYPE (fndecl),
  2034. DECL_CONV_FN_TYPE (base_fndecl))))
  2035. {
  2036. tree fntype = TREE_TYPE (fndecl);
  2037. tree base_fntype = TREE_TYPE (base_fndecl);
  2038. if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
  2039. && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
  2040. && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
  2041. FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
  2042. return 1;
  2043. }
  2044. return 0;
  2045. }
  2046. /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
  2047. subobject. */
  2048. static bool
  2049. base_derived_from (tree derived, tree base)
  2050. {
  2051. tree probe;
  2052. for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
  2053. {
  2054. if (probe == derived)
  2055. return true;
  2056. else if (BINFO_VIRTUAL_P (probe))
  2057. /* If we meet a virtual base, we can't follow the inheritance
  2058. any more. See if the complete type of DERIVED contains
  2059. such a virtual base. */
  2060. return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
  2061. != NULL_TREE);
  2062. }
  2063. return false;
  2064. }
  2065. typedef struct find_final_overrider_data_s {
  2066. /* The function for which we are trying to find a final overrider. */
  2067. tree fn;
  2068. /* The base class in which the function was declared. */
  2069. tree declaring_base;
  2070. /* The candidate overriders. */
  2071. tree candidates;
  2072. /* Path to most derived. */
  2073. vec<tree> path;
  2074. } find_final_overrider_data;
  2075. /* Add the overrider along the current path to FFOD->CANDIDATES.
  2076. Returns true if an overrider was found; false otherwise. */
  2077. static bool
  2078. dfs_find_final_overrider_1 (tree binfo,
  2079. find_final_overrider_data *ffod,
  2080. unsigned depth)
  2081. {
  2082. tree method;
  2083. /* If BINFO is not the most derived type, try a more derived class.
  2084. A definition there will overrider a definition here. */
  2085. if (depth)
  2086. {
  2087. depth--;
  2088. if (dfs_find_final_overrider_1
  2089. (ffod->path[depth], ffod, depth))
  2090. return true;
  2091. }
  2092. method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
  2093. if (method)
  2094. {
  2095. tree *candidate = &ffod->candidates;
  2096. /* Remove any candidates overridden by this new function. */
  2097. while (*candidate)
  2098. {
  2099. /* If *CANDIDATE overrides METHOD, then METHOD
  2100. cannot override anything else on the list. */
  2101. if (base_derived_from (TREE_VALUE (*candidate), binfo))
  2102. return true;
  2103. /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
  2104. if (base_derived_from (binfo, TREE_VALUE (*candidate)))
  2105. *candidate = TREE_CHAIN (*candidate);
  2106. else
  2107. candidate = &TREE_CHAIN (*candidate);
  2108. }
  2109. /* Add the new function. */
  2110. ffod->candidates = tree_cons (method, binfo, ffod->candidates);
  2111. return true;
  2112. }
  2113. return false;
  2114. }
  2115. /* Called from find_final_overrider via dfs_walk. */
  2116. static tree
  2117. dfs_find_final_overrider_pre (tree binfo, void *data)
  2118. {
  2119. find_final_overrider_data *ffod = (find_final_overrider_data *) data;
  2120. if (binfo == ffod->declaring_base)
  2121. dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
  2122. ffod->path.safe_push (binfo);
  2123. return NULL_TREE;
  2124. }
  2125. static tree
  2126. dfs_find_final_overrider_post (tree /*binfo*/, void *data)
  2127. {
  2128. find_final_overrider_data *ffod = (find_final_overrider_data *) data;
  2129. ffod->path.pop ();
  2130. return NULL_TREE;
  2131. }
  2132. /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
  2133. FN and whose TREE_VALUE is the binfo for the base where the
  2134. overriding occurs. BINFO (in the hierarchy dominated by the binfo
  2135. DERIVED) is the base object in which FN is declared. */
  2136. static tree
  2137. find_final_overrider (tree derived, tree binfo, tree fn)
  2138. {
  2139. find_final_overrider_data ffod;
  2140. /* Getting this right is a little tricky. This is valid:
  2141. struct S { virtual void f (); };
  2142. struct T { virtual void f (); };
  2143. struct U : public S, public T { };
  2144. even though calling `f' in `U' is ambiguous. But,
  2145. struct R { virtual void f(); };
  2146. struct S : virtual public R { virtual void f (); };
  2147. struct T : virtual public R { virtual void f (); };
  2148. struct U : public S, public T { };
  2149. is not -- there's no way to decide whether to put `S::f' or
  2150. `T::f' in the vtable for `R'.
  2151. The solution is to look at all paths to BINFO. If we find
  2152. different overriders along any two, then there is a problem. */
  2153. if (DECL_THUNK_P (fn))
  2154. fn = THUNK_TARGET (fn);
  2155. /* Determine the depth of the hierarchy. */
  2156. ffod.fn = fn;
  2157. ffod.declaring_base = binfo;
  2158. ffod.candidates = NULL_TREE;
  2159. ffod.path.create (30);
  2160. dfs_walk_all (derived, dfs_find_final_overrider_pre,
  2161. dfs_find_final_overrider_post, &ffod);
  2162. ffod.path.release ();
  2163. /* If there was no winner, issue an error message. */
  2164. if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
  2165. return error_mark_node;
  2166. return ffod.candidates;
  2167. }
  2168. /* Return the index of the vcall offset for FN when TYPE is used as a
  2169. virtual base. */
  2170. static tree
  2171. get_vcall_index (tree fn, tree type)
  2172. {
  2173. vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
  2174. tree_pair_p p;
  2175. unsigned ix;
  2176. FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
  2177. if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
  2178. || same_signature_p (fn, p->purpose))
  2179. return p->value;
  2180. /* There should always be an appropriate index. */
  2181. gcc_unreachable ();
  2182. }
  2183. /* Update an entry in the vtable for BINFO, which is in the hierarchy
  2184. dominated by T. FN is the old function; VIRTUALS points to the
  2185. corresponding position in the new BINFO_VIRTUALS list. IX is the index
  2186. of that entry in the list. */
  2187. static void
  2188. update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
  2189. unsigned ix)
  2190. {
  2191. tree b;
  2192. tree overrider;
  2193. tree delta;
  2194. tree virtual_base;
  2195. tree first_defn;
  2196. tree overrider_fn, overrider_target;
  2197. tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
  2198. tree over_return, base_return;
  2199. bool lost = false;
  2200. /* Find the nearest primary base (possibly binfo itself) which defines
  2201. this function; this is the class the caller will convert to when
  2202. calling FN through BINFO. */
  2203. for (b = binfo; ; b = get_primary_binfo (b))
  2204. {
  2205. gcc_assert (b);
  2206. if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
  2207. break;
  2208. /* The nearest definition is from a lost primary. */
  2209. if (BINFO_LOST_PRIMARY_P (b))
  2210. lost = true;
  2211. }
  2212. first_defn = b;
  2213. /* Find the final overrider. */
  2214. overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
  2215. if (overrider == error_mark_node)
  2216. {
  2217. error ("no unique final overrider for %qD in %qT", target_fn, t);
  2218. return;
  2219. }
  2220. overrider_target = overrider_fn = TREE_PURPOSE (overrider);
  2221. /* Check for adjusting covariant return types. */
  2222. over_return = TREE_TYPE (TREE_TYPE (overrider_target));
  2223. base_return = TREE_TYPE (TREE_TYPE (target_fn));
  2224. if (POINTER_TYPE_P (over_return)
  2225. && TREE_CODE (over_return) == TREE_CODE (base_return)
  2226. && CLASS_TYPE_P (TREE_TYPE (over_return))
  2227. && CLASS_TYPE_P (TREE_TYPE (base_return))
  2228. /* If the overrider is invalid, don't even try. */
  2229. && !DECL_INVALID_OVERRIDER_P (overrider_target))
  2230. {
  2231. /* If FN is a covariant thunk, we must figure out the adjustment
  2232. to the final base FN was converting to. As OVERRIDER_TARGET might
  2233. also be converting to the return type of FN, we have to
  2234. combine the two conversions here. */
  2235. tree fixed_offset, virtual_offset;
  2236. over_return = TREE_TYPE (over_return);
  2237. base_return = TREE_TYPE (base_return);
  2238. if (DECL_THUNK_P (fn))
  2239. {
  2240. gcc_assert (DECL_RESULT_THUNK_P (fn));
  2241. fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
  2242. virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
  2243. }
  2244. else
  2245. fixed_offset = virtual_offset = NULL_TREE;
  2246. if (virtual_offset)
  2247. /* Find the equivalent binfo within the return type of the
  2248. overriding function. We will want the vbase offset from
  2249. there. */
  2250. virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
  2251. over_return);
  2252. else if (!same_type_ignoring_top_level_qualifiers_p
  2253. (over_return, base_return))
  2254. {
  2255. /* There was no existing virtual thunk (which takes
  2256. precedence). So find the binfo of the base function's
  2257. return type within the overriding function's return type.
  2258. We cannot call lookup base here, because we're inside a
  2259. dfs_walk, and will therefore clobber the BINFO_MARKED
  2260. flags. Fortunately we know the covariancy is valid (it
  2261. has already been checked), so we can just iterate along
  2262. the binfos, which have been chained in inheritance graph
  2263. order. Of course it is lame that we have to repeat the
  2264. search here anyway -- we should really be caching pieces
  2265. of the vtable and avoiding this repeated work. */
  2266. tree thunk_binfo, base_binfo;
  2267. /* Find the base binfo within the overriding function's
  2268. return type. We will always find a thunk_binfo, except
  2269. when the covariancy is invalid (which we will have
  2270. already diagnosed). */
  2271. for (base_binfo = TYPE_BINFO (base_return),
  2272. thunk_binfo = TYPE_BINFO (over_return);
  2273. thunk_binfo;
  2274. thunk_binfo = TREE_CHAIN (thunk_binfo))
  2275. if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
  2276. BINFO_TYPE (base_binfo)))
  2277. break;
  2278. /* See if virtual inheritance is involved. */
  2279. for (virtual_offset = thunk_binfo;
  2280. virtual_offset;
  2281. virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
  2282. if (BINFO_VIRTUAL_P (virtual_offset))
  2283. break;
  2284. if (virtual_offset
  2285. || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
  2286. {
  2287. tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
  2288. if (virtual_offset)
  2289. {
  2290. /* We convert via virtual base. Adjust the fixed
  2291. offset to be from there. */
  2292. offset =
  2293. size_diffop (offset,
  2294. convert (ssizetype,
  2295. BINFO_OFFSET (virtual_offset)));
  2296. }
  2297. if (fixed_offset)
  2298. /* There was an existing fixed offset, this must be
  2299. from the base just converted to, and the base the
  2300. FN was thunking to. */
  2301. fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
  2302. else
  2303. fixed_offset = offset;
  2304. }
  2305. }
  2306. if (fixed_offset || virtual_offset)
  2307. /* Replace the overriding function with a covariant thunk. We
  2308. will emit the overriding function in its own slot as
  2309. well. */
  2310. overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
  2311. fixed_offset, virtual_offset);
  2312. }
  2313. else
  2314. gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
  2315. !DECL_THUNK_P (fn));
  2316. /* If we need a covariant thunk, then we may need to adjust first_defn.
  2317. The ABI specifies that the thunks emitted with a function are
  2318. determined by which bases the function overrides, so we need to be
  2319. sure that we're using a thunk for some overridden base; even if we
  2320. know that the necessary this adjustment is zero, there may not be an
  2321. appropriate zero-this-adjusment thunk for us to use since thunks for
  2322. overriding virtual bases always use the vcall offset.
  2323. Furthermore, just choosing any base that overrides this function isn't
  2324. quite right, as this slot won't be used for calls through a type that
  2325. puts a covariant thunk here. Calling the function through such a type
  2326. will use a different slot, and that slot is the one that determines
  2327. the thunk emitted for that base.
  2328. So, keep looking until we find the base that we're really overriding
  2329. in this slot: the nearest primary base that doesn't use a covariant
  2330. thunk in this slot. */
  2331. if (overrider_target != overrider_fn)
  2332. {
  2333. if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
  2334. /* We already know that the overrider needs a covariant thunk. */
  2335. b = get_primary_binfo (b);
  2336. for (; ; b = get_primary_binfo (b))
  2337. {
  2338. tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
  2339. tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
  2340. if (!DECL_THUNK_P (TREE_VALUE (bv)))
  2341. break;
  2342. if (BINFO_LOST_PRIMARY_P (b))
  2343. lost = true;
  2344. }
  2345. first_defn = b;
  2346. }
  2347. /* Assume that we will produce a thunk that convert all the way to
  2348. the final overrider, and not to an intermediate virtual base. */
  2349. virtual_base = NULL_TREE;
  2350. /* See if we can convert to an intermediate virtual base first, and then
  2351. use the vcall offset located there to finish the conversion. */
  2352. for (; b; b = BINFO_INHERITANCE_CHAIN (b))
  2353. {
  2354. /* If we find the final overrider, then we can stop
  2355. walking. */
  2356. if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
  2357. BINFO_TYPE (TREE_VALUE (overrider))))
  2358. break;
  2359. /* If we find a virtual base, and we haven't yet found the
  2360. overrider, then there is a virtual base between the
  2361. declaring base (first_defn) and the final overrider. */
  2362. if (BINFO_VIRTUAL_P (b))
  2363. {
  2364. virtual_base = b;
  2365. break;
  2366. }
  2367. }
  2368. /* Compute the constant adjustment to the `this' pointer. The
  2369. `this' pointer, when this function is called, will point at BINFO
  2370. (or one of its primary bases, which are at the same offset). */
  2371. if (virtual_base)
  2372. /* The `this' pointer needs to be adjusted from the declaration to
  2373. the nearest virtual base. */
  2374. delta = size_diffop_loc (input_location,
  2375. convert (ssizetype, BINFO_OFFSET (virtual_base)),
  2376. convert (ssizetype, BINFO_OFFSET (first_defn)));
  2377. else if (lost)
  2378. /* If the nearest definition is in a lost primary, we don't need an
  2379. entry in our vtable. Except possibly in a constructor vtable,
  2380. if we happen to get our primary back. In that case, the offset
  2381. will be zero, as it will be a primary base. */
  2382. delta = size_zero_node;
  2383. else
  2384. /* The `this' pointer needs to be adjusted from pointing to
  2385. BINFO to pointing at the base where the final overrider
  2386. appears. */
  2387. delta = size_diffop_loc (input_location,
  2388. convert (ssizetype,
  2389. BINFO_OFFSET (TREE_VALUE (overrider))),
  2390. convert (ssizetype, BINFO_OFFSET (binfo)));
  2391. modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
  2392. if (virtual_base)
  2393. BV_VCALL_INDEX (*virtuals)
  2394. = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
  2395. else
  2396. BV_VCALL_INDEX (*virtuals) = NULL_TREE;
  2397. BV_LOST_PRIMARY (*virtuals) = lost;
  2398. }
  2399. /* Called from modify_all_vtables via dfs_walk. */
  2400. static tree
  2401. dfs_modify_vtables (tree binfo, void* data)
  2402. {
  2403. tree t = (tree) data;
  2404. tree virtuals;
  2405. tree old_virtuals;
  2406. unsigned ix;
  2407. if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
  2408. /* A base without a vtable needs no modification, and its bases
  2409. are uninteresting. */
  2410. return dfs_skip_bases;
  2411. if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
  2412. && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
  2413. /* Don't do the primary vtable, if it's new. */
  2414. return NULL_TREE;
  2415. if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
  2416. /* There's no need to modify the vtable for a non-virtual primary
  2417. base; we're not going to use that vtable anyhow. We do still
  2418. need to do this for virtual primary bases, as they could become
  2419. non-primary in a construction vtable. */
  2420. return NULL_TREE;
  2421. make_new_vtable (t, binfo);
  2422. /* Now, go through each of the virtual functions in the virtual
  2423. function table for BINFO. Find the final overrider, and update
  2424. the BINFO_VIRTUALS list appropriately. */
  2425. for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
  2426. old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
  2427. virtuals;
  2428. ix++, virtuals = TREE_CHAIN (virtuals),
  2429. old_virtuals = TREE_CHAIN (old_virtuals))
  2430. update_vtable_entry_for_fn (t,
  2431. binfo,
  2432. BV_FN (old_virtuals),
  2433. &virtuals, ix);
  2434. return NULL_TREE;
  2435. }
  2436. /* Update all of the primary and secondary vtables for T. Create new
  2437. vtables as required, and initialize their RTTI information. Each
  2438. of the functions in VIRTUALS is declared in T and may override a
  2439. virtual function from a base class; find and modify the appropriate
  2440. entries to point to the overriding functions. Returns a list, in
  2441. declaration order, of the virtual functions that are declared in T,
  2442. but do not appear in the primary base class vtable, and which
  2443. should therefore be appended to the end of the vtable for T. */
  2444. static tree
  2445. modify_all_vtables (tree t, tree virtuals)
  2446. {
  2447. tree binfo = TYPE_BINFO (t);
  2448. tree *fnsp;
  2449. /* Mangle the vtable name before entering dfs_walk (c++/51884). */
  2450. if (TYPE_CONTAINS_VPTR_P (t))
  2451. get_vtable_decl (t, false);
  2452. /* Update all of the vtables. */
  2453. dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
  2454. /* Add virtual functions not already in our primary vtable. These
  2455. will be both those introduced by this class, and those overridden
  2456. from secondary bases. It does not include virtuals merely
  2457. inherited from secondary bases. */
  2458. for (fnsp = &virtuals; *fnsp; )
  2459. {
  2460. tree fn = TREE_VALUE (*fnsp);
  2461. if (!value_member (fn, BINFO_VIRTUALS (binfo))
  2462. || DECL_VINDEX (fn) == error_mark_node)
  2463. {
  2464. /* We don't need to adjust the `this' pointer when
  2465. calling this function. */
  2466. BV_DELTA (*fnsp) = integer_zero_node;
  2467. BV_VCALL_INDEX (*fnsp) = NULL_TREE;
  2468. /* This is a function not already in our vtable. Keep it. */
  2469. fnsp = &TREE_CHAIN (*fnsp);
  2470. }
  2471. else
  2472. /* We've already got an entry for this function. Skip it. */
  2473. *fnsp = TREE_CHAIN (*fnsp);
  2474. }
  2475. return virtuals;
  2476. }
  2477. /* Get the base virtual function declarations in T that have the
  2478. indicated NAME. */
  2479. static void
  2480. get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
  2481. {
  2482. tree methods;
  2483. int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
  2484. int i;
  2485. /* Find virtual functions in T with the indicated NAME. */
  2486. i = lookup_fnfields_1 (t, name);
  2487. bool found_decls = false;
  2488. if (i != -1)
  2489. for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
  2490. methods;
  2491. methods = OVL_NEXT (methods))
  2492. {
  2493. tree method = OVL_CURRENT (methods);
  2494. if (TREE_CODE (method) == FUNCTION_DECL
  2495. && DECL_VINDEX (method))
  2496. {
  2497. base_fndecls->safe_push (method);
  2498. found_decls = true;
  2499. }
  2500. }
  2501. if (found_decls)
  2502. return;
  2503. for (i = 0; i < n_baseclasses; i++)
  2504. {
  2505. tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
  2506. get_basefndecls (name, basetype, base_fndecls);
  2507. }
  2508. }
  2509. /* If this declaration supersedes the declaration of
  2510. a method declared virtual in the base class, then
  2511. mark this field as being virtual as well. */
  2512. void
  2513. check_for_override (tree decl, tree ctype)
  2514. {
  2515. bool overrides_found = false;
  2516. if (TREE_CODE (decl) == TEMPLATE_DECL)
  2517. /* In [temp.mem] we have:
  2518. A specialization of a member function template does not
  2519. override a virtual function from a base class. */
  2520. return;
  2521. if ((DECL_DESTRUCTOR_P (decl)
  2522. || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
  2523. || DECL_CONV_FN_P (decl))
  2524. && look_for_overrides (ctype, decl)
  2525. && !DECL_STATIC_FUNCTION_P (decl))
  2526. /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
  2527. the error_mark_node so that we know it is an overriding
  2528. function. */
  2529. {
  2530. DECL_VINDEX (decl) = decl;
  2531. overrides_found = true;
  2532. if (warn_override && !DECL_OVERRIDE_P (decl)
  2533. && !DECL_DESTRUCTOR_P (decl))
  2534. warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
  2535. "%q+D can be marked override", decl);
  2536. }
  2537. if (DECL_VIRTUAL_P (decl))
  2538. {
  2539. if (!DECL_VINDEX (decl))
  2540. DECL_VINDEX (decl) = error_mark_node;
  2541. IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
  2542. if (DECL_DESTRUCTOR_P (decl))
  2543. TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
  2544. }
  2545. else if (DECL_FINAL_P (decl))
  2546. error ("%q+#D marked %<final%>, but is not virtual", decl);
  2547. if (DECL_OVERRIDE_P (decl) && !overrides_found)
  2548. error ("%q+#D marked %<override%>, but does not override", decl);
  2549. }
  2550. /* Warn about hidden virtual functions that are not overridden in t.
  2551. We know that constructors and destructors don't apply. */
  2552. static void
  2553. warn_hidden (tree t)
  2554. {
  2555. vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
  2556. tree fns;
  2557. size_t i;
  2558. /* We go through each separately named virtual function. */
  2559. for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
  2560. vec_safe_iterate (method_vec, i, &fns);
  2561. ++i)
  2562. {
  2563. tree fn;
  2564. tree name;
  2565. tree fndecl;
  2566. tree base_binfo;
  2567. tree binfo;
  2568. int j;
  2569. /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
  2570. have the same name. Figure out what name that is. */
  2571. name = DECL_NAME (OVL_CURRENT (fns));
  2572. /* There are no possibly hidden functions yet. */
  2573. auto_vec<tree, 20> base_fndecls;
  2574. /* Iterate through all of the base classes looking for possibly
  2575. hidden functions. */
  2576. for (binfo = TYPE_BINFO (t), j = 0;
  2577. BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
  2578. {
  2579. tree basetype = BINFO_TYPE (base_binfo);
  2580. get_basefndecls (name, basetype, &base_fndecls);
  2581. }
  2582. /* If there are no functions to hide, continue. */
  2583. if (base_fndecls.is_empty ())
  2584. continue;
  2585. /* Remove any overridden functions. */
  2586. for (fn = fns; fn; fn = OVL_NEXT (fn))
  2587. {
  2588. fndecl = OVL_CURRENT (fn);
  2589. if (TREE_CODE (fndecl) == FUNCTION_DECL
  2590. && DECL_VINDEX (fndecl))
  2591. {
  2592. /* If the method from the base class has the same
  2593. signature as the method from the derived class, it
  2594. has been overridden. */
  2595. for (size_t k = 0; k < base_fndecls.length (); k++)
  2596. if (base_fndecls[k]
  2597. && same_signature_p (fndecl, base_fndecls[k]))
  2598. base_fndecls[k] = NULL_TREE;
  2599. }
  2600. }
  2601. /* Now give a warning for all base functions without overriders,
  2602. as they are hidden. */
  2603. size_t k;
  2604. tree base_fndecl;
  2605. FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
  2606. if (base_fndecl)
  2607. {
  2608. /* Here we know it is a hider, and no overrider exists. */
  2609. warning (OPT_Woverloaded_virtual, "%q+D was hidden", base_fndecl);
  2610. warning (OPT_Woverloaded_virtual, " by %q+D", fns);
  2611. }
  2612. }
  2613. }
  2614. /* Recursive helper for finish_struct_anon. */
  2615. static void
  2616. finish_struct_anon_r (tree field, bool complain)
  2617. {
  2618. bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
  2619. tree elt = TYPE_FIELDS (TREE_TYPE (field));
  2620. for (; elt; elt = DECL_CHAIN (elt))
  2621. {
  2622. /* We're generally only interested in entities the user
  2623. declared, but we also find nested classes by noticing
  2624. the TYPE_DECL that we create implicitly. You're
  2625. allowed to put one anonymous union inside another,
  2626. though, so we explicitly tolerate that. We use
  2627. TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
  2628. we also allow unnamed types used for defining fields. */
  2629. if (DECL_ARTIFICIAL (elt)
  2630. && (!DECL_IMPLICIT_TYPEDEF_P (elt)
  2631. || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
  2632. continue;
  2633. if (TREE_CODE (elt) != FIELD_DECL)
  2634. {
  2635. /* We already complained about static data members in
  2636. finish_static_data_member_decl. */
  2637. if (complain && TREE_CODE (elt) != VAR_DECL)
  2638. {
  2639. if (is_union)
  2640. permerror (input_location,
  2641. "%q+#D invalid; an anonymous union can "
  2642. "only have non-static data members", elt);
  2643. else
  2644. permerror (input_location,
  2645. "%q+#D invalid; an anonymous struct can "
  2646. "only have non-static data members", elt);
  2647. }
  2648. continue;
  2649. }
  2650. if (complain)
  2651. {
  2652. if (TREE_PRIVATE (elt))
  2653. {
  2654. if (is_union)
  2655. permerror (input_location,
  2656. "private member %q+#D in anonymous union", elt);
  2657. else
  2658. permerror (input_location,
  2659. "private member %q+#D in anonymous struct", elt);
  2660. }
  2661. else if (TREE_PROTECTED (elt))
  2662. {
  2663. if (is_union)
  2664. permerror (input_location,
  2665. "protected member %q+#D in anonymous union", elt);
  2666. else
  2667. permerror (input_location,
  2668. "protected member %q+#D in anonymous struct", elt);
  2669. }
  2670. }
  2671. TREE_PRIVATE (elt) = TREE_PRIVATE (field);
  2672. TREE_PROTECTED (elt) = TREE_PROTECTED (field);
  2673. /* Recurse into the anonymous aggregates to handle correctly
  2674. access control (c++/24926):
  2675. class A {
  2676. union {
  2677. union {
  2678. int i;
  2679. };
  2680. };
  2681. };
  2682. int j=A().i; */
  2683. if (DECL_NAME (elt) == NULL_TREE
  2684. && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
  2685. finish_struct_anon_r (elt, /*complain=*/false);
  2686. }
  2687. }
  2688. /* Check for things that are invalid. There are probably plenty of other
  2689. things we should check for also. */
  2690. static void
  2691. finish_struct_anon (tree t)
  2692. {
  2693. for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
  2694. {
  2695. if (TREE_STATIC (field))
  2696. continue;
  2697. if (TREE_CODE (field) != FIELD_DECL)
  2698. continue;
  2699. if (DECL_NAME (field) == NULL_TREE
  2700. && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
  2701. finish_struct_anon_r (field, /*complain=*/true);
  2702. }
  2703. }
  2704. /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
  2705. will be used later during class template instantiation.
  2706. When FRIEND_P is zero, T can be a static member data (VAR_DECL),
  2707. a non-static member data (FIELD_DECL), a member function
  2708. (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
  2709. a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
  2710. When FRIEND_P is nonzero, T is either a friend class
  2711. (RECORD_TYPE, TEMPLATE_DECL) or a friend function
  2712. (FUNCTION_DECL, TEMPLATE_DECL). */
  2713. void
  2714. maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
  2715. {
  2716. /* Save some memory by not creating TREE_LIST if TYPE is not template. */
  2717. if (CLASSTYPE_TEMPLATE_INFO (type))
  2718. CLASSTYPE_DECL_LIST (type)
  2719. = tree_cons (friend_p ? NULL_TREE : type,
  2720. t, CLASSTYPE_DECL_LIST (type));
  2721. }
  2722. /* This function is called from declare_virt_assop_and_dtor via
  2723. dfs_walk_all.
  2724. DATA is a type that direcly or indirectly inherits the base
  2725. represented by BINFO. If BINFO contains a virtual assignment [copy
  2726. assignment or move assigment] operator or a virtual constructor,
  2727. declare that function in DATA if it hasn't been already declared. */
  2728. static tree
  2729. dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
  2730. {
  2731. tree bv, fn, t = (tree)data;
  2732. tree opname = ansi_assopname (NOP_EXPR);
  2733. gcc_assert (t && CLASS_TYPE_P (t));
  2734. gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
  2735. if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
  2736. /* A base without a vtable needs no modification, and its bases
  2737. are uninteresting. */
  2738. return dfs_skip_bases;
  2739. if (BINFO_PRIMARY_P (binfo))
  2740. /* If this is a primary base, then we have already looked at the
  2741. virtual functions of its vtable. */
  2742. return NULL_TREE;
  2743. for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
  2744. {
  2745. fn = BV_FN (bv);
  2746. if (DECL_NAME (fn) == opname)
  2747. {
  2748. if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
  2749. lazily_declare_fn (sfk_copy_assignment, t);
  2750. if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
  2751. lazily_declare_fn (sfk_move_assignment, t);
  2752. }
  2753. else if (DECL_DESTRUCTOR_P (fn)
  2754. && CLASSTYPE_LAZY_DESTRUCTOR (t))
  2755. lazily_declare_fn (sfk_destructor, t);
  2756. }
  2757. return NULL_TREE;
  2758. }
  2759. /* If the class type T has a direct or indirect base that contains a
  2760. virtual assignment operator or a virtual destructor, declare that
  2761. function in T if it hasn't been already declared. */
  2762. static void
  2763. declare_virt_assop_and_dtor (tree t)
  2764. {
  2765. if (!(TYPE_POLYMORPHIC_P (t)
  2766. && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
  2767. || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
  2768. || CLASSTYPE_LAZY_DESTRUCTOR (t))))
  2769. return;
  2770. dfs_walk_all (TYPE_BINFO (t),
  2771. dfs_declare_virt_assop_and_dtor,
  2772. NULL, t);
  2773. }
  2774. /* Declare the inheriting constructor for class T inherited from base
  2775. constructor CTOR with the parameter array PARMS of size NPARMS. */
  2776. static void
  2777. one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
  2778. {
  2779. /* We don't declare an inheriting ctor that would be a default,
  2780. copy or move ctor for derived or base. */
  2781. if (nparms == 0)
  2782. return;
  2783. if (nparms == 1
  2784. && TREE_CODE (parms[0]) == REFERENCE_TYPE)
  2785. {
  2786. tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
  2787. if (parm == t || parm == DECL_CONTEXT (ctor))
  2788. return;
  2789. }
  2790. tree parmlist = void_list_node;
  2791. for (int i = nparms - 1; i >= 0; i--)
  2792. parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
  2793. tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
  2794. t, false, ctor, parmlist);
  2795. if (add_method (t, fn, NULL_TREE))
  2796. {
  2797. DECL_CHAIN (fn) = TYPE_METHODS (t);
  2798. TYPE_METHODS (t) = fn;
  2799. }
  2800. }
  2801. /* Declare all the inheriting constructors for class T inherited from base
  2802. constructor CTOR. */
  2803. static void
  2804. one_inherited_ctor (tree ctor, tree t)
  2805. {
  2806. tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
  2807. tree *new_parms = XALLOCAVEC (tree, list_length (parms));
  2808. int i = 0;
  2809. for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
  2810. {
  2811. if (TREE_PURPOSE (parms))
  2812. one_inheriting_sig (t, ctor, new_parms, i);
  2813. new_parms[i++] = TREE_VALUE (parms);
  2814. }
  2815. one_inheriting_sig (t, ctor, new_parms, i);
  2816. if (parms == NULL_TREE)
  2817. {
  2818. if (warning (OPT_Winherited_variadic_ctor,
  2819. "the ellipsis in %qD is not inherited", ctor))
  2820. inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
  2821. }
  2822. }
  2823. /* Create default constructors, assignment operators, and so forth for
  2824. the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
  2825. and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
  2826. the class cannot have a default constructor, copy constructor
  2827. taking a const reference argument, or an assignment operator taking
  2828. a const reference, respectively. */
  2829. static void
  2830. add_implicitly_declared_members (tree t, tree* access_decls,
  2831. int cant_have_const_cctor,
  2832. int cant_have_const_assignment)
  2833. {
  2834. bool move_ok = false;
  2835. if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
  2836. && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
  2837. && !type_has_move_constructor (t) && !type_has_move_assign (t))
  2838. move_ok = true;
  2839. /* Destructor. */
  2840. if (!CLASSTYPE_DESTRUCTORS (t))
  2841. {
  2842. /* In general, we create destructors lazily. */
  2843. CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
  2844. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
  2845. && TYPE_FOR_JAVA (t))
  2846. /* But if this is a Java class, any non-trivial destructor is
  2847. invalid, even if compiler-generated. Therefore, if the
  2848. destructor is non-trivial we create it now. */
  2849. lazily_declare_fn (sfk_destructor, t);
  2850. }
  2851. /* [class.ctor]
  2852. If there is no user-declared constructor for a class, a default
  2853. constructor is implicitly declared. */
  2854. if (! TYPE_HAS_USER_CONSTRUCTOR (t))
  2855. {
  2856. TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
  2857. CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
  2858. if (cxx_dialect >= cxx11)
  2859. TYPE_HAS_CONSTEXPR_CTOR (t)
  2860. /* This might force the declaration. */
  2861. = type_has_constexpr_default_constructor (t);
  2862. }
  2863. /* [class.ctor]
  2864. If a class definition does not explicitly declare a copy
  2865. constructor, one is declared implicitly. */
  2866. if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
  2867. {
  2868. TYPE_HAS_COPY_CTOR (t) = 1;
  2869. TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
  2870. CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
  2871. if (move_ok)
  2872. CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
  2873. }
  2874. /* If there is no assignment operator, one will be created if and
  2875. when it is needed. For now, just record whether or not the type
  2876. of the parameter to the assignment operator will be a const or
  2877. non-const reference. */
  2878. if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
  2879. {
  2880. TYPE_HAS_COPY_ASSIGN (t) = 1;
  2881. TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
  2882. CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
  2883. if (move_ok && !LAMBDA_TYPE_P (t))
  2884. CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
  2885. }
  2886. /* We can't be lazy about declaring functions that might override
  2887. a virtual function from a base class. */
  2888. declare_virt_assop_and_dtor (t);
  2889. while (*access_decls)
  2890. {
  2891. tree using_decl = TREE_VALUE (*access_decls);
  2892. tree decl = USING_DECL_DECLS (using_decl);
  2893. if (DECL_NAME (using_decl) == ctor_identifier)
  2894. {
  2895. /* declare, then remove the decl */
  2896. tree ctor_list = decl;
  2897. location_t loc = input_location;
  2898. input_location = DECL_SOURCE_LOCATION (using_decl);
  2899. if (ctor_list)
  2900. for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
  2901. one_inherited_ctor (OVL_CURRENT (ctor_list), t);
  2902. *access_decls = TREE_CHAIN (*access_decls);
  2903. input_location = loc;
  2904. }
  2905. else
  2906. access_decls = &TREE_CHAIN (*access_decls);
  2907. }
  2908. }
  2909. /* Subroutine of insert_into_classtype_sorted_fields. Recursively
  2910. count the number of fields in TYPE, including anonymous union
  2911. members. */
  2912. static int
  2913. count_fields (tree fields)
  2914. {
  2915. tree x;
  2916. int n_fields = 0;
  2917. for (x = fields; x; x = DECL_CHAIN (x))
  2918. {
  2919. if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
  2920. n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
  2921. else
  2922. n_fields += 1;
  2923. }
  2924. return n_fields;
  2925. }
  2926. /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
  2927. all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
  2928. elts, starting at offset IDX. */
  2929. static int
  2930. add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
  2931. {
  2932. tree x;
  2933. for (x = fields; x; x = DECL_CHAIN (x))
  2934. {
  2935. if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
  2936. idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
  2937. else
  2938. field_vec->elts[idx++] = x;
  2939. }
  2940. return idx;
  2941. }
  2942. /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
  2943. starting at offset IDX. */
  2944. static int
  2945. add_enum_fields_to_record_type (tree enumtype,
  2946. struct sorted_fields_type *field_vec,
  2947. int idx)
  2948. {
  2949. tree values;
  2950. for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
  2951. field_vec->elts[idx++] = TREE_VALUE (values);
  2952. return idx;
  2953. }
  2954. /* FIELD is a bit-field. We are finishing the processing for its
  2955. enclosing type. Issue any appropriate messages and set appropriate
  2956. flags. Returns false if an error has been diagnosed. */
  2957. static bool
  2958. check_bitfield_decl (tree field)
  2959. {
  2960. tree type = TREE_TYPE (field);
  2961. tree w;
  2962. /* Extract the declared width of the bitfield, which has been
  2963. temporarily stashed in DECL_INITIAL. */
  2964. w = DECL_INITIAL (field);
  2965. gcc_assert (w != NULL_TREE);
  2966. /* Remove the bit-field width indicator so that the rest of the
  2967. compiler does not treat that value as an initializer. */
  2968. DECL_INITIAL (field) = NULL_TREE;
  2969. /* Detect invalid bit-field type. */
  2970. if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
  2971. {
  2972. error ("bit-field %q+#D with non-integral type", field);
  2973. w = error_mark_node;
  2974. }
  2975. else
  2976. {
  2977. location_t loc = input_location;
  2978. /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
  2979. STRIP_NOPS (w);
  2980. /* detect invalid field size. */
  2981. input_location = DECL_SOURCE_LOCATION (field);
  2982. w = cxx_constant_value (w);
  2983. input_location = loc;
  2984. if (TREE_CODE (w) != INTEGER_CST)
  2985. {
  2986. error ("bit-field %q+D width not an integer constant", field);
  2987. w = error_mark_node;
  2988. }
  2989. else if (tree_int_cst_sgn (w) < 0)
  2990. {
  2991. error ("negative width in bit-field %q+D", field);
  2992. w = error_mark_node;
  2993. }
  2994. else if (integer_zerop (w) && DECL_NAME (field) != 0)
  2995. {
  2996. error ("zero width for bit-field %q+D", field);
  2997. w = error_mark_node;
  2998. }
  2999. else if ((TREE_CODE (type) != ENUMERAL_TYPE
  3000. && TREE_CODE (type) != BOOLEAN_TYPE
  3001. && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
  3002. || ((TREE_CODE (type) == ENUMERAL_TYPE
  3003. || TREE_CODE (type) == BOOLEAN_TYPE)
  3004. && tree_int_cst_lt (TYPE_SIZE (type), w)))
  3005. warning (0, "width of %q+D exceeds its type", field);
  3006. else if (TREE_CODE (type) == ENUMERAL_TYPE
  3007. && (0 > (compare_tree_int
  3008. (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
  3009. warning (0, "%q+D is too small to hold all values of %q#T", field, type);
  3010. }
  3011. if (w != error_mark_node)
  3012. {
  3013. DECL_SIZE (field) = convert (bitsizetype, w);
  3014. DECL_BIT_FIELD (field) = 1;
  3015. return true;
  3016. }
  3017. else
  3018. {
  3019. /* Non-bit-fields are aligned for their type. */
  3020. DECL_BIT_FIELD (field) = 0;
  3021. CLEAR_DECL_C_BIT_FIELD (field);
  3022. return false;
  3023. }
  3024. }
  3025. /* FIELD is a non bit-field. We are finishing the processing for its
  3026. enclosing type T. Issue any appropriate messages and set appropriate
  3027. flags. */
  3028. static void
  3029. check_field_decl (tree field,
  3030. tree t,
  3031. int* cant_have_const_ctor,
  3032. int* no_const_asn_ref,
  3033. int* any_default_members)
  3034. {
  3035. tree type = strip_array_types (TREE_TYPE (field));
  3036. /* In C++98 an anonymous union cannot contain any fields which would change
  3037. the settings of CANT_HAVE_CONST_CTOR and friends. */
  3038. if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
  3039. ;
  3040. /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
  3041. structs. So, we recurse through their fields here. */
  3042. else if (ANON_AGGR_TYPE_P (type))
  3043. {
  3044. tree fields;
  3045. for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
  3046. if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
  3047. check_field_decl (fields, t, cant_have_const_ctor,
  3048. no_const_asn_ref, any_default_members);
  3049. }
  3050. /* Check members with class type for constructors, destructors,
  3051. etc. */
  3052. else if (CLASS_TYPE_P (type))
  3053. {
  3054. /* Never let anything with uninheritable virtuals
  3055. make it through without complaint. */
  3056. abstract_virtuals_error (field, type);
  3057. if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
  3058. {
  3059. static bool warned;
  3060. int oldcount = errorcount;
  3061. if (TYPE_NEEDS_CONSTRUCTING (type))
  3062. error ("member %q+#D with constructor not allowed in union",
  3063. field);
  3064. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  3065. error ("member %q+#D with destructor not allowed in union", field);
  3066. if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
  3067. error ("member %q+#D with copy assignment operator not allowed in union",
  3068. field);
  3069. if (!warned && errorcount > oldcount)
  3070. {
  3071. inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
  3072. "only available with -std=c++11 or -std=gnu++11");
  3073. warned = true;
  3074. }
  3075. }
  3076. else
  3077. {
  3078. TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
  3079. TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
  3080. |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
  3081. TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
  3082. |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
  3083. || !TYPE_HAS_COPY_ASSIGN (type));
  3084. TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
  3085. || !TYPE_HAS_COPY_CTOR (type));
  3086. TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
  3087. TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
  3088. TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
  3089. || TYPE_HAS_COMPLEX_DFLT (type));
  3090. }
  3091. if (TYPE_HAS_COPY_CTOR (type)
  3092. && !TYPE_HAS_CONST_COPY_CTOR (type))
  3093. *cant_have_const_ctor = 1;
  3094. if (TYPE_HAS_COPY_ASSIGN (type)
  3095. && !TYPE_HAS_CONST_COPY_ASSIGN (type))
  3096. *no_const_asn_ref = 1;
  3097. }
  3098. check_abi_tags (t, field);
  3099. if (DECL_INITIAL (field) != NULL_TREE)
  3100. {
  3101. /* `build_class_init_list' does not recognize
  3102. non-FIELD_DECLs. */
  3103. if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
  3104. error ("multiple fields in union %qT initialized", t);
  3105. *any_default_members = 1;
  3106. }
  3107. }
  3108. /* Check the data members (both static and non-static), class-scoped
  3109. typedefs, etc., appearing in the declaration of T. Issue
  3110. appropriate diagnostics. Sets ACCESS_DECLS to a list (in
  3111. declaration order) of access declarations; each TREE_VALUE in this
  3112. list is a USING_DECL.
  3113. In addition, set the following flags:
  3114. EMPTY_P
  3115. The class is empty, i.e., contains no non-static data members.
  3116. CANT_HAVE_CONST_CTOR_P
  3117. This class cannot have an implicitly generated copy constructor
  3118. taking a const reference.
  3119. CANT_HAVE_CONST_ASN_REF
  3120. This class cannot have an implicitly generated assignment
  3121. operator taking a const reference.
  3122. All of these flags should be initialized before calling this
  3123. function.
  3124. Returns a pointer to the end of the TYPE_FIELDs chain; additional
  3125. fields can be added by adding to this chain. */
  3126. static void
  3127. check_field_decls (tree t, tree *access_decls,
  3128. int *cant_have_const_ctor_p,
  3129. int *no_const_asn_ref_p)
  3130. {
  3131. tree *field;
  3132. tree *next;
  3133. bool has_pointers;
  3134. int any_default_members;
  3135. int cant_pack = 0;
  3136. int field_access = -1;
  3137. /* Assume there are no access declarations. */
  3138. *access_decls = NULL_TREE;
  3139. /* Assume this class has no pointer members. */
  3140. has_pointers = false;
  3141. /* Assume none of the members of this class have default
  3142. initializations. */
  3143. any_default_members = 0;
  3144. for (field = &TYPE_FIELDS (t); *field; field = next)
  3145. {
  3146. tree x = *field;
  3147. tree type = TREE_TYPE (x);
  3148. int this_field_access;
  3149. next = &DECL_CHAIN (x);
  3150. if (TREE_CODE (x) == USING_DECL)
  3151. {
  3152. /* Save the access declarations for our caller. */
  3153. *access_decls = tree_cons (NULL_TREE, x, *access_decls);
  3154. continue;
  3155. }
  3156. if (TREE_CODE (x) == TYPE_DECL
  3157. || TREE_CODE (x) == TEMPLATE_DECL)
  3158. continue;
  3159. /* If we've gotten this far, it's a data member, possibly static,
  3160. or an enumerator. */
  3161. if (TREE_CODE (x) != CONST_DECL)
  3162. DECL_CONTEXT (x) = t;
  3163. /* When this goes into scope, it will be a non-local reference. */
  3164. DECL_NONLOCAL (x) = 1;
  3165. if (TREE_CODE (t) == UNION_TYPE
  3166. && cxx_dialect < cxx11)
  3167. {
  3168. /* [class.union] (C++98)
  3169. If a union contains a static data member, or a member of
  3170. reference type, the program is ill-formed.
  3171. In C++11 this limitation doesn't exist anymore. */
  3172. if (VAR_P (x))
  3173. {
  3174. error ("in C++98 %q+D may not be static because it is "
  3175. "a member of a union", x);
  3176. continue;
  3177. }
  3178. if (TREE_CODE (type) == REFERENCE_TYPE)
  3179. {
  3180. error ("in C++98 %q+D may not have reference type %qT "
  3181. "because it is a member of a union", x, type);
  3182. continue;
  3183. }
  3184. }
  3185. /* Perform error checking that did not get done in
  3186. grokdeclarator. */
  3187. if (TREE_CODE (type) == FUNCTION_TYPE)
  3188. {
  3189. error ("field %q+D invalidly declared function type", x);
  3190. type = build_pointer_type (type);
  3191. TREE_TYPE (x) = type;
  3192. }
  3193. else if (TREE_CODE (type) == METHOD_TYPE)
  3194. {
  3195. error ("field %q+D invalidly declared method type", x);
  3196. type = build_pointer_type (type);
  3197. TREE_TYPE (x) = type;
  3198. }
  3199. if (type == error_mark_node)
  3200. continue;
  3201. if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
  3202. continue;
  3203. /* Now it can only be a FIELD_DECL. */
  3204. if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
  3205. CLASSTYPE_NON_AGGREGATE (t) = 1;
  3206. /* If at least one non-static data member is non-literal, the whole
  3207. class becomes non-literal. Per Core/1453, volatile non-static
  3208. data members and base classes are also not allowed.
  3209. Note: if the type is incomplete we will complain later on. */
  3210. if (COMPLETE_TYPE_P (type)
  3211. && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
  3212. CLASSTYPE_LITERAL_P (t) = false;
  3213. /* A standard-layout class is a class that:
  3214. ...
  3215. has the same access control (Clause 11) for all non-static data members,
  3216. ... */
  3217. this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
  3218. if (field_access == -1)
  3219. field_access = this_field_access;
  3220. else if (this_field_access != field_access)
  3221. CLASSTYPE_NON_STD_LAYOUT (t) = 1;
  3222. /* If this is of reference type, check if it needs an init. */
  3223. if (TREE_CODE (type) == REFERENCE_TYPE)
  3224. {
  3225. CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
  3226. CLASSTYPE_NON_STD_LAYOUT (t) = 1;
  3227. if (DECL_INITIAL (x) == NULL_TREE)
  3228. SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
  3229. if (cxx_dialect < cxx11)
  3230. {
  3231. /* ARM $12.6.2: [A member initializer list] (or, for an
  3232. aggregate, initialization by a brace-enclosed list) is the
  3233. only way to initialize nonstatic const and reference
  3234. members. */
  3235. TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
  3236. TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
  3237. }
  3238. }
  3239. type = strip_array_types (type);
  3240. if (TYPE_PACKED (t))
  3241. {
  3242. if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
  3243. {
  3244. warning
  3245. (0,
  3246. "ignoring packed attribute because of unpacked non-POD field %q+#D",
  3247. x);
  3248. cant_pack = 1;
  3249. }
  3250. else if (DECL_C_BIT_FIELD (x)
  3251. || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
  3252. DECL_PACKED (x) = 1;
  3253. }
  3254. if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
  3255. /* We don't treat zero-width bitfields as making a class
  3256. non-empty. */
  3257. ;
  3258. else
  3259. {
  3260. /* The class is non-empty. */
  3261. CLASSTYPE_EMPTY_P (t) = 0;
  3262. /* The class is not even nearly empty. */
  3263. CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
  3264. /* If one of the data members contains an empty class,
  3265. so does T. */
  3266. if (CLASS_TYPE_P (type)
  3267. && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
  3268. CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
  3269. }
  3270. /* This is used by -Weffc++ (see below). Warn only for pointers
  3271. to members which might hold dynamic memory. So do not warn
  3272. for pointers to functions or pointers to members. */
  3273. if (TYPE_PTR_P (type)
  3274. && !TYPE_PTRFN_P (type))
  3275. has_pointers = true;
  3276. if (CLASS_TYPE_P (type))
  3277. {
  3278. if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
  3279. SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
  3280. if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
  3281. SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
  3282. }
  3283. if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
  3284. CLASSTYPE_HAS_MUTABLE (t) = 1;
  3285. if (DECL_MUTABLE_P (x))
  3286. {
  3287. if (CP_TYPE_CONST_P (type))
  3288. {
  3289. error ("member %q+D cannot be declared both %<const%> "
  3290. "and %<mutable%>", x);
  3291. continue;
  3292. }
  3293. if (TREE_CODE (type) == REFERENCE_TYPE)
  3294. {
  3295. error ("member %q+D cannot be declared as a %<mutable%> "
  3296. "reference", x);
  3297. continue;
  3298. }
  3299. }
  3300. if (! layout_pod_type_p (type))
  3301. /* DR 148 now allows pointers to members (which are POD themselves),
  3302. to be allowed in POD structs. */
  3303. CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
  3304. if (!std_layout_type_p (type))
  3305. CLASSTYPE_NON_STD_LAYOUT (t) = 1;
  3306. if (! zero_init_p (type))
  3307. CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
  3308. /* We set DECL_C_BIT_FIELD in grokbitfield.
  3309. If the type and width are valid, we'll also set DECL_BIT_FIELD. */
  3310. if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
  3311. check_field_decl (x, t,
  3312. cant_have_const_ctor_p,
  3313. no_const_asn_ref_p,
  3314. &any_default_members);
  3315. /* Now that we've removed bit-field widths from DECL_INITIAL,
  3316. anything left in DECL_INITIAL is an NSDMI that makes the class
  3317. non-aggregate in C++11. */
  3318. if (DECL_INITIAL (x) && cxx_dialect < cxx14)
  3319. CLASSTYPE_NON_AGGREGATE (t) = true;
  3320. /* If any field is const, the structure type is pseudo-const. */
  3321. if (CP_TYPE_CONST_P (type))
  3322. {
  3323. C_TYPE_FIELDS_READONLY (t) = 1;
  3324. if (DECL_INITIAL (x) == NULL_TREE)
  3325. SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
  3326. if (cxx_dialect < cxx11)
  3327. {
  3328. /* ARM $12.6.2: [A member initializer list] (or, for an
  3329. aggregate, initialization by a brace-enclosed list) is the
  3330. only way to initialize nonstatic const and reference
  3331. members. */
  3332. TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
  3333. TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
  3334. }
  3335. }
  3336. /* A field that is pseudo-const makes the structure likewise. */
  3337. else if (CLASS_TYPE_P (type))
  3338. {
  3339. C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
  3340. SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
  3341. CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
  3342. | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
  3343. }
  3344. /* Core issue 80: A nonstatic data member is required to have a
  3345. different name from the class iff the class has a
  3346. user-declared constructor. */
  3347. if (constructor_name_p (DECL_NAME (x), t)
  3348. && TYPE_HAS_USER_CONSTRUCTOR (t))
  3349. permerror (input_location, "field %q+#D with same name as class", x);
  3350. }
  3351. /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
  3352. it should also define a copy constructor and an assignment operator to
  3353. implement the correct copy semantic (deep vs shallow, etc.). As it is
  3354. not feasible to check whether the constructors do allocate dynamic memory
  3355. and store it within members, we approximate the warning like this:
  3356. -- Warn only if there are members which are pointers
  3357. -- Warn only if there is a non-trivial constructor (otherwise,
  3358. there cannot be memory allocated).
  3359. -- Warn only if there is a non-trivial destructor. We assume that the
  3360. user at least implemented the cleanup correctly, and a destructor
  3361. is needed to free dynamic memory.
  3362. This seems enough for practical purposes. */
  3363. if (warn_ecpp
  3364. && has_pointers
  3365. && TYPE_HAS_USER_CONSTRUCTOR (t)
  3366. && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
  3367. && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
  3368. {
  3369. warning (OPT_Weffc__, "%q#T has pointer data members", t);
  3370. if (! TYPE_HAS_COPY_CTOR (t))
  3371. {
  3372. warning (OPT_Weffc__,
  3373. " but does not override %<%T(const %T&)%>", t, t);
  3374. if (!TYPE_HAS_COPY_ASSIGN (t))
  3375. warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
  3376. }
  3377. else if (! TYPE_HAS_COPY_ASSIGN (t))
  3378. warning (OPT_Weffc__,
  3379. " but does not override %<operator=(const %T&)%>", t);
  3380. }
  3381. /* Non-static data member initializers make the default constructor
  3382. non-trivial. */
  3383. if (any_default_members)
  3384. {
  3385. TYPE_NEEDS_CONSTRUCTING (t) = true;
  3386. TYPE_HAS_COMPLEX_DFLT (t) = true;
  3387. }
  3388. /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
  3389. if (cant_pack)
  3390. TYPE_PACKED (t) = 0;
  3391. /* Check anonymous struct/anonymous union fields. */
  3392. finish_struct_anon (t);
  3393. /* We've built up the list of access declarations in reverse order.
  3394. Fix that now. */
  3395. *access_decls = nreverse (*access_decls);
  3396. }
  3397. /* If TYPE is an empty class type, records its OFFSET in the table of
  3398. OFFSETS. */
  3399. static int
  3400. record_subobject_offset (tree type, tree offset, splay_tree offsets)
  3401. {
  3402. splay_tree_node n;
  3403. if (!is_empty_class (type))
  3404. return 0;
  3405. /* Record the location of this empty object in OFFSETS. */
  3406. n = splay_tree_lookup (offsets, (splay_tree_key) offset);
  3407. if (!n)
  3408. n = splay_tree_insert (offsets,
  3409. (splay_tree_key) offset,
  3410. (splay_tree_value) NULL_TREE);
  3411. n->value = ((splay_tree_value)
  3412. tree_cons (NULL_TREE,
  3413. type,
  3414. (tree) n->value));
  3415. return 0;
  3416. }
  3417. /* Returns nonzero if TYPE is an empty class type and there is
  3418. already an entry in OFFSETS for the same TYPE as the same OFFSET. */
  3419. static int
  3420. check_subobject_offset (tree type, tree offset, splay_tree offsets)
  3421. {
  3422. splay_tree_node n;
  3423. tree t;
  3424. if (!is_empty_class (type))
  3425. return 0;
  3426. /* Record the location of this empty object in OFFSETS. */
  3427. n = splay_tree_lookup (offsets, (splay_tree_key) offset);
  3428. if (!n)
  3429. return 0;
  3430. for (t = (tree) n->value; t; t = TREE_CHAIN (t))
  3431. if (same_type_p (TREE_VALUE (t), type))
  3432. return 1;
  3433. return 0;
  3434. }
  3435. /* Walk through all the subobjects of TYPE (located at OFFSET). Call
  3436. F for every subobject, passing it the type, offset, and table of
  3437. OFFSETS. If VBASES_P is one, then virtual non-primary bases should
  3438. be traversed.
  3439. If MAX_OFFSET is non-NULL, then subobjects with an offset greater
  3440. than MAX_OFFSET will not be walked.
  3441. If F returns a nonzero value, the traversal ceases, and that value
  3442. is returned. Otherwise, returns zero. */
  3443. static int
  3444. walk_subobject_offsets (tree type,
  3445. subobject_offset_fn f,
  3446. tree offset,
  3447. splay_tree offsets,
  3448. tree max_offset,
  3449. int vbases_p)
  3450. {
  3451. int r = 0;
  3452. tree type_binfo = NULL_TREE;
  3453. /* If this OFFSET is bigger than the MAX_OFFSET, then we should
  3454. stop. */
  3455. if (max_offset && tree_int_cst_lt (max_offset, offset))
  3456. return 0;
  3457. if (type == error_mark_node)
  3458. return 0;
  3459. if (!TYPE_P (type))
  3460. {
  3461. type_binfo = type;
  3462. type = BINFO_TYPE (type);
  3463. }
  3464. if (CLASS_TYPE_P (type))
  3465. {
  3466. tree field;
  3467. tree binfo;
  3468. int i;
  3469. /* Avoid recursing into objects that are not interesting. */
  3470. if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
  3471. return 0;
  3472. /* Record the location of TYPE. */
  3473. r = (*f) (type, offset, offsets);
  3474. if (r)
  3475. return r;
  3476. /* Iterate through the direct base classes of TYPE. */
  3477. if (!type_binfo)
  3478. type_binfo = TYPE_BINFO (type);
  3479. for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
  3480. {
  3481. tree binfo_offset;
  3482. if (BINFO_VIRTUAL_P (binfo))
  3483. continue;
  3484. tree orig_binfo;
  3485. /* We cannot rely on BINFO_OFFSET being set for the base
  3486. class yet, but the offsets for direct non-virtual
  3487. bases can be calculated by going back to the TYPE. */
  3488. orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
  3489. binfo_offset = size_binop (PLUS_EXPR,
  3490. offset,
  3491. BINFO_OFFSET (orig_binfo));
  3492. r = walk_subobject_offsets (binfo,
  3493. f,
  3494. binfo_offset,
  3495. offsets,
  3496. max_offset,
  3497. /*vbases_p=*/0);
  3498. if (r)
  3499. return r;
  3500. }
  3501. if (CLASSTYPE_VBASECLASSES (type))
  3502. {
  3503. unsigned ix;
  3504. vec<tree, va_gc> *vbases;
  3505. /* Iterate through the virtual base classes of TYPE. In G++
  3506. 3.2, we included virtual bases in the direct base class
  3507. loop above, which results in incorrect results; the
  3508. correct offsets for virtual bases are only known when
  3509. working with the most derived type. */
  3510. if (vbases_p)
  3511. for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
  3512. vec_safe_iterate (vbases, ix, &binfo); ix++)
  3513. {
  3514. r = walk_subobject_offsets (binfo,
  3515. f,
  3516. size_binop (PLUS_EXPR,
  3517. offset,
  3518. BINFO_OFFSET (binfo)),
  3519. offsets,
  3520. max_offset,
  3521. /*vbases_p=*/0);
  3522. if (r)
  3523. return r;
  3524. }
  3525. else
  3526. {
  3527. /* We still have to walk the primary base, if it is
  3528. virtual. (If it is non-virtual, then it was walked
  3529. above.) */
  3530. tree vbase = get_primary_binfo (type_binfo);
  3531. if (vbase && BINFO_VIRTUAL_P (vbase)
  3532. && BINFO_PRIMARY_P (vbase)
  3533. && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
  3534. {
  3535. r = (walk_subobject_offsets
  3536. (vbase, f, offset,
  3537. offsets, max_offset, /*vbases_p=*/0));
  3538. if (r)
  3539. return r;
  3540. }
  3541. }
  3542. }
  3543. /* Iterate through the fields of TYPE. */
  3544. for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  3545. if (TREE_CODE (field) == FIELD_DECL
  3546. && TREE_TYPE (field) != error_mark_node
  3547. && !DECL_ARTIFICIAL (field))
  3548. {
  3549. tree field_offset;
  3550. field_offset = byte_position (field);
  3551. r = walk_subobject_offsets (TREE_TYPE (field),
  3552. f,
  3553. size_binop (PLUS_EXPR,
  3554. offset,
  3555. field_offset),
  3556. offsets,
  3557. max_offset,
  3558. /*vbases_p=*/1);
  3559. if (r)
  3560. return r;
  3561. }
  3562. }
  3563. else if (TREE_CODE (type) == ARRAY_TYPE)
  3564. {
  3565. tree element_type = strip_array_types (type);
  3566. tree domain = TYPE_DOMAIN (type);
  3567. tree index;
  3568. /* Avoid recursing into objects that are not interesting. */
  3569. if (!CLASS_TYPE_P (element_type)
  3570. || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
  3571. return 0;
  3572. /* Step through each of the elements in the array. */
  3573. for (index = size_zero_node;
  3574. !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
  3575. index = size_binop (PLUS_EXPR, index, size_one_node))
  3576. {
  3577. r = walk_subobject_offsets (TREE_TYPE (type),
  3578. f,
  3579. offset,
  3580. offsets,
  3581. max_offset,
  3582. /*vbases_p=*/1);
  3583. if (r)
  3584. return r;
  3585. offset = size_binop (PLUS_EXPR, offset,
  3586. TYPE_SIZE_UNIT (TREE_TYPE (type)));
  3587. /* If this new OFFSET is bigger than the MAX_OFFSET, then
  3588. there's no point in iterating through the remaining
  3589. elements of the array. */
  3590. if (max_offset && tree_int_cst_lt (max_offset, offset))
  3591. break;
  3592. }
  3593. }
  3594. return 0;
  3595. }
  3596. /* Record all of the empty subobjects of TYPE (either a type or a
  3597. binfo). If IS_DATA_MEMBER is true, then a non-static data member
  3598. is being placed at OFFSET; otherwise, it is a base class that is
  3599. being placed at OFFSET. */
  3600. static void
  3601. record_subobject_offsets (tree type,
  3602. tree offset,
  3603. splay_tree offsets,
  3604. bool is_data_member)
  3605. {
  3606. tree max_offset;
  3607. /* If recording subobjects for a non-static data member or a
  3608. non-empty base class , we do not need to record offsets beyond
  3609. the size of the biggest empty class. Additional data members
  3610. will go at the end of the class. Additional base classes will go
  3611. either at offset zero (if empty, in which case they cannot
  3612. overlap with offsets past the size of the biggest empty class) or
  3613. at the end of the class.
  3614. However, if we are placing an empty base class, then we must record
  3615. all offsets, as either the empty class is at offset zero (where
  3616. other empty classes might later be placed) or at the end of the
  3617. class (where other objects might then be placed, so other empty
  3618. subobjects might later overlap). */
  3619. if (is_data_member
  3620. || !is_empty_class (BINFO_TYPE (type)))
  3621. max_offset = sizeof_biggest_empty_class;
  3622. else
  3623. max_offset = NULL_TREE;
  3624. walk_subobject_offsets (type, record_subobject_offset, offset,
  3625. offsets, max_offset, is_data_member);
  3626. }
  3627. /* Returns nonzero if any of the empty subobjects of TYPE (located at
  3628. OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
  3629. virtual bases of TYPE are examined. */
  3630. static int
  3631. layout_conflict_p (tree type,
  3632. tree offset,
  3633. splay_tree offsets,
  3634. int vbases_p)
  3635. {
  3636. splay_tree_node max_node;
  3637. /* Get the node in OFFSETS that indicates the maximum offset where
  3638. an empty subobject is located. */
  3639. max_node = splay_tree_max (offsets);
  3640. /* If there aren't any empty subobjects, then there's no point in
  3641. performing this check. */
  3642. if (!max_node)
  3643. return 0;
  3644. return walk_subobject_offsets (type, check_subobject_offset, offset,
  3645. offsets, (tree) (max_node->key),
  3646. vbases_p);
  3647. }
  3648. /* DECL is a FIELD_DECL corresponding either to a base subobject of a
  3649. non-static data member of the type indicated by RLI. BINFO is the
  3650. binfo corresponding to the base subobject, OFFSETS maps offsets to
  3651. types already located at those offsets. This function determines
  3652. the position of the DECL. */
  3653. static void
  3654. layout_nonempty_base_or_field (record_layout_info rli,
  3655. tree decl,
  3656. tree binfo,
  3657. splay_tree offsets)
  3658. {
  3659. tree offset = NULL_TREE;
  3660. bool field_p;
  3661. tree type;
  3662. if (binfo)
  3663. {
  3664. /* For the purposes of determining layout conflicts, we want to
  3665. use the class type of BINFO; TREE_TYPE (DECL) will be the
  3666. CLASSTYPE_AS_BASE version, which does not contain entries for
  3667. zero-sized bases. */
  3668. type = TREE_TYPE (binfo);
  3669. field_p = false;
  3670. }
  3671. else
  3672. {
  3673. type = TREE_TYPE (decl);
  3674. field_p = true;
  3675. }
  3676. /* Try to place the field. It may take more than one try if we have
  3677. a hard time placing the field without putting two objects of the
  3678. same type at the same address. */
  3679. while (1)
  3680. {
  3681. struct record_layout_info_s old_rli = *rli;
  3682. /* Place this field. */
  3683. place_field (rli, decl);
  3684. offset = byte_position (decl);
  3685. /* We have to check to see whether or not there is already
  3686. something of the same type at the offset we're about to use.
  3687. For example, consider:
  3688. struct S {};
  3689. struct T : public S { int i; };
  3690. struct U : public S, public T {};
  3691. Here, we put S at offset zero in U. Then, we can't put T at
  3692. offset zero -- its S component would be at the same address
  3693. as the S we already allocated. So, we have to skip ahead.
  3694. Since all data members, including those whose type is an
  3695. empty class, have nonzero size, any overlap can happen only
  3696. with a direct or indirect base-class -- it can't happen with
  3697. a data member. */
  3698. /* In a union, overlap is permitted; all members are placed at
  3699. offset zero. */
  3700. if (TREE_CODE (rli->t) == UNION_TYPE)
  3701. break;
  3702. if (layout_conflict_p (field_p ? type : binfo, offset,
  3703. offsets, field_p))
  3704. {
  3705. /* Strip off the size allocated to this field. That puts us
  3706. at the first place we could have put the field with
  3707. proper alignment. */
  3708. *rli = old_rli;
  3709. /* Bump up by the alignment required for the type. */
  3710. rli->bitpos
  3711. = size_binop (PLUS_EXPR, rli->bitpos,
  3712. bitsize_int (binfo
  3713. ? CLASSTYPE_ALIGN (type)
  3714. : TYPE_ALIGN (type)));
  3715. normalize_rli (rli);
  3716. }
  3717. else if (TREE_CODE (type) == NULLPTR_TYPE
  3718. && warn_abi && abi_version_crosses (9))
  3719. {
  3720. /* Before ABI v9, we were giving nullptr_t alignment of 1; if
  3721. the offset wasn't aligned like a pointer when we started to
  3722. layout this field, that affects its position. */
  3723. tree pos = rli_size_unit_so_far (&old_rli);
  3724. if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
  3725. {
  3726. if (abi_version_at_least (9))
  3727. warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
  3728. "alignment of %qD increased in -fabi-version=9 "
  3729. "(GCC 5.2)", decl);
  3730. else
  3731. warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
  3732. "of %qD will increase in -fabi-version=9", decl);
  3733. }
  3734. break;
  3735. }
  3736. else
  3737. /* There was no conflict. We're done laying out this field. */
  3738. break;
  3739. }
  3740. /* Now that we know where it will be placed, update its
  3741. BINFO_OFFSET. */
  3742. if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
  3743. /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
  3744. this point because their BINFO_OFFSET is copied from another
  3745. hierarchy. Therefore, we may not need to add the entire
  3746. OFFSET. */
  3747. propagate_binfo_offsets (binfo,
  3748. size_diffop_loc (input_location,
  3749. convert (ssizetype, offset),
  3750. convert (ssizetype,
  3751. BINFO_OFFSET (binfo))));
  3752. }
  3753. /* Returns true if TYPE is empty and OFFSET is nonzero. */
  3754. static int
  3755. empty_base_at_nonzero_offset_p (tree type,
  3756. tree offset,
  3757. splay_tree /*offsets*/)
  3758. {
  3759. return is_empty_class (type) && !integer_zerop (offset);
  3760. }
  3761. /* Layout the empty base BINFO. EOC indicates the byte currently just
  3762. past the end of the class, and should be correctly aligned for a
  3763. class of the type indicated by BINFO; OFFSETS gives the offsets of
  3764. the empty bases allocated so far. T is the most derived
  3765. type. Return nonzero iff we added it at the end. */
  3766. static bool
  3767. layout_empty_base (record_layout_info rli, tree binfo,
  3768. tree eoc, splay_tree offsets)
  3769. {
  3770. tree alignment;
  3771. tree basetype = BINFO_TYPE (binfo);
  3772. bool atend = false;
  3773. /* This routine should only be used for empty classes. */
  3774. gcc_assert (is_empty_class (basetype));
  3775. alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
  3776. if (!integer_zerop (BINFO_OFFSET (binfo)))
  3777. propagate_binfo_offsets
  3778. (binfo, size_diffop_loc (input_location,
  3779. size_zero_node, BINFO_OFFSET (binfo)));
  3780. /* This is an empty base class. We first try to put it at offset
  3781. zero. */
  3782. if (layout_conflict_p (binfo,
  3783. BINFO_OFFSET (binfo),
  3784. offsets,
  3785. /*vbases_p=*/0))
  3786. {
  3787. /* That didn't work. Now, we move forward from the next
  3788. available spot in the class. */
  3789. atend = true;
  3790. propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
  3791. while (1)
  3792. {
  3793. if (!layout_conflict_p (binfo,
  3794. BINFO_OFFSET (binfo),
  3795. offsets,
  3796. /*vbases_p=*/0))
  3797. /* We finally found a spot where there's no overlap. */
  3798. break;
  3799. /* There's overlap here, too. Bump along to the next spot. */
  3800. propagate_binfo_offsets (binfo, alignment);
  3801. }
  3802. }
  3803. if (CLASSTYPE_USER_ALIGN (basetype))
  3804. {
  3805. rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
  3806. if (warn_packed)
  3807. rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
  3808. TYPE_USER_ALIGN (rli->t) = 1;
  3809. }
  3810. return atend;
  3811. }
  3812. /* Layout the base given by BINFO in the class indicated by RLI.
  3813. *BASE_ALIGN is a running maximum of the alignments of
  3814. any base class. OFFSETS gives the location of empty base
  3815. subobjects. T is the most derived type. Return nonzero if the new
  3816. object cannot be nearly-empty. A new FIELD_DECL is inserted at
  3817. *NEXT_FIELD, unless BINFO is for an empty base class.
  3818. Returns the location at which the next field should be inserted. */
  3819. static tree *
  3820. build_base_field (record_layout_info rli, tree binfo,
  3821. splay_tree offsets, tree *next_field)
  3822. {
  3823. tree t = rli->t;
  3824. tree basetype = BINFO_TYPE (binfo);
  3825. if (!COMPLETE_TYPE_P (basetype))
  3826. /* This error is now reported in xref_tag, thus giving better
  3827. location information. */
  3828. return next_field;
  3829. /* Place the base class. */
  3830. if (!is_empty_class (basetype))
  3831. {
  3832. tree decl;
  3833. /* The containing class is non-empty because it has a non-empty
  3834. base class. */
  3835. CLASSTYPE_EMPTY_P (t) = 0;
  3836. /* Create the FIELD_DECL. */
  3837. decl = build_decl (input_location,
  3838. FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
  3839. DECL_ARTIFICIAL (decl) = 1;
  3840. DECL_IGNORED_P (decl) = 1;
  3841. DECL_FIELD_CONTEXT (decl) = t;
  3842. if (CLASSTYPE_AS_BASE (basetype))
  3843. {
  3844. DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
  3845. DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
  3846. DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
  3847. DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
  3848. DECL_MODE (decl) = TYPE_MODE (basetype);
  3849. DECL_FIELD_IS_BASE (decl) = 1;
  3850. /* Try to place the field. It may take more than one try if we
  3851. have a hard time placing the field without putting two
  3852. objects of the same type at the same address. */
  3853. layout_nonempty_base_or_field (rli, decl, binfo, offsets);
  3854. /* Add the new FIELD_DECL to the list of fields for T. */
  3855. DECL_CHAIN (decl) = *next_field;
  3856. *next_field = decl;
  3857. next_field = &DECL_CHAIN (decl);
  3858. }
  3859. }
  3860. else
  3861. {
  3862. tree eoc;
  3863. bool atend;
  3864. /* On some platforms (ARM), even empty classes will not be
  3865. byte-aligned. */
  3866. eoc = round_up_loc (input_location,
  3867. rli_size_unit_so_far (rli),
  3868. CLASSTYPE_ALIGN_UNIT (basetype));
  3869. atend = layout_empty_base (rli, binfo, eoc, offsets);
  3870. /* A nearly-empty class "has no proper base class that is empty,
  3871. not morally virtual, and at an offset other than zero." */
  3872. if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
  3873. {
  3874. if (atend)
  3875. CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
  3876. /* The check above (used in G++ 3.2) is insufficient because
  3877. an empty class placed at offset zero might itself have an
  3878. empty base at a nonzero offset. */
  3879. else if (walk_subobject_offsets (basetype,
  3880. empty_base_at_nonzero_offset_p,
  3881. size_zero_node,
  3882. /*offsets=*/NULL,
  3883. /*max_offset=*/NULL_TREE,
  3884. /*vbases_p=*/true))
  3885. CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
  3886. }
  3887. /* We do not create a FIELD_DECL for empty base classes because
  3888. it might overlap some other field. We want to be able to
  3889. create CONSTRUCTORs for the class by iterating over the
  3890. FIELD_DECLs, and the back end does not handle overlapping
  3891. FIELD_DECLs. */
  3892. /* An empty virtual base causes a class to be non-empty
  3893. -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
  3894. here because that was already done when the virtual table
  3895. pointer was created. */
  3896. }
  3897. /* Record the offsets of BINFO and its base subobjects. */
  3898. record_subobject_offsets (binfo,
  3899. BINFO_OFFSET (binfo),
  3900. offsets,
  3901. /*is_data_member=*/false);
  3902. return next_field;
  3903. }
  3904. /* Layout all of the non-virtual base classes. Record empty
  3905. subobjects in OFFSETS. T is the most derived type. Return nonzero
  3906. if the type cannot be nearly empty. The fields created
  3907. corresponding to the base classes will be inserted at
  3908. *NEXT_FIELD. */
  3909. static void
  3910. build_base_fields (record_layout_info rli,
  3911. splay_tree offsets, tree *next_field)
  3912. {
  3913. /* Chain to hold all the new FIELD_DECLs which stand in for base class
  3914. subobjects. */
  3915. tree t = rli->t;
  3916. int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
  3917. int i;
  3918. /* The primary base class is always allocated first. */
  3919. if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
  3920. next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
  3921. offsets, next_field);
  3922. /* Now allocate the rest of the bases. */
  3923. for (i = 0; i < n_baseclasses; ++i)
  3924. {
  3925. tree base_binfo;
  3926. base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
  3927. /* The primary base was already allocated above, so we don't
  3928. need to allocate it again here. */
  3929. if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
  3930. continue;
  3931. /* Virtual bases are added at the end (a primary virtual base
  3932. will have already been added). */
  3933. if (BINFO_VIRTUAL_P (base_binfo))
  3934. continue;
  3935. next_field = build_base_field (rli, base_binfo,
  3936. offsets, next_field);
  3937. }
  3938. }
  3939. /* Go through the TYPE_METHODS of T issuing any appropriate
  3940. diagnostics, figuring out which methods override which other
  3941. methods, and so forth. */
  3942. static void
  3943. check_methods (tree t)
  3944. {
  3945. tree x;
  3946. for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
  3947. {
  3948. check_for_override (x, t);
  3949. if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
  3950. error ("initializer specified for non-virtual method %q+D", x);
  3951. /* The name of the field is the original field name
  3952. Save this in auxiliary field for later overloading. */
  3953. if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
  3954. {
  3955. TYPE_POLYMORPHIC_P (t) = 1;
  3956. if (DECL_PURE_VIRTUAL_P (x))
  3957. vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
  3958. }
  3959. /* All user-provided destructors are non-trivial.
  3960. Constructors and assignment ops are handled in
  3961. grok_special_member_properties. */
  3962. if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
  3963. TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
  3964. }
  3965. }
  3966. /* FN is a constructor or destructor. Clone the declaration to create
  3967. a specialized in-charge or not-in-charge version, as indicated by
  3968. NAME. */
  3969. static tree
  3970. build_clone (tree fn, tree name)
  3971. {
  3972. tree parms;
  3973. tree clone;
  3974. /* Copy the function. */
  3975. clone = copy_decl (fn);
  3976. /* Reset the function name. */
  3977. DECL_NAME (clone) = name;
  3978. /* Remember where this function came from. */
  3979. DECL_ABSTRACT_ORIGIN (clone) = fn;
  3980. /* Make it easy to find the CLONE given the FN. */
  3981. DECL_CHAIN (clone) = DECL_CHAIN (fn);
  3982. DECL_CHAIN (fn) = clone;
  3983. /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
  3984. if (TREE_CODE (clone) == TEMPLATE_DECL)
  3985. {
  3986. tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
  3987. DECL_TEMPLATE_RESULT (clone) = result;
  3988. DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
  3989. DECL_TI_TEMPLATE (result) = clone;
  3990. TREE_TYPE (clone) = TREE_TYPE (result);
  3991. return clone;
  3992. }
  3993. SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
  3994. DECL_CLONED_FUNCTION (clone) = fn;
  3995. /* There's no pending inline data for this function. */
  3996. DECL_PENDING_INLINE_INFO (clone) = NULL;
  3997. DECL_PENDING_INLINE_P (clone) = 0;
  3998. /* The base-class destructor is not virtual. */
  3999. if (name == base_dtor_identifier)
  4000. {
  4001. DECL_VIRTUAL_P (clone) = 0;
  4002. if (TREE_CODE (clone) != TEMPLATE_DECL)
  4003. DECL_VINDEX (clone) = NULL_TREE;
  4004. }
  4005. /* If there was an in-charge parameter, drop it from the function
  4006. type. */
  4007. if (DECL_HAS_IN_CHARGE_PARM_P (clone))
  4008. {
  4009. tree basetype;
  4010. tree parmtypes;
  4011. tree exceptions;
  4012. exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
  4013. basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
  4014. parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
  4015. /* Skip the `this' parameter. */
  4016. parmtypes = TREE_CHAIN (parmtypes);
  4017. /* Skip the in-charge parameter. */
  4018. parmtypes = TREE_CHAIN (parmtypes);
  4019. /* And the VTT parm, in a complete [cd]tor. */
  4020. if (DECL_HAS_VTT_PARM_P (fn)
  4021. && ! DECL_NEEDS_VTT_PARM_P (clone))
  4022. parmtypes = TREE_CHAIN (parmtypes);
  4023. /* If this is subobject constructor or destructor, add the vtt
  4024. parameter. */
  4025. TREE_TYPE (clone)
  4026. = build_method_type_directly (basetype,
  4027. TREE_TYPE (TREE_TYPE (clone)),
  4028. parmtypes);
  4029. if (exceptions)
  4030. TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
  4031. exceptions);
  4032. TREE_TYPE (clone)
  4033. = cp_build_type_attribute_variant (TREE_TYPE (clone),
  4034. TYPE_ATTRIBUTES (TREE_TYPE (fn)));
  4035. }
  4036. /* Copy the function parameters. */
  4037. DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
  4038. /* Remove the in-charge parameter. */
  4039. if (DECL_HAS_IN_CHARGE_PARM_P (clone))
  4040. {
  4041. DECL_CHAIN (DECL_ARGUMENTS (clone))
  4042. = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
  4043. DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
  4044. }
  4045. /* And the VTT parm, in a complete [cd]tor. */
  4046. if (DECL_HAS_VTT_PARM_P (fn))
  4047. {
  4048. if (DECL_NEEDS_VTT_PARM_P (clone))
  4049. DECL_HAS_VTT_PARM_P (clone) = 1;
  4050. else
  4051. {
  4052. DECL_CHAIN (DECL_ARGUMENTS (clone))
  4053. = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
  4054. DECL_HAS_VTT_PARM_P (clone) = 0;
  4055. }
  4056. }
  4057. for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
  4058. {
  4059. DECL_CONTEXT (parms) = clone;
  4060. cxx_dup_lang_specific_decl (parms);
  4061. }
  4062. /* Create the RTL for this function. */
  4063. SET_DECL_RTL (clone, NULL);
  4064. rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
  4065. if (pch_file)
  4066. note_decl_for_pch (clone);
  4067. return clone;
  4068. }
  4069. /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
  4070. not invoke this function directly.
  4071. For a non-thunk function, returns the address of the slot for storing
  4072. the function it is a clone of. Otherwise returns NULL_TREE.
  4073. If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
  4074. cloned_function is unset. This is to support the separate
  4075. DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
  4076. on a template makes sense, but not the former. */
  4077. tree *
  4078. decl_cloned_function_p (const_tree decl, bool just_testing)
  4079. {
  4080. tree *ptr;
  4081. if (just_testing)
  4082. decl = STRIP_TEMPLATE (decl);
  4083. if (TREE_CODE (decl) != FUNCTION_DECL
  4084. || !DECL_LANG_SPECIFIC (decl)
  4085. || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
  4086. {
  4087. #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
  4088. if (!just_testing)
  4089. lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
  4090. else
  4091. #endif
  4092. return NULL;
  4093. }
  4094. ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
  4095. if (just_testing && *ptr == NULL_TREE)
  4096. return NULL;
  4097. else
  4098. return ptr;
  4099. }
  4100. /* Produce declarations for all appropriate clones of FN. If
  4101. UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
  4102. CLASTYPE_METHOD_VEC as well. */
  4103. void
  4104. clone_function_decl (tree fn, int update_method_vec_p)
  4105. {
  4106. tree clone;
  4107. /* Avoid inappropriate cloning. */
  4108. if (DECL_CHAIN (fn)
  4109. && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
  4110. return;
  4111. if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
  4112. {
  4113. /* For each constructor, we need two variants: an in-charge version
  4114. and a not-in-charge version. */
  4115. clone = build_clone (fn, complete_ctor_identifier);
  4116. if (update_method_vec_p)
  4117. add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
  4118. clone = build_clone (fn, base_ctor_identifier);
  4119. if (update_method_vec_p)
  4120. add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
  4121. }
  4122. else
  4123. {
  4124. gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
  4125. /* For each destructor, we need three variants: an in-charge
  4126. version, a not-in-charge version, and an in-charge deleting
  4127. version. We clone the deleting version first because that
  4128. means it will go second on the TYPE_METHODS list -- and that
  4129. corresponds to the correct layout order in the virtual
  4130. function table.
  4131. For a non-virtual destructor, we do not build a deleting
  4132. destructor. */
  4133. if (DECL_VIRTUAL_P (fn))
  4134. {
  4135. clone = build_clone (fn, deleting_dtor_identifier);
  4136. if (update_method_vec_p)
  4137. add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
  4138. }
  4139. clone = build_clone (fn, complete_dtor_identifier);
  4140. if (update_method_vec_p)
  4141. add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
  4142. clone = build_clone (fn, base_dtor_identifier);
  4143. if (update_method_vec_p)
  4144. add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
  4145. }
  4146. /* Note that this is an abstract function that is never emitted. */
  4147. DECL_ABSTRACT_P (fn) = true;
  4148. }
  4149. /* DECL is an in charge constructor, which is being defined. This will
  4150. have had an in class declaration, from whence clones were
  4151. declared. An out-of-class definition can specify additional default
  4152. arguments. As it is the clones that are involved in overload
  4153. resolution, we must propagate the information from the DECL to its
  4154. clones. */
  4155. void
  4156. adjust_clone_args (tree decl)
  4157. {
  4158. tree clone;
  4159. for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
  4160. clone = DECL_CHAIN (clone))
  4161. {
  4162. tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
  4163. tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
  4164. tree decl_parms, clone_parms;
  4165. clone_parms = orig_clone_parms;
  4166. /* Skip the 'this' parameter. */
  4167. orig_clone_parms = TREE_CHAIN (orig_clone_parms);
  4168. orig_decl_parms = TREE_CHAIN (orig_decl_parms);
  4169. if (DECL_HAS_IN_CHARGE_PARM_P (decl))
  4170. orig_decl_parms = TREE_CHAIN (orig_decl_parms);
  4171. if (DECL_HAS_VTT_PARM_P (decl))
  4172. orig_decl_parms = TREE_CHAIN (orig_decl_parms);
  4173. clone_parms = orig_clone_parms;
  4174. if (DECL_HAS_VTT_PARM_P (clone))
  4175. clone_parms = TREE_CHAIN (clone_parms);
  4176. for (decl_parms = orig_decl_parms; decl_parms;
  4177. decl_parms = TREE_CHAIN (decl_parms),
  4178. clone_parms = TREE_CHAIN (clone_parms))
  4179. {
  4180. gcc_assert (same_type_p (TREE_TYPE (decl_parms),
  4181. TREE_TYPE (clone_parms)));
  4182. if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
  4183. {
  4184. /* A default parameter has been added. Adjust the
  4185. clone's parameters. */
  4186. tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
  4187. tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
  4188. tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
  4189. tree type;
  4190. clone_parms = orig_decl_parms;
  4191. if (DECL_HAS_VTT_PARM_P (clone))
  4192. {
  4193. clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
  4194. TREE_VALUE (orig_clone_parms),
  4195. clone_parms);
  4196. TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
  4197. }
  4198. type = build_method_type_directly (basetype,
  4199. TREE_TYPE (TREE_TYPE (clone)),
  4200. clone_parms);
  4201. if (exceptions)
  4202. type = build_exception_variant (type, exceptions);
  4203. if (attrs)
  4204. type = cp_build_type_attribute_variant (type, attrs);
  4205. TREE_TYPE (clone) = type;
  4206. clone_parms = NULL_TREE;
  4207. break;
  4208. }
  4209. }
  4210. gcc_assert (!clone_parms);
  4211. }
  4212. }
  4213. /* For each of the constructors and destructors in T, create an
  4214. in-charge and not-in-charge variant. */
  4215. static void
  4216. clone_constructors_and_destructors (tree t)
  4217. {
  4218. tree fns;
  4219. /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
  4220. out now. */
  4221. if (!CLASSTYPE_METHOD_VEC (t))
  4222. return;
  4223. for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4224. clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
  4225. for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4226. clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
  4227. }
  4228. /* Deduce noexcept for a destructor DTOR. */
  4229. void
  4230. deduce_noexcept_on_destructor (tree dtor)
  4231. {
  4232. if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
  4233. {
  4234. tree eh_spec = unevaluated_noexcept_spec ();
  4235. TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
  4236. }
  4237. }
  4238. /* For each destructor in T, deduce noexcept:
  4239. 12.4/3: A declaration of a destructor that does not have an
  4240. exception-specification is implicitly considered to have the
  4241. same exception-specification as an implicit declaration (15.4). */
  4242. static void
  4243. deduce_noexcept_on_destructors (tree t)
  4244. {
  4245. /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
  4246. out now. */
  4247. if (!CLASSTYPE_METHOD_VEC (t))
  4248. return;
  4249. for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4250. deduce_noexcept_on_destructor (OVL_CURRENT (fns));
  4251. }
  4252. /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
  4253. of TYPE for virtual functions which FNDECL overrides. Return a
  4254. mask of the tm attributes found therein. */
  4255. static int
  4256. look_for_tm_attr_overrides (tree type, tree fndecl)
  4257. {
  4258. tree binfo = TYPE_BINFO (type);
  4259. tree base_binfo;
  4260. int ix, found = 0;
  4261. for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
  4262. {
  4263. tree o, basetype = BINFO_TYPE (base_binfo);
  4264. if (!TYPE_POLYMORPHIC_P (basetype))
  4265. continue;
  4266. o = look_for_overrides_here (basetype, fndecl);
  4267. if (o)
  4268. found |= tm_attr_to_mask (find_tm_attribute
  4269. (TYPE_ATTRIBUTES (TREE_TYPE (o))));
  4270. else
  4271. found |= look_for_tm_attr_overrides (basetype, fndecl);
  4272. }
  4273. return found;
  4274. }
  4275. /* Subroutine of set_method_tm_attributes. Handle the checks and
  4276. inheritance for one virtual method FNDECL. */
  4277. static void
  4278. set_one_vmethod_tm_attributes (tree type, tree fndecl)
  4279. {
  4280. tree tm_attr;
  4281. int found, have;
  4282. found = look_for_tm_attr_overrides (type, fndecl);
  4283. /* If FNDECL doesn't actually override anything (i.e. T is the
  4284. class that first declares FNDECL virtual), then we're done. */
  4285. if (found == 0)
  4286. return;
  4287. tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
  4288. have = tm_attr_to_mask (tm_attr);
  4289. /* Intel STM Language Extension 3.0, Section 4.2 table 4:
  4290. tm_pure must match exactly, otherwise no weakening of
  4291. tm_safe > tm_callable > nothing. */
  4292. /* ??? The tm_pure attribute didn't make the transition to the
  4293. multivendor language spec. */
  4294. if (have == TM_ATTR_PURE)
  4295. {
  4296. if (found != TM_ATTR_PURE)
  4297. {
  4298. found &= -found;
  4299. goto err_override;
  4300. }
  4301. }
  4302. /* If the overridden function is tm_pure, then FNDECL must be. */
  4303. else if (found == TM_ATTR_PURE && tm_attr)
  4304. goto err_override;
  4305. /* Look for base class combinations that cannot be satisfied. */
  4306. else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
  4307. {
  4308. found &= ~TM_ATTR_PURE;
  4309. found &= -found;
  4310. error_at (DECL_SOURCE_LOCATION (fndecl),
  4311. "method overrides both %<transaction_pure%> and %qE methods",
  4312. tm_mask_to_attr (found));
  4313. }
  4314. /* If FNDECL did not declare an attribute, then inherit the most
  4315. restrictive one. */
  4316. else if (tm_attr == NULL)
  4317. {
  4318. apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
  4319. }
  4320. /* Otherwise validate that we're not weaker than a function
  4321. that is being overridden. */
  4322. else
  4323. {
  4324. found &= -found;
  4325. if (found <= TM_ATTR_CALLABLE && have > found)
  4326. goto err_override;
  4327. }
  4328. return;
  4329. err_override:
  4330. error_at (DECL_SOURCE_LOCATION (fndecl),
  4331. "method declared %qE overriding %qE method",
  4332. tm_attr, tm_mask_to_attr (found));
  4333. }
  4334. /* For each of the methods in T, propagate a class-level tm attribute. */
  4335. static void
  4336. set_method_tm_attributes (tree t)
  4337. {
  4338. tree class_tm_attr, fndecl;
  4339. /* Don't bother collecting tm attributes if transactional memory
  4340. support is not enabled. */
  4341. if (!flag_tm)
  4342. return;
  4343. /* Process virtual methods first, as they inherit directly from the
  4344. base virtual function and also require validation of new attributes. */
  4345. if (TYPE_CONTAINS_VPTR_P (t))
  4346. {
  4347. tree vchain;
  4348. for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
  4349. vchain = TREE_CHAIN (vchain))
  4350. {
  4351. fndecl = BV_FN (vchain);
  4352. if (DECL_THUNK_P (fndecl))
  4353. fndecl = THUNK_TARGET (fndecl);
  4354. set_one_vmethod_tm_attributes (t, fndecl);
  4355. }
  4356. }
  4357. /* If the class doesn't have an attribute, nothing more to do. */
  4358. class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
  4359. if (class_tm_attr == NULL)
  4360. return;
  4361. /* Any method that does not yet have a tm attribute inherits
  4362. the one from the class. */
  4363. for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
  4364. {
  4365. if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
  4366. apply_tm_attr (fndecl, class_tm_attr);
  4367. }
  4368. }
  4369. /* Returns true iff class T has a user-defined constructor other than
  4370. the default constructor. */
  4371. bool
  4372. type_has_user_nondefault_constructor (tree t)
  4373. {
  4374. tree fns;
  4375. if (!TYPE_HAS_USER_CONSTRUCTOR (t))
  4376. return false;
  4377. for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4378. {
  4379. tree fn = OVL_CURRENT (fns);
  4380. if (!DECL_ARTIFICIAL (fn)
  4381. && (TREE_CODE (fn) == TEMPLATE_DECL
  4382. || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
  4383. != NULL_TREE)))
  4384. return true;
  4385. }
  4386. return false;
  4387. }
  4388. /* Returns the defaulted constructor if T has one. Otherwise, returns
  4389. NULL_TREE. */
  4390. tree
  4391. in_class_defaulted_default_constructor (tree t)
  4392. {
  4393. tree fns, args;
  4394. if (!TYPE_HAS_USER_CONSTRUCTOR (t))
  4395. return NULL_TREE;
  4396. for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4397. {
  4398. tree fn = OVL_CURRENT (fns);
  4399. if (DECL_DEFAULTED_IN_CLASS_P (fn))
  4400. {
  4401. args = FUNCTION_FIRST_USER_PARMTYPE (fn);
  4402. while (args && TREE_PURPOSE (args))
  4403. args = TREE_CHAIN (args);
  4404. if (!args || args == void_list_node)
  4405. return fn;
  4406. }
  4407. }
  4408. return NULL_TREE;
  4409. }
  4410. /* Returns true iff FN is a user-provided function, i.e. user-declared
  4411. and not defaulted at its first declaration; or explicit, private,
  4412. protected, or non-const. */
  4413. bool
  4414. user_provided_p (tree fn)
  4415. {
  4416. if (TREE_CODE (fn) == TEMPLATE_DECL)
  4417. return true;
  4418. else
  4419. return (!DECL_ARTIFICIAL (fn)
  4420. && !(DECL_INITIALIZED_IN_CLASS_P (fn)
  4421. && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
  4422. }
  4423. /* Returns true iff class T has a user-provided constructor. */
  4424. bool
  4425. type_has_user_provided_constructor (tree t)
  4426. {
  4427. tree fns;
  4428. if (!CLASS_TYPE_P (t))
  4429. return false;
  4430. if (!TYPE_HAS_USER_CONSTRUCTOR (t))
  4431. return false;
  4432. /* This can happen in error cases; avoid crashing. */
  4433. if (!CLASSTYPE_METHOD_VEC (t))
  4434. return false;
  4435. for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4436. if (user_provided_p (OVL_CURRENT (fns)))
  4437. return true;
  4438. return false;
  4439. }
  4440. /* Returns true iff class T has a non-user-provided (i.e. implicitly
  4441. declared or explicitly defaulted in the class body) default
  4442. constructor. */
  4443. bool
  4444. type_has_non_user_provided_default_constructor (tree t)
  4445. {
  4446. tree fns;
  4447. if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
  4448. return false;
  4449. if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
  4450. return true;
  4451. for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4452. {
  4453. tree fn = OVL_CURRENT (fns);
  4454. if (TREE_CODE (fn) == FUNCTION_DECL
  4455. && !user_provided_p (fn)
  4456. && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
  4457. return true;
  4458. }
  4459. return false;
  4460. }
  4461. /* TYPE is being used as a virtual base, and has a non-trivial move
  4462. assignment. Return true if this is due to there being a user-provided
  4463. move assignment in TYPE or one of its subobjects; if there isn't, then
  4464. multiple move assignment can't cause any harm. */
  4465. bool
  4466. vbase_has_user_provided_move_assign (tree type)
  4467. {
  4468. /* Does the type itself have a user-provided move assignment operator? */
  4469. for (tree fns
  4470. = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
  4471. fns; fns = OVL_NEXT (fns))
  4472. {
  4473. tree fn = OVL_CURRENT (fns);
  4474. if (move_fn_p (fn) && user_provided_p (fn))
  4475. return true;
  4476. }
  4477. /* Do any of its bases? */
  4478. tree binfo = TYPE_BINFO (type);
  4479. tree base_binfo;
  4480. for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
  4481. if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
  4482. return true;
  4483. /* Or non-static data members? */
  4484. for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  4485. {
  4486. if (TREE_CODE (field) == FIELD_DECL
  4487. && CLASS_TYPE_P (TREE_TYPE (field))
  4488. && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
  4489. return true;
  4490. }
  4491. /* Seems not. */
  4492. return false;
  4493. }
  4494. /* If default-initialization leaves part of TYPE uninitialized, returns
  4495. a DECL for the field or TYPE itself (DR 253). */
  4496. tree
  4497. default_init_uninitialized_part (tree type)
  4498. {
  4499. tree t, r, binfo;
  4500. int i;
  4501. type = strip_array_types (type);
  4502. if (!CLASS_TYPE_P (type))
  4503. return type;
  4504. if (!type_has_non_user_provided_default_constructor (type))
  4505. return NULL_TREE;
  4506. for (binfo = TYPE_BINFO (type), i = 0;
  4507. BINFO_BASE_ITERATE (binfo, i, t); ++i)
  4508. {
  4509. r = default_init_uninitialized_part (BINFO_TYPE (t));
  4510. if (r)
  4511. return r;
  4512. }
  4513. for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
  4514. if (TREE_CODE (t) == FIELD_DECL
  4515. && !DECL_ARTIFICIAL (t)
  4516. && !DECL_INITIAL (t))
  4517. {
  4518. r = default_init_uninitialized_part (TREE_TYPE (t));
  4519. if (r)
  4520. return DECL_P (r) ? r : t;
  4521. }
  4522. return NULL_TREE;
  4523. }
  4524. /* Returns true iff for class T, a trivial synthesized default constructor
  4525. would be constexpr. */
  4526. bool
  4527. trivial_default_constructor_is_constexpr (tree t)
  4528. {
  4529. /* A defaulted trivial default constructor is constexpr
  4530. if there is nothing to initialize. */
  4531. gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
  4532. return is_really_empty_class (t);
  4533. }
  4534. /* Returns true iff class T has a constexpr default constructor. */
  4535. bool
  4536. type_has_constexpr_default_constructor (tree t)
  4537. {
  4538. tree fns;
  4539. if (!CLASS_TYPE_P (t))
  4540. {
  4541. /* The caller should have stripped an enclosing array. */
  4542. gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
  4543. return false;
  4544. }
  4545. if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
  4546. {
  4547. if (!TYPE_HAS_COMPLEX_DFLT (t))
  4548. return trivial_default_constructor_is_constexpr (t);
  4549. /* Non-trivial, we need to check subobject constructors. */
  4550. lazily_declare_fn (sfk_constructor, t);
  4551. }
  4552. fns = locate_ctor (t);
  4553. return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
  4554. }
  4555. /* Returns true iff class TYPE has a virtual destructor. */
  4556. bool
  4557. type_has_virtual_destructor (tree type)
  4558. {
  4559. tree dtor;
  4560. if (!CLASS_TYPE_P (type))
  4561. return false;
  4562. gcc_assert (COMPLETE_TYPE_P (type));
  4563. dtor = CLASSTYPE_DESTRUCTORS (type);
  4564. return (dtor && DECL_VIRTUAL_P (dtor));
  4565. }
  4566. /* Returns true iff class T has a move constructor. */
  4567. bool
  4568. type_has_move_constructor (tree t)
  4569. {
  4570. tree fns;
  4571. if (CLASSTYPE_LAZY_MOVE_CTOR (t))
  4572. {
  4573. gcc_assert (COMPLETE_TYPE_P (t));
  4574. lazily_declare_fn (sfk_move_constructor, t);
  4575. }
  4576. if (!CLASSTYPE_METHOD_VEC (t))
  4577. return false;
  4578. for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4579. if (move_fn_p (OVL_CURRENT (fns)))
  4580. return true;
  4581. return false;
  4582. }
  4583. /* Returns true iff class T has a move assignment operator. */
  4584. bool
  4585. type_has_move_assign (tree t)
  4586. {
  4587. tree fns;
  4588. if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
  4589. {
  4590. gcc_assert (COMPLETE_TYPE_P (t));
  4591. lazily_declare_fn (sfk_move_assignment, t);
  4592. }
  4593. for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
  4594. fns; fns = OVL_NEXT (fns))
  4595. if (move_fn_p (OVL_CURRENT (fns)))
  4596. return true;
  4597. return false;
  4598. }
  4599. /* Returns true iff class T has a move constructor that was explicitly
  4600. declared in the class body. Note that this is different from
  4601. "user-provided", which doesn't include functions that are defaulted in
  4602. the class. */
  4603. bool
  4604. type_has_user_declared_move_constructor (tree t)
  4605. {
  4606. tree fns;
  4607. if (CLASSTYPE_LAZY_MOVE_CTOR (t))
  4608. return false;
  4609. if (!CLASSTYPE_METHOD_VEC (t))
  4610. return false;
  4611. for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4612. {
  4613. tree fn = OVL_CURRENT (fns);
  4614. if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
  4615. return true;
  4616. }
  4617. return false;
  4618. }
  4619. /* Returns true iff class T has a move assignment operator that was
  4620. explicitly declared in the class body. */
  4621. bool
  4622. type_has_user_declared_move_assign (tree t)
  4623. {
  4624. tree fns;
  4625. if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
  4626. return false;
  4627. for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
  4628. fns; fns = OVL_NEXT (fns))
  4629. {
  4630. tree fn = OVL_CURRENT (fns);
  4631. if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
  4632. return true;
  4633. }
  4634. return false;
  4635. }
  4636. /* Nonzero if we need to build up a constructor call when initializing an
  4637. object of this class, either because it has a user-declared constructor
  4638. or because it doesn't have a default constructor (so we need to give an
  4639. error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
  4640. what you care about is whether or not an object can be produced by a
  4641. constructor (e.g. so we don't set TREE_READONLY on const variables of
  4642. such type); use this function when what you care about is whether or not
  4643. to try to call a constructor to create an object. The latter case is
  4644. the former plus some cases of constructors that cannot be called. */
  4645. bool
  4646. type_build_ctor_call (tree t)
  4647. {
  4648. tree inner;
  4649. if (TYPE_NEEDS_CONSTRUCTING (t))
  4650. return true;
  4651. inner = strip_array_types (t);
  4652. if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
  4653. return false;
  4654. if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
  4655. return true;
  4656. if (cxx_dialect < cxx11)
  4657. return false;
  4658. /* A user-declared constructor might be private, and a constructor might
  4659. be trivial but deleted. */
  4660. for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
  4661. fns; fns = OVL_NEXT (fns))
  4662. {
  4663. tree fn = OVL_CURRENT (fns);
  4664. if (!DECL_ARTIFICIAL (fn)
  4665. || DECL_DELETED_FN (fn))
  4666. return true;
  4667. }
  4668. return false;
  4669. }
  4670. /* Like type_build_ctor_call, but for destructors. */
  4671. bool
  4672. type_build_dtor_call (tree t)
  4673. {
  4674. tree inner;
  4675. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
  4676. return true;
  4677. inner = strip_array_types (t);
  4678. if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
  4679. || !COMPLETE_TYPE_P (inner))
  4680. return false;
  4681. if (cxx_dialect < cxx11)
  4682. return false;
  4683. /* A user-declared destructor might be private, and a destructor might
  4684. be trivial but deleted. */
  4685. for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
  4686. fns; fns = OVL_NEXT (fns))
  4687. {
  4688. tree fn = OVL_CURRENT (fns);
  4689. if (!DECL_ARTIFICIAL (fn)
  4690. || DECL_DELETED_FN (fn))
  4691. return true;
  4692. }
  4693. return false;
  4694. }
  4695. /* Remove all zero-width bit-fields from T. */
  4696. static void
  4697. remove_zero_width_bit_fields (tree t)
  4698. {
  4699. tree *fieldsp;
  4700. fieldsp = &TYPE_FIELDS (t);
  4701. while (*fieldsp)
  4702. {
  4703. if (TREE_CODE (*fieldsp) == FIELD_DECL
  4704. && DECL_C_BIT_FIELD (*fieldsp)
  4705. /* We should not be confused by the fact that grokbitfield
  4706. temporarily sets the width of the bit field into
  4707. DECL_INITIAL (*fieldsp).
  4708. check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
  4709. to that width. */
  4710. && (DECL_SIZE (*fieldsp) == NULL_TREE
  4711. || integer_zerop (DECL_SIZE (*fieldsp))))
  4712. *fieldsp = DECL_CHAIN (*fieldsp);
  4713. else
  4714. fieldsp = &DECL_CHAIN (*fieldsp);
  4715. }
  4716. }
  4717. /* Returns TRUE iff we need a cookie when dynamically allocating an
  4718. array whose elements have the indicated class TYPE. */
  4719. static bool
  4720. type_requires_array_cookie (tree type)
  4721. {
  4722. tree fns;
  4723. bool has_two_argument_delete_p = false;
  4724. gcc_assert (CLASS_TYPE_P (type));
  4725. /* If there's a non-trivial destructor, we need a cookie. In order
  4726. to iterate through the array calling the destructor for each
  4727. element, we'll have to know how many elements there are. */
  4728. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  4729. return true;
  4730. /* If the usual deallocation function is a two-argument whose second
  4731. argument is of type `size_t', then we have to pass the size of
  4732. the array to the deallocation function, so we will need to store
  4733. a cookie. */
  4734. fns = lookup_fnfields (TYPE_BINFO (type),
  4735. ansi_opname (VEC_DELETE_EXPR),
  4736. /*protect=*/0);
  4737. /* If there are no `operator []' members, or the lookup is
  4738. ambiguous, then we don't need a cookie. */
  4739. if (!fns || fns == error_mark_node)
  4740. return false;
  4741. /* Loop through all of the functions. */
  4742. for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
  4743. {
  4744. tree fn;
  4745. tree second_parm;
  4746. /* Select the current function. */
  4747. fn = OVL_CURRENT (fns);
  4748. /* See if this function is a one-argument delete function. If
  4749. it is, then it will be the usual deallocation function. */
  4750. second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
  4751. if (second_parm == void_list_node)
  4752. return false;
  4753. /* Do not consider this function if its second argument is an
  4754. ellipsis. */
  4755. if (!second_parm)
  4756. continue;
  4757. /* Otherwise, if we have a two-argument function and the second
  4758. argument is `size_t', it will be the usual deallocation
  4759. function -- unless there is one-argument function, too. */
  4760. if (TREE_CHAIN (second_parm) == void_list_node
  4761. && same_type_p (TREE_VALUE (second_parm), size_type_node))
  4762. has_two_argument_delete_p = true;
  4763. }
  4764. return has_two_argument_delete_p;
  4765. }
  4766. /* Finish computing the `literal type' property of class type T.
  4767. At this point, we have already processed base classes and
  4768. non-static data members. We need to check whether the copy
  4769. constructor is trivial, the destructor is trivial, and there
  4770. is a trivial default constructor or at least one constexpr
  4771. constructor other than the copy constructor. */
  4772. static void
  4773. finalize_literal_type_property (tree t)
  4774. {
  4775. tree fn;
  4776. if (cxx_dialect < cxx11
  4777. || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
  4778. CLASSTYPE_LITERAL_P (t) = false;
  4779. else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
  4780. && CLASSTYPE_NON_AGGREGATE (t)
  4781. && !TYPE_HAS_CONSTEXPR_CTOR (t))
  4782. CLASSTYPE_LITERAL_P (t) = false;
  4783. if (!CLASSTYPE_LITERAL_P (t))
  4784. for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
  4785. if (DECL_DECLARED_CONSTEXPR_P (fn)
  4786. && TREE_CODE (fn) != TEMPLATE_DECL
  4787. && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
  4788. && !DECL_CONSTRUCTOR_P (fn))
  4789. {
  4790. DECL_DECLARED_CONSTEXPR_P (fn) = false;
  4791. if (!DECL_GENERATED_P (fn))
  4792. {
  4793. error ("enclosing class of constexpr non-static member "
  4794. "function %q+#D is not a literal type", fn);
  4795. explain_non_literal_class (t);
  4796. }
  4797. }
  4798. }
  4799. /* T is a non-literal type used in a context which requires a constant
  4800. expression. Explain why it isn't literal. */
  4801. void
  4802. explain_non_literal_class (tree t)
  4803. {
  4804. static hash_set<tree> *diagnosed;
  4805. if (!CLASS_TYPE_P (t))
  4806. return;
  4807. t = TYPE_MAIN_VARIANT (t);
  4808. if (diagnosed == NULL)
  4809. diagnosed = new hash_set<tree>;
  4810. if (diagnosed->add (t))
  4811. /* Already explained. */
  4812. return;
  4813. inform (0, "%q+T is not literal because:", t);
  4814. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
  4815. inform (0, " %q+T has a non-trivial destructor", t);
  4816. else if (CLASSTYPE_NON_AGGREGATE (t)
  4817. && !TYPE_HAS_TRIVIAL_DFLT (t)
  4818. && !TYPE_HAS_CONSTEXPR_CTOR (t))
  4819. {
  4820. inform (0, " %q+T is not an aggregate, does not have a trivial "
  4821. "default constructor, and has no constexpr constructor that "
  4822. "is not a copy or move constructor", t);
  4823. if (type_has_non_user_provided_default_constructor (t))
  4824. {
  4825. /* Note that we can't simply call locate_ctor because when the
  4826. constructor is deleted it just returns NULL_TREE. */
  4827. tree fns;
  4828. for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
  4829. {
  4830. tree fn = OVL_CURRENT (fns);
  4831. tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
  4832. parms = skip_artificial_parms_for (fn, parms);
  4833. if (sufficient_parms_p (parms))
  4834. {
  4835. if (DECL_DELETED_FN (fn))
  4836. maybe_explain_implicit_delete (fn);
  4837. else
  4838. explain_invalid_constexpr_fn (fn);
  4839. break;
  4840. }
  4841. }
  4842. }
  4843. }
  4844. else
  4845. {
  4846. tree binfo, base_binfo, field; int i;
  4847. for (binfo = TYPE_BINFO (t), i = 0;
  4848. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  4849. {
  4850. tree basetype = TREE_TYPE (base_binfo);
  4851. if (!CLASSTYPE_LITERAL_P (basetype))
  4852. {
  4853. inform (0, " base class %qT of %q+T is non-literal",
  4854. basetype, t);
  4855. explain_non_literal_class (basetype);
  4856. return;
  4857. }
  4858. }
  4859. for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
  4860. {
  4861. tree ftype;
  4862. if (TREE_CODE (field) != FIELD_DECL)
  4863. continue;
  4864. ftype = TREE_TYPE (field);
  4865. if (!literal_type_p (ftype))
  4866. {
  4867. inform (0, " non-static data member %q+D has "
  4868. "non-literal type", field);
  4869. if (CLASS_TYPE_P (ftype))
  4870. explain_non_literal_class (ftype);
  4871. }
  4872. if (CP_TYPE_VOLATILE_P (ftype))
  4873. inform (0, " non-static data member %q+D has "
  4874. "volatile type", field);
  4875. }
  4876. }
  4877. }
  4878. /* Check the validity of the bases and members declared in T. Add any
  4879. implicitly-generated functions (like copy-constructors and
  4880. assignment operators). Compute various flag bits (like
  4881. CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
  4882. level: i.e., independently of the ABI in use. */
  4883. static void
  4884. check_bases_and_members (tree t)
  4885. {
  4886. /* Nonzero if the implicitly generated copy constructor should take
  4887. a non-const reference argument. */
  4888. int cant_have_const_ctor;
  4889. /* Nonzero if the implicitly generated assignment operator
  4890. should take a non-const reference argument. */
  4891. int no_const_asn_ref;
  4892. tree access_decls;
  4893. bool saved_complex_asn_ref;
  4894. bool saved_nontrivial_dtor;
  4895. tree fn;
  4896. /* By default, we use const reference arguments and generate default
  4897. constructors. */
  4898. cant_have_const_ctor = 0;
  4899. no_const_asn_ref = 0;
  4900. /* Check all the base-classes. */
  4901. check_bases (t, &cant_have_const_ctor,
  4902. &no_const_asn_ref);
  4903. /* Deduce noexcept on destructors. This needs to happen after we've set
  4904. triviality flags appropriately for our bases. */
  4905. if (cxx_dialect >= cxx11)
  4906. deduce_noexcept_on_destructors (t);
  4907. /* Check all the method declarations. */
  4908. check_methods (t);
  4909. /* Save the initial values of these flags which only indicate whether
  4910. or not the class has user-provided functions. As we analyze the
  4911. bases and members we can set these flags for other reasons. */
  4912. saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
  4913. saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
  4914. /* Check all the data member declarations. We cannot call
  4915. check_field_decls until we have called check_bases check_methods,
  4916. as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
  4917. being set appropriately. */
  4918. check_field_decls (t, &access_decls,
  4919. &cant_have_const_ctor,
  4920. &no_const_asn_ref);
  4921. /* A nearly-empty class has to be vptr-containing; a nearly empty
  4922. class contains just a vptr. */
  4923. if (!TYPE_CONTAINS_VPTR_P (t))
  4924. CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
  4925. /* Do some bookkeeping that will guide the generation of implicitly
  4926. declared member functions. */
  4927. TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
  4928. TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
  4929. /* We need to call a constructor for this class if it has a
  4930. user-provided constructor, or if the default constructor is going
  4931. to initialize the vptr. (This is not an if-and-only-if;
  4932. TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
  4933. themselves need constructing.) */
  4934. TYPE_NEEDS_CONSTRUCTING (t)
  4935. |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
  4936. /* [dcl.init.aggr]
  4937. An aggregate is an array or a class with no user-provided
  4938. constructors ... and no virtual functions.
  4939. Again, other conditions for being an aggregate are checked
  4940. elsewhere. */
  4941. CLASSTYPE_NON_AGGREGATE (t)
  4942. |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
  4943. /* This is the C++98/03 definition of POD; it changed in C++0x, but we
  4944. retain the old definition internally for ABI reasons. */
  4945. CLASSTYPE_NON_LAYOUT_POD_P (t)
  4946. |= (CLASSTYPE_NON_AGGREGATE (t)
  4947. || saved_nontrivial_dtor || saved_complex_asn_ref);
  4948. CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
  4949. TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
  4950. TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
  4951. TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
  4952. /* If the only explicitly declared default constructor is user-provided,
  4953. set TYPE_HAS_COMPLEX_DFLT. */
  4954. if (!TYPE_HAS_COMPLEX_DFLT (t)
  4955. && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
  4956. && !type_has_non_user_provided_default_constructor (t))
  4957. TYPE_HAS_COMPLEX_DFLT (t) = true;
  4958. /* Warn if a public base of a polymorphic type has an accessible
  4959. non-virtual destructor. It is only now that we know the class is
  4960. polymorphic. Although a polymorphic base will have a already
  4961. been diagnosed during its definition, we warn on use too. */
  4962. if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
  4963. {
  4964. tree binfo = TYPE_BINFO (t);
  4965. vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
  4966. tree base_binfo;
  4967. unsigned i;
  4968. for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  4969. {
  4970. tree basetype = TREE_TYPE (base_binfo);
  4971. if ((*accesses)[i] == access_public_node
  4972. && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
  4973. && accessible_nvdtor_p (basetype))
  4974. warning (OPT_Wnon_virtual_dtor,
  4975. "base class %q#T has accessible non-virtual destructor",
  4976. basetype);
  4977. }
  4978. }
  4979. /* If the class has no user-declared constructor, but does have
  4980. non-static const or reference data members that can never be
  4981. initialized, issue a warning. */
  4982. if (warn_uninitialized
  4983. /* Classes with user-declared constructors are presumed to
  4984. initialize these members. */
  4985. && !TYPE_HAS_USER_CONSTRUCTOR (t)
  4986. /* Aggregates can be initialized with brace-enclosed
  4987. initializers. */
  4988. && CLASSTYPE_NON_AGGREGATE (t))
  4989. {
  4990. tree field;
  4991. for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
  4992. {
  4993. tree type;
  4994. if (TREE_CODE (field) != FIELD_DECL
  4995. || DECL_INITIAL (field) != NULL_TREE)
  4996. continue;
  4997. type = TREE_TYPE (field);
  4998. if (TREE_CODE (type) == REFERENCE_TYPE)
  4999. warning (OPT_Wuninitialized, "non-static reference %q+#D "
  5000. "in class without a constructor", field);
  5001. else if (CP_TYPE_CONST_P (type)
  5002. && (!CLASS_TYPE_P (type)
  5003. || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
  5004. warning (OPT_Wuninitialized, "non-static const member %q+#D "
  5005. "in class without a constructor", field);
  5006. }
  5007. }
  5008. /* Synthesize any needed methods. */
  5009. add_implicitly_declared_members (t, &access_decls,
  5010. cant_have_const_ctor,
  5011. no_const_asn_ref);
  5012. /* Check defaulted declarations here so we have cant_have_const_ctor
  5013. and don't need to worry about clones. */
  5014. for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
  5015. if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
  5016. {
  5017. int copy = copy_fn_p (fn);
  5018. if (copy > 0)
  5019. {
  5020. bool imp_const_p
  5021. = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
  5022. : !no_const_asn_ref);
  5023. bool fn_const_p = (copy == 2);
  5024. if (fn_const_p && !imp_const_p)
  5025. /* If the function is defaulted outside the class, we just
  5026. give the synthesis error. */
  5027. error ("%q+D declared to take const reference, but implicit "
  5028. "declaration would take non-const", fn);
  5029. }
  5030. defaulted_late_check (fn);
  5031. }
  5032. if (LAMBDA_TYPE_P (t))
  5033. {
  5034. /* "This class type is not an aggregate." */
  5035. CLASSTYPE_NON_AGGREGATE (t) = 1;
  5036. }
  5037. /* Compute the 'literal type' property before we
  5038. do anything with non-static member functions. */
  5039. finalize_literal_type_property (t);
  5040. /* Create the in-charge and not-in-charge variants of constructors
  5041. and destructors. */
  5042. clone_constructors_and_destructors (t);
  5043. /* Process the using-declarations. */
  5044. for (; access_decls; access_decls = TREE_CHAIN (access_decls))
  5045. handle_using_decl (TREE_VALUE (access_decls), t);
  5046. /* Build and sort the CLASSTYPE_METHOD_VEC. */
  5047. finish_struct_methods (t);
  5048. /* Figure out whether or not we will need a cookie when dynamically
  5049. allocating an array of this type. */
  5050. TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
  5051. = type_requires_array_cookie (t);
  5052. }
  5053. /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
  5054. accordingly. If a new vfield was created (because T doesn't have a
  5055. primary base class), then the newly created field is returned. It
  5056. is not added to the TYPE_FIELDS list; it is the caller's
  5057. responsibility to do that. Accumulate declared virtual functions
  5058. on VIRTUALS_P. */
  5059. static tree
  5060. create_vtable_ptr (tree t, tree* virtuals_p)
  5061. {
  5062. tree fn;
  5063. /* Collect the virtual functions declared in T. */
  5064. for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
  5065. if (TREE_CODE (fn) == FUNCTION_DECL
  5066. && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
  5067. && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
  5068. {
  5069. tree new_virtual = make_node (TREE_LIST);
  5070. BV_FN (new_virtual) = fn;
  5071. BV_DELTA (new_virtual) = integer_zero_node;
  5072. BV_VCALL_INDEX (new_virtual) = NULL_TREE;
  5073. TREE_CHAIN (new_virtual) = *virtuals_p;
  5074. *virtuals_p = new_virtual;
  5075. }
  5076. /* If we couldn't find an appropriate base class, create a new field
  5077. here. Even if there weren't any new virtual functions, we might need a
  5078. new virtual function table if we're supposed to include vptrs in
  5079. all classes that need them. */
  5080. if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
  5081. {
  5082. /* We build this decl with vtbl_ptr_type_node, which is a
  5083. `vtable_entry_type*'. It might seem more precise to use
  5084. `vtable_entry_type (*)[N]' where N is the number of virtual
  5085. functions. However, that would require the vtable pointer in
  5086. base classes to have a different type than the vtable pointer
  5087. in derived classes. We could make that happen, but that
  5088. still wouldn't solve all the problems. In particular, the
  5089. type-based alias analysis code would decide that assignments
  5090. to the base class vtable pointer can't alias assignments to
  5091. the derived class vtable pointer, since they have different
  5092. types. Thus, in a derived class destructor, where the base
  5093. class constructor was inlined, we could generate bad code for
  5094. setting up the vtable pointer.
  5095. Therefore, we use one type for all vtable pointers. We still
  5096. use a type-correct type; it's just doesn't indicate the array
  5097. bounds. That's better than using `void*' or some such; it's
  5098. cleaner, and it let's the alias analysis code know that these
  5099. stores cannot alias stores to void*! */
  5100. tree field;
  5101. field = build_decl (input_location,
  5102. FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
  5103. DECL_VIRTUAL_P (field) = 1;
  5104. DECL_ARTIFICIAL (field) = 1;
  5105. DECL_FIELD_CONTEXT (field) = t;
  5106. DECL_FCONTEXT (field) = t;
  5107. if (TYPE_PACKED (t))
  5108. DECL_PACKED (field) = 1;
  5109. TYPE_VFIELD (t) = field;
  5110. /* This class is non-empty. */
  5111. CLASSTYPE_EMPTY_P (t) = 0;
  5112. return field;
  5113. }
  5114. return NULL_TREE;
  5115. }
  5116. /* Add OFFSET to all base types of BINFO which is a base in the
  5117. hierarchy dominated by T.
  5118. OFFSET, which is a type offset, is number of bytes. */
  5119. static void
  5120. propagate_binfo_offsets (tree binfo, tree offset)
  5121. {
  5122. int i;
  5123. tree primary_binfo;
  5124. tree base_binfo;
  5125. /* Update BINFO's offset. */
  5126. BINFO_OFFSET (binfo)
  5127. = convert (sizetype,
  5128. size_binop (PLUS_EXPR,
  5129. convert (ssizetype, BINFO_OFFSET (binfo)),
  5130. offset));
  5131. /* Find the primary base class. */
  5132. primary_binfo = get_primary_binfo (binfo);
  5133. if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
  5134. propagate_binfo_offsets (primary_binfo, offset);
  5135. /* Scan all of the bases, pushing the BINFO_OFFSET adjust
  5136. downwards. */
  5137. for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
  5138. {
  5139. /* Don't do the primary base twice. */
  5140. if (base_binfo == primary_binfo)
  5141. continue;
  5142. if (BINFO_VIRTUAL_P (base_binfo))
  5143. continue;
  5144. propagate_binfo_offsets (base_binfo, offset);
  5145. }
  5146. }
  5147. /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
  5148. TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
  5149. empty subobjects of T. */
  5150. static void
  5151. layout_virtual_bases (record_layout_info rli, splay_tree offsets)
  5152. {
  5153. tree vbase;
  5154. tree t = rli->t;
  5155. tree *next_field;
  5156. if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
  5157. return;
  5158. /* Find the last field. The artificial fields created for virtual
  5159. bases will go after the last extant field to date. */
  5160. next_field = &TYPE_FIELDS (t);
  5161. while (*next_field)
  5162. next_field = &DECL_CHAIN (*next_field);
  5163. /* Go through the virtual bases, allocating space for each virtual
  5164. base that is not already a primary base class. These are
  5165. allocated in inheritance graph order. */
  5166. for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
  5167. {
  5168. if (!BINFO_VIRTUAL_P (vbase))
  5169. continue;
  5170. if (!BINFO_PRIMARY_P (vbase))
  5171. {
  5172. /* This virtual base is not a primary base of any class in the
  5173. hierarchy, so we have to add space for it. */
  5174. next_field = build_base_field (rli, vbase,
  5175. offsets, next_field);
  5176. }
  5177. }
  5178. }
  5179. /* Returns the offset of the byte just past the end of the base class
  5180. BINFO. */
  5181. static tree
  5182. end_of_base (tree binfo)
  5183. {
  5184. tree size;
  5185. if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
  5186. size = TYPE_SIZE_UNIT (char_type_node);
  5187. else if (is_empty_class (BINFO_TYPE (binfo)))
  5188. /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
  5189. allocate some space for it. It cannot have virtual bases, so
  5190. TYPE_SIZE_UNIT is fine. */
  5191. size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
  5192. else
  5193. size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
  5194. return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
  5195. }
  5196. /* Returns the offset of the byte just past the end of the base class
  5197. with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
  5198. only non-virtual bases are included. */
  5199. static tree
  5200. end_of_class (tree t, int include_virtuals_p)
  5201. {
  5202. tree result = size_zero_node;
  5203. vec<tree, va_gc> *vbases;
  5204. tree binfo;
  5205. tree base_binfo;
  5206. tree offset;
  5207. int i;
  5208. for (binfo = TYPE_BINFO (t), i = 0;
  5209. BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
  5210. {
  5211. if (!include_virtuals_p
  5212. && BINFO_VIRTUAL_P (base_binfo)
  5213. && (!BINFO_PRIMARY_P (base_binfo)
  5214. || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
  5215. continue;
  5216. offset = end_of_base (base_binfo);
  5217. if (tree_int_cst_lt (result, offset))
  5218. result = offset;
  5219. }
  5220. if (include_virtuals_p)
  5221. for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
  5222. vec_safe_iterate (vbases, i, &base_binfo); i++)
  5223. {
  5224. offset = end_of_base (base_binfo);
  5225. if (tree_int_cst_lt (result, offset))
  5226. result = offset;
  5227. }
  5228. return result;
  5229. }
  5230. /* Warn about bases of T that are inaccessible because they are
  5231. ambiguous. For example:
  5232. struct S {};
  5233. struct T : public S {};
  5234. struct U : public S, public T {};
  5235. Here, `(S*) new U' is not allowed because there are two `S'
  5236. subobjects of U. */
  5237. static void
  5238. warn_about_ambiguous_bases (tree t)
  5239. {
  5240. int i;
  5241. vec<tree, va_gc> *vbases;
  5242. tree basetype;
  5243. tree binfo;
  5244. tree base_binfo;
  5245. /* If there are no repeated bases, nothing can be ambiguous. */
  5246. if (!CLASSTYPE_REPEATED_BASE_P (t))
  5247. return;
  5248. /* Check direct bases. */
  5249. for (binfo = TYPE_BINFO (t), i = 0;
  5250. BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
  5251. {
  5252. basetype = BINFO_TYPE (base_binfo);
  5253. if (!uniquely_derived_from_p (basetype, t))
  5254. warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
  5255. basetype, t);
  5256. }
  5257. /* Check for ambiguous virtual bases. */
  5258. if (extra_warnings)
  5259. for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
  5260. vec_safe_iterate (vbases, i, &binfo); i++)
  5261. {
  5262. basetype = BINFO_TYPE (binfo);
  5263. if (!uniquely_derived_from_p (basetype, t))
  5264. warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
  5265. "to ambiguity", basetype, t);
  5266. }
  5267. }
  5268. /* Compare two INTEGER_CSTs K1 and K2. */
  5269. static int
  5270. splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
  5271. {
  5272. return tree_int_cst_compare ((tree) k1, (tree) k2);
  5273. }
  5274. /* Increase the size indicated in RLI to account for empty classes
  5275. that are "off the end" of the class. */
  5276. static void
  5277. include_empty_classes (record_layout_info rli)
  5278. {
  5279. tree eoc;
  5280. tree rli_size;
  5281. /* It might be the case that we grew the class to allocate a
  5282. zero-sized base class. That won't be reflected in RLI, yet,
  5283. because we are willing to overlay multiple bases at the same
  5284. offset. However, now we need to make sure that RLI is big enough
  5285. to reflect the entire class. */
  5286. eoc = end_of_class (rli->t,
  5287. CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
  5288. rli_size = rli_size_unit_so_far (rli);
  5289. if (TREE_CODE (rli_size) == INTEGER_CST
  5290. && tree_int_cst_lt (rli_size, eoc))
  5291. {
  5292. /* The size should have been rounded to a whole byte. */
  5293. gcc_assert (tree_int_cst_equal
  5294. (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
  5295. rli->bitpos
  5296. = size_binop (PLUS_EXPR,
  5297. rli->bitpos,
  5298. size_binop (MULT_EXPR,
  5299. convert (bitsizetype,
  5300. size_binop (MINUS_EXPR,
  5301. eoc, rli_size)),
  5302. bitsize_int (BITS_PER_UNIT)));
  5303. normalize_rli (rli);
  5304. }
  5305. }
  5306. /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
  5307. BINFO_OFFSETs for all of the base-classes. Position the vtable
  5308. pointer. Accumulate declared virtual functions on VIRTUALS_P. */
  5309. static void
  5310. layout_class_type (tree t, tree *virtuals_p)
  5311. {
  5312. tree non_static_data_members;
  5313. tree field;
  5314. tree vptr;
  5315. record_layout_info rli;
  5316. /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
  5317. types that appear at that offset. */
  5318. splay_tree empty_base_offsets;
  5319. /* True if the last field laid out was a bit-field. */
  5320. bool last_field_was_bitfield = false;
  5321. /* The location at which the next field should be inserted. */
  5322. tree *next_field;
  5323. /* T, as a base class. */
  5324. tree base_t;
  5325. /* Keep track of the first non-static data member. */
  5326. non_static_data_members = TYPE_FIELDS (t);
  5327. /* Start laying out the record. */
  5328. rli = start_record_layout (t);
  5329. /* Mark all the primary bases in the hierarchy. */
  5330. determine_primary_bases (t);
  5331. /* Create a pointer to our virtual function table. */
  5332. vptr = create_vtable_ptr (t, virtuals_p);
  5333. /* The vptr is always the first thing in the class. */
  5334. if (vptr)
  5335. {
  5336. DECL_CHAIN (vptr) = TYPE_FIELDS (t);
  5337. TYPE_FIELDS (t) = vptr;
  5338. next_field = &DECL_CHAIN (vptr);
  5339. place_field (rli, vptr);
  5340. }
  5341. else
  5342. next_field = &TYPE_FIELDS (t);
  5343. /* Build FIELD_DECLs for all of the non-virtual base-types. */
  5344. empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
  5345. NULL, NULL);
  5346. build_base_fields (rli, empty_base_offsets, next_field);
  5347. /* Layout the non-static data members. */
  5348. for (field = non_static_data_members; field; field = DECL_CHAIN (field))
  5349. {
  5350. tree type;
  5351. tree padding;
  5352. /* We still pass things that aren't non-static data members to
  5353. the back end, in case it wants to do something with them. */
  5354. if (TREE_CODE (field) != FIELD_DECL)
  5355. {
  5356. place_field (rli, field);
  5357. /* If the static data member has incomplete type, keep track
  5358. of it so that it can be completed later. (The handling
  5359. of pending statics in finish_record_layout is
  5360. insufficient; consider:
  5361. struct S1;
  5362. struct S2 { static S1 s1; };
  5363. At this point, finish_record_layout will be called, but
  5364. S1 is still incomplete.) */
  5365. if (VAR_P (field))
  5366. {
  5367. maybe_register_incomplete_var (field);
  5368. /* The visibility of static data members is determined
  5369. at their point of declaration, not their point of
  5370. definition. */
  5371. determine_visibility (field);
  5372. }
  5373. continue;
  5374. }
  5375. type = TREE_TYPE (field);
  5376. if (type == error_mark_node)
  5377. continue;
  5378. padding = NULL_TREE;
  5379. /* If this field is a bit-field whose width is greater than its
  5380. type, then there are some special rules for allocating
  5381. it. */
  5382. if (DECL_C_BIT_FIELD (field)
  5383. && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
  5384. {
  5385. unsigned int itk;
  5386. tree integer_type;
  5387. bool was_unnamed_p = false;
  5388. /* We must allocate the bits as if suitably aligned for the
  5389. longest integer type that fits in this many bits. type
  5390. of the field. Then, we are supposed to use the left over
  5391. bits as additional padding. */
  5392. for (itk = itk_char; itk != itk_none; ++itk)
  5393. if (integer_types[itk] != NULL_TREE
  5394. && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
  5395. TYPE_SIZE (integer_types[itk]))
  5396. || tree_int_cst_lt (DECL_SIZE (field),
  5397. TYPE_SIZE (integer_types[itk]))))
  5398. break;
  5399. /* ITK now indicates a type that is too large for the
  5400. field. We have to back up by one to find the largest
  5401. type that fits. */
  5402. do
  5403. {
  5404. --itk;
  5405. integer_type = integer_types[itk];
  5406. } while (itk > 0 && integer_type == NULL_TREE);
  5407. /* Figure out how much additional padding is required. */
  5408. if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
  5409. {
  5410. if (TREE_CODE (t) == UNION_TYPE)
  5411. /* In a union, the padding field must have the full width
  5412. of the bit-field; all fields start at offset zero. */
  5413. padding = DECL_SIZE (field);
  5414. else
  5415. padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
  5416. TYPE_SIZE (integer_type));
  5417. }
  5418. #ifdef PCC_BITFIELD_TYPE_MATTERS
  5419. /* An unnamed bitfield does not normally affect the
  5420. alignment of the containing class on a target where
  5421. PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
  5422. make any exceptions for unnamed bitfields when the
  5423. bitfields are longer than their types. Therefore, we
  5424. temporarily give the field a name. */
  5425. if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
  5426. {
  5427. was_unnamed_p = true;
  5428. DECL_NAME (field) = make_anon_name ();
  5429. }
  5430. #endif
  5431. DECL_SIZE (field) = TYPE_SIZE (integer_type);
  5432. DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
  5433. DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
  5434. layout_nonempty_base_or_field (rli, field, NULL_TREE,
  5435. empty_base_offsets);
  5436. if (was_unnamed_p)
  5437. DECL_NAME (field) = NULL_TREE;
  5438. /* Now that layout has been performed, set the size of the
  5439. field to the size of its declared type; the rest of the
  5440. field is effectively invisible. */
  5441. DECL_SIZE (field) = TYPE_SIZE (type);
  5442. /* We must also reset the DECL_MODE of the field. */
  5443. DECL_MODE (field) = TYPE_MODE (type);
  5444. }
  5445. else
  5446. layout_nonempty_base_or_field (rli, field, NULL_TREE,
  5447. empty_base_offsets);
  5448. /* Remember the location of any empty classes in FIELD. */
  5449. record_subobject_offsets (TREE_TYPE (field),
  5450. byte_position(field),
  5451. empty_base_offsets,
  5452. /*is_data_member=*/true);
  5453. /* If a bit-field does not immediately follow another bit-field,
  5454. and yet it starts in the middle of a byte, we have failed to
  5455. comply with the ABI. */
  5456. if (warn_abi
  5457. && DECL_C_BIT_FIELD (field)
  5458. /* The TREE_NO_WARNING flag gets set by Objective-C when
  5459. laying out an Objective-C class. The ObjC ABI differs
  5460. from the C++ ABI, and so we do not want a warning
  5461. here. */
  5462. && !TREE_NO_WARNING (field)
  5463. && !last_field_was_bitfield
  5464. && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
  5465. DECL_FIELD_BIT_OFFSET (field),
  5466. bitsize_unit_node)))
  5467. warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
  5468. "change in a future version of GCC", field);
  5469. /* The middle end uses the type of expressions to determine the
  5470. possible range of expression values. In order to optimize
  5471. "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
  5472. must be made aware of the width of "i", via its type.
  5473. Because C++ does not have integer types of arbitrary width,
  5474. we must (for the purposes of the front end) convert from the
  5475. type assigned here to the declared type of the bitfield
  5476. whenever a bitfield expression is used as an rvalue.
  5477. Similarly, when assigning a value to a bitfield, the value
  5478. must be converted to the type given the bitfield here. */
  5479. if (DECL_C_BIT_FIELD (field))
  5480. {
  5481. unsigned HOST_WIDE_INT width;
  5482. tree ftype = TREE_TYPE (field);
  5483. width = tree_to_uhwi (DECL_SIZE (field));
  5484. if (width != TYPE_PRECISION (ftype))
  5485. {
  5486. TREE_TYPE (field)
  5487. = c_build_bitfield_integer_type (width,
  5488. TYPE_UNSIGNED (ftype));
  5489. TREE_TYPE (field)
  5490. = cp_build_qualified_type (TREE_TYPE (field),
  5491. cp_type_quals (ftype));
  5492. }
  5493. }
  5494. /* If we needed additional padding after this field, add it
  5495. now. */
  5496. if (padding)
  5497. {
  5498. tree padding_field;
  5499. padding_field = build_decl (input_location,
  5500. FIELD_DECL,
  5501. NULL_TREE,
  5502. char_type_node);
  5503. DECL_BIT_FIELD (padding_field) = 1;
  5504. DECL_SIZE (padding_field) = padding;
  5505. DECL_CONTEXT (padding_field) = t;
  5506. DECL_ARTIFICIAL (padding_field) = 1;
  5507. DECL_IGNORED_P (padding_field) = 1;
  5508. layout_nonempty_base_or_field (rli, padding_field,
  5509. NULL_TREE,
  5510. empty_base_offsets);
  5511. }
  5512. last_field_was_bitfield = DECL_C_BIT_FIELD (field);
  5513. }
  5514. if (!integer_zerop (rli->bitpos))
  5515. {
  5516. /* Make sure that we are on a byte boundary so that the size of
  5517. the class without virtual bases will always be a round number
  5518. of bytes. */
  5519. rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
  5520. normalize_rli (rli);
  5521. }
  5522. /* Delete all zero-width bit-fields from the list of fields. Now
  5523. that the type is laid out they are no longer important. */
  5524. remove_zero_width_bit_fields (t);
  5525. /* Create the version of T used for virtual bases. We do not use
  5526. make_class_type for this version; this is an artificial type. For
  5527. a POD type, we just reuse T. */
  5528. if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
  5529. {
  5530. base_t = make_node (TREE_CODE (t));
  5531. /* Set the size and alignment for the new type. */
  5532. tree eoc;
  5533. /* If the ABI version is not at least two, and the last
  5534. field was a bit-field, RLI may not be on a byte
  5535. boundary. In particular, rli_size_unit_so_far might
  5536. indicate the last complete byte, while rli_size_so_far
  5537. indicates the total number of bits used. Therefore,
  5538. rli_size_so_far, rather than rli_size_unit_so_far, is
  5539. used to compute TYPE_SIZE_UNIT. */
  5540. eoc = end_of_class (t, /*include_virtuals_p=*/0);
  5541. TYPE_SIZE_UNIT (base_t)
  5542. = size_binop (MAX_EXPR,
  5543. convert (sizetype,
  5544. size_binop (CEIL_DIV_EXPR,
  5545. rli_size_so_far (rli),
  5546. bitsize_int (BITS_PER_UNIT))),
  5547. eoc);
  5548. TYPE_SIZE (base_t)
  5549. = size_binop (MAX_EXPR,
  5550. rli_size_so_far (rli),
  5551. size_binop (MULT_EXPR,
  5552. convert (bitsizetype, eoc),
  5553. bitsize_int (BITS_PER_UNIT)));
  5554. TYPE_ALIGN (base_t) = rli->record_align;
  5555. TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
  5556. /* Copy the fields from T. */
  5557. next_field = &TYPE_FIELDS (base_t);
  5558. for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
  5559. if (TREE_CODE (field) == FIELD_DECL)
  5560. {
  5561. *next_field = build_decl (input_location,
  5562. FIELD_DECL,
  5563. DECL_NAME (field),
  5564. TREE_TYPE (field));
  5565. DECL_CONTEXT (*next_field) = base_t;
  5566. DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
  5567. DECL_FIELD_BIT_OFFSET (*next_field)
  5568. = DECL_FIELD_BIT_OFFSET (field);
  5569. DECL_SIZE (*next_field) = DECL_SIZE (field);
  5570. DECL_MODE (*next_field) = DECL_MODE (field);
  5571. next_field = &DECL_CHAIN (*next_field);
  5572. }
  5573. /* Record the base version of the type. */
  5574. CLASSTYPE_AS_BASE (t) = base_t;
  5575. TYPE_CONTEXT (base_t) = t;
  5576. }
  5577. else
  5578. CLASSTYPE_AS_BASE (t) = t;
  5579. /* Every empty class contains an empty class. */
  5580. if (CLASSTYPE_EMPTY_P (t))
  5581. CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
  5582. /* Set the TYPE_DECL for this type to contain the right
  5583. value for DECL_OFFSET, so that we can use it as part
  5584. of a COMPONENT_REF for multiple inheritance. */
  5585. layout_decl (TYPE_MAIN_DECL (t), 0);
  5586. /* Now fix up any virtual base class types that we left lying
  5587. around. We must get these done before we try to lay out the
  5588. virtual function table. As a side-effect, this will remove the
  5589. base subobject fields. */
  5590. layout_virtual_bases (rli, empty_base_offsets);
  5591. /* Make sure that empty classes are reflected in RLI at this
  5592. point. */
  5593. include_empty_classes(rli);
  5594. /* Make sure not to create any structures with zero size. */
  5595. if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
  5596. place_field (rli,
  5597. build_decl (input_location,
  5598. FIELD_DECL, NULL_TREE, char_type_node));
  5599. /* If this is a non-POD, declaring it packed makes a difference to how it
  5600. can be used as a field; don't let finalize_record_size undo it. */
  5601. if (TYPE_PACKED (t) && !layout_pod_type_p (t))
  5602. rli->packed_maybe_necessary = true;
  5603. /* Let the back end lay out the type. */
  5604. finish_record_layout (rli, /*free_p=*/true);
  5605. if (TYPE_SIZE_UNIT (t)
  5606. && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
  5607. && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
  5608. && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
  5609. error ("type %qT is too large", t);
  5610. /* Warn about bases that can't be talked about due to ambiguity. */
  5611. warn_about_ambiguous_bases (t);
  5612. /* Now that we're done with layout, give the base fields the real types. */
  5613. for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
  5614. if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
  5615. TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
  5616. /* Clean up. */
  5617. splay_tree_delete (empty_base_offsets);
  5618. if (CLASSTYPE_EMPTY_P (t)
  5619. && tree_int_cst_lt (sizeof_biggest_empty_class,
  5620. TYPE_SIZE_UNIT (t)))
  5621. sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
  5622. }
  5623. /* Determine the "key method" for the class type indicated by TYPE,
  5624. and set CLASSTYPE_KEY_METHOD accordingly. */
  5625. void
  5626. determine_key_method (tree type)
  5627. {
  5628. tree method;
  5629. if (TYPE_FOR_JAVA (type)
  5630. || processing_template_decl
  5631. || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
  5632. || CLASSTYPE_INTERFACE_KNOWN (type))
  5633. return;
  5634. /* The key method is the first non-pure virtual function that is not
  5635. inline at the point of class definition. On some targets the
  5636. key function may not be inline; those targets should not call
  5637. this function until the end of the translation unit. */
  5638. for (method = TYPE_METHODS (type); method != NULL_TREE;
  5639. method = DECL_CHAIN (method))
  5640. if (TREE_CODE (method) == FUNCTION_DECL
  5641. && DECL_VINDEX (method) != NULL_TREE
  5642. && ! DECL_DECLARED_INLINE_P (method)
  5643. && ! DECL_PURE_VIRTUAL_P (method))
  5644. {
  5645. CLASSTYPE_KEY_METHOD (type) = method;
  5646. break;
  5647. }
  5648. return;
  5649. }
  5650. /* Allocate and return an instance of struct sorted_fields_type with
  5651. N fields. */
  5652. static struct sorted_fields_type *
  5653. sorted_fields_type_new (int n)
  5654. {
  5655. struct sorted_fields_type *sft;
  5656. sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
  5657. + n * sizeof (tree));
  5658. sft->len = n;
  5659. return sft;
  5660. }
  5661. /* Perform processing required when the definition of T (a class type)
  5662. is complete. */
  5663. void
  5664. finish_struct_1 (tree t)
  5665. {
  5666. tree x;
  5667. /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
  5668. tree virtuals = NULL_TREE;
  5669. if (COMPLETE_TYPE_P (t))
  5670. {
  5671. gcc_assert (MAYBE_CLASS_TYPE_P (t));
  5672. error ("redefinition of %q#T", t);
  5673. popclass ();
  5674. return;
  5675. }
  5676. /* If this type was previously laid out as a forward reference,
  5677. make sure we lay it out again. */
  5678. TYPE_SIZE (t) = NULL_TREE;
  5679. CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
  5680. /* Make assumptions about the class; we'll reset the flags if
  5681. necessary. */
  5682. CLASSTYPE_EMPTY_P (t) = 1;
  5683. CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
  5684. CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
  5685. CLASSTYPE_LITERAL_P (t) = true;
  5686. /* Do end-of-class semantic processing: checking the validity of the
  5687. bases and members and add implicitly generated methods. */
  5688. check_bases_and_members (t);
  5689. /* Find the key method. */
  5690. if (TYPE_CONTAINS_VPTR_P (t))
  5691. {
  5692. /* The Itanium C++ ABI permits the key method to be chosen when
  5693. the class is defined -- even though the key method so
  5694. selected may later turn out to be an inline function. On
  5695. some systems (such as ARM Symbian OS) the key method cannot
  5696. be determined until the end of the translation unit. On such
  5697. systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
  5698. will cause the class to be added to KEYED_CLASSES. Then, in
  5699. finish_file we will determine the key method. */
  5700. if (targetm.cxx.key_method_may_be_inline ())
  5701. determine_key_method (t);
  5702. /* If a polymorphic class has no key method, we may emit the vtable
  5703. in every translation unit where the class definition appears. If
  5704. we're devirtualizing, we can look into the vtable even if we
  5705. aren't emitting it. */
  5706. if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
  5707. keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
  5708. }
  5709. /* Layout the class itself. */
  5710. layout_class_type (t, &virtuals);
  5711. if (CLASSTYPE_AS_BASE (t) != t)
  5712. /* We use the base type for trivial assignments, and hence it
  5713. needs a mode. */
  5714. compute_record_mode (CLASSTYPE_AS_BASE (t));
  5715. virtuals = modify_all_vtables (t, nreverse (virtuals));
  5716. /* If necessary, create the primary vtable for this class. */
  5717. if (virtuals || TYPE_CONTAINS_VPTR_P (t))
  5718. {
  5719. /* We must enter these virtuals into the table. */
  5720. if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
  5721. build_primary_vtable (NULL_TREE, t);
  5722. else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
  5723. /* Here we know enough to change the type of our virtual
  5724. function table, but we will wait until later this function. */
  5725. build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
  5726. /* If we're warning about ABI tags, check the types of the new
  5727. virtual functions. */
  5728. if (warn_abi_tag)
  5729. for (tree v = virtuals; v; v = TREE_CHAIN (v))
  5730. check_abi_tags (t, TREE_VALUE (v));
  5731. }
  5732. if (TYPE_CONTAINS_VPTR_P (t))
  5733. {
  5734. int vindex;
  5735. tree fn;
  5736. if (BINFO_VTABLE (TYPE_BINFO (t)))
  5737. gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
  5738. if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
  5739. gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
  5740. /* Add entries for virtual functions introduced by this class. */
  5741. BINFO_VIRTUALS (TYPE_BINFO (t))
  5742. = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
  5743. /* Set DECL_VINDEX for all functions declared in this class. */
  5744. for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
  5745. fn;
  5746. fn = TREE_CHAIN (fn),
  5747. vindex += (TARGET_VTABLE_USES_DESCRIPTORS
  5748. ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
  5749. {
  5750. tree fndecl = BV_FN (fn);
  5751. if (DECL_THUNK_P (fndecl))
  5752. /* A thunk. We should never be calling this entry directly
  5753. from this vtable -- we'd use the entry for the non
  5754. thunk base function. */
  5755. DECL_VINDEX (fndecl) = NULL_TREE;
  5756. else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
  5757. DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
  5758. }
  5759. }
  5760. finish_struct_bits (t);
  5761. set_method_tm_attributes (t);
  5762. /* Complete the rtl for any static member objects of the type we're
  5763. working on. */
  5764. for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
  5765. if (VAR_P (x) && TREE_STATIC (x)
  5766. && TREE_TYPE (x) != error_mark_node
  5767. && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
  5768. DECL_MODE (x) = TYPE_MODE (t);
  5769. /* Done with FIELDS...now decide whether to sort these for
  5770. faster lookups later.
  5771. We use a small number because most searches fail (succeeding
  5772. ultimately as the search bores through the inheritance
  5773. hierarchy), and we want this failure to occur quickly. */
  5774. insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
  5775. /* Complain if one of the field types requires lower visibility. */
  5776. constrain_class_visibility (t);
  5777. /* Make the rtl for any new vtables we have created, and unmark
  5778. the base types we marked. */
  5779. finish_vtbls (t);
  5780. /* Build the VTT for T. */
  5781. build_vtt (t);
  5782. /* This warning does not make sense for Java classes, since they
  5783. cannot have destructors. */
  5784. if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
  5785. && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
  5786. && !CLASSTYPE_FINAL (t))
  5787. warning (OPT_Wnon_virtual_dtor,
  5788. "%q#T has virtual functions and accessible"
  5789. " non-virtual destructor", t);
  5790. complete_vars (t);
  5791. if (warn_overloaded_virtual)
  5792. warn_hidden (t);
  5793. /* Class layout, assignment of virtual table slots, etc., is now
  5794. complete. Give the back end a chance to tweak the visibility of
  5795. the class or perform any other required target modifications. */
  5796. targetm.cxx.adjust_class_at_definition (t);
  5797. maybe_suppress_debug_info (t);
  5798. if (flag_vtable_verify)
  5799. vtv_save_class_info (t);
  5800. dump_class_hierarchy (t);
  5801. /* Finish debugging output for this type. */
  5802. rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
  5803. if (TYPE_TRANSPARENT_AGGR (t))
  5804. {
  5805. tree field = first_field (t);
  5806. if (field == NULL_TREE || error_operand_p (field))
  5807. {
  5808. error ("type transparent %q#T does not have any fields", t);
  5809. TYPE_TRANSPARENT_AGGR (t) = 0;
  5810. }
  5811. else if (DECL_ARTIFICIAL (field))
  5812. {
  5813. if (DECL_FIELD_IS_BASE (field))
  5814. error ("type transparent class %qT has base classes", t);
  5815. else
  5816. {
  5817. gcc_checking_assert (DECL_VIRTUAL_P (field));
  5818. error ("type transparent class %qT has virtual functions", t);
  5819. }
  5820. TYPE_TRANSPARENT_AGGR (t) = 0;
  5821. }
  5822. else if (TYPE_MODE (t) != DECL_MODE (field))
  5823. {
  5824. error ("type transparent %q#T cannot be made transparent because "
  5825. "the type of the first field has a different ABI from the "
  5826. "class overall", t);
  5827. TYPE_TRANSPARENT_AGGR (t) = 0;
  5828. }
  5829. }
  5830. }
  5831. /* Insert FIELDS into T for the sorted case if the FIELDS count is
  5832. equal to THRESHOLD or greater than THRESHOLD. */
  5833. static void
  5834. insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
  5835. {
  5836. int n_fields = count_fields (fields);
  5837. if (n_fields >= threshold)
  5838. {
  5839. struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
  5840. add_fields_to_record_type (fields, field_vec, 0);
  5841. qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
  5842. CLASSTYPE_SORTED_FIELDS (t) = field_vec;
  5843. }
  5844. }
  5845. /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
  5846. void
  5847. insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
  5848. {
  5849. struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
  5850. if (sorted_fields)
  5851. {
  5852. int i;
  5853. int n_fields
  5854. = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
  5855. struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
  5856. for (i = 0; i < sorted_fields->len; ++i)
  5857. field_vec->elts[i] = sorted_fields->elts[i];
  5858. add_enum_fields_to_record_type (enumtype, field_vec,
  5859. sorted_fields->len);
  5860. qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
  5861. CLASSTYPE_SORTED_FIELDS (t) = field_vec;
  5862. }
  5863. }
  5864. /* When T was built up, the member declarations were added in reverse
  5865. order. Rearrange them to declaration order. */
  5866. void
  5867. unreverse_member_declarations (tree t)
  5868. {
  5869. tree next;
  5870. tree prev;
  5871. tree x;
  5872. /* The following lists are all in reverse order. Put them in
  5873. declaration order now. */
  5874. TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
  5875. CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
  5876. /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
  5877. reverse order, so we can't just use nreverse. */
  5878. prev = NULL_TREE;
  5879. for (x = TYPE_FIELDS (t);
  5880. x && TREE_CODE (x) != TYPE_DECL;
  5881. x = next)
  5882. {
  5883. next = DECL_CHAIN (x);
  5884. DECL_CHAIN (x) = prev;
  5885. prev = x;
  5886. }
  5887. if (prev)
  5888. {
  5889. DECL_CHAIN (TYPE_FIELDS (t)) = x;
  5890. if (prev)
  5891. TYPE_FIELDS (t) = prev;
  5892. }
  5893. }
  5894. tree
  5895. finish_struct (tree t, tree attributes)
  5896. {
  5897. location_t saved_loc = input_location;
  5898. /* Now that we've got all the field declarations, reverse everything
  5899. as necessary. */
  5900. unreverse_member_declarations (t);
  5901. cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
  5902. fixup_attribute_variants (t);
  5903. /* Nadger the current location so that diagnostics point to the start of
  5904. the struct, not the end. */
  5905. input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
  5906. if (processing_template_decl)
  5907. {
  5908. tree x;
  5909. finish_struct_methods (t);
  5910. TYPE_SIZE (t) = bitsize_zero_node;
  5911. TYPE_SIZE_UNIT (t) = size_zero_node;
  5912. /* We need to emit an error message if this type was used as a parameter
  5913. and it is an abstract type, even if it is a template. We construct
  5914. a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
  5915. account and we call complete_vars with this type, which will check
  5916. the PARM_DECLS. Note that while the type is being defined,
  5917. CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
  5918. (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
  5919. CLASSTYPE_PURE_VIRTUALS (t) = NULL;
  5920. for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
  5921. if (DECL_PURE_VIRTUAL_P (x))
  5922. vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
  5923. complete_vars (t);
  5924. /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
  5925. an enclosing scope is a template class, so that this function be
  5926. found by lookup_fnfields_1 when the using declaration is not
  5927. instantiated yet. */
  5928. for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
  5929. if (TREE_CODE (x) == USING_DECL)
  5930. {
  5931. tree fn = strip_using_decl (x);
  5932. if (is_overloaded_fn (fn))
  5933. for (; fn; fn = OVL_NEXT (fn))
  5934. add_method (t, OVL_CURRENT (fn), x);
  5935. }
  5936. /* Remember current #pragma pack value. */
  5937. TYPE_PRECISION (t) = maximum_field_alignment;
  5938. /* Fix up any variants we've already built. */
  5939. for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
  5940. {
  5941. TYPE_SIZE (x) = TYPE_SIZE (t);
  5942. TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
  5943. TYPE_FIELDS (x) = TYPE_FIELDS (t);
  5944. TYPE_METHODS (x) = TYPE_METHODS (t);
  5945. }
  5946. }
  5947. else
  5948. finish_struct_1 (t);
  5949. if (is_std_init_list (t))
  5950. {
  5951. /* People keep complaining that the compiler crashes on an invalid
  5952. definition of initializer_list, so I guess we should explicitly
  5953. reject it. What the compiler internals care about is that it's a
  5954. template and has a pointer field followed by an integer field. */
  5955. bool ok = false;
  5956. if (processing_template_decl)
  5957. {
  5958. tree f = next_initializable_field (TYPE_FIELDS (t));
  5959. if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
  5960. {
  5961. f = next_initializable_field (DECL_CHAIN (f));
  5962. if (f && same_type_p (TREE_TYPE (f), size_type_node))
  5963. ok = true;
  5964. }
  5965. }
  5966. if (!ok)
  5967. fatal_error (input_location,
  5968. "definition of std::initializer_list does not match "
  5969. "#include <initializer_list>");
  5970. }
  5971. input_location = saved_loc;
  5972. TYPE_BEING_DEFINED (t) = 0;
  5973. if (current_class_type)
  5974. popclass ();
  5975. else
  5976. error ("trying to finish struct, but kicked out due to previous parse errors");
  5977. if (processing_template_decl && at_function_scope_p ()
  5978. /* Lambdas are defined by the LAMBDA_EXPR. */
  5979. && !LAMBDA_TYPE_P (t))
  5980. add_stmt (build_min (TAG_DEFN, t));
  5981. return t;
  5982. }
  5983. /* Hash table to avoid endless recursion when handling references. */
  5984. static hash_table<pointer_hash<tree_node> > *fixed_type_or_null_ref_ht;
  5985. /* Return the dynamic type of INSTANCE, if known.
  5986. Used to determine whether the virtual function table is needed
  5987. or not.
  5988. *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
  5989. of our knowledge of its type. *NONNULL should be initialized
  5990. before this function is called. */
  5991. static tree
  5992. fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
  5993. {
  5994. #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
  5995. switch (TREE_CODE (instance))
  5996. {
  5997. case INDIRECT_REF:
  5998. if (POINTER_TYPE_P (TREE_TYPE (instance)))
  5999. return NULL_TREE;
  6000. else
  6001. return RECUR (TREE_OPERAND (instance, 0));
  6002. case CALL_EXPR:
  6003. /* This is a call to a constructor, hence it's never zero. */
  6004. if (TREE_HAS_CONSTRUCTOR (instance))
  6005. {
  6006. if (nonnull)
  6007. *nonnull = 1;
  6008. return TREE_TYPE (instance);
  6009. }
  6010. return NULL_TREE;
  6011. case SAVE_EXPR:
  6012. /* This is a call to a constructor, hence it's never zero. */
  6013. if (TREE_HAS_CONSTRUCTOR (instance))
  6014. {
  6015. if (nonnull)
  6016. *nonnull = 1;
  6017. return TREE_TYPE (instance);
  6018. }
  6019. return RECUR (TREE_OPERAND (instance, 0));
  6020. case POINTER_PLUS_EXPR:
  6021. case PLUS_EXPR:
  6022. case MINUS_EXPR:
  6023. if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
  6024. return RECUR (TREE_OPERAND (instance, 0));
  6025. if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
  6026. /* Propagate nonnull. */
  6027. return RECUR (TREE_OPERAND (instance, 0));
  6028. return NULL_TREE;
  6029. CASE_CONVERT:
  6030. return RECUR (TREE_OPERAND (instance, 0));
  6031. case ADDR_EXPR:
  6032. instance = TREE_OPERAND (instance, 0);
  6033. if (nonnull)
  6034. {
  6035. /* Just because we see an ADDR_EXPR doesn't mean we're dealing
  6036. with a real object -- given &p->f, p can still be null. */
  6037. tree t = get_base_address (instance);
  6038. /* ??? Probably should check DECL_WEAK here. */
  6039. if (t && DECL_P (t))
  6040. *nonnull = 1;
  6041. }
  6042. return RECUR (instance);
  6043. case COMPONENT_REF:
  6044. /* If this component is really a base class reference, then the field
  6045. itself isn't definitive. */
  6046. if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
  6047. return RECUR (TREE_OPERAND (instance, 0));
  6048. return RECUR (TREE_OPERAND (instance, 1));
  6049. case VAR_DECL:
  6050. case FIELD_DECL:
  6051. if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
  6052. && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
  6053. {
  6054. if (nonnull)
  6055. *nonnull = 1;
  6056. return TREE_TYPE (TREE_TYPE (instance));
  6057. }
  6058. /* fall through... */
  6059. case TARGET_EXPR:
  6060. case PARM_DECL:
  6061. case RESULT_DECL:
  6062. if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
  6063. {
  6064. if (nonnull)
  6065. *nonnull = 1;
  6066. return TREE_TYPE (instance);
  6067. }
  6068. else if (instance == current_class_ptr)
  6069. {
  6070. if (nonnull)
  6071. *nonnull = 1;
  6072. /* if we're in a ctor or dtor, we know our type. If
  6073. current_class_ptr is set but we aren't in a function, we're in
  6074. an NSDMI (and therefore a constructor). */
  6075. if (current_scope () != current_function_decl
  6076. || (DECL_LANG_SPECIFIC (current_function_decl)
  6077. && (DECL_CONSTRUCTOR_P (current_function_decl)
  6078. || DECL_DESTRUCTOR_P (current_function_decl))))
  6079. {
  6080. if (cdtorp)
  6081. *cdtorp = 1;
  6082. return TREE_TYPE (TREE_TYPE (instance));
  6083. }
  6084. }
  6085. else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
  6086. {
  6087. /* We only need one hash table because it is always left empty. */
  6088. if (!fixed_type_or_null_ref_ht)
  6089. fixed_type_or_null_ref_ht
  6090. = new hash_table<pointer_hash<tree_node> > (37);
  6091. /* Reference variables should be references to objects. */
  6092. if (nonnull)
  6093. *nonnull = 1;
  6094. /* Enter the INSTANCE in a table to prevent recursion; a
  6095. variable's initializer may refer to the variable
  6096. itself. */
  6097. if (VAR_P (instance)
  6098. && DECL_INITIAL (instance)
  6099. && !type_dependent_expression_p_push (DECL_INITIAL (instance))
  6100. && !fixed_type_or_null_ref_ht->find (instance))
  6101. {
  6102. tree type;
  6103. tree_node **slot;
  6104. slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
  6105. *slot = instance;
  6106. type = RECUR (DECL_INITIAL (instance));
  6107. fixed_type_or_null_ref_ht->remove_elt (instance);
  6108. return type;
  6109. }
  6110. }
  6111. return NULL_TREE;
  6112. default:
  6113. return NULL_TREE;
  6114. }
  6115. #undef RECUR
  6116. }
  6117. /* Return nonzero if the dynamic type of INSTANCE is known, and
  6118. equivalent to the static type. We also handle the case where
  6119. INSTANCE is really a pointer. Return negative if this is a
  6120. ctor/dtor. There the dynamic type is known, but this might not be
  6121. the most derived base of the original object, and hence virtual
  6122. bases may not be laid out according to this type.
  6123. Used to determine whether the virtual function table is needed
  6124. or not.
  6125. *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
  6126. of our knowledge of its type. *NONNULL should be initialized
  6127. before this function is called. */
  6128. int
  6129. resolves_to_fixed_type_p (tree instance, int* nonnull)
  6130. {
  6131. tree t = TREE_TYPE (instance);
  6132. int cdtorp = 0;
  6133. tree fixed;
  6134. /* processing_template_decl can be false in a template if we're in
  6135. instantiate_non_dependent_expr, but we still want to suppress
  6136. this check. */
  6137. if (in_template_function ())
  6138. {
  6139. /* In a template we only care about the type of the result. */
  6140. if (nonnull)
  6141. *nonnull = true;
  6142. return true;
  6143. }
  6144. fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
  6145. if (fixed == NULL_TREE)
  6146. return 0;
  6147. if (POINTER_TYPE_P (t))
  6148. t = TREE_TYPE (t);
  6149. if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
  6150. return 0;
  6151. return cdtorp ? -1 : 1;
  6152. }
  6153. void
  6154. init_class_processing (void)
  6155. {
  6156. current_class_depth = 0;
  6157. current_class_stack_size = 10;
  6158. current_class_stack
  6159. = XNEWVEC (struct class_stack_node, current_class_stack_size);
  6160. vec_alloc (local_classes, 8);
  6161. sizeof_biggest_empty_class = size_zero_node;
  6162. ridpointers[(int) RID_PUBLIC] = access_public_node;
  6163. ridpointers[(int) RID_PRIVATE] = access_private_node;
  6164. ridpointers[(int) RID_PROTECTED] = access_protected_node;
  6165. }
  6166. /* Restore the cached PREVIOUS_CLASS_LEVEL. */
  6167. static void
  6168. restore_class_cache (void)
  6169. {
  6170. tree type;
  6171. /* We are re-entering the same class we just left, so we don't
  6172. have to search the whole inheritance matrix to find all the
  6173. decls to bind again. Instead, we install the cached
  6174. class_shadowed list and walk through it binding names. */
  6175. push_binding_level (previous_class_level);
  6176. class_binding_level = previous_class_level;
  6177. /* Restore IDENTIFIER_TYPE_VALUE. */
  6178. for (type = class_binding_level->type_shadowed;
  6179. type;
  6180. type = TREE_CHAIN (type))
  6181. SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
  6182. }
  6183. /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
  6184. appropriate for TYPE.
  6185. So that we may avoid calls to lookup_name, we cache the _TYPE
  6186. nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
  6187. For multiple inheritance, we perform a two-pass depth-first search
  6188. of the type lattice. */
  6189. void
  6190. pushclass (tree type)
  6191. {
  6192. class_stack_node_t csn;
  6193. type = TYPE_MAIN_VARIANT (type);
  6194. /* Make sure there is enough room for the new entry on the stack. */
  6195. if (current_class_depth + 1 >= current_class_stack_size)
  6196. {
  6197. current_class_stack_size *= 2;
  6198. current_class_stack
  6199. = XRESIZEVEC (struct class_stack_node, current_class_stack,
  6200. current_class_stack_size);
  6201. }
  6202. /* Insert a new entry on the class stack. */
  6203. csn = current_class_stack + current_class_depth;
  6204. csn->name = current_class_name;
  6205. csn->type = current_class_type;
  6206. csn->access = current_access_specifier;
  6207. csn->names_used = 0;
  6208. csn->hidden = 0;
  6209. current_class_depth++;
  6210. /* Now set up the new type. */
  6211. current_class_name = TYPE_NAME (type);
  6212. if (TREE_CODE (current_class_name) == TYPE_DECL)
  6213. current_class_name = DECL_NAME (current_class_name);
  6214. current_class_type = type;
  6215. /* By default, things in classes are private, while things in
  6216. structures or unions are public. */
  6217. current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
  6218. ? access_private_node
  6219. : access_public_node);
  6220. if (previous_class_level
  6221. && type != previous_class_level->this_entity
  6222. && current_class_depth == 1)
  6223. {
  6224. /* Forcibly remove any old class remnants. */
  6225. invalidate_class_lookup_cache ();
  6226. }
  6227. if (!previous_class_level
  6228. || type != previous_class_level->this_entity
  6229. || current_class_depth > 1)
  6230. pushlevel_class ();
  6231. else
  6232. restore_class_cache ();
  6233. }
  6234. /* When we exit a toplevel class scope, we save its binding level so
  6235. that we can restore it quickly. Here, we've entered some other
  6236. class, so we must invalidate our cache. */
  6237. void
  6238. invalidate_class_lookup_cache (void)
  6239. {
  6240. previous_class_level = NULL;
  6241. }
  6242. /* Get out of the current class scope. If we were in a class scope
  6243. previously, that is the one popped to. */
  6244. void
  6245. popclass (void)
  6246. {
  6247. poplevel_class ();
  6248. current_class_depth--;
  6249. current_class_name = current_class_stack[current_class_depth].name;
  6250. current_class_type = current_class_stack[current_class_depth].type;
  6251. current_access_specifier = current_class_stack[current_class_depth].access;
  6252. if (current_class_stack[current_class_depth].names_used)
  6253. splay_tree_delete (current_class_stack[current_class_depth].names_used);
  6254. }
  6255. /* Mark the top of the class stack as hidden. */
  6256. void
  6257. push_class_stack (void)
  6258. {
  6259. if (current_class_depth)
  6260. ++current_class_stack[current_class_depth - 1].hidden;
  6261. }
  6262. /* Mark the top of the class stack as un-hidden. */
  6263. void
  6264. pop_class_stack (void)
  6265. {
  6266. if (current_class_depth)
  6267. --current_class_stack[current_class_depth - 1].hidden;
  6268. }
  6269. /* Returns 1 if the class type currently being defined is either T or
  6270. a nested type of T. */
  6271. bool
  6272. currently_open_class (tree t)
  6273. {
  6274. int i;
  6275. if (!CLASS_TYPE_P (t))
  6276. return false;
  6277. t = TYPE_MAIN_VARIANT (t);
  6278. /* We start looking from 1 because entry 0 is from global scope,
  6279. and has no type. */
  6280. for (i = current_class_depth; i > 0; --i)
  6281. {
  6282. tree c;
  6283. if (i == current_class_depth)
  6284. c = current_class_type;
  6285. else
  6286. {
  6287. if (current_class_stack[i].hidden)
  6288. break;
  6289. c = current_class_stack[i].type;
  6290. }
  6291. if (!c)
  6292. continue;
  6293. if (same_type_p (c, t))
  6294. return true;
  6295. }
  6296. return false;
  6297. }
  6298. /* If either current_class_type or one of its enclosing classes are derived
  6299. from T, return the appropriate type. Used to determine how we found
  6300. something via unqualified lookup. */
  6301. tree
  6302. currently_open_derived_class (tree t)
  6303. {
  6304. int i;
  6305. /* The bases of a dependent type are unknown. */
  6306. if (dependent_type_p (t))
  6307. return NULL_TREE;
  6308. if (!current_class_type)
  6309. return NULL_TREE;
  6310. if (DERIVED_FROM_P (t, current_class_type))
  6311. return current_class_type;
  6312. for (i = current_class_depth - 1; i > 0; --i)
  6313. {
  6314. if (current_class_stack[i].hidden)
  6315. break;
  6316. if (DERIVED_FROM_P (t, current_class_stack[i].type))
  6317. return current_class_stack[i].type;
  6318. }
  6319. return NULL_TREE;
  6320. }
  6321. /* Return the outermost enclosing class type that is still open, or
  6322. NULL_TREE. */
  6323. tree
  6324. outermost_open_class (void)
  6325. {
  6326. if (!current_class_type)
  6327. return NULL_TREE;
  6328. tree r = NULL_TREE;
  6329. if (TYPE_BEING_DEFINED (current_class_type))
  6330. r = current_class_type;
  6331. for (int i = current_class_depth - 1; i > 0; --i)
  6332. {
  6333. if (current_class_stack[i].hidden)
  6334. break;
  6335. tree t = current_class_stack[i].type;
  6336. if (!TYPE_BEING_DEFINED (t))
  6337. break;
  6338. r = t;
  6339. }
  6340. return r;
  6341. }
  6342. /* Returns the innermost class type which is not a lambda closure type. */
  6343. tree
  6344. current_nonlambda_class_type (void)
  6345. {
  6346. int i;
  6347. /* We start looking from 1 because entry 0 is from global scope,
  6348. and has no type. */
  6349. for (i = current_class_depth; i > 0; --i)
  6350. {
  6351. tree c;
  6352. if (i == current_class_depth)
  6353. c = current_class_type;
  6354. else
  6355. {
  6356. if (current_class_stack[i].hidden)
  6357. break;
  6358. c = current_class_stack[i].type;
  6359. }
  6360. if (!c)
  6361. continue;
  6362. if (!LAMBDA_TYPE_P (c))
  6363. return c;
  6364. }
  6365. return NULL_TREE;
  6366. }
  6367. /* When entering a class scope, all enclosing class scopes' names with
  6368. static meaning (static variables, static functions, types and
  6369. enumerators) have to be visible. This recursive function calls
  6370. pushclass for all enclosing class contexts until global or a local
  6371. scope is reached. TYPE is the enclosed class. */
  6372. void
  6373. push_nested_class (tree type)
  6374. {
  6375. /* A namespace might be passed in error cases, like A::B:C. */
  6376. if (type == NULL_TREE
  6377. || !CLASS_TYPE_P (type))
  6378. return;
  6379. push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
  6380. pushclass (type);
  6381. }
  6382. /* Undoes a push_nested_class call. */
  6383. void
  6384. pop_nested_class (void)
  6385. {
  6386. tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
  6387. popclass ();
  6388. if (context && CLASS_TYPE_P (context))
  6389. pop_nested_class ();
  6390. }
  6391. /* Returns the number of extern "LANG" blocks we are nested within. */
  6392. int
  6393. current_lang_depth (void)
  6394. {
  6395. return vec_safe_length (current_lang_base);
  6396. }
  6397. /* Set global variables CURRENT_LANG_NAME to appropriate value
  6398. so that behavior of name-mangling machinery is correct. */
  6399. void
  6400. push_lang_context (tree name)
  6401. {
  6402. vec_safe_push (current_lang_base, current_lang_name);
  6403. if (name == lang_name_cplusplus)
  6404. {
  6405. current_lang_name = name;
  6406. }
  6407. else if (name == lang_name_java)
  6408. {
  6409. current_lang_name = name;
  6410. /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
  6411. (See record_builtin_java_type in decl.c.) However, that causes
  6412. incorrect debug entries if these types are actually used.
  6413. So we re-enable debug output after extern "Java". */
  6414. DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
  6415. DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
  6416. DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
  6417. DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
  6418. DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
  6419. DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
  6420. DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
  6421. DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
  6422. }
  6423. else if (name == lang_name_c)
  6424. {
  6425. current_lang_name = name;
  6426. }
  6427. else
  6428. error ("language string %<\"%E\"%> not recognized", name);
  6429. }
  6430. /* Get out of the current language scope. */
  6431. void
  6432. pop_lang_context (void)
  6433. {
  6434. current_lang_name = current_lang_base->pop ();
  6435. }
  6436. /* Type instantiation routines. */
  6437. /* Given an OVERLOAD and a TARGET_TYPE, return the function that
  6438. matches the TARGET_TYPE. If there is no satisfactory match, return
  6439. error_mark_node, and issue an error & warning messages under
  6440. control of FLAGS. Permit pointers to member function if FLAGS
  6441. permits. If TEMPLATE_ONLY, the name of the overloaded function was
  6442. a template-id, and EXPLICIT_TARGS are the explicitly provided
  6443. template arguments.
  6444. If OVERLOAD is for one or more member functions, then ACCESS_PATH
  6445. is the base path used to reference those member functions. If
  6446. the address is resolved to a member function, access checks will be
  6447. performed and errors issued if appropriate. */
  6448. static tree
  6449. resolve_address_of_overloaded_function (tree target_type,
  6450. tree overload,
  6451. tsubst_flags_t flags,
  6452. bool template_only,
  6453. tree explicit_targs,
  6454. tree access_path)
  6455. {
  6456. /* Here's what the standard says:
  6457. [over.over]
  6458. If the name is a function template, template argument deduction
  6459. is done, and if the argument deduction succeeds, the deduced
  6460. arguments are used to generate a single template function, which
  6461. is added to the set of overloaded functions considered.
  6462. Non-member functions and static member functions match targets of
  6463. type "pointer-to-function" or "reference-to-function." Nonstatic
  6464. member functions match targets of type "pointer-to-member
  6465. function;" the function type of the pointer to member is used to
  6466. select the member function from the set of overloaded member
  6467. functions. If a nonstatic member function is selected, the
  6468. reference to the overloaded function name is required to have the
  6469. form of a pointer to member as described in 5.3.1.
  6470. If more than one function is selected, any template functions in
  6471. the set are eliminated if the set also contains a non-template
  6472. function, and any given template function is eliminated if the
  6473. set contains a second template function that is more specialized
  6474. than the first according to the partial ordering rules 14.5.5.2.
  6475. After such eliminations, if any, there shall remain exactly one
  6476. selected function. */
  6477. int is_ptrmem = 0;
  6478. /* We store the matches in a TREE_LIST rooted here. The functions
  6479. are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
  6480. interoperability with most_specialized_instantiation. */
  6481. tree matches = NULL_TREE;
  6482. tree fn;
  6483. tree target_fn_type;
  6484. /* By the time we get here, we should be seeing only real
  6485. pointer-to-member types, not the internal POINTER_TYPE to
  6486. METHOD_TYPE representation. */
  6487. gcc_assert (!TYPE_PTR_P (target_type)
  6488. || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
  6489. gcc_assert (is_overloaded_fn (overload));
  6490. /* Check that the TARGET_TYPE is reasonable. */
  6491. if (TYPE_PTRFN_P (target_type)
  6492. || TYPE_REFFN_P (target_type))
  6493. /* This is OK. */;
  6494. else if (TYPE_PTRMEMFUNC_P (target_type))
  6495. /* This is OK, too. */
  6496. is_ptrmem = 1;
  6497. else if (TREE_CODE (target_type) == FUNCTION_TYPE)
  6498. /* This is OK, too. This comes from a conversion to reference
  6499. type. */
  6500. target_type = build_reference_type (target_type);
  6501. else
  6502. {
  6503. if (flags & tf_error)
  6504. error ("cannot resolve overloaded function %qD based on"
  6505. " conversion to type %qT",
  6506. DECL_NAME (OVL_FUNCTION (overload)), target_type);
  6507. return error_mark_node;
  6508. }
  6509. /* Non-member functions and static member functions match targets of type
  6510. "pointer-to-function" or "reference-to-function." Nonstatic member
  6511. functions match targets of type "pointer-to-member-function;" the
  6512. function type of the pointer to member is used to select the member
  6513. function from the set of overloaded member functions.
  6514. So figure out the FUNCTION_TYPE that we want to match against. */
  6515. target_fn_type = static_fn_type (target_type);
  6516. /* If we can find a non-template function that matches, we can just
  6517. use it. There's no point in generating template instantiations
  6518. if we're just going to throw them out anyhow. But, of course, we
  6519. can only do this when we don't *need* a template function. */
  6520. if (!template_only)
  6521. {
  6522. tree fns;
  6523. for (fns = overload; fns; fns = OVL_NEXT (fns))
  6524. {
  6525. tree fn = OVL_CURRENT (fns);
  6526. if (TREE_CODE (fn) == TEMPLATE_DECL)
  6527. /* We're not looking for templates just yet. */
  6528. continue;
  6529. if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
  6530. != is_ptrmem)
  6531. /* We're looking for a non-static member, and this isn't
  6532. one, or vice versa. */
  6533. continue;
  6534. /* Ignore functions which haven't been explicitly
  6535. declared. */
  6536. if (DECL_ANTICIPATED (fn))
  6537. continue;
  6538. /* See if there's a match. */
  6539. if (same_type_p (target_fn_type, static_fn_type (fn)))
  6540. matches = tree_cons (fn, NULL_TREE, matches);
  6541. }
  6542. }
  6543. /* Now, if we've already got a match (or matches), there's no need
  6544. to proceed to the template functions. But, if we don't have a
  6545. match we need to look at them, too. */
  6546. if (!matches)
  6547. {
  6548. tree target_arg_types;
  6549. tree target_ret_type;
  6550. tree fns;
  6551. tree *args;
  6552. unsigned int nargs, ia;
  6553. tree arg;
  6554. target_arg_types = TYPE_ARG_TYPES (target_fn_type);
  6555. target_ret_type = TREE_TYPE (target_fn_type);
  6556. nargs = list_length (target_arg_types);
  6557. args = XALLOCAVEC (tree, nargs);
  6558. for (arg = target_arg_types, ia = 0;
  6559. arg != NULL_TREE && arg != void_list_node;
  6560. arg = TREE_CHAIN (arg), ++ia)
  6561. args[ia] = TREE_VALUE (arg);
  6562. nargs = ia;
  6563. for (fns = overload; fns; fns = OVL_NEXT (fns))
  6564. {
  6565. tree fn = OVL_CURRENT (fns);
  6566. tree instantiation;
  6567. tree targs;
  6568. if (TREE_CODE (fn) != TEMPLATE_DECL)
  6569. /* We're only looking for templates. */
  6570. continue;
  6571. if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
  6572. != is_ptrmem)
  6573. /* We're not looking for a non-static member, and this is
  6574. one, or vice versa. */
  6575. continue;
  6576. tree ret = target_ret_type;
  6577. /* If the template has a deduced return type, don't expose it to
  6578. template argument deduction. */
  6579. if (undeduced_auto_decl (fn))
  6580. ret = NULL_TREE;
  6581. /* Try to do argument deduction. */
  6582. targs = make_tree_vec (DECL_NTPARMS (fn));
  6583. instantiation = fn_type_unification (fn, explicit_targs, targs, args,
  6584. nargs, ret,
  6585. DEDUCE_EXACT, LOOKUP_NORMAL,
  6586. false, false);
  6587. if (instantiation == error_mark_node)
  6588. /* Instantiation failed. */
  6589. continue;
  6590. /* And now force instantiation to do return type deduction. */
  6591. if (undeduced_auto_decl (instantiation))
  6592. {
  6593. ++function_depth;
  6594. instantiate_decl (instantiation, /*defer*/false, /*class*/false);
  6595. --function_depth;
  6596. require_deduced_type (instantiation);
  6597. }
  6598. /* See if there's a match. */
  6599. if (same_type_p (target_fn_type, static_fn_type (instantiation)))
  6600. matches = tree_cons (instantiation, fn, matches);
  6601. }
  6602. /* Now, remove all but the most specialized of the matches. */
  6603. if (matches)
  6604. {
  6605. tree match = most_specialized_instantiation (matches);
  6606. if (match != error_mark_node)
  6607. matches = tree_cons (TREE_PURPOSE (match),
  6608. NULL_TREE,
  6609. NULL_TREE);
  6610. }
  6611. }
  6612. /* Now we should have exactly one function in MATCHES. */
  6613. if (matches == NULL_TREE)
  6614. {
  6615. /* There were *no* matches. */
  6616. if (flags & tf_error)
  6617. {
  6618. error ("no matches converting function %qD to type %q#T",
  6619. DECL_NAME (OVL_CURRENT (overload)),
  6620. target_type);
  6621. print_candidates (overload);
  6622. }
  6623. return error_mark_node;
  6624. }
  6625. else if (TREE_CHAIN (matches))
  6626. {
  6627. /* There were too many matches. First check if they're all
  6628. the same function. */
  6629. tree match = NULL_TREE;
  6630. fn = TREE_PURPOSE (matches);
  6631. /* For multi-versioned functions, more than one match is just fine and
  6632. decls_match will return false as they are different. */
  6633. for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
  6634. if (!decls_match (fn, TREE_PURPOSE (match))
  6635. && !targetm.target_option.function_versions
  6636. (fn, TREE_PURPOSE (match)))
  6637. break;
  6638. if (match)
  6639. {
  6640. if (flags & tf_error)
  6641. {
  6642. error ("converting overloaded function %qD to type %q#T is ambiguous",
  6643. DECL_NAME (OVL_FUNCTION (overload)),
  6644. target_type);
  6645. /* Since print_candidates expects the functions in the
  6646. TREE_VALUE slot, we flip them here. */
  6647. for (match = matches; match; match = TREE_CHAIN (match))
  6648. TREE_VALUE (match) = TREE_PURPOSE (match);
  6649. print_candidates (matches);
  6650. }
  6651. return error_mark_node;
  6652. }
  6653. }
  6654. /* Good, exactly one match. Now, convert it to the correct type. */
  6655. fn = TREE_PURPOSE (matches);
  6656. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
  6657. && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
  6658. {
  6659. static int explained;
  6660. if (!(flags & tf_error))
  6661. return error_mark_node;
  6662. permerror (input_location, "assuming pointer to member %qD", fn);
  6663. if (!explained)
  6664. {
  6665. inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
  6666. explained = 1;
  6667. }
  6668. }
  6669. /* If a pointer to a function that is multi-versioned is requested, the
  6670. pointer to the dispatcher function is returned instead. This works
  6671. well because indirectly calling the function will dispatch the right
  6672. function version at run-time. */
  6673. if (DECL_FUNCTION_VERSIONED (fn))
  6674. {
  6675. fn = get_function_version_dispatcher (fn);
  6676. if (fn == NULL)
  6677. return error_mark_node;
  6678. /* Mark all the versions corresponding to the dispatcher as used. */
  6679. if (!(flags & tf_conv))
  6680. mark_versions_used (fn);
  6681. }
  6682. /* If we're doing overload resolution purely for the purpose of
  6683. determining conversion sequences, we should not consider the
  6684. function used. If this conversion sequence is selected, the
  6685. function will be marked as used at this point. */
  6686. if (!(flags & tf_conv))
  6687. {
  6688. /* Make =delete work with SFINAE. */
  6689. if (DECL_DELETED_FN (fn) && !(flags & tf_error))
  6690. return error_mark_node;
  6691. mark_used (fn);
  6692. }
  6693. /* We could not check access to member functions when this
  6694. expression was originally created since we did not know at that
  6695. time to which function the expression referred. */
  6696. if (DECL_FUNCTION_MEMBER_P (fn))
  6697. {
  6698. gcc_assert (access_path);
  6699. perform_or_defer_access_check (access_path, fn, fn, flags);
  6700. }
  6701. if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
  6702. return cp_build_addr_expr (fn, flags);
  6703. else
  6704. {
  6705. /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
  6706. will mark the function as addressed, but here we must do it
  6707. explicitly. */
  6708. cxx_mark_addressable (fn);
  6709. return fn;
  6710. }
  6711. }
  6712. /* This function will instantiate the type of the expression given in
  6713. RHS to match the type of LHSTYPE. If errors exist, then return
  6714. error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
  6715. we complain on errors. If we are not complaining, never modify rhs,
  6716. as overload resolution wants to try many possible instantiations, in
  6717. the hope that at least one will work.
  6718. For non-recursive calls, LHSTYPE should be a function, pointer to
  6719. function, or a pointer to member function. */
  6720. tree
  6721. instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
  6722. {
  6723. tsubst_flags_t flags_in = flags;
  6724. tree access_path = NULL_TREE;
  6725. flags &= ~tf_ptrmem_ok;
  6726. if (lhstype == unknown_type_node)
  6727. {
  6728. if (flags & tf_error)
  6729. error ("not enough type information");
  6730. return error_mark_node;
  6731. }
  6732. if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
  6733. {
  6734. tree fntype = non_reference (lhstype);
  6735. if (same_type_p (fntype, TREE_TYPE (rhs)))
  6736. return rhs;
  6737. if (flag_ms_extensions
  6738. && TYPE_PTRMEMFUNC_P (fntype)
  6739. && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
  6740. /* Microsoft allows `A::f' to be resolved to a
  6741. pointer-to-member. */
  6742. ;
  6743. else
  6744. {
  6745. if (flags & tf_error)
  6746. error ("cannot convert %qE from type %qT to type %qT",
  6747. rhs, TREE_TYPE (rhs), fntype);
  6748. return error_mark_node;
  6749. }
  6750. }
  6751. if (BASELINK_P (rhs))
  6752. {
  6753. access_path = BASELINK_ACCESS_BINFO (rhs);
  6754. rhs = BASELINK_FUNCTIONS (rhs);
  6755. }
  6756. /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
  6757. deduce any type information. */
  6758. if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
  6759. {
  6760. if (flags & tf_error)
  6761. error ("not enough type information");
  6762. return error_mark_node;
  6763. }
  6764. /* There only a few kinds of expressions that may have a type
  6765. dependent on overload resolution. */
  6766. gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
  6767. || TREE_CODE (rhs) == COMPONENT_REF
  6768. || is_overloaded_fn (rhs)
  6769. || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
  6770. /* This should really only be used when attempting to distinguish
  6771. what sort of a pointer to function we have. For now, any
  6772. arithmetic operation which is not supported on pointers
  6773. is rejected as an error. */
  6774. switch (TREE_CODE (rhs))
  6775. {
  6776. case COMPONENT_REF:
  6777. {
  6778. tree member = TREE_OPERAND (rhs, 1);
  6779. member = instantiate_type (lhstype, member, flags);
  6780. if (member != error_mark_node
  6781. && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
  6782. /* Do not lose object's side effects. */
  6783. return build2 (COMPOUND_EXPR, TREE_TYPE (member),
  6784. TREE_OPERAND (rhs, 0), member);
  6785. return member;
  6786. }
  6787. case OFFSET_REF:
  6788. rhs = TREE_OPERAND (rhs, 1);
  6789. if (BASELINK_P (rhs))
  6790. return instantiate_type (lhstype, rhs, flags_in);
  6791. /* This can happen if we are forming a pointer-to-member for a
  6792. member template. */
  6793. gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
  6794. /* Fall through. */
  6795. case TEMPLATE_ID_EXPR:
  6796. {
  6797. tree fns = TREE_OPERAND (rhs, 0);
  6798. tree args = TREE_OPERAND (rhs, 1);
  6799. return
  6800. resolve_address_of_overloaded_function (lhstype, fns, flags_in,
  6801. /*template_only=*/true,
  6802. args, access_path);
  6803. }
  6804. case OVERLOAD:
  6805. case FUNCTION_DECL:
  6806. return
  6807. resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
  6808. /*template_only=*/false,
  6809. /*explicit_targs=*/NULL_TREE,
  6810. access_path);
  6811. case ADDR_EXPR:
  6812. {
  6813. if (PTRMEM_OK_P (rhs))
  6814. flags |= tf_ptrmem_ok;
  6815. return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
  6816. }
  6817. case ERROR_MARK:
  6818. return error_mark_node;
  6819. default:
  6820. gcc_unreachable ();
  6821. }
  6822. return error_mark_node;
  6823. }
  6824. /* Return the name of the virtual function pointer field
  6825. (as an IDENTIFIER_NODE) for the given TYPE. Note that
  6826. this may have to look back through base types to find the
  6827. ultimate field name. (For single inheritance, these could
  6828. all be the same name. Who knows for multiple inheritance). */
  6829. static tree
  6830. get_vfield_name (tree type)
  6831. {
  6832. tree binfo, base_binfo;
  6833. char *buf;
  6834. for (binfo = TYPE_BINFO (type);
  6835. BINFO_N_BASE_BINFOS (binfo);
  6836. binfo = base_binfo)
  6837. {
  6838. base_binfo = BINFO_BASE_BINFO (binfo, 0);
  6839. if (BINFO_VIRTUAL_P (base_binfo)
  6840. || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
  6841. break;
  6842. }
  6843. type = BINFO_TYPE (binfo);
  6844. buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
  6845. + TYPE_NAME_LENGTH (type) + 2);
  6846. sprintf (buf, VFIELD_NAME_FORMAT,
  6847. IDENTIFIER_POINTER (constructor_name (type)));
  6848. return get_identifier (buf);
  6849. }
  6850. void
  6851. print_class_statistics (void)
  6852. {
  6853. if (! GATHER_STATISTICS)
  6854. return;
  6855. fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
  6856. fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
  6857. if (n_vtables)
  6858. {
  6859. fprintf (stderr, "vtables = %d; vtable searches = %d\n",
  6860. n_vtables, n_vtable_searches);
  6861. fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
  6862. n_vtable_entries, n_vtable_elems);
  6863. }
  6864. }
  6865. /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
  6866. according to [class]:
  6867. The class-name is also inserted
  6868. into the scope of the class itself. For purposes of access checking,
  6869. the inserted class name is treated as if it were a public member name. */
  6870. void
  6871. build_self_reference (void)
  6872. {
  6873. tree name = constructor_name (current_class_type);
  6874. tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
  6875. tree saved_cas;
  6876. DECL_NONLOCAL (value) = 1;
  6877. DECL_CONTEXT (value) = current_class_type;
  6878. DECL_ARTIFICIAL (value) = 1;
  6879. SET_DECL_SELF_REFERENCE_P (value);
  6880. set_underlying_type (value);
  6881. if (processing_template_decl)
  6882. value = push_template_decl (value);
  6883. saved_cas = current_access_specifier;
  6884. current_access_specifier = access_public_node;
  6885. finish_member_declaration (value);
  6886. current_access_specifier = saved_cas;
  6887. }
  6888. /* Returns 1 if TYPE contains only padding bytes. */
  6889. int
  6890. is_empty_class (tree type)
  6891. {
  6892. if (type == error_mark_node)
  6893. return 0;
  6894. if (! CLASS_TYPE_P (type))
  6895. return 0;
  6896. return CLASSTYPE_EMPTY_P (type);
  6897. }
  6898. /* Returns true if TYPE contains no actual data, just various
  6899. possible combinations of empty classes and possibly a vptr. */
  6900. bool
  6901. is_really_empty_class (tree type)
  6902. {
  6903. if (CLASS_TYPE_P (type))
  6904. {
  6905. tree field;
  6906. tree binfo;
  6907. tree base_binfo;
  6908. int i;
  6909. /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
  6910. out, but we'd like to be able to check this before then. */
  6911. if (COMPLETE_TYPE_P (type) && is_empty_class (type))
  6912. return true;
  6913. for (binfo = TYPE_BINFO (type), i = 0;
  6914. BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
  6915. if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
  6916. return false;
  6917. for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  6918. if (TREE_CODE (field) == FIELD_DECL
  6919. && !DECL_ARTIFICIAL (field)
  6920. && !is_really_empty_class (TREE_TYPE (field)))
  6921. return false;
  6922. return true;
  6923. }
  6924. else if (TREE_CODE (type) == ARRAY_TYPE)
  6925. return is_really_empty_class (TREE_TYPE (type));
  6926. return false;
  6927. }
  6928. /* Note that NAME was looked up while the current class was being
  6929. defined and that the result of that lookup was DECL. */
  6930. void
  6931. maybe_note_name_used_in_class (tree name, tree decl)
  6932. {
  6933. splay_tree names_used;
  6934. /* If we're not defining a class, there's nothing to do. */
  6935. if (!(innermost_scope_kind() == sk_class
  6936. && TYPE_BEING_DEFINED (current_class_type)
  6937. && !LAMBDA_TYPE_P (current_class_type)))
  6938. return;
  6939. /* If there's already a binding for this NAME, then we don't have
  6940. anything to worry about. */
  6941. if (lookup_member (current_class_type, name,
  6942. /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
  6943. return;
  6944. if (!current_class_stack[current_class_depth - 1].names_used)
  6945. current_class_stack[current_class_depth - 1].names_used
  6946. = splay_tree_new (splay_tree_compare_pointers, 0, 0);
  6947. names_used = current_class_stack[current_class_depth - 1].names_used;
  6948. splay_tree_insert (names_used,
  6949. (splay_tree_key) name,
  6950. (splay_tree_value) decl);
  6951. }
  6952. /* Note that NAME was declared (as DECL) in the current class. Check
  6953. to see that the declaration is valid. */
  6954. void
  6955. note_name_declared_in_class (tree name, tree decl)
  6956. {
  6957. splay_tree names_used;
  6958. splay_tree_node n;
  6959. /* Look to see if we ever used this name. */
  6960. names_used
  6961. = current_class_stack[current_class_depth - 1].names_used;
  6962. if (!names_used)
  6963. return;
  6964. /* The C language allows members to be declared with a type of the same
  6965. name, and the C++ standard says this diagnostic is not required. So
  6966. allow it in extern "C" blocks unless predantic is specified.
  6967. Allow it in all cases if -ms-extensions is specified. */
  6968. if ((!pedantic && current_lang_name == lang_name_c)
  6969. || flag_ms_extensions)
  6970. return;
  6971. n = splay_tree_lookup (names_used, (splay_tree_key) name);
  6972. if (n)
  6973. {
  6974. /* [basic.scope.class]
  6975. A name N used in a class S shall refer to the same declaration
  6976. in its context and when re-evaluated in the completed scope of
  6977. S. */
  6978. permerror (input_location, "declaration of %q#D", decl);
  6979. permerror (input_location, "changes meaning of %qD from %q+#D",
  6980. DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
  6981. }
  6982. }
  6983. /* Returns the VAR_DECL for the complete vtable associated with BINFO.
  6984. Secondary vtables are merged with primary vtables; this function
  6985. will return the VAR_DECL for the primary vtable. */
  6986. tree
  6987. get_vtbl_decl_for_binfo (tree binfo)
  6988. {
  6989. tree decl;
  6990. decl = BINFO_VTABLE (binfo);
  6991. if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
  6992. {
  6993. gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
  6994. decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
  6995. }
  6996. if (decl)
  6997. gcc_assert (VAR_P (decl));
  6998. return decl;
  6999. }
  7000. /* Returns the binfo for the primary base of BINFO. If the resulting
  7001. BINFO is a virtual base, and it is inherited elsewhere in the
  7002. hierarchy, then the returned binfo might not be the primary base of
  7003. BINFO in the complete object. Check BINFO_PRIMARY_P or
  7004. BINFO_LOST_PRIMARY_P to be sure. */
  7005. static tree
  7006. get_primary_binfo (tree binfo)
  7007. {
  7008. tree primary_base;
  7009. primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
  7010. if (!primary_base)
  7011. return NULL_TREE;
  7012. return copied_binfo (primary_base, binfo);
  7013. }
  7014. /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
  7015. static int
  7016. maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
  7017. {
  7018. if (!indented_p)
  7019. fprintf (stream, "%*s", indent, "");
  7020. return 1;
  7021. }
  7022. /* Dump the offsets of all the bases rooted at BINFO to STREAM.
  7023. INDENT should be zero when called from the top level; it is
  7024. incremented recursively. IGO indicates the next expected BINFO in
  7025. inheritance graph ordering. */
  7026. static tree
  7027. dump_class_hierarchy_r (FILE *stream,
  7028. int flags,
  7029. tree binfo,
  7030. tree igo,
  7031. int indent)
  7032. {
  7033. int indented = 0;
  7034. tree base_binfo;
  7035. int i;
  7036. indented = maybe_indent_hierarchy (stream, indent, 0);
  7037. fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
  7038. type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
  7039. (HOST_WIDE_INT) (uintptr_t) binfo);
  7040. if (binfo != igo)
  7041. {
  7042. fprintf (stream, "alternative-path\n");
  7043. return igo;
  7044. }
  7045. igo = TREE_CHAIN (binfo);
  7046. fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
  7047. tree_to_shwi (BINFO_OFFSET (binfo)));
  7048. if (is_empty_class (BINFO_TYPE (binfo)))
  7049. fprintf (stream, " empty");
  7050. else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
  7051. fprintf (stream, " nearly-empty");
  7052. if (BINFO_VIRTUAL_P (binfo))
  7053. fprintf (stream, " virtual");
  7054. fprintf (stream, "\n");
  7055. indented = 0;
  7056. if (BINFO_PRIMARY_P (binfo))
  7057. {
  7058. indented = maybe_indent_hierarchy (stream, indent + 3, indented);
  7059. fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
  7060. type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
  7061. TFF_PLAIN_IDENTIFIER),
  7062. (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
  7063. }
  7064. if (BINFO_LOST_PRIMARY_P (binfo))
  7065. {
  7066. indented = maybe_indent_hierarchy (stream, indent + 3, indented);
  7067. fprintf (stream, " lost-primary");
  7068. }
  7069. if (indented)
  7070. fprintf (stream, "\n");
  7071. if (!(flags & TDF_SLIM))
  7072. {
  7073. int indented = 0;
  7074. if (BINFO_SUBVTT_INDEX (binfo))
  7075. {
  7076. indented = maybe_indent_hierarchy (stream, indent + 3, indented);
  7077. fprintf (stream, " subvttidx=%s",
  7078. expr_as_string (BINFO_SUBVTT_INDEX (binfo),
  7079. TFF_PLAIN_IDENTIFIER));
  7080. }
  7081. if (BINFO_VPTR_INDEX (binfo))
  7082. {
  7083. indented = maybe_indent_hierarchy (stream, indent + 3, indented);
  7084. fprintf (stream, " vptridx=%s",
  7085. expr_as_string (BINFO_VPTR_INDEX (binfo),
  7086. TFF_PLAIN_IDENTIFIER));
  7087. }
  7088. if (BINFO_VPTR_FIELD (binfo))
  7089. {
  7090. indented = maybe_indent_hierarchy (stream, indent + 3, indented);
  7091. fprintf (stream, " vbaseoffset=%s",
  7092. expr_as_string (BINFO_VPTR_FIELD (binfo),
  7093. TFF_PLAIN_IDENTIFIER));
  7094. }
  7095. if (BINFO_VTABLE (binfo))
  7096. {
  7097. indented = maybe_indent_hierarchy (stream, indent + 3, indented);
  7098. fprintf (stream, " vptr=%s",
  7099. expr_as_string (BINFO_VTABLE (binfo),
  7100. TFF_PLAIN_IDENTIFIER));
  7101. }
  7102. if (indented)
  7103. fprintf (stream, "\n");
  7104. }
  7105. for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  7106. igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
  7107. return igo;
  7108. }
  7109. /* Dump the BINFO hierarchy for T. */
  7110. static void
  7111. dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
  7112. {
  7113. fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
  7114. fprintf (stream, " size=%lu align=%lu\n",
  7115. (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
  7116. (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
  7117. fprintf (stream, " base size=%lu base align=%lu\n",
  7118. (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
  7119. / BITS_PER_UNIT),
  7120. (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
  7121. / BITS_PER_UNIT));
  7122. dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
  7123. fprintf (stream, "\n");
  7124. }
  7125. /* Debug interface to hierarchy dumping. */
  7126. void
  7127. debug_class (tree t)
  7128. {
  7129. dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
  7130. }
  7131. static void
  7132. dump_class_hierarchy (tree t)
  7133. {
  7134. int flags;
  7135. FILE *stream = get_dump_info (TDI_class, &flags);
  7136. if (stream)
  7137. {
  7138. dump_class_hierarchy_1 (stream, flags, t);
  7139. }
  7140. }
  7141. static void
  7142. dump_array (FILE * stream, tree decl)
  7143. {
  7144. tree value;
  7145. unsigned HOST_WIDE_INT ix;
  7146. HOST_WIDE_INT elt;
  7147. tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
  7148. elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
  7149. / BITS_PER_UNIT);
  7150. fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
  7151. fprintf (stream, " %s entries",
  7152. expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
  7153. TFF_PLAIN_IDENTIFIER));
  7154. fprintf (stream, "\n");
  7155. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
  7156. ix, value)
  7157. fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
  7158. expr_as_string (value, TFF_PLAIN_IDENTIFIER));
  7159. }
  7160. static void
  7161. dump_vtable (tree t, tree binfo, tree vtable)
  7162. {
  7163. int flags;
  7164. FILE *stream = get_dump_info (TDI_class, &flags);
  7165. if (!stream)
  7166. return;
  7167. if (!(flags & TDF_SLIM))
  7168. {
  7169. int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
  7170. fprintf (stream, "%s for %s",
  7171. ctor_vtbl_p ? "Construction vtable" : "Vtable",
  7172. type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
  7173. if (ctor_vtbl_p)
  7174. {
  7175. if (!BINFO_VIRTUAL_P (binfo))
  7176. fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
  7177. (HOST_WIDE_INT) (uintptr_t) binfo);
  7178. fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
  7179. }
  7180. fprintf (stream, "\n");
  7181. dump_array (stream, vtable);
  7182. fprintf (stream, "\n");
  7183. }
  7184. }
  7185. static void
  7186. dump_vtt (tree t, tree vtt)
  7187. {
  7188. int flags;
  7189. FILE *stream = get_dump_info (TDI_class, &flags);
  7190. if (!stream)
  7191. return;
  7192. if (!(flags & TDF_SLIM))
  7193. {
  7194. fprintf (stream, "VTT for %s\n",
  7195. type_as_string (t, TFF_PLAIN_IDENTIFIER));
  7196. dump_array (stream, vtt);
  7197. fprintf (stream, "\n");
  7198. }
  7199. }
  7200. /* Dump a function or thunk and its thunkees. */
  7201. static void
  7202. dump_thunk (FILE *stream, int indent, tree thunk)
  7203. {
  7204. static const char spaces[] = " ";
  7205. tree name = DECL_NAME (thunk);
  7206. tree thunks;
  7207. fprintf (stream, "%.*s%p %s %s", indent, spaces,
  7208. (void *)thunk,
  7209. !DECL_THUNK_P (thunk) ? "function"
  7210. : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
  7211. name ? IDENTIFIER_POINTER (name) : "<unset>");
  7212. if (DECL_THUNK_P (thunk))
  7213. {
  7214. HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
  7215. tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
  7216. fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
  7217. if (!virtual_adjust)
  7218. /*NOP*/;
  7219. else if (DECL_THIS_THUNK_P (thunk))
  7220. fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
  7221. tree_to_shwi (virtual_adjust));
  7222. else
  7223. fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
  7224. tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
  7225. type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
  7226. if (THUNK_ALIAS (thunk))
  7227. fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
  7228. }
  7229. fprintf (stream, "\n");
  7230. for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
  7231. dump_thunk (stream, indent + 2, thunks);
  7232. }
  7233. /* Dump the thunks for FN. */
  7234. void
  7235. debug_thunks (tree fn)
  7236. {
  7237. dump_thunk (stderr, 0, fn);
  7238. }
  7239. /* Virtual function table initialization. */
  7240. /* Create all the necessary vtables for T and its base classes. */
  7241. static void
  7242. finish_vtbls (tree t)
  7243. {
  7244. tree vbase;
  7245. vec<constructor_elt, va_gc> *v = NULL;
  7246. tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
  7247. /* We lay out the primary and secondary vtables in one contiguous
  7248. vtable. The primary vtable is first, followed by the non-virtual
  7249. secondary vtables in inheritance graph order. */
  7250. accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
  7251. vtable, t, &v);
  7252. /* Then come the virtual bases, also in inheritance graph order. */
  7253. for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
  7254. {
  7255. if (!BINFO_VIRTUAL_P (vbase))
  7256. continue;
  7257. accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
  7258. }
  7259. if (BINFO_VTABLE (TYPE_BINFO (t)))
  7260. initialize_vtable (TYPE_BINFO (t), v);
  7261. }
  7262. /* Initialize the vtable for BINFO with the INITS. */
  7263. static void
  7264. initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
  7265. {
  7266. tree decl;
  7267. layout_vtable_decl (binfo, vec_safe_length (inits));
  7268. decl = get_vtbl_decl_for_binfo (binfo);
  7269. initialize_artificial_var (decl, inits);
  7270. dump_vtable (BINFO_TYPE (binfo), binfo, decl);
  7271. }
  7272. /* Build the VTT (virtual table table) for T.
  7273. A class requires a VTT if it has virtual bases.
  7274. This holds
  7275. 1 - primary virtual pointer for complete object T
  7276. 2 - secondary VTTs for each direct non-virtual base of T which requires a
  7277. VTT
  7278. 3 - secondary virtual pointers for each direct or indirect base of T which
  7279. has virtual bases or is reachable via a virtual path from T.
  7280. 4 - secondary VTTs for each direct or indirect virtual base of T.
  7281. Secondary VTTs look like complete object VTTs without part 4. */
  7282. static void
  7283. build_vtt (tree t)
  7284. {
  7285. tree type;
  7286. tree vtt;
  7287. tree index;
  7288. vec<constructor_elt, va_gc> *inits;
  7289. /* Build up the initializers for the VTT. */
  7290. inits = NULL;
  7291. index = size_zero_node;
  7292. build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
  7293. /* If we didn't need a VTT, we're done. */
  7294. if (!inits)
  7295. return;
  7296. /* Figure out the type of the VTT. */
  7297. type = build_array_of_n_type (const_ptr_type_node,
  7298. inits->length ());
  7299. /* Now, build the VTT object itself. */
  7300. vtt = build_vtable (t, mangle_vtt_for_type (t), type);
  7301. initialize_artificial_var (vtt, inits);
  7302. /* Add the VTT to the vtables list. */
  7303. DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
  7304. DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
  7305. dump_vtt (t, vtt);
  7306. }
  7307. /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
  7308. PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
  7309. and CHAIN the vtable pointer for this binfo after construction is
  7310. complete. VALUE can also be another BINFO, in which case we recurse. */
  7311. static tree
  7312. binfo_ctor_vtable (tree binfo)
  7313. {
  7314. tree vt;
  7315. while (1)
  7316. {
  7317. vt = BINFO_VTABLE (binfo);
  7318. if (TREE_CODE (vt) == TREE_LIST)
  7319. vt = TREE_VALUE (vt);
  7320. if (TREE_CODE (vt) == TREE_BINFO)
  7321. binfo = vt;
  7322. else
  7323. break;
  7324. }
  7325. return vt;
  7326. }
  7327. /* Data for secondary VTT initialization. */
  7328. typedef struct secondary_vptr_vtt_init_data_s
  7329. {
  7330. /* Is this the primary VTT? */
  7331. bool top_level_p;
  7332. /* Current index into the VTT. */
  7333. tree index;
  7334. /* Vector of initializers built up. */
  7335. vec<constructor_elt, va_gc> *inits;
  7336. /* The type being constructed by this secondary VTT. */
  7337. tree type_being_constructed;
  7338. } secondary_vptr_vtt_init_data;
  7339. /* Recursively build the VTT-initializer for BINFO (which is in the
  7340. hierarchy dominated by T). INITS points to the end of the initializer
  7341. list to date. INDEX is the VTT index where the next element will be
  7342. replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
  7343. not a subvtt for some base of T). When that is so, we emit the sub-VTTs
  7344. for virtual bases of T. When it is not so, we build the constructor
  7345. vtables for the BINFO-in-T variant. */
  7346. static void
  7347. build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
  7348. tree *index)
  7349. {
  7350. int i;
  7351. tree b;
  7352. tree init;
  7353. secondary_vptr_vtt_init_data data;
  7354. int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
  7355. /* We only need VTTs for subobjects with virtual bases. */
  7356. if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
  7357. return;
  7358. /* We need to use a construction vtable if this is not the primary
  7359. VTT. */
  7360. if (!top_level_p)
  7361. {
  7362. build_ctor_vtbl_group (binfo, t);
  7363. /* Record the offset in the VTT where this sub-VTT can be found. */
  7364. BINFO_SUBVTT_INDEX (binfo) = *index;
  7365. }
  7366. /* Add the address of the primary vtable for the complete object. */
  7367. init = binfo_ctor_vtable (binfo);
  7368. CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
  7369. if (top_level_p)
  7370. {
  7371. gcc_assert (!BINFO_VPTR_INDEX (binfo));
  7372. BINFO_VPTR_INDEX (binfo) = *index;
  7373. }
  7374. *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
  7375. /* Recursively add the secondary VTTs for non-virtual bases. */
  7376. for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
  7377. if (!BINFO_VIRTUAL_P (b))
  7378. build_vtt_inits (b, t, inits, index);
  7379. /* Add secondary virtual pointers for all subobjects of BINFO with
  7380. either virtual bases or reachable along a virtual path, except
  7381. subobjects that are non-virtual primary bases. */
  7382. data.top_level_p = top_level_p;
  7383. data.index = *index;
  7384. data.inits = *inits;
  7385. data.type_being_constructed = BINFO_TYPE (binfo);
  7386. dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
  7387. *index = data.index;
  7388. /* data.inits might have grown as we added secondary virtual pointers.
  7389. Make sure our caller knows about the new vector. */
  7390. *inits = data.inits;
  7391. if (top_level_p)
  7392. /* Add the secondary VTTs for virtual bases in inheritance graph
  7393. order. */
  7394. for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
  7395. {
  7396. if (!BINFO_VIRTUAL_P (b))
  7397. continue;
  7398. build_vtt_inits (b, t, inits, index);
  7399. }
  7400. else
  7401. /* Remove the ctor vtables we created. */
  7402. dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
  7403. }
  7404. /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
  7405. in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
  7406. static tree
  7407. dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
  7408. {
  7409. secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
  7410. /* We don't care about bases that don't have vtables. */
  7411. if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
  7412. return dfs_skip_bases;
  7413. /* We're only interested in proper subobjects of the type being
  7414. constructed. */
  7415. if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
  7416. return NULL_TREE;
  7417. /* We're only interested in bases with virtual bases or reachable
  7418. via a virtual path from the type being constructed. */
  7419. if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
  7420. || binfo_via_virtual (binfo, data->type_being_constructed)))
  7421. return dfs_skip_bases;
  7422. /* We're not interested in non-virtual primary bases. */
  7423. if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
  7424. return NULL_TREE;
  7425. /* Record the index where this secondary vptr can be found. */
  7426. if (data->top_level_p)
  7427. {
  7428. gcc_assert (!BINFO_VPTR_INDEX (binfo));
  7429. BINFO_VPTR_INDEX (binfo) = data->index;
  7430. if (BINFO_VIRTUAL_P (binfo))
  7431. {
  7432. /* It's a primary virtual base, and this is not a
  7433. construction vtable. Find the base this is primary of in
  7434. the inheritance graph, and use that base's vtable
  7435. now. */
  7436. while (BINFO_PRIMARY_P (binfo))
  7437. binfo = BINFO_INHERITANCE_CHAIN (binfo);
  7438. }
  7439. }
  7440. /* Add the initializer for the secondary vptr itself. */
  7441. CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
  7442. /* Advance the vtt index. */
  7443. data->index = size_binop (PLUS_EXPR, data->index,
  7444. TYPE_SIZE_UNIT (ptr_type_node));
  7445. return NULL_TREE;
  7446. }
  7447. /* Called from build_vtt_inits via dfs_walk. After building
  7448. constructor vtables and generating the sub-vtt from them, we need
  7449. to restore the BINFO_VTABLES that were scribbled on. DATA is the
  7450. binfo of the base whose sub vtt was generated. */
  7451. static tree
  7452. dfs_fixup_binfo_vtbls (tree binfo, void* data)
  7453. {
  7454. tree vtable = BINFO_VTABLE (binfo);
  7455. if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
  7456. /* If this class has no vtable, none of its bases do. */
  7457. return dfs_skip_bases;
  7458. if (!vtable)
  7459. /* This might be a primary base, so have no vtable in this
  7460. hierarchy. */
  7461. return NULL_TREE;
  7462. /* If we scribbled the construction vtable vptr into BINFO, clear it
  7463. out now. */
  7464. if (TREE_CODE (vtable) == TREE_LIST
  7465. && (TREE_PURPOSE (vtable) == (tree) data))
  7466. BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
  7467. return NULL_TREE;
  7468. }
  7469. /* Build the construction vtable group for BINFO which is in the
  7470. hierarchy dominated by T. */
  7471. static void
  7472. build_ctor_vtbl_group (tree binfo, tree t)
  7473. {
  7474. tree type;
  7475. tree vtbl;
  7476. tree id;
  7477. tree vbase;
  7478. vec<constructor_elt, va_gc> *v;
  7479. /* See if we've already created this construction vtable group. */
  7480. id = mangle_ctor_vtbl_for_type (t, binfo);
  7481. if (IDENTIFIER_GLOBAL_VALUE (id))
  7482. return;
  7483. gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
  7484. /* Build a version of VTBL (with the wrong type) for use in
  7485. constructing the addresses of secondary vtables in the
  7486. construction vtable group. */
  7487. vtbl = build_vtable (t, id, ptr_type_node);
  7488. DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
  7489. /* Don't export construction vtables from shared libraries. Even on
  7490. targets that don't support hidden visibility, this tells
  7491. can_refer_decl_in_current_unit_p not to assume that it's safe to
  7492. access from a different compilation unit (bz 54314). */
  7493. DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
  7494. DECL_VISIBILITY_SPECIFIED (vtbl) = true;
  7495. v = NULL;
  7496. accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
  7497. binfo, vtbl, t, &v);
  7498. /* Add the vtables for each of our virtual bases using the vbase in T
  7499. binfo. */
  7500. for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
  7501. vbase;
  7502. vbase = TREE_CHAIN (vbase))
  7503. {
  7504. tree b;
  7505. if (!BINFO_VIRTUAL_P (vbase))
  7506. continue;
  7507. b = copied_binfo (vbase, binfo);
  7508. accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
  7509. }
  7510. /* Figure out the type of the construction vtable. */
  7511. type = build_array_of_n_type (vtable_entry_type, v->length ());
  7512. layout_type (type);
  7513. TREE_TYPE (vtbl) = type;
  7514. DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
  7515. layout_decl (vtbl, 0);
  7516. /* Initialize the construction vtable. */
  7517. CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
  7518. initialize_artificial_var (vtbl, v);
  7519. dump_vtable (t, binfo, vtbl);
  7520. }
  7521. /* Add the vtbl initializers for BINFO (and its bases other than
  7522. non-virtual primaries) to the list of INITS. BINFO is in the
  7523. hierarchy dominated by T. RTTI_BINFO is the binfo within T of
  7524. the constructor the vtbl inits should be accumulated for. (If this
  7525. is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
  7526. ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
  7527. BINFO is the active base equivalent of ORIG_BINFO in the inheritance
  7528. graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
  7529. but are not necessarily the same in terms of layout. */
  7530. static void
  7531. accumulate_vtbl_inits (tree binfo,
  7532. tree orig_binfo,
  7533. tree rtti_binfo,
  7534. tree vtbl,
  7535. tree t,
  7536. vec<constructor_elt, va_gc> **inits)
  7537. {
  7538. int i;
  7539. tree base_binfo;
  7540. int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
  7541. gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
  7542. /* If it doesn't have a vptr, we don't do anything. */
  7543. if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
  7544. return;
  7545. /* If we're building a construction vtable, we're not interested in
  7546. subobjects that don't require construction vtables. */
  7547. if (ctor_vtbl_p
  7548. && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
  7549. && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
  7550. return;
  7551. /* Build the initializers for the BINFO-in-T vtable. */
  7552. dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
  7553. /* Walk the BINFO and its bases. We walk in preorder so that as we
  7554. initialize each vtable we can figure out at what offset the
  7555. secondary vtable lies from the primary vtable. We can't use
  7556. dfs_walk here because we need to iterate through bases of BINFO
  7557. and RTTI_BINFO simultaneously. */
  7558. for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
  7559. {
  7560. /* Skip virtual bases. */
  7561. if (BINFO_VIRTUAL_P (base_binfo))
  7562. continue;
  7563. accumulate_vtbl_inits (base_binfo,
  7564. BINFO_BASE_BINFO (orig_binfo, i),
  7565. rtti_binfo, vtbl, t,
  7566. inits);
  7567. }
  7568. }
  7569. /* Called from accumulate_vtbl_inits. Adds the initializers for the
  7570. BINFO vtable to L. */
  7571. static void
  7572. dfs_accumulate_vtbl_inits (tree binfo,
  7573. tree orig_binfo,
  7574. tree rtti_binfo,
  7575. tree orig_vtbl,
  7576. tree t,
  7577. vec<constructor_elt, va_gc> **l)
  7578. {
  7579. tree vtbl = NULL_TREE;
  7580. int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
  7581. int n_inits;
  7582. if (ctor_vtbl_p
  7583. && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
  7584. {
  7585. /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
  7586. primary virtual base. If it is not the same primary in
  7587. the hierarchy of T, we'll need to generate a ctor vtable
  7588. for it, to place at its location in T. If it is the same
  7589. primary, we still need a VTT entry for the vtable, but it
  7590. should point to the ctor vtable for the base it is a
  7591. primary for within the sub-hierarchy of RTTI_BINFO.
  7592. There are three possible cases:
  7593. 1) We are in the same place.
  7594. 2) We are a primary base within a lost primary virtual base of
  7595. RTTI_BINFO.
  7596. 3) We are primary to something not a base of RTTI_BINFO. */
  7597. tree b;
  7598. tree last = NULL_TREE;
  7599. /* First, look through the bases we are primary to for RTTI_BINFO
  7600. or a virtual base. */
  7601. b = binfo;
  7602. while (BINFO_PRIMARY_P (b))
  7603. {
  7604. b = BINFO_INHERITANCE_CHAIN (b);
  7605. last = b;
  7606. if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
  7607. goto found;
  7608. }
  7609. /* If we run out of primary links, keep looking down our
  7610. inheritance chain; we might be an indirect primary. */
  7611. for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
  7612. if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
  7613. break;
  7614. found:
  7615. /* If we found RTTI_BINFO, this is case 1. If we found a virtual
  7616. base B and it is a base of RTTI_BINFO, this is case 2. In
  7617. either case, we share our vtable with LAST, i.e. the
  7618. derived-most base within B of which we are a primary. */
  7619. if (b == rtti_binfo
  7620. || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
  7621. /* Just set our BINFO_VTABLE to point to LAST, as we may not have
  7622. set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
  7623. binfo_ctor_vtable after everything's been set up. */
  7624. vtbl = last;
  7625. /* Otherwise, this is case 3 and we get our own. */
  7626. }
  7627. else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
  7628. return;
  7629. n_inits = vec_safe_length (*l);
  7630. if (!vtbl)
  7631. {
  7632. tree index;
  7633. int non_fn_entries;
  7634. /* Add the initializer for this vtable. */
  7635. build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
  7636. &non_fn_entries, l);
  7637. /* Figure out the position to which the VPTR should point. */
  7638. vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
  7639. index = size_binop (MULT_EXPR,
  7640. TYPE_SIZE_UNIT (vtable_entry_type),
  7641. size_int (non_fn_entries + n_inits));
  7642. vtbl = fold_build_pointer_plus (vtbl, index);
  7643. }
  7644. if (ctor_vtbl_p)
  7645. /* For a construction vtable, we can't overwrite BINFO_VTABLE.
  7646. So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
  7647. straighten this out. */
  7648. BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
  7649. else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
  7650. /* Throw away any unneeded intializers. */
  7651. (*l)->truncate (n_inits);
  7652. else
  7653. /* For an ordinary vtable, set BINFO_VTABLE. */
  7654. BINFO_VTABLE (binfo) = vtbl;
  7655. }
  7656. static GTY(()) tree abort_fndecl_addr;
  7657. /* Construct the initializer for BINFO's virtual function table. BINFO
  7658. is part of the hierarchy dominated by T. If we're building a
  7659. construction vtable, the ORIG_BINFO is the binfo we should use to
  7660. find the actual function pointers to put in the vtable - but they
  7661. can be overridden on the path to most-derived in the graph that
  7662. ORIG_BINFO belongs. Otherwise,
  7663. ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
  7664. BINFO that should be indicated by the RTTI information in the
  7665. vtable; it will be a base class of T, rather than T itself, if we
  7666. are building a construction vtable.
  7667. The value returned is a TREE_LIST suitable for wrapping in a
  7668. CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
  7669. NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
  7670. number of non-function entries in the vtable.
  7671. It might seem that this function should never be called with a
  7672. BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
  7673. base is always subsumed by a derived class vtable. However, when
  7674. we are building construction vtables, we do build vtables for
  7675. primary bases; we need these while the primary base is being
  7676. constructed. */
  7677. static void
  7678. build_vtbl_initializer (tree binfo,
  7679. tree orig_binfo,
  7680. tree t,
  7681. tree rtti_binfo,
  7682. int* non_fn_entries_p,
  7683. vec<constructor_elt, va_gc> **inits)
  7684. {
  7685. tree v;
  7686. vtbl_init_data vid;
  7687. unsigned ix, jx;
  7688. tree vbinfo;
  7689. vec<tree, va_gc> *vbases;
  7690. constructor_elt *e;
  7691. /* Initialize VID. */
  7692. memset (&vid, 0, sizeof (vid));
  7693. vid.binfo = binfo;
  7694. vid.derived = t;
  7695. vid.rtti_binfo = rtti_binfo;
  7696. vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
  7697. vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
  7698. vid.generate_vcall_entries = true;
  7699. /* The first vbase or vcall offset is at index -3 in the vtable. */
  7700. vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
  7701. /* Add entries to the vtable for RTTI. */
  7702. build_rtti_vtbl_entries (binfo, &vid);
  7703. /* Create an array for keeping track of the functions we've
  7704. processed. When we see multiple functions with the same
  7705. signature, we share the vcall offsets. */
  7706. vec_alloc (vid.fns, 32);
  7707. /* Add the vcall and vbase offset entries. */
  7708. build_vcall_and_vbase_vtbl_entries (binfo, &vid);
  7709. /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
  7710. build_vbase_offset_vtbl_entries. */
  7711. for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
  7712. vec_safe_iterate (vbases, ix, &vbinfo); ix++)
  7713. BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
  7714. /* If the target requires padding between data entries, add that now. */
  7715. if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
  7716. {
  7717. int n_entries = vec_safe_length (vid.inits);
  7718. vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
  7719. /* Move data entries into their new positions and add padding
  7720. after the new positions. Iterate backwards so we don't
  7721. overwrite entries that we would need to process later. */
  7722. for (ix = n_entries - 1;
  7723. vid.inits->iterate (ix, &e);
  7724. ix--)
  7725. {
  7726. int j;
  7727. int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
  7728. + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
  7729. (*vid.inits)[new_position] = *e;
  7730. for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
  7731. {
  7732. constructor_elt *f = &(*vid.inits)[new_position - j];
  7733. f->index = NULL_TREE;
  7734. f->value = build1 (NOP_EXPR, vtable_entry_type,
  7735. null_pointer_node);
  7736. }
  7737. }
  7738. }
  7739. if (non_fn_entries_p)
  7740. *non_fn_entries_p = vec_safe_length (vid.inits);
  7741. /* The initializers for virtual functions were built up in reverse
  7742. order. Straighten them out and add them to the running list in one
  7743. step. */
  7744. jx = vec_safe_length (*inits);
  7745. vec_safe_grow (*inits, jx + vid.inits->length ());
  7746. for (ix = vid.inits->length () - 1;
  7747. vid.inits->iterate (ix, &e);
  7748. ix--, jx++)
  7749. (**inits)[jx] = *e;
  7750. /* Go through all the ordinary virtual functions, building up
  7751. initializers. */
  7752. for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
  7753. {
  7754. tree delta;
  7755. tree vcall_index;
  7756. tree fn, fn_original;
  7757. tree init = NULL_TREE;
  7758. fn = BV_FN (v);
  7759. fn_original = fn;
  7760. if (DECL_THUNK_P (fn))
  7761. {
  7762. if (!DECL_NAME (fn))
  7763. finish_thunk (fn);
  7764. if (THUNK_ALIAS (fn))
  7765. {
  7766. fn = THUNK_ALIAS (fn);
  7767. BV_FN (v) = fn;
  7768. }
  7769. fn_original = THUNK_TARGET (fn);
  7770. }
  7771. /* If the only definition of this function signature along our
  7772. primary base chain is from a lost primary, this vtable slot will
  7773. never be used, so just zero it out. This is important to avoid
  7774. requiring extra thunks which cannot be generated with the function.
  7775. We first check this in update_vtable_entry_for_fn, so we handle
  7776. restored primary bases properly; we also need to do it here so we
  7777. zero out unused slots in ctor vtables, rather than filling them
  7778. with erroneous values (though harmless, apart from relocation
  7779. costs). */
  7780. if (BV_LOST_PRIMARY (v))
  7781. init = size_zero_node;
  7782. if (! init)
  7783. {
  7784. /* Pull the offset for `this', and the function to call, out of
  7785. the list. */
  7786. delta = BV_DELTA (v);
  7787. vcall_index = BV_VCALL_INDEX (v);
  7788. gcc_assert (TREE_CODE (delta) == INTEGER_CST);
  7789. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
  7790. /* You can't call an abstract virtual function; it's abstract.
  7791. So, we replace these functions with __pure_virtual. */
  7792. if (DECL_PURE_VIRTUAL_P (fn_original))
  7793. {
  7794. fn = abort_fndecl;
  7795. if (!TARGET_VTABLE_USES_DESCRIPTORS)
  7796. {
  7797. if (abort_fndecl_addr == NULL)
  7798. abort_fndecl_addr
  7799. = fold_convert (vfunc_ptr_type_node,
  7800. build_fold_addr_expr (fn));
  7801. init = abort_fndecl_addr;
  7802. }
  7803. }
  7804. /* Likewise for deleted virtuals. */
  7805. else if (DECL_DELETED_FN (fn_original))
  7806. {
  7807. fn = get_identifier ("__cxa_deleted_virtual");
  7808. if (!get_global_value_if_present (fn, &fn))
  7809. fn = push_library_fn (fn, (build_function_type_list
  7810. (void_type_node, NULL_TREE)),
  7811. NULL_TREE, ECF_NORETURN);
  7812. if (!TARGET_VTABLE_USES_DESCRIPTORS)
  7813. init = fold_convert (vfunc_ptr_type_node,
  7814. build_fold_addr_expr (fn));
  7815. }
  7816. else
  7817. {
  7818. if (!integer_zerop (delta) || vcall_index)
  7819. {
  7820. fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
  7821. if (!DECL_NAME (fn))
  7822. finish_thunk (fn);
  7823. }
  7824. /* Take the address of the function, considering it to be of an
  7825. appropriate generic type. */
  7826. if (!TARGET_VTABLE_USES_DESCRIPTORS)
  7827. init = fold_convert (vfunc_ptr_type_node,
  7828. build_fold_addr_expr (fn));
  7829. /* Don't refer to a virtual destructor from a constructor
  7830. vtable or a vtable for an abstract class, since destroying
  7831. an object under construction is undefined behavior and we
  7832. don't want it to be considered a candidate for speculative
  7833. devirtualization. But do create the thunk for ABI
  7834. compliance. */
  7835. if (DECL_DESTRUCTOR_P (fn_original)
  7836. && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
  7837. || orig_binfo != binfo))
  7838. init = size_zero_node;
  7839. }
  7840. }
  7841. /* And add it to the chain of initializers. */
  7842. if (TARGET_VTABLE_USES_DESCRIPTORS)
  7843. {
  7844. int i;
  7845. if (init == size_zero_node)
  7846. for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
  7847. CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
  7848. else
  7849. for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
  7850. {
  7851. tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
  7852. fn, build_int_cst (NULL_TREE, i));
  7853. TREE_CONSTANT (fdesc) = 1;
  7854. CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
  7855. }
  7856. }
  7857. else
  7858. CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
  7859. }
  7860. }
  7861. /* Adds to vid->inits the initializers for the vbase and vcall
  7862. offsets in BINFO, which is in the hierarchy dominated by T. */
  7863. static void
  7864. build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
  7865. {
  7866. tree b;
  7867. /* If this is a derived class, we must first create entries
  7868. corresponding to the primary base class. */
  7869. b = get_primary_binfo (binfo);
  7870. if (b)
  7871. build_vcall_and_vbase_vtbl_entries (b, vid);
  7872. /* Add the vbase entries for this base. */
  7873. build_vbase_offset_vtbl_entries (binfo, vid);
  7874. /* Add the vcall entries for this base. */
  7875. build_vcall_offset_vtbl_entries (binfo, vid);
  7876. }
  7877. /* Returns the initializers for the vbase offset entries in the vtable
  7878. for BINFO (which is part of the class hierarchy dominated by T), in
  7879. reverse order. VBASE_OFFSET_INDEX gives the vtable index
  7880. where the next vbase offset will go. */
  7881. static void
  7882. build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
  7883. {
  7884. tree vbase;
  7885. tree t;
  7886. tree non_primary_binfo;
  7887. /* If there are no virtual baseclasses, then there is nothing to
  7888. do. */
  7889. if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
  7890. return;
  7891. t = vid->derived;
  7892. /* We might be a primary base class. Go up the inheritance hierarchy
  7893. until we find the most derived class of which we are a primary base:
  7894. it is the offset of that which we need to use. */
  7895. non_primary_binfo = binfo;
  7896. while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
  7897. {
  7898. tree b;
  7899. /* If we have reached a virtual base, then it must be a primary
  7900. base (possibly multi-level) of vid->binfo, or we wouldn't
  7901. have called build_vcall_and_vbase_vtbl_entries for it. But it
  7902. might be a lost primary, so just skip down to vid->binfo. */
  7903. if (BINFO_VIRTUAL_P (non_primary_binfo))
  7904. {
  7905. non_primary_binfo = vid->binfo;
  7906. break;
  7907. }
  7908. b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
  7909. if (get_primary_binfo (b) != non_primary_binfo)
  7910. break;
  7911. non_primary_binfo = b;
  7912. }
  7913. /* Go through the virtual bases, adding the offsets. */
  7914. for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
  7915. vbase;
  7916. vbase = TREE_CHAIN (vbase))
  7917. {
  7918. tree b;
  7919. tree delta;
  7920. if (!BINFO_VIRTUAL_P (vbase))
  7921. continue;
  7922. /* Find the instance of this virtual base in the complete
  7923. object. */
  7924. b = copied_binfo (vbase, binfo);
  7925. /* If we've already got an offset for this virtual base, we
  7926. don't need another one. */
  7927. if (BINFO_VTABLE_PATH_MARKED (b))
  7928. continue;
  7929. BINFO_VTABLE_PATH_MARKED (b) = 1;
  7930. /* Figure out where we can find this vbase offset. */
  7931. delta = size_binop (MULT_EXPR,
  7932. vid->index,
  7933. convert (ssizetype,
  7934. TYPE_SIZE_UNIT (vtable_entry_type)));
  7935. if (vid->primary_vtbl_p)
  7936. BINFO_VPTR_FIELD (b) = delta;
  7937. if (binfo != TYPE_BINFO (t))
  7938. /* The vbase offset had better be the same. */
  7939. gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
  7940. /* The next vbase will come at a more negative offset. */
  7941. vid->index = size_binop (MINUS_EXPR, vid->index,
  7942. ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
  7943. /* The initializer is the delta from BINFO to this virtual base.
  7944. The vbase offsets go in reverse inheritance-graph order, and
  7945. we are walking in inheritance graph order so these end up in
  7946. the right order. */
  7947. delta = size_diffop_loc (input_location,
  7948. BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
  7949. CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
  7950. fold_build1_loc (input_location, NOP_EXPR,
  7951. vtable_entry_type, delta));
  7952. }
  7953. }
  7954. /* Adds the initializers for the vcall offset entries in the vtable
  7955. for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
  7956. to VID->INITS. */
  7957. static void
  7958. build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
  7959. {
  7960. /* We only need these entries if this base is a virtual base. We
  7961. compute the indices -- but do not add to the vtable -- when
  7962. building the main vtable for a class. */
  7963. if (binfo == TYPE_BINFO (vid->derived)
  7964. || (BINFO_VIRTUAL_P (binfo)
  7965. /* If BINFO is RTTI_BINFO, then (since BINFO does not
  7966. correspond to VID->DERIVED), we are building a primary
  7967. construction virtual table. Since this is a primary
  7968. virtual table, we do not need the vcall offsets for
  7969. BINFO. */
  7970. && binfo != vid->rtti_binfo))
  7971. {
  7972. /* We need a vcall offset for each of the virtual functions in this
  7973. vtable. For example:
  7974. class A { virtual void f (); };
  7975. class B1 : virtual public A { virtual void f (); };
  7976. class B2 : virtual public A { virtual void f (); };
  7977. class C: public B1, public B2 { virtual void f (); };
  7978. A C object has a primary base of B1, which has a primary base of A. A
  7979. C also has a secondary base of B2, which no longer has a primary base
  7980. of A. So the B2-in-C construction vtable needs a secondary vtable for
  7981. A, which will adjust the A* to a B2* to call f. We have no way of
  7982. knowing what (or even whether) this offset will be when we define B2,
  7983. so we store this "vcall offset" in the A sub-vtable and look it up in
  7984. a "virtual thunk" for B2::f.
  7985. We need entries for all the functions in our primary vtable and
  7986. in our non-virtual bases' secondary vtables. */
  7987. vid->vbase = binfo;
  7988. /* If we are just computing the vcall indices -- but do not need
  7989. the actual entries -- not that. */
  7990. if (!BINFO_VIRTUAL_P (binfo))
  7991. vid->generate_vcall_entries = false;
  7992. /* Now, walk through the non-virtual bases, adding vcall offsets. */
  7993. add_vcall_offset_vtbl_entries_r (binfo, vid);
  7994. }
  7995. }
  7996. /* Build vcall offsets, starting with those for BINFO. */
  7997. static void
  7998. add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
  7999. {
  8000. int i;
  8001. tree primary_binfo;
  8002. tree base_binfo;
  8003. /* Don't walk into virtual bases -- except, of course, for the
  8004. virtual base for which we are building vcall offsets. Any
  8005. primary virtual base will have already had its offsets generated
  8006. through the recursion in build_vcall_and_vbase_vtbl_entries. */
  8007. if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
  8008. return;
  8009. /* If BINFO has a primary base, process it first. */
  8010. primary_binfo = get_primary_binfo (binfo);
  8011. if (primary_binfo)
  8012. add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
  8013. /* Add BINFO itself to the list. */
  8014. add_vcall_offset_vtbl_entries_1 (binfo, vid);
  8015. /* Scan the non-primary bases of BINFO. */
  8016. for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
  8017. if (base_binfo != primary_binfo)
  8018. add_vcall_offset_vtbl_entries_r (base_binfo, vid);
  8019. }
  8020. /* Called from build_vcall_offset_vtbl_entries_r. */
  8021. static void
  8022. add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
  8023. {
  8024. /* Make entries for the rest of the virtuals. */
  8025. tree orig_fn;
  8026. /* The ABI requires that the methods be processed in declaration
  8027. order. */
  8028. for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
  8029. orig_fn;
  8030. orig_fn = DECL_CHAIN (orig_fn))
  8031. if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
  8032. add_vcall_offset (orig_fn, binfo, vid);
  8033. }
  8034. /* Add a vcall offset entry for ORIG_FN to the vtable. */
  8035. static void
  8036. add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
  8037. {
  8038. size_t i;
  8039. tree vcall_offset;
  8040. tree derived_entry;
  8041. /* If there is already an entry for a function with the same
  8042. signature as FN, then we do not need a second vcall offset.
  8043. Check the list of functions already present in the derived
  8044. class vtable. */
  8045. FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
  8046. {
  8047. if (same_signature_p (derived_entry, orig_fn)
  8048. /* We only use one vcall offset for virtual destructors,
  8049. even though there are two virtual table entries. */
  8050. || (DECL_DESTRUCTOR_P (derived_entry)
  8051. && DECL_DESTRUCTOR_P (orig_fn)))
  8052. return;
  8053. }
  8054. /* If we are building these vcall offsets as part of building
  8055. the vtable for the most derived class, remember the vcall
  8056. offset. */
  8057. if (vid->binfo == TYPE_BINFO (vid->derived))
  8058. {
  8059. tree_pair_s elt = {orig_fn, vid->index};
  8060. vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
  8061. }
  8062. /* The next vcall offset will be found at a more negative
  8063. offset. */
  8064. vid->index = size_binop (MINUS_EXPR, vid->index,
  8065. ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
  8066. /* Keep track of this function. */
  8067. vec_safe_push (vid->fns, orig_fn);
  8068. if (vid->generate_vcall_entries)
  8069. {
  8070. tree base;
  8071. tree fn;
  8072. /* Find the overriding function. */
  8073. fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
  8074. if (fn == error_mark_node)
  8075. vcall_offset = build_zero_cst (vtable_entry_type);
  8076. else
  8077. {
  8078. base = TREE_VALUE (fn);
  8079. /* The vbase we're working on is a primary base of
  8080. vid->binfo. But it might be a lost primary, so its
  8081. BINFO_OFFSET might be wrong, so we just use the
  8082. BINFO_OFFSET from vid->binfo. */
  8083. vcall_offset = size_diffop_loc (input_location,
  8084. BINFO_OFFSET (base),
  8085. BINFO_OFFSET (vid->binfo));
  8086. vcall_offset = fold_build1_loc (input_location,
  8087. NOP_EXPR, vtable_entry_type,
  8088. vcall_offset);
  8089. }
  8090. /* Add the initializer to the vtable. */
  8091. CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
  8092. }
  8093. }
  8094. /* Return vtbl initializers for the RTTI entries corresponding to the
  8095. BINFO's vtable. The RTTI entries should indicate the object given
  8096. by VID->rtti_binfo. */
  8097. static void
  8098. build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
  8099. {
  8100. tree b;
  8101. tree t;
  8102. tree offset;
  8103. tree decl;
  8104. tree init;
  8105. t = BINFO_TYPE (vid->rtti_binfo);
  8106. /* To find the complete object, we will first convert to our most
  8107. primary base, and then add the offset in the vtbl to that value. */
  8108. b = binfo;
  8109. while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
  8110. && !BINFO_LOST_PRIMARY_P (b))
  8111. {
  8112. tree primary_base;
  8113. primary_base = get_primary_binfo (b);
  8114. gcc_assert (BINFO_PRIMARY_P (primary_base)
  8115. && BINFO_INHERITANCE_CHAIN (primary_base) == b);
  8116. b = primary_base;
  8117. }
  8118. offset = size_diffop_loc (input_location,
  8119. BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
  8120. /* The second entry is the address of the typeinfo object. */
  8121. if (flag_rtti)
  8122. decl = build_address (get_tinfo_decl (t));
  8123. else
  8124. decl = integer_zero_node;
  8125. /* Convert the declaration to a type that can be stored in the
  8126. vtable. */
  8127. init = build_nop (vfunc_ptr_type_node, decl);
  8128. CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
  8129. /* Add the offset-to-top entry. It comes earlier in the vtable than
  8130. the typeinfo entry. Convert the offset to look like a
  8131. function pointer, so that we can put it in the vtable. */
  8132. init = build_nop (vfunc_ptr_type_node, offset);
  8133. CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
  8134. }
  8135. /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
  8136. accessibility. */
  8137. bool
  8138. uniquely_derived_from_p (tree parent, tree type)
  8139. {
  8140. tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
  8141. return base && base != error_mark_node;
  8142. }
  8143. /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
  8144. bool
  8145. publicly_uniquely_derived_p (tree parent, tree type)
  8146. {
  8147. tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
  8148. NULL, tf_none);
  8149. return base && base != error_mark_node;
  8150. }
  8151. /* CTX1 and CTX2 are declaration contexts. Return the innermost common
  8152. class between them, if any. */
  8153. tree
  8154. common_enclosing_class (tree ctx1, tree ctx2)
  8155. {
  8156. if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
  8157. return NULL_TREE;
  8158. gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
  8159. && ctx2 == TYPE_MAIN_VARIANT (ctx2));
  8160. if (ctx1 == ctx2)
  8161. return ctx1;
  8162. for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
  8163. TYPE_MARKED_P (t) = true;
  8164. tree found = NULL_TREE;
  8165. for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
  8166. if (TYPE_MARKED_P (t))
  8167. {
  8168. found = t;
  8169. break;
  8170. }
  8171. for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
  8172. TYPE_MARKED_P (t) = false;
  8173. return found;
  8174. }
  8175. #include "gt-cp-class.h"