DeclCXX.h 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DeclCXX.h - Classes for representing C++ declarations --*- C++ -*-=====//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file
  15. /// Defines the C++ Decl subclasses, other than those for templates
  16. /// (found in DeclTemplate.h) and friends (in DeclFriend.h).
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_AST_DECLCXX_H
  20. #define LLVM_CLANG_AST_DECLCXX_H
  21. #include "clang/AST/ASTUnresolvedSet.h"
  22. #include "clang/AST/Decl.h"
  23. #include "clang/AST/DeclBase.h"
  24. #include "clang/AST/DeclarationName.h"
  25. #include "clang/AST/Expr.h"
  26. #include "clang/AST/ExternalASTSource.h"
  27. #include "clang/AST/LambdaCapture.h"
  28. #include "clang/AST/NestedNameSpecifier.h"
  29. #include "clang/AST/Redeclarable.h"
  30. #include "clang/AST/Stmt.h"
  31. #include "clang/AST/Type.h"
  32. #include "clang/AST/TypeLoc.h"
  33. #include "clang/AST/UnresolvedSet.h"
  34. #include "clang/Basic/LLVM.h"
  35. #include "clang/Basic/Lambda.h"
  36. #include "clang/Basic/LangOptions.h"
  37. #include "clang/Basic/OperatorKinds.h"
  38. #include "clang/Basic/SourceLocation.h"
  39. #include "clang/Basic/Specifiers.h"
  40. #include "llvm/ADT/ArrayRef.h"
  41. #include "llvm/ADT/DenseMap.h"
  42. #include "llvm/ADT/PointerIntPair.h"
  43. #include "llvm/ADT/PointerUnion.h"
  44. #include "llvm/ADT/STLExtras.h"
  45. #include "llvm/ADT/TinyPtrVector.h"
  46. #include "llvm/ADT/iterator_range.h"
  47. #include "llvm/Support/Casting.h"
  48. #include "llvm/Support/Compiler.h"
  49. #include "llvm/Support/PointerLikeTypeTraits.h"
  50. #include "llvm/Support/TrailingObjects.h"
  51. #include <cassert>
  52. #include <cstddef>
  53. #include <iterator>
  54. #include <memory>
  55. #include <vector>
  56. namespace clang {
  57. class ASTContext;
  58. class ClassTemplateDecl;
  59. class ConstructorUsingShadowDecl;
  60. class CXXBasePath;
  61. class CXXBasePaths;
  62. class CXXConstructorDecl;
  63. class CXXDestructorDecl;
  64. class CXXFinalOverriderMap;
  65. class CXXIndirectPrimaryBaseSet;
  66. class CXXMethodDecl;
  67. class DecompositionDecl;
  68. class FriendDecl;
  69. class FunctionTemplateDecl;
  70. class IdentifierInfo;
  71. class MemberSpecializationInfo;
  72. class BaseUsingDecl;
  73. class TemplateDecl;
  74. class TemplateParameterList;
  75. class UsingDecl;
  76. /// Represents an access specifier followed by colon ':'.
  77. ///
  78. /// An objects of this class represents sugar for the syntactic occurrence
  79. /// of an access specifier followed by a colon in the list of member
  80. /// specifiers of a C++ class definition.
  81. ///
  82. /// Note that they do not represent other uses of access specifiers,
  83. /// such as those occurring in a list of base specifiers.
  84. /// Also note that this class has nothing to do with so-called
  85. /// "access declarations" (C++98 11.3 [class.access.dcl]).
  86. class AccessSpecDecl : public Decl {
  87. /// The location of the ':'.
  88. SourceLocation ColonLoc;
  89. AccessSpecDecl(AccessSpecifier AS, DeclContext *DC,
  90. SourceLocation ASLoc, SourceLocation ColonLoc)
  91. : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) {
  92. setAccess(AS);
  93. }
  94. AccessSpecDecl(EmptyShell Empty) : Decl(AccessSpec, Empty) {}
  95. virtual void anchor();
  96. public:
  97. /// The location of the access specifier.
  98. SourceLocation getAccessSpecifierLoc() const { return getLocation(); }
  99. /// Sets the location of the access specifier.
  100. void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); }
  101. /// The location of the colon following the access specifier.
  102. SourceLocation getColonLoc() const { return ColonLoc; }
  103. /// Sets the location of the colon.
  104. void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; }
  105. SourceRange getSourceRange() const override LLVM_READONLY {
  106. return SourceRange(getAccessSpecifierLoc(), getColonLoc());
  107. }
  108. static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS,
  109. DeclContext *DC, SourceLocation ASLoc,
  110. SourceLocation ColonLoc) {
  111. return new (C, DC) AccessSpecDecl(AS, DC, ASLoc, ColonLoc);
  112. }
  113. static AccessSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  114. // Implement isa/cast/dyncast/etc.
  115. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  116. static bool classofKind(Kind K) { return K == AccessSpec; }
  117. };
  118. /// Represents a base class of a C++ class.
  119. ///
  120. /// Each CXXBaseSpecifier represents a single, direct base class (or
  121. /// struct) of a C++ class (or struct). It specifies the type of that
  122. /// base class, whether it is a virtual or non-virtual base, and what
  123. /// level of access (public, protected, private) is used for the
  124. /// derivation. For example:
  125. ///
  126. /// \code
  127. /// class A { };
  128. /// class B { };
  129. /// class C : public virtual A, protected B { };
  130. /// \endcode
  131. ///
  132. /// In this code, C will have two CXXBaseSpecifiers, one for "public
  133. /// virtual A" and the other for "protected B".
  134. class CXXBaseSpecifier {
  135. /// The source code range that covers the full base
  136. /// specifier, including the "virtual" (if present) and access
  137. /// specifier (if present).
  138. SourceRange Range;
  139. /// The source location of the ellipsis, if this is a pack
  140. /// expansion.
  141. SourceLocation EllipsisLoc;
  142. /// Whether this is a virtual base class or not.
  143. unsigned Virtual : 1;
  144. /// Whether this is the base of a class (true) or of a struct (false).
  145. ///
  146. /// This determines the mapping from the access specifier as written in the
  147. /// source code to the access specifier used for semantic analysis.
  148. unsigned BaseOfClass : 1;
  149. /// Access specifier as written in the source code (may be AS_none).
  150. ///
  151. /// The actual type of data stored here is an AccessSpecifier, but we use
  152. /// "unsigned" here to work around a VC++ bug.
  153. unsigned Access : 2;
  154. /// Whether the class contains a using declaration
  155. /// to inherit the named class's constructors.
  156. unsigned InheritConstructors : 1;
  157. /// The type of the base class.
  158. ///
  159. /// This will be a class or struct (or a typedef of such). The source code
  160. /// range does not include the \c virtual or the access specifier.
  161. TypeSourceInfo *BaseTypeInfo;
  162. public:
  163. CXXBaseSpecifier() = default;
  164. CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A,
  165. TypeSourceInfo *TInfo, SourceLocation EllipsisLoc)
  166. : Range(R), EllipsisLoc(EllipsisLoc), Virtual(V), BaseOfClass(BC),
  167. Access(A), InheritConstructors(false), BaseTypeInfo(TInfo) {}
  168. /// Retrieves the source range that contains the entire base specifier.
  169. SourceRange getSourceRange() const LLVM_READONLY { return Range; }
  170. SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
  171. SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
  172. /// Get the location at which the base class type was written.
  173. SourceLocation getBaseTypeLoc() const LLVM_READONLY {
  174. return BaseTypeInfo->getTypeLoc().getBeginLoc();
  175. }
  176. /// Determines whether the base class is a virtual base class (or not).
  177. bool isVirtual() const { return Virtual; }
  178. /// Determine whether this base class is a base of a class declared
  179. /// with the 'class' keyword (vs. one declared with the 'struct' keyword).
  180. bool isBaseOfClass() const { return BaseOfClass; }
  181. /// Determine whether this base specifier is a pack expansion.
  182. bool isPackExpansion() const { return EllipsisLoc.isValid(); }
  183. /// Determine whether this base class's constructors get inherited.
  184. bool getInheritConstructors() const { return InheritConstructors; }
  185. /// Set that this base class's constructors should be inherited.
  186. void setInheritConstructors(bool Inherit = true) {
  187. InheritConstructors = Inherit;
  188. }
  189. /// For a pack expansion, determine the location of the ellipsis.
  190. SourceLocation getEllipsisLoc() const {
  191. return EllipsisLoc;
  192. }
  193. /// Returns the access specifier for this base specifier.
  194. ///
  195. /// This is the actual base specifier as used for semantic analysis, so
  196. /// the result can never be AS_none. To retrieve the access specifier as
  197. /// written in the source code, use getAccessSpecifierAsWritten().
  198. AccessSpecifier getAccessSpecifier() const {
  199. if ((AccessSpecifier)Access == AS_none)
  200. return BaseOfClass? AS_private : AS_public;
  201. else
  202. return (AccessSpecifier)Access;
  203. }
  204. /// Retrieves the access specifier as written in the source code
  205. /// (which may mean that no access specifier was explicitly written).
  206. ///
  207. /// Use getAccessSpecifier() to retrieve the access specifier for use in
  208. /// semantic analysis.
  209. AccessSpecifier getAccessSpecifierAsWritten() const {
  210. return (AccessSpecifier)Access;
  211. }
  212. /// Retrieves the type of the base class.
  213. ///
  214. /// This type will always be an unqualified class type.
  215. QualType getType() const {
  216. return BaseTypeInfo->getType().getUnqualifiedType();
  217. }
  218. /// Retrieves the type and source location of the base class.
  219. TypeSourceInfo *getTypeSourceInfo() const { return BaseTypeInfo; }
  220. };
  221. /// Represents a C++ struct/union/class.
  222. class CXXRecordDecl : public RecordDecl {
  223. friend class ASTDeclReader;
  224. friend class ASTDeclWriter;
  225. friend class ASTNodeImporter;
  226. friend class ASTReader;
  227. friend class ASTRecordWriter;
  228. friend class ASTWriter;
  229. friend class DeclContext;
  230. friend class LambdaExpr;
  231. friend void FunctionDecl::setPure(bool);
  232. friend void TagDecl::startDefinition();
  233. /// Values used in DefinitionData fields to represent special members.
  234. enum SpecialMemberFlags {
  235. SMF_DefaultConstructor = 0x1,
  236. SMF_CopyConstructor = 0x2,
  237. SMF_MoveConstructor = 0x4,
  238. SMF_CopyAssignment = 0x8,
  239. SMF_MoveAssignment = 0x10,
  240. SMF_Destructor = 0x20,
  241. SMF_All = 0x3f
  242. };
  243. struct DefinitionData {
  244. #define FIELD(Name, Width, Merge) \
  245. unsigned Name : Width;
  246. #include "CXXRecordDeclDefinitionBits.def"
  247. /// Whether this class describes a C++ lambda.
  248. unsigned IsLambda : 1;
  249. /// Whether we are currently parsing base specifiers.
  250. unsigned IsParsingBaseSpecifiers : 1;
  251. /// True when visible conversion functions are already computed
  252. /// and are available.
  253. unsigned ComputedVisibleConversions : 1;
  254. unsigned HasODRHash : 1;
  255. /// A hash of parts of the class to help in ODR checking.
  256. unsigned ODRHash = 0;
  257. /// The number of base class specifiers in Bases.
  258. unsigned NumBases = 0;
  259. /// The number of virtual base class specifiers in VBases.
  260. unsigned NumVBases = 0;
  261. /// Base classes of this class.
  262. ///
  263. /// FIXME: This is wasted space for a union.
  264. LazyCXXBaseSpecifiersPtr Bases;
  265. /// direct and indirect virtual base classes of this class.
  266. LazyCXXBaseSpecifiersPtr VBases;
  267. /// The conversion functions of this C++ class (but not its
  268. /// inherited conversion functions).
  269. ///
  270. /// Each of the entries in this overload set is a CXXConversionDecl.
  271. LazyASTUnresolvedSet Conversions;
  272. /// The conversion functions of this C++ class and all those
  273. /// inherited conversion functions that are visible in this class.
  274. ///
  275. /// Each of the entries in this overload set is a CXXConversionDecl or a
  276. /// FunctionTemplateDecl.
  277. LazyASTUnresolvedSet VisibleConversions;
  278. /// The declaration which defines this record.
  279. CXXRecordDecl *Definition;
  280. /// The first friend declaration in this class, or null if there
  281. /// aren't any.
  282. ///
  283. /// This is actually currently stored in reverse order.
  284. LazyDeclPtr FirstFriend;
  285. DefinitionData(CXXRecordDecl *D);
  286. /// Retrieve the set of direct base classes.
  287. CXXBaseSpecifier *getBases() const {
  288. if (!Bases.isOffset())
  289. return Bases.get(nullptr);
  290. return getBasesSlowCase();
  291. }
  292. /// Retrieve the set of virtual base classes.
  293. CXXBaseSpecifier *getVBases() const {
  294. if (!VBases.isOffset())
  295. return VBases.get(nullptr);
  296. return getVBasesSlowCase();
  297. }
  298. ArrayRef<CXXBaseSpecifier> bases() const {
  299. return llvm::makeArrayRef(getBases(), NumBases);
  300. }
  301. ArrayRef<CXXBaseSpecifier> vbases() const {
  302. return llvm::makeArrayRef(getVBases(), NumVBases);
  303. }
  304. private:
  305. CXXBaseSpecifier *getBasesSlowCase() const;
  306. CXXBaseSpecifier *getVBasesSlowCase() const;
  307. };
  308. struct DefinitionData *DefinitionData;
  309. /// Describes a C++ closure type (generated by a lambda expression).
  310. struct LambdaDefinitionData : public DefinitionData {
  311. using Capture = LambdaCapture;
  312. /// Whether this lambda is known to be dependent, even if its
  313. /// context isn't dependent.
  314. ///
  315. /// A lambda with a non-dependent context can be dependent if it occurs
  316. /// within the default argument of a function template, because the
  317. /// lambda will have been created with the enclosing context as its
  318. /// declaration context, rather than function. This is an unfortunate
  319. /// artifact of having to parse the default arguments before.
  320. unsigned Dependent : 1;
  321. /// Whether this lambda is a generic lambda.
  322. unsigned IsGenericLambda : 1;
  323. /// The Default Capture.
  324. unsigned CaptureDefault : 2;
  325. /// The number of captures in this lambda is limited 2^NumCaptures.
  326. unsigned NumCaptures : 15;
  327. /// The number of explicit captures in this lambda.
  328. unsigned NumExplicitCaptures : 13;
  329. /// Has known `internal` linkage.
  330. unsigned HasKnownInternalLinkage : 1;
  331. /// The number used to indicate this lambda expression for name
  332. /// mangling in the Itanium C++ ABI.
  333. unsigned ManglingNumber : 31;
  334. /// The declaration that provides context for this lambda, if the
  335. /// actual DeclContext does not suffice. This is used for lambdas that
  336. /// occur within default arguments of function parameters within the class
  337. /// or within a data member initializer.
  338. LazyDeclPtr ContextDecl;
  339. /// The list of captures, both explicit and implicit, for this
  340. /// lambda.
  341. Capture *Captures = nullptr;
  342. /// The type of the call method.
  343. TypeSourceInfo *MethodTyInfo;
  344. LambdaDefinitionData(CXXRecordDecl *D, TypeSourceInfo *Info, bool Dependent,
  345. bool IsGeneric, LambdaCaptureDefault CaptureDefault)
  346. : DefinitionData(D), Dependent(Dependent), IsGenericLambda(IsGeneric),
  347. CaptureDefault(CaptureDefault), NumCaptures(0),
  348. NumExplicitCaptures(0), HasKnownInternalLinkage(0), ManglingNumber(0),
  349. MethodTyInfo(Info) {
  350. IsLambda = true;
  351. // C++1z [expr.prim.lambda]p4:
  352. // This class type is not an aggregate type.
  353. Aggregate = false;
  354. PlainOldData = false;
  355. }
  356. };
  357. struct DefinitionData *dataPtr() const {
  358. // Complete the redecl chain (if necessary).
  359. getMostRecentDecl();
  360. return DefinitionData;
  361. }
  362. struct DefinitionData &data() const {
  363. auto *DD = dataPtr();
  364. assert(DD && "queried property of class with no definition");
  365. return *DD;
  366. }
  367. struct LambdaDefinitionData &getLambdaData() const {
  368. // No update required: a merged definition cannot change any lambda
  369. // properties.
  370. auto *DD = DefinitionData;
  371. assert(DD && DD->IsLambda && "queried lambda property of non-lambda class");
  372. return static_cast<LambdaDefinitionData&>(*DD);
  373. }
  374. /// The template or declaration that this declaration
  375. /// describes or was instantiated from, respectively.
  376. ///
  377. /// For non-templates, this value will be null. For record
  378. /// declarations that describe a class template, this will be a
  379. /// pointer to a ClassTemplateDecl. For member
  380. /// classes of class template specializations, this will be the
  381. /// MemberSpecializationInfo referring to the member class that was
  382. /// instantiated or specialized.
  383. llvm::PointerUnion<ClassTemplateDecl *, MemberSpecializationInfo *>
  384. TemplateOrInstantiation;
  385. /// Called from setBases and addedMember to notify the class that a
  386. /// direct or virtual base class or a member of class type has been added.
  387. void addedClassSubobject(CXXRecordDecl *Base);
  388. /// Notify the class that member has been added.
  389. ///
  390. /// This routine helps maintain information about the class based on which
  391. /// members have been added. It will be invoked by DeclContext::addDecl()
  392. /// whenever a member is added to this record.
  393. void addedMember(Decl *D);
  394. void markedVirtualFunctionPure();
  395. /// Get the head of our list of friend declarations, possibly
  396. /// deserializing the friends from an external AST source.
  397. FriendDecl *getFirstFriend() const;
  398. /// Determine whether this class has an empty base class subobject of type X
  399. /// or of one of the types that might be at offset 0 within X (per the C++
  400. /// "standard layout" rules).
  401. bool hasSubobjectAtOffsetZeroOfEmptyBaseType(ASTContext &Ctx,
  402. const CXXRecordDecl *X);
  403. protected:
  404. CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C, DeclContext *DC,
  405. SourceLocation StartLoc, SourceLocation IdLoc,
  406. IdentifierInfo *Id, CXXRecordDecl *PrevDecl);
  407. public:
  408. /// Iterator that traverses the base classes of a class.
  409. using base_class_iterator = CXXBaseSpecifier *;
  410. /// Iterator that traverses the base classes of a class.
  411. using base_class_const_iterator = const CXXBaseSpecifier *;
  412. CXXRecordDecl *getCanonicalDecl() override {
  413. return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl());
  414. }
  415. const CXXRecordDecl *getCanonicalDecl() const {
  416. return const_cast<CXXRecordDecl*>(this)->getCanonicalDecl();
  417. }
  418. CXXRecordDecl *getPreviousDecl() {
  419. return cast_or_null<CXXRecordDecl>(
  420. static_cast<RecordDecl *>(this)->getPreviousDecl());
  421. }
  422. const CXXRecordDecl *getPreviousDecl() const {
  423. return const_cast<CXXRecordDecl*>(this)->getPreviousDecl();
  424. }
  425. CXXRecordDecl *getMostRecentDecl() {
  426. return cast<CXXRecordDecl>(
  427. static_cast<RecordDecl *>(this)->getMostRecentDecl());
  428. }
  429. const CXXRecordDecl *getMostRecentDecl() const {
  430. return const_cast<CXXRecordDecl*>(this)->getMostRecentDecl();
  431. }
  432. CXXRecordDecl *getMostRecentNonInjectedDecl() {
  433. CXXRecordDecl *Recent =
  434. static_cast<CXXRecordDecl *>(this)->getMostRecentDecl();
  435. while (Recent->isInjectedClassName()) {
  436. // FIXME: Does injected class name need to be in the redeclarations chain?
  437. assert(Recent->getPreviousDecl());
  438. Recent = Recent->getPreviousDecl();
  439. }
  440. return Recent;
  441. }
  442. const CXXRecordDecl *getMostRecentNonInjectedDecl() const {
  443. return const_cast<CXXRecordDecl*>(this)->getMostRecentNonInjectedDecl();
  444. }
  445. CXXRecordDecl *getDefinition() const {
  446. // We only need an update if we don't already know which
  447. // declaration is the definition.
  448. auto *DD = DefinitionData ? DefinitionData : dataPtr();
  449. return DD ? DD->Definition : nullptr;
  450. }
  451. bool hasDefinition() const { return DefinitionData || dataPtr(); }
  452. static CXXRecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC,
  453. SourceLocation StartLoc, SourceLocation IdLoc,
  454. IdentifierInfo *Id,
  455. CXXRecordDecl *PrevDecl = nullptr,
  456. bool DelayTypeCreation = false);
  457. static CXXRecordDecl *CreateLambda(const ASTContext &C, DeclContext *DC,
  458. TypeSourceInfo *Info, SourceLocation Loc,
  459. bool DependentLambda, bool IsGeneric,
  460. LambdaCaptureDefault CaptureDefault);
  461. static CXXRecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
  462. bool isDynamicClass() const {
  463. return data().Polymorphic || data().NumVBases != 0;
  464. }
  465. /// @returns true if class is dynamic or might be dynamic because the
  466. /// definition is incomplete of dependent.
  467. bool mayBeDynamicClass() const {
  468. return !hasDefinition() || isDynamicClass() || hasAnyDependentBases();
  469. }
  470. /// @returns true if class is non dynamic or might be non dynamic because the
  471. /// definition is incomplete of dependent.
  472. bool mayBeNonDynamicClass() const {
  473. return !hasDefinition() || !isDynamicClass() || hasAnyDependentBases();
  474. }
  475. void setIsParsingBaseSpecifiers() { data().IsParsingBaseSpecifiers = true; }
  476. bool isParsingBaseSpecifiers() const {
  477. return data().IsParsingBaseSpecifiers;
  478. }
  479. unsigned getODRHash() const;
  480. /// Sets the base classes of this struct or class.
  481. void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
  482. /// Retrieves the number of base classes of this class.
  483. unsigned getNumBases() const { return data().NumBases; }
  484. using base_class_range = llvm::iterator_range<base_class_iterator>;
  485. using base_class_const_range =
  486. llvm::iterator_range<base_class_const_iterator>;
  487. base_class_range bases() {
  488. return base_class_range(bases_begin(), bases_end());
  489. }
  490. base_class_const_range bases() const {
  491. return base_class_const_range(bases_begin(), bases_end());
  492. }
  493. base_class_iterator bases_begin() { return data().getBases(); }
  494. base_class_const_iterator bases_begin() const { return data().getBases(); }
  495. base_class_iterator bases_end() { return bases_begin() + data().NumBases; }
  496. base_class_const_iterator bases_end() const {
  497. return bases_begin() + data().NumBases;
  498. }
  499. /// Retrieves the number of virtual base classes of this class.
  500. unsigned getNumVBases() const { return data().NumVBases; }
  501. base_class_range vbases() {
  502. return base_class_range(vbases_begin(), vbases_end());
  503. }
  504. base_class_const_range vbases() const {
  505. return base_class_const_range(vbases_begin(), vbases_end());
  506. }
  507. base_class_iterator vbases_begin() { return data().getVBases(); }
  508. base_class_const_iterator vbases_begin() const { return data().getVBases(); }
  509. base_class_iterator vbases_end() { return vbases_begin() + data().NumVBases; }
  510. base_class_const_iterator vbases_end() const {
  511. return vbases_begin() + data().NumVBases;
  512. }
  513. /// Determine whether this class has any dependent base classes which
  514. /// are not the current instantiation.
  515. bool hasAnyDependentBases() const;
  516. /// Iterator access to method members. The method iterator visits
  517. /// all method members of the class, including non-instance methods,
  518. /// special methods, etc.
  519. using method_iterator = specific_decl_iterator<CXXMethodDecl>;
  520. using method_range =
  521. llvm::iterator_range<specific_decl_iterator<CXXMethodDecl>>;
  522. method_range methods() const {
  523. return method_range(method_begin(), method_end());
  524. }
  525. /// Method begin iterator. Iterates in the order the methods
  526. /// were declared.
  527. method_iterator method_begin() const {
  528. return method_iterator(decls_begin());
  529. }
  530. /// Method past-the-end iterator.
  531. method_iterator method_end() const {
  532. return method_iterator(decls_end());
  533. }
  534. /// Iterator access to constructor members.
  535. using ctor_iterator = specific_decl_iterator<CXXConstructorDecl>;
  536. using ctor_range =
  537. llvm::iterator_range<specific_decl_iterator<CXXConstructorDecl>>;
  538. ctor_range ctors() const { return ctor_range(ctor_begin(), ctor_end()); }
  539. ctor_iterator ctor_begin() const {
  540. return ctor_iterator(decls_begin());
  541. }
  542. ctor_iterator ctor_end() const {
  543. return ctor_iterator(decls_end());
  544. }
  545. /// An iterator over friend declarations. All of these are defined
  546. /// in DeclFriend.h.
  547. class friend_iterator;
  548. using friend_range = llvm::iterator_range<friend_iterator>;
  549. friend_range friends() const;
  550. friend_iterator friend_begin() const;
  551. friend_iterator friend_end() const;
  552. void pushFriendDecl(FriendDecl *FD);
  553. /// Determines whether this record has any friends.
  554. bool hasFriends() const {
  555. return data().FirstFriend.isValid();
  556. }
  557. /// \c true if a defaulted copy constructor for this class would be
  558. /// deleted.
  559. bool defaultedCopyConstructorIsDeleted() const {
  560. assert((!needsOverloadResolutionForCopyConstructor() ||
  561. (data().DeclaredSpecialMembers & SMF_CopyConstructor)) &&
  562. "this property has not yet been computed by Sema");
  563. return data().DefaultedCopyConstructorIsDeleted;
  564. }
  565. /// \c true if a defaulted move constructor for this class would be
  566. /// deleted.
  567. bool defaultedMoveConstructorIsDeleted() const {
  568. assert((!needsOverloadResolutionForMoveConstructor() ||
  569. (data().DeclaredSpecialMembers & SMF_MoveConstructor)) &&
  570. "this property has not yet been computed by Sema");
  571. return data().DefaultedMoveConstructorIsDeleted;
  572. }
  573. /// \c true if a defaulted destructor for this class would be deleted.
  574. bool defaultedDestructorIsDeleted() const {
  575. assert((!needsOverloadResolutionForDestructor() ||
  576. (data().DeclaredSpecialMembers & SMF_Destructor)) &&
  577. "this property has not yet been computed by Sema");
  578. return data().DefaultedDestructorIsDeleted;
  579. }
  580. /// \c true if we know for sure that this class has a single,
  581. /// accessible, unambiguous copy constructor that is not deleted.
  582. bool hasSimpleCopyConstructor() const {
  583. return !hasUserDeclaredCopyConstructor() &&
  584. !data().DefaultedCopyConstructorIsDeleted;
  585. }
  586. /// \c true if we know for sure that this class has a single,
  587. /// accessible, unambiguous move constructor that is not deleted.
  588. bool hasSimpleMoveConstructor() const {
  589. return !hasUserDeclaredMoveConstructor() && hasMoveConstructor() &&
  590. !data().DefaultedMoveConstructorIsDeleted;
  591. }
  592. /// \c true if we know for sure that this class has a single,
  593. /// accessible, unambiguous copy assignment operator that is not deleted.
  594. bool hasSimpleCopyAssignment() const {
  595. return !hasUserDeclaredCopyAssignment() &&
  596. !data().DefaultedCopyAssignmentIsDeleted;
  597. }
  598. /// \c true if we know for sure that this class has a single,
  599. /// accessible, unambiguous move assignment operator that is not deleted.
  600. bool hasSimpleMoveAssignment() const {
  601. return !hasUserDeclaredMoveAssignment() && hasMoveAssignment() &&
  602. !data().DefaultedMoveAssignmentIsDeleted;
  603. }
  604. /// \c true if we know for sure that this class has an accessible
  605. /// destructor that is not deleted.
  606. bool hasSimpleDestructor() const {
  607. return !hasUserDeclaredDestructor() &&
  608. !data().DefaultedDestructorIsDeleted;
  609. }
  610. /// Determine whether this class has any default constructors.
  611. bool hasDefaultConstructor() const {
  612. return (data().DeclaredSpecialMembers & SMF_DefaultConstructor) ||
  613. needsImplicitDefaultConstructor();
  614. }
  615. /// Determine if we need to declare a default constructor for
  616. /// this class.
  617. ///
  618. /// This value is used for lazy creation of default constructors.
  619. bool needsImplicitDefaultConstructor() const {
  620. return (!data().UserDeclaredConstructor &&
  621. !(data().DeclaredSpecialMembers & SMF_DefaultConstructor) &&
  622. (!isLambda() || lambdaIsDefaultConstructibleAndAssignable())) ||
  623. // FIXME: Proposed fix to core wording issue: if a class inherits
  624. // a default constructor and doesn't explicitly declare one, one
  625. // is declared implicitly.
  626. (data().HasInheritedDefaultConstructor &&
  627. !(data().DeclaredSpecialMembers & SMF_DefaultConstructor));
  628. }
  629. /// Determine whether this class has any user-declared constructors.
  630. ///
  631. /// When true, a default constructor will not be implicitly declared.
  632. bool hasUserDeclaredConstructor() const {
  633. return data().UserDeclaredConstructor;
  634. }
  635. /// Whether this class has a user-provided default constructor
  636. /// per C++11.
  637. bool hasUserProvidedDefaultConstructor() const {
  638. return data().UserProvidedDefaultConstructor;
  639. }
  640. /// Determine whether this class has a user-declared copy constructor.
  641. ///
  642. /// When false, a copy constructor will be implicitly declared.
  643. bool hasUserDeclaredCopyConstructor() const {
  644. return data().UserDeclaredSpecialMembers & SMF_CopyConstructor;
  645. }
  646. /// Determine whether this class needs an implicit copy
  647. /// constructor to be lazily declared.
  648. bool needsImplicitCopyConstructor() const {
  649. return !(data().DeclaredSpecialMembers & SMF_CopyConstructor);
  650. }
  651. /// Determine whether we need to eagerly declare a defaulted copy
  652. /// constructor for this class.
  653. bool needsOverloadResolutionForCopyConstructor() const {
  654. // C++17 [class.copy.ctor]p6:
  655. // If the class definition declares a move constructor or move assignment
  656. // operator, the implicitly declared copy constructor is defined as
  657. // deleted.
  658. // In MSVC mode, sometimes a declared move assignment does not delete an
  659. // implicit copy constructor, so defer this choice to Sema.
  660. if (data().UserDeclaredSpecialMembers &
  661. (SMF_MoveConstructor | SMF_MoveAssignment))
  662. return true;
  663. return data().NeedOverloadResolutionForCopyConstructor;
  664. }
  665. /// Determine whether an implicit copy constructor for this type
  666. /// would have a parameter with a const-qualified reference type.
  667. bool implicitCopyConstructorHasConstParam() const {
  668. return data().ImplicitCopyConstructorCanHaveConstParamForNonVBase &&
  669. (isAbstract() ||
  670. data().ImplicitCopyConstructorCanHaveConstParamForVBase);
  671. }
  672. /// Determine whether this class has a copy constructor with
  673. /// a parameter type which is a reference to a const-qualified type.
  674. bool hasCopyConstructorWithConstParam() const {
  675. return data().HasDeclaredCopyConstructorWithConstParam ||
  676. (needsImplicitCopyConstructor() &&
  677. implicitCopyConstructorHasConstParam());
  678. }
  679. /// Whether this class has a user-declared move constructor or
  680. /// assignment operator.
  681. ///
  682. /// When false, a move constructor and assignment operator may be
  683. /// implicitly declared.
  684. bool hasUserDeclaredMoveOperation() const {
  685. return data().UserDeclaredSpecialMembers &
  686. (SMF_MoveConstructor | SMF_MoveAssignment);
  687. }
  688. /// Determine whether this class has had a move constructor
  689. /// declared by the user.
  690. bool hasUserDeclaredMoveConstructor() const {
  691. return data().UserDeclaredSpecialMembers & SMF_MoveConstructor;
  692. }
  693. /// Determine whether this class has a move constructor.
  694. bool hasMoveConstructor() const {
  695. return (data().DeclaredSpecialMembers & SMF_MoveConstructor) ||
  696. needsImplicitMoveConstructor();
  697. }
  698. /// Set that we attempted to declare an implicit copy
  699. /// constructor, but overload resolution failed so we deleted it.
  700. void setImplicitCopyConstructorIsDeleted() {
  701. assert((data().DefaultedCopyConstructorIsDeleted ||
  702. needsOverloadResolutionForCopyConstructor()) &&
  703. "Copy constructor should not be deleted");
  704. data().DefaultedCopyConstructorIsDeleted = true;
  705. }
  706. /// Set that we attempted to declare an implicit move
  707. /// constructor, but overload resolution failed so we deleted it.
  708. void setImplicitMoveConstructorIsDeleted() {
  709. assert((data().DefaultedMoveConstructorIsDeleted ||
  710. needsOverloadResolutionForMoveConstructor()) &&
  711. "move constructor should not be deleted");
  712. data().DefaultedMoveConstructorIsDeleted = true;
  713. }
  714. /// Set that we attempted to declare an implicit destructor,
  715. /// but overload resolution failed so we deleted it.
  716. void setImplicitDestructorIsDeleted() {
  717. assert((data().DefaultedDestructorIsDeleted ||
  718. needsOverloadResolutionForDestructor()) &&
  719. "destructor should not be deleted");
  720. data().DefaultedDestructorIsDeleted = true;
  721. }
  722. /// Determine whether this class should get an implicit move
  723. /// constructor or if any existing special member function inhibits this.
  724. bool needsImplicitMoveConstructor() const {
  725. return !(data().DeclaredSpecialMembers & SMF_MoveConstructor) &&
  726. !hasUserDeclaredCopyConstructor() &&
  727. !hasUserDeclaredCopyAssignment() &&
  728. !hasUserDeclaredMoveAssignment() &&
  729. !hasUserDeclaredDestructor();
  730. }
  731. /// Determine whether we need to eagerly declare a defaulted move
  732. /// constructor for this class.
  733. bool needsOverloadResolutionForMoveConstructor() const {
  734. return data().NeedOverloadResolutionForMoveConstructor;
  735. }
  736. /// Determine whether this class has a user-declared copy assignment
  737. /// operator.
  738. ///
  739. /// When false, a copy assignment operator will be implicitly declared.
  740. bool hasUserDeclaredCopyAssignment() const {
  741. return data().UserDeclaredSpecialMembers & SMF_CopyAssignment;
  742. }
  743. /// Set that we attempted to declare an implicit copy assignment
  744. /// operator, but overload resolution failed so we deleted it.
  745. void setImplicitCopyAssignmentIsDeleted() {
  746. assert((data().DefaultedCopyAssignmentIsDeleted ||
  747. needsOverloadResolutionForCopyAssignment()) &&
  748. "copy assignment should not be deleted");
  749. data().DefaultedCopyAssignmentIsDeleted = true;
  750. }
  751. /// Determine whether this class needs an implicit copy
  752. /// assignment operator to be lazily declared.
  753. bool needsImplicitCopyAssignment() const {
  754. return !(data().DeclaredSpecialMembers & SMF_CopyAssignment);
  755. }
  756. /// Determine whether we need to eagerly declare a defaulted copy
  757. /// assignment operator for this class.
  758. bool needsOverloadResolutionForCopyAssignment() const {
  759. // C++20 [class.copy.assign]p2:
  760. // If the class definition declares a move constructor or move assignment
  761. // operator, the implicitly declared copy assignment operator is defined
  762. // as deleted.
  763. // In MSVC mode, sometimes a declared move constructor does not delete an
  764. // implicit copy assignment, so defer this choice to Sema.
  765. if (data().UserDeclaredSpecialMembers &
  766. (SMF_MoveConstructor | SMF_MoveAssignment))
  767. return true;
  768. return data().NeedOverloadResolutionForCopyAssignment;
  769. }
  770. /// Determine whether an implicit copy assignment operator for this
  771. /// type would have a parameter with a const-qualified reference type.
  772. bool implicitCopyAssignmentHasConstParam() const {
  773. return data().ImplicitCopyAssignmentHasConstParam;
  774. }
  775. /// Determine whether this class has a copy assignment operator with
  776. /// a parameter type which is a reference to a const-qualified type or is not
  777. /// a reference.
  778. bool hasCopyAssignmentWithConstParam() const {
  779. return data().HasDeclaredCopyAssignmentWithConstParam ||
  780. (needsImplicitCopyAssignment() &&
  781. implicitCopyAssignmentHasConstParam());
  782. }
  783. /// Determine whether this class has had a move assignment
  784. /// declared by the user.
  785. bool hasUserDeclaredMoveAssignment() const {
  786. return data().UserDeclaredSpecialMembers & SMF_MoveAssignment;
  787. }
  788. /// Determine whether this class has a move assignment operator.
  789. bool hasMoveAssignment() const {
  790. return (data().DeclaredSpecialMembers & SMF_MoveAssignment) ||
  791. needsImplicitMoveAssignment();
  792. }
  793. /// Set that we attempted to declare an implicit move assignment
  794. /// operator, but overload resolution failed so we deleted it.
  795. void setImplicitMoveAssignmentIsDeleted() {
  796. assert((data().DefaultedMoveAssignmentIsDeleted ||
  797. needsOverloadResolutionForMoveAssignment()) &&
  798. "move assignment should not be deleted");
  799. data().DefaultedMoveAssignmentIsDeleted = true;
  800. }
  801. /// Determine whether this class should get an implicit move
  802. /// assignment operator or if any existing special member function inhibits
  803. /// this.
  804. bool needsImplicitMoveAssignment() const {
  805. return !(data().DeclaredSpecialMembers & SMF_MoveAssignment) &&
  806. !hasUserDeclaredCopyConstructor() &&
  807. !hasUserDeclaredCopyAssignment() &&
  808. !hasUserDeclaredMoveConstructor() &&
  809. !hasUserDeclaredDestructor() &&
  810. (!isLambda() || lambdaIsDefaultConstructibleAndAssignable());
  811. }
  812. /// Determine whether we need to eagerly declare a move assignment
  813. /// operator for this class.
  814. bool needsOverloadResolutionForMoveAssignment() const {
  815. return data().NeedOverloadResolutionForMoveAssignment;
  816. }
  817. /// Determine whether this class has a user-declared destructor.
  818. ///
  819. /// When false, a destructor will be implicitly declared.
  820. bool hasUserDeclaredDestructor() const {
  821. return data().UserDeclaredSpecialMembers & SMF_Destructor;
  822. }
  823. /// Determine whether this class needs an implicit destructor to
  824. /// be lazily declared.
  825. bool needsImplicitDestructor() const {
  826. return !(data().DeclaredSpecialMembers & SMF_Destructor);
  827. }
  828. /// Determine whether we need to eagerly declare a destructor for this
  829. /// class.
  830. bool needsOverloadResolutionForDestructor() const {
  831. return data().NeedOverloadResolutionForDestructor;
  832. }
  833. /// Determine whether this class describes a lambda function object.
  834. bool isLambda() const {
  835. // An update record can't turn a non-lambda into a lambda.
  836. auto *DD = DefinitionData;
  837. return DD && DD->IsLambda;
  838. }
  839. /// Determine whether this class describes a generic
  840. /// lambda function object (i.e. function call operator is
  841. /// a template).
  842. bool isGenericLambda() const;
  843. /// Determine whether this lambda should have an implicit default constructor
  844. /// and copy and move assignment operators.
  845. bool lambdaIsDefaultConstructibleAndAssignable() const;
  846. /// Retrieve the lambda call operator of the closure type
  847. /// if this is a closure type.
  848. CXXMethodDecl *getLambdaCallOperator() const;
  849. /// Retrieve the dependent lambda call operator of the closure type
  850. /// if this is a templated closure type.
  851. FunctionTemplateDecl *getDependentLambdaCallOperator() const;
  852. /// Retrieve the lambda static invoker, the address of which
  853. /// is returned by the conversion operator, and the body of which
  854. /// is forwarded to the lambda call operator. The version that does not
  855. /// take a calling convention uses the 'default' calling convention for free
  856. /// functions if the Lambda's calling convention was not modified via
  857. /// attribute. Otherwise, it will return the calling convention specified for
  858. /// the lambda.
  859. CXXMethodDecl *getLambdaStaticInvoker() const;
  860. CXXMethodDecl *getLambdaStaticInvoker(CallingConv CC) const;
  861. /// Retrieve the generic lambda's template parameter list.
  862. /// Returns null if the class does not represent a lambda or a generic
  863. /// lambda.
  864. TemplateParameterList *getGenericLambdaTemplateParameterList() const;
  865. /// Retrieve the lambda template parameters that were specified explicitly.
  866. ArrayRef<NamedDecl *> getLambdaExplicitTemplateParameters() const;
  867. LambdaCaptureDefault getLambdaCaptureDefault() const {
  868. assert(isLambda());
  869. return static_cast<LambdaCaptureDefault>(getLambdaData().CaptureDefault);
  870. }
  871. /// Set the captures for this lambda closure type.
  872. void setCaptures(ASTContext &Context, ArrayRef<LambdaCapture> Captures);
  873. /// For a closure type, retrieve the mapping from captured
  874. /// variables and \c this to the non-static data members that store the
  875. /// values or references of the captures.
  876. ///
  877. /// \param Captures Will be populated with the mapping from captured
  878. /// variables to the corresponding fields.
  879. ///
  880. /// \param ThisCapture Will be set to the field declaration for the
  881. /// \c this capture.
  882. ///
  883. /// \note No entries will be added for init-captures, as they do not capture
  884. /// variables.
  885. void getCaptureFields(llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
  886. FieldDecl *&ThisCapture) const;
  887. using capture_const_iterator = const LambdaCapture *;
  888. using capture_const_range = llvm::iterator_range<capture_const_iterator>;
  889. capture_const_range captures() const {
  890. return capture_const_range(captures_begin(), captures_end());
  891. }
  892. capture_const_iterator captures_begin() const {
  893. return isLambda() ? getLambdaData().Captures : nullptr;
  894. }
  895. capture_const_iterator captures_end() const {
  896. return isLambda() ? captures_begin() + getLambdaData().NumCaptures
  897. : nullptr;
  898. }
  899. unsigned capture_size() const { return getLambdaData().NumCaptures; }
  900. using conversion_iterator = UnresolvedSetIterator;
  901. conversion_iterator conversion_begin() const {
  902. return data().Conversions.get(getASTContext()).begin();
  903. }
  904. conversion_iterator conversion_end() const {
  905. return data().Conversions.get(getASTContext()).end();
  906. }
  907. /// Removes a conversion function from this class. The conversion
  908. /// function must currently be a member of this class. Furthermore,
  909. /// this class must currently be in the process of being defined.
  910. void removeConversion(const NamedDecl *Old);
  911. /// Get all conversion functions visible in current class,
  912. /// including conversion function templates.
  913. llvm::iterator_range<conversion_iterator>
  914. getVisibleConversionFunctions() const;
  915. /// Determine whether this class is an aggregate (C++ [dcl.init.aggr]),
  916. /// which is a class with no user-declared constructors, no private
  917. /// or protected non-static data members, no base classes, and no virtual
  918. /// functions (C++ [dcl.init.aggr]p1).
  919. bool isAggregate() const { return data().Aggregate; }
  920. /// Whether this class has any in-class initializers
  921. /// for non-static data members (including those in anonymous unions or
  922. /// structs).
  923. bool hasInClassInitializer() const { return data().HasInClassInitializer; }
  924. /// Whether this class or any of its subobjects has any members of
  925. /// reference type which would make value-initialization ill-formed.
  926. ///
  927. /// Per C++03 [dcl.init]p5:
  928. /// - if T is a non-union class type without a user-declared constructor,
  929. /// then every non-static data member and base-class component of T is
  930. /// value-initialized [...] A program that calls for [...]
  931. /// value-initialization of an entity of reference type is ill-formed.
  932. bool hasUninitializedReferenceMember() const {
  933. return !isUnion() && !hasUserDeclaredConstructor() &&
  934. data().HasUninitializedReferenceMember;
  935. }
  936. /// Whether this class is a POD-type (C++ [class]p4)
  937. ///
  938. /// For purposes of this function a class is POD if it is an aggregate
  939. /// that has no non-static non-POD data members, no reference data
  940. /// members, no user-defined copy assignment operator and no
  941. /// user-defined destructor.
  942. ///
  943. /// Note that this is the C++ TR1 definition of POD.
  944. bool isPOD() const { return data().PlainOldData; }
  945. /// True if this class is C-like, without C++-specific features, e.g.
  946. /// it contains only public fields, no bases, tag kind is not 'class', etc.
  947. bool isCLike() const;
  948. /// Determine whether this is an empty class in the sense of
  949. /// (C++11 [meta.unary.prop]).
  950. ///
  951. /// The CXXRecordDecl is a class type, but not a union type,
  952. /// with no non-static data members other than bit-fields of length 0,
  953. /// no virtual member functions, no virtual base classes,
  954. /// and no base class B for which is_empty<B>::value is false.
  955. ///
  956. /// \note This does NOT include a check for union-ness.
  957. bool isEmpty() const { return data().Empty; }
  958. void setInitMethod(bool Val) { data().HasInitMethod = Val; }
  959. bool hasInitMethod() const { return data().HasInitMethod; }
  960. bool hasPrivateFields() const {
  961. return data().HasPrivateFields;
  962. }
  963. bool hasProtectedFields() const {
  964. return data().HasProtectedFields;
  965. }
  966. /// Determine whether this class has direct non-static data members.
  967. bool hasDirectFields() const {
  968. auto &D = data();
  969. return D.HasPublicFields || D.HasProtectedFields || D.HasPrivateFields;
  970. }
  971. /// Whether this class is polymorphic (C++ [class.virtual]),
  972. /// which means that the class contains or inherits a virtual function.
  973. bool isPolymorphic() const { return data().Polymorphic; }
  974. /// Determine whether this class has a pure virtual function.
  975. ///
  976. /// The class is is abstract per (C++ [class.abstract]p2) if it declares
  977. /// a pure virtual function or inherits a pure virtual function that is
  978. /// not overridden.
  979. bool isAbstract() const { return data().Abstract; }
  980. /// Determine whether this class is standard-layout per
  981. /// C++ [class]p7.
  982. bool isStandardLayout() const { return data().IsStandardLayout; }
  983. /// Determine whether this class was standard-layout per
  984. /// C++11 [class]p7, specifically using the C++11 rules without any DRs.
  985. bool isCXX11StandardLayout() const { return data().IsCXX11StandardLayout; }
  986. /// Determine whether this class, or any of its class subobjects,
  987. /// contains a mutable field.
  988. bool hasMutableFields() const { return data().HasMutableFields; }
  989. /// Determine whether this class has any variant members.
  990. bool hasVariantMembers() const { return data().HasVariantMembers; }
  991. /// Determine whether this class has a trivial default constructor
  992. /// (C++11 [class.ctor]p5).
  993. bool hasTrivialDefaultConstructor() const {
  994. return hasDefaultConstructor() &&
  995. (data().HasTrivialSpecialMembers & SMF_DefaultConstructor);
  996. }
  997. /// Determine whether this class has a non-trivial default constructor
  998. /// (C++11 [class.ctor]p5).
  999. bool hasNonTrivialDefaultConstructor() const {
  1000. return (data().DeclaredNonTrivialSpecialMembers & SMF_DefaultConstructor) ||
  1001. (needsImplicitDefaultConstructor() &&
  1002. !(data().HasTrivialSpecialMembers & SMF_DefaultConstructor));
  1003. }
  1004. /// Determine whether this class has at least one constexpr constructor
  1005. /// other than the copy or move constructors.
  1006. bool hasConstexprNonCopyMoveConstructor() const {
  1007. return data().HasConstexprNonCopyMoveConstructor ||
  1008. (needsImplicitDefaultConstructor() &&
  1009. defaultedDefaultConstructorIsConstexpr());
  1010. }
  1011. /// Determine whether a defaulted default constructor for this class
  1012. /// would be constexpr.
  1013. bool defaultedDefaultConstructorIsConstexpr() const {
  1014. return data().DefaultedDefaultConstructorIsConstexpr &&
  1015. (!isUnion() || hasInClassInitializer() || !hasVariantMembers() ||
  1016. getLangOpts().CPlusPlus20);
  1017. }
  1018. /// Determine whether this class has a constexpr default constructor.
  1019. bool hasConstexprDefaultConstructor() const {
  1020. return data().HasConstexprDefaultConstructor ||
  1021. (needsImplicitDefaultConstructor() &&
  1022. defaultedDefaultConstructorIsConstexpr());
  1023. }
  1024. /// Determine whether this class has a trivial copy constructor
  1025. /// (C++ [class.copy]p6, C++11 [class.copy]p12)
  1026. bool hasTrivialCopyConstructor() const {
  1027. return data().HasTrivialSpecialMembers & SMF_CopyConstructor;
  1028. }
  1029. bool hasTrivialCopyConstructorForCall() const {
  1030. return data().HasTrivialSpecialMembersForCall & SMF_CopyConstructor;
  1031. }
  1032. /// Determine whether this class has a non-trivial copy constructor
  1033. /// (C++ [class.copy]p6, C++11 [class.copy]p12)
  1034. bool hasNonTrivialCopyConstructor() const {
  1035. return data().DeclaredNonTrivialSpecialMembers & SMF_CopyConstructor ||
  1036. !hasTrivialCopyConstructor();
  1037. }
  1038. bool hasNonTrivialCopyConstructorForCall() const {
  1039. return (data().DeclaredNonTrivialSpecialMembersForCall &
  1040. SMF_CopyConstructor) ||
  1041. !hasTrivialCopyConstructorForCall();
  1042. }
  1043. /// Determine whether this class has a trivial move constructor
  1044. /// (C++11 [class.copy]p12)
  1045. bool hasTrivialMoveConstructor() const {
  1046. return hasMoveConstructor() &&
  1047. (data().HasTrivialSpecialMembers & SMF_MoveConstructor);
  1048. }
  1049. bool hasTrivialMoveConstructorForCall() const {
  1050. return hasMoveConstructor() &&
  1051. (data().HasTrivialSpecialMembersForCall & SMF_MoveConstructor);
  1052. }
  1053. /// Determine whether this class has a non-trivial move constructor
  1054. /// (C++11 [class.copy]p12)
  1055. bool hasNonTrivialMoveConstructor() const {
  1056. return (data().DeclaredNonTrivialSpecialMembers & SMF_MoveConstructor) ||
  1057. (needsImplicitMoveConstructor() &&
  1058. !(data().HasTrivialSpecialMembers & SMF_MoveConstructor));
  1059. }
  1060. bool hasNonTrivialMoveConstructorForCall() const {
  1061. return (data().DeclaredNonTrivialSpecialMembersForCall &
  1062. SMF_MoveConstructor) ||
  1063. (needsImplicitMoveConstructor() &&
  1064. !(data().HasTrivialSpecialMembersForCall & SMF_MoveConstructor));
  1065. }
  1066. /// Determine whether this class has a trivial copy assignment operator
  1067. /// (C++ [class.copy]p11, C++11 [class.copy]p25)
  1068. bool hasTrivialCopyAssignment() const {
  1069. return data().HasTrivialSpecialMembers & SMF_CopyAssignment;
  1070. }
  1071. /// Determine whether this class has a non-trivial copy assignment
  1072. /// operator (C++ [class.copy]p11, C++11 [class.copy]p25)
  1073. bool hasNonTrivialCopyAssignment() const {
  1074. return data().DeclaredNonTrivialSpecialMembers & SMF_CopyAssignment ||
  1075. !hasTrivialCopyAssignment();
  1076. }
  1077. /// Determine whether this class has a trivial move assignment operator
  1078. /// (C++11 [class.copy]p25)
  1079. bool hasTrivialMoveAssignment() const {
  1080. return hasMoveAssignment() &&
  1081. (data().HasTrivialSpecialMembers & SMF_MoveAssignment);
  1082. }
  1083. /// Determine whether this class has a non-trivial move assignment
  1084. /// operator (C++11 [class.copy]p25)
  1085. bool hasNonTrivialMoveAssignment() const {
  1086. return (data().DeclaredNonTrivialSpecialMembers & SMF_MoveAssignment) ||
  1087. (needsImplicitMoveAssignment() &&
  1088. !(data().HasTrivialSpecialMembers & SMF_MoveAssignment));
  1089. }
  1090. /// Determine whether a defaulted default constructor for this class
  1091. /// would be constexpr.
  1092. bool defaultedDestructorIsConstexpr() const {
  1093. return data().DefaultedDestructorIsConstexpr &&
  1094. getLangOpts().CPlusPlus20;
  1095. }
  1096. /// Determine whether this class has a constexpr destructor.
  1097. bool hasConstexprDestructor() const;
  1098. /// Determine whether this class has a trivial destructor
  1099. /// (C++ [class.dtor]p3)
  1100. bool hasTrivialDestructor() const {
  1101. return data().HasTrivialSpecialMembers & SMF_Destructor;
  1102. }
  1103. bool hasTrivialDestructorForCall() const {
  1104. return data().HasTrivialSpecialMembersForCall & SMF_Destructor;
  1105. }
  1106. /// Determine whether this class has a non-trivial destructor
  1107. /// (C++ [class.dtor]p3)
  1108. bool hasNonTrivialDestructor() const {
  1109. return !(data().HasTrivialSpecialMembers & SMF_Destructor);
  1110. }
  1111. bool hasNonTrivialDestructorForCall() const {
  1112. return !(data().HasTrivialSpecialMembersForCall & SMF_Destructor);
  1113. }
  1114. void setHasTrivialSpecialMemberForCall() {
  1115. data().HasTrivialSpecialMembersForCall =
  1116. (SMF_CopyConstructor | SMF_MoveConstructor | SMF_Destructor);
  1117. }
  1118. /// Determine whether declaring a const variable with this type is ok
  1119. /// per core issue 253.
  1120. bool allowConstDefaultInit() const {
  1121. return !data().HasUninitializedFields ||
  1122. !(data().HasDefaultedDefaultConstructor ||
  1123. needsImplicitDefaultConstructor());
  1124. }
  1125. /// Determine whether this class has a destructor which has no
  1126. /// semantic effect.
  1127. ///
  1128. /// Any such destructor will be trivial, public, defaulted and not deleted,
  1129. /// and will call only irrelevant destructors.
  1130. bool hasIrrelevantDestructor() const {
  1131. return data().HasIrrelevantDestructor;
  1132. }
  1133. /// Determine whether this class has a non-literal or/ volatile type
  1134. /// non-static data member or base class.
  1135. bool hasNonLiteralTypeFieldsOrBases() const {
  1136. return data().HasNonLiteralTypeFieldsOrBases;
  1137. }
  1138. /// Determine whether this class has a using-declaration that names
  1139. /// a user-declared base class constructor.
  1140. bool hasInheritedConstructor() const {
  1141. return data().HasInheritedConstructor;
  1142. }
  1143. /// Determine whether this class has a using-declaration that names
  1144. /// a base class assignment operator.
  1145. bool hasInheritedAssignment() const {
  1146. return data().HasInheritedAssignment;
  1147. }
  1148. /// Determine whether this class is considered trivially copyable per
  1149. /// (C++11 [class]p6).
  1150. bool isTriviallyCopyable() const;
  1151. /// Determine whether this class is considered trivial.
  1152. ///
  1153. /// C++11 [class]p6:
  1154. /// "A trivial class is a class that has a trivial default constructor and
  1155. /// is trivially copyable."
  1156. bool isTrivial() const {
  1157. return isTriviallyCopyable() && hasTrivialDefaultConstructor();
  1158. }
  1159. /// Determine whether this class is a literal type.
  1160. ///
  1161. /// C++11 [basic.types]p10:
  1162. /// A class type that has all the following properties:
  1163. /// - it has a trivial destructor
  1164. /// - every constructor call and full-expression in the
  1165. /// brace-or-equal-intializers for non-static data members (if any) is
  1166. /// a constant expression.
  1167. /// - it is an aggregate type or has at least one constexpr constructor
  1168. /// or constructor template that is not a copy or move constructor, and
  1169. /// - all of its non-static data members and base classes are of literal
  1170. /// types
  1171. ///
  1172. /// We resolve DR1361 by ignoring the second bullet. We resolve DR1452 by
  1173. /// treating types with trivial default constructors as literal types.
  1174. ///
  1175. /// Only in C++17 and beyond, are lambdas literal types.
  1176. bool isLiteral() const {
  1177. const LangOptions &LangOpts = getLangOpts();
  1178. return (LangOpts.CPlusPlus20 ? hasConstexprDestructor()
  1179. : hasTrivialDestructor()) &&
  1180. (!isLambda() || LangOpts.CPlusPlus17) &&
  1181. !hasNonLiteralTypeFieldsOrBases() &&
  1182. (isAggregate() || isLambda() ||
  1183. hasConstexprNonCopyMoveConstructor() ||
  1184. hasTrivialDefaultConstructor());
  1185. }
  1186. /// Determine whether this is a structural type.
  1187. bool isStructural() const {
  1188. return isLiteral() && data().StructuralIfLiteral;
  1189. }
  1190. /// If this record is an instantiation of a member class,
  1191. /// retrieves the member class from which it was instantiated.
  1192. ///
  1193. /// This routine will return non-null for (non-templated) member
  1194. /// classes of class templates. For example, given:
  1195. ///
  1196. /// \code
  1197. /// template<typename T>
  1198. /// struct X {
  1199. /// struct A { };
  1200. /// };
  1201. /// \endcode
  1202. ///
  1203. /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl
  1204. /// whose parent is the class template specialization X<int>. For
  1205. /// this declaration, getInstantiatedFromMemberClass() will return
  1206. /// the CXXRecordDecl X<T>::A. When a complete definition of
  1207. /// X<int>::A is required, it will be instantiated from the
  1208. /// declaration returned by getInstantiatedFromMemberClass().
  1209. CXXRecordDecl *getInstantiatedFromMemberClass() const;
  1210. /// If this class is an instantiation of a member class of a
  1211. /// class template specialization, retrieves the member specialization
  1212. /// information.
  1213. MemberSpecializationInfo *getMemberSpecializationInfo() const;
  1214. /// Specify that this record is an instantiation of the
  1215. /// member class \p RD.
  1216. void setInstantiationOfMemberClass(CXXRecordDecl *RD,
  1217. TemplateSpecializationKind TSK);
  1218. /// Retrieves the class template that is described by this
  1219. /// class declaration.
  1220. ///
  1221. /// Every class template is represented as a ClassTemplateDecl and a
  1222. /// CXXRecordDecl. The former contains template properties (such as
  1223. /// the template parameter lists) while the latter contains the
  1224. /// actual description of the template's
  1225. /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the
  1226. /// CXXRecordDecl that from a ClassTemplateDecl, while
  1227. /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from
  1228. /// a CXXRecordDecl.
  1229. ClassTemplateDecl *getDescribedClassTemplate() const;
  1230. void setDescribedClassTemplate(ClassTemplateDecl *Template);
  1231. /// Determine whether this particular class is a specialization or
  1232. /// instantiation of a class template or member class of a class template,
  1233. /// and how it was instantiated or specialized.
  1234. TemplateSpecializationKind getTemplateSpecializationKind() const;
  1235. /// Set the kind of specialization or template instantiation this is.
  1236. void setTemplateSpecializationKind(TemplateSpecializationKind TSK);
  1237. /// Retrieve the record declaration from which this record could be
  1238. /// instantiated. Returns null if this class is not a template instantiation.
  1239. const CXXRecordDecl *getTemplateInstantiationPattern() const;
  1240. CXXRecordDecl *getTemplateInstantiationPattern() {
  1241. return const_cast<CXXRecordDecl *>(const_cast<const CXXRecordDecl *>(this)
  1242. ->getTemplateInstantiationPattern());
  1243. }
  1244. /// Returns the destructor decl for this class.
  1245. CXXDestructorDecl *getDestructor() const;
  1246. /// Returns true if the class destructor, or any implicitly invoked
  1247. /// destructors are marked noreturn.
  1248. bool isAnyDestructorNoReturn() const { return data().IsAnyDestructorNoReturn; }
  1249. /// If the class is a local class [class.local], returns
  1250. /// the enclosing function declaration.
  1251. const FunctionDecl *isLocalClass() const {
  1252. if (const auto *RD = dyn_cast<CXXRecordDecl>(getDeclContext()))
  1253. return RD->isLocalClass();
  1254. return dyn_cast<FunctionDecl>(getDeclContext());
  1255. }
  1256. FunctionDecl *isLocalClass() {
  1257. return const_cast<FunctionDecl*>(
  1258. const_cast<const CXXRecordDecl*>(this)->isLocalClass());
  1259. }
  1260. /// Determine whether this dependent class is a current instantiation,
  1261. /// when viewed from within the given context.
  1262. bool isCurrentInstantiation(const DeclContext *CurContext) const;
  1263. /// Determine whether this class is derived from the class \p Base.
  1264. ///
  1265. /// This routine only determines whether this class is derived from \p Base,
  1266. /// but does not account for factors that may make a Derived -> Base class
  1267. /// ill-formed, such as private/protected inheritance or multiple, ambiguous
  1268. /// base class subobjects.
  1269. ///
  1270. /// \param Base the base class we are searching for.
  1271. ///
  1272. /// \returns true if this class is derived from Base, false otherwise.
  1273. bool isDerivedFrom(const CXXRecordDecl *Base) const;
  1274. /// Determine whether this class is derived from the type \p Base.
  1275. ///
  1276. /// This routine only determines whether this class is derived from \p Base,
  1277. /// but does not account for factors that may make a Derived -> Base class
  1278. /// ill-formed, such as private/protected inheritance or multiple, ambiguous
  1279. /// base class subobjects.
  1280. ///
  1281. /// \param Base the base class we are searching for.
  1282. ///
  1283. /// \param Paths will contain the paths taken from the current class to the
  1284. /// given \p Base class.
  1285. ///
  1286. /// \returns true if this class is derived from \p Base, false otherwise.
  1287. ///
  1288. /// \todo add a separate parameter to configure IsDerivedFrom, rather than
  1289. /// tangling input and output in \p Paths
  1290. bool isDerivedFrom(const CXXRecordDecl *Base, CXXBasePaths &Paths) const;
  1291. /// Determine whether this class is virtually derived from
  1292. /// the class \p Base.
  1293. ///
  1294. /// This routine only determines whether this class is virtually
  1295. /// derived from \p Base, but does not account for factors that may
  1296. /// make a Derived -> Base class ill-formed, such as
  1297. /// private/protected inheritance or multiple, ambiguous base class
  1298. /// subobjects.
  1299. ///
  1300. /// \param Base the base class we are searching for.
  1301. ///
  1302. /// \returns true if this class is virtually derived from Base,
  1303. /// false otherwise.
  1304. bool isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const;
  1305. /// Determine whether this class is provably not derived from
  1306. /// the type \p Base.
  1307. bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const;
  1308. /// Function type used by forallBases() as a callback.
  1309. ///
  1310. /// \param BaseDefinition the definition of the base class
  1311. ///
  1312. /// \returns true if this base matched the search criteria
  1313. using ForallBasesCallback =
  1314. llvm::function_ref<bool(const CXXRecordDecl *BaseDefinition)>;
  1315. /// Determines if the given callback holds for all the direct
  1316. /// or indirect base classes of this type.
  1317. ///
  1318. /// The class itself does not count as a base class. This routine
  1319. /// returns false if the class has non-computable base classes.
  1320. ///
  1321. /// \param BaseMatches Callback invoked for each (direct or indirect) base
  1322. /// class of this type until a call returns false.
  1323. bool forallBases(ForallBasesCallback BaseMatches) const;
  1324. /// Function type used by lookupInBases() to determine whether a
  1325. /// specific base class subobject matches the lookup criteria.
  1326. ///
  1327. /// \param Specifier the base-class specifier that describes the inheritance
  1328. /// from the base class we are trying to match.
  1329. ///
  1330. /// \param Path the current path, from the most-derived class down to the
  1331. /// base named by the \p Specifier.
  1332. ///
  1333. /// \returns true if this base matched the search criteria, false otherwise.
  1334. using BaseMatchesCallback =
  1335. llvm::function_ref<bool(const CXXBaseSpecifier *Specifier,
  1336. CXXBasePath &Path)>;
  1337. /// Look for entities within the base classes of this C++ class,
  1338. /// transitively searching all base class subobjects.
  1339. ///
  1340. /// This routine uses the callback function \p BaseMatches to find base
  1341. /// classes meeting some search criteria, walking all base class subobjects
  1342. /// and populating the given \p Paths structure with the paths through the
  1343. /// inheritance hierarchy that resulted in a match. On a successful search,
  1344. /// the \p Paths structure can be queried to retrieve the matching paths and
  1345. /// to determine if there were any ambiguities.
  1346. ///
  1347. /// \param BaseMatches callback function used to determine whether a given
  1348. /// base matches the user-defined search criteria.
  1349. ///
  1350. /// \param Paths used to record the paths from this class to its base class
  1351. /// subobjects that match the search criteria.
  1352. ///
  1353. /// \param LookupInDependent can be set to true to extend the search to
  1354. /// dependent base classes.
  1355. ///
  1356. /// \returns true if there exists any path from this class to a base class
  1357. /// subobject that matches the search criteria.
  1358. bool lookupInBases(BaseMatchesCallback BaseMatches, CXXBasePaths &Paths,
  1359. bool LookupInDependent = false) const;
  1360. /// Base-class lookup callback that determines whether the given
  1361. /// base class specifier refers to a specific class declaration.
  1362. ///
  1363. /// This callback can be used with \c lookupInBases() to determine whether
  1364. /// a given derived class has is a base class subobject of a particular type.
  1365. /// The base record pointer should refer to the canonical CXXRecordDecl of the
  1366. /// base class that we are searching for.
  1367. static bool FindBaseClass(const CXXBaseSpecifier *Specifier,
  1368. CXXBasePath &Path, const CXXRecordDecl *BaseRecord);
  1369. /// Base-class lookup callback that determines whether the
  1370. /// given base class specifier refers to a specific class
  1371. /// declaration and describes virtual derivation.
  1372. ///
  1373. /// This callback can be used with \c lookupInBases() to determine
  1374. /// whether a given derived class has is a virtual base class
  1375. /// subobject of a particular type. The base record pointer should
  1376. /// refer to the canonical CXXRecordDecl of the base class that we
  1377. /// are searching for.
  1378. static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
  1379. CXXBasePath &Path,
  1380. const CXXRecordDecl *BaseRecord);
  1381. /// Retrieve the final overriders for each virtual member
  1382. /// function in the class hierarchy where this class is the
  1383. /// most-derived class in the class hierarchy.
  1384. void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const;
  1385. /// Get the indirect primary bases for this class.
  1386. void getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const;
  1387. /// Determine whether this class has a member with the given name, possibly
  1388. /// in a non-dependent base class.
  1389. ///
  1390. /// No check for ambiguity is performed, so this should never be used when
  1391. /// implementing language semantics, but it may be appropriate for warnings,
  1392. /// static analysis, or similar.
  1393. bool hasMemberName(DeclarationName N) const;
  1394. /// Performs an imprecise lookup of a dependent name in this class.
  1395. ///
  1396. /// This function does not follow strict semantic rules and should be used
  1397. /// only when lookup rules can be relaxed, e.g. indexing.
  1398. std::vector<const NamedDecl *>
  1399. lookupDependentName(DeclarationName Name,
  1400. llvm::function_ref<bool(const NamedDecl *ND)> Filter);
  1401. /// Renders and displays an inheritance diagram
  1402. /// for this C++ class and all of its base classes (transitively) using
  1403. /// GraphViz.
  1404. void viewInheritance(ASTContext& Context) const;
  1405. /// Calculates the access of a decl that is reached
  1406. /// along a path.
  1407. static AccessSpecifier MergeAccess(AccessSpecifier PathAccess,
  1408. AccessSpecifier DeclAccess) {
  1409. assert(DeclAccess != AS_none);
  1410. if (DeclAccess == AS_private) return AS_none;
  1411. return (PathAccess > DeclAccess ? PathAccess : DeclAccess);
  1412. }
  1413. /// Indicates that the declaration of a defaulted or deleted special
  1414. /// member function is now complete.
  1415. void finishedDefaultedOrDeletedMember(CXXMethodDecl *MD);
  1416. void setTrivialForCallFlags(CXXMethodDecl *MD);
  1417. /// Indicates that the definition of this class is now complete.
  1418. void completeDefinition() override;
  1419. /// Indicates that the definition of this class is now complete,
  1420. /// and provides a final overrider map to help determine
  1421. ///
  1422. /// \param FinalOverriders The final overrider map for this class, which can
  1423. /// be provided as an optimization for abstract-class checking. If NULL,
  1424. /// final overriders will be computed if they are needed to complete the
  1425. /// definition.
  1426. void completeDefinition(CXXFinalOverriderMap *FinalOverriders);
  1427. /// Determine whether this class may end up being abstract, even though
  1428. /// it is not yet known to be abstract.
  1429. ///
  1430. /// \returns true if this class is not known to be abstract but has any
  1431. /// base classes that are abstract. In this case, \c completeDefinition()
  1432. /// will need to compute final overriders to determine whether the class is
  1433. /// actually abstract.
  1434. bool mayBeAbstract() const;
  1435. /// Determine whether it's impossible for a class to be derived from this
  1436. /// class. This is best-effort, and may conservatively return false.
  1437. bool isEffectivelyFinal() const;
  1438. /// If this is the closure type of a lambda expression, retrieve the
  1439. /// number to be used for name mangling in the Itanium C++ ABI.
  1440. ///
  1441. /// Zero indicates that this closure type has internal linkage, so the
  1442. /// mangling number does not matter, while a non-zero value indicates which
  1443. /// lambda expression this is in this particular context.
  1444. unsigned getLambdaManglingNumber() const {
  1445. assert(isLambda() && "Not a lambda closure type!");
  1446. return getLambdaData().ManglingNumber;
  1447. }
  1448. /// The lambda is known to has internal linkage no matter whether it has name
  1449. /// mangling number.
  1450. bool hasKnownLambdaInternalLinkage() const {
  1451. assert(isLambda() && "Not a lambda closure type!");
  1452. return getLambdaData().HasKnownInternalLinkage;
  1453. }
  1454. /// Retrieve the declaration that provides additional context for a
  1455. /// lambda, when the normal declaration context is not specific enough.
  1456. ///
  1457. /// Certain contexts (default arguments of in-class function parameters and
  1458. /// the initializers of data members) have separate name mangling rules for
  1459. /// lambdas within the Itanium C++ ABI. For these cases, this routine provides
  1460. /// the declaration in which the lambda occurs, e.g., the function parameter
  1461. /// or the non-static data member. Otherwise, it returns NULL to imply that
  1462. /// the declaration context suffices.
  1463. Decl *getLambdaContextDecl() const;
  1464. /// Set the mangling number and context declaration for a lambda
  1465. /// class.
  1466. void setLambdaMangling(unsigned ManglingNumber, Decl *ContextDecl,
  1467. bool HasKnownInternalLinkage = false) {
  1468. assert(isLambda() && "Not a lambda closure type!");
  1469. getLambdaData().ManglingNumber = ManglingNumber;
  1470. getLambdaData().ContextDecl = ContextDecl;
  1471. getLambdaData().HasKnownInternalLinkage = HasKnownInternalLinkage;
  1472. }
  1473. /// Set the device side mangling number.
  1474. void setDeviceLambdaManglingNumber(unsigned Num) const;
  1475. /// Retrieve the device side mangling number.
  1476. unsigned getDeviceLambdaManglingNumber() const;
  1477. /// Returns the inheritance model used for this record.
  1478. MSInheritanceModel getMSInheritanceModel() const;
  1479. /// Calculate what the inheritance model would be for this class.
  1480. MSInheritanceModel calculateInheritanceModel() const;
  1481. /// In the Microsoft C++ ABI, use zero for the field offset of a null data
  1482. /// member pointer if we can guarantee that zero is not a valid field offset,
  1483. /// or if the member pointer has multiple fields. Polymorphic classes have a
  1484. /// vfptr at offset zero, so we can use zero for null. If there are multiple
  1485. /// fields, we can use zero even if it is a valid field offset because
  1486. /// null-ness testing will check the other fields.
  1487. bool nullFieldOffsetIsZero() const;
  1488. /// Controls when vtordisps will be emitted if this record is used as a
  1489. /// virtual base.
  1490. MSVtorDispMode getMSVtorDispMode() const;
  1491. /// Determine whether this lambda expression was known to be dependent
  1492. /// at the time it was created, even if its context does not appear to be
  1493. /// dependent.
  1494. ///
  1495. /// This flag is a workaround for an issue with parsing, where default
  1496. /// arguments are parsed before their enclosing function declarations have
  1497. /// been created. This means that any lambda expressions within those
  1498. /// default arguments will have as their DeclContext the context enclosing
  1499. /// the function declaration, which may be non-dependent even when the
  1500. /// function declaration itself is dependent. This flag indicates when we
  1501. /// know that the lambda is dependent despite that.
  1502. bool isDependentLambda() const {
  1503. return isLambda() && getLambdaData().Dependent;
  1504. }
  1505. TypeSourceInfo *getLambdaTypeInfo() const {
  1506. return getLambdaData().MethodTyInfo;
  1507. }
  1508. // Determine whether this type is an Interface Like type for
  1509. // __interface inheritance purposes.
  1510. bool isInterfaceLike() const;
  1511. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1512. static bool classofKind(Kind K) {
  1513. return K >= firstCXXRecord && K <= lastCXXRecord;
  1514. }
  1515. void markAbstract() { data().Abstract = true; }
  1516. };
  1517. /// Store information needed for an explicit specifier.
  1518. /// Used by CXXDeductionGuideDecl, CXXConstructorDecl and CXXConversionDecl.
  1519. class ExplicitSpecifier {
  1520. llvm::PointerIntPair<Expr *, 2, ExplicitSpecKind> ExplicitSpec{
  1521. nullptr, ExplicitSpecKind::ResolvedFalse};
  1522. public:
  1523. ExplicitSpecifier() = default;
  1524. ExplicitSpecifier(Expr *Expression, ExplicitSpecKind Kind)
  1525. : ExplicitSpec(Expression, Kind) {}
  1526. ExplicitSpecKind getKind() const { return ExplicitSpec.getInt(); }
  1527. const Expr *getExpr() const { return ExplicitSpec.getPointer(); }
  1528. Expr *getExpr() { return ExplicitSpec.getPointer(); }
  1529. /// Determine if the declaration had an explicit specifier of any kind.
  1530. bool isSpecified() const {
  1531. return ExplicitSpec.getInt() != ExplicitSpecKind::ResolvedFalse ||
  1532. ExplicitSpec.getPointer();
  1533. }
  1534. /// Check for equivalence of explicit specifiers.
  1535. /// \return true if the explicit specifier are equivalent, false otherwise.
  1536. bool isEquivalent(const ExplicitSpecifier Other) const;
  1537. /// Determine whether this specifier is known to correspond to an explicit
  1538. /// declaration. Returns false if the specifier is absent or has an
  1539. /// expression that is value-dependent or evaluates to false.
  1540. bool isExplicit() const {
  1541. return ExplicitSpec.getInt() == ExplicitSpecKind::ResolvedTrue;
  1542. }
  1543. /// Determine if the explicit specifier is invalid.
  1544. /// This state occurs after a substitution failures.
  1545. bool isInvalid() const {
  1546. return ExplicitSpec.getInt() == ExplicitSpecKind::Unresolved &&
  1547. !ExplicitSpec.getPointer();
  1548. }
  1549. void setKind(ExplicitSpecKind Kind) { ExplicitSpec.setInt(Kind); }
  1550. void setExpr(Expr *E) { ExplicitSpec.setPointer(E); }
  1551. // Retrieve the explicit specifier in the given declaration, if any.
  1552. static ExplicitSpecifier getFromDecl(FunctionDecl *Function);
  1553. static const ExplicitSpecifier getFromDecl(const FunctionDecl *Function) {
  1554. return getFromDecl(const_cast<FunctionDecl *>(Function));
  1555. }
  1556. static ExplicitSpecifier Invalid() {
  1557. return ExplicitSpecifier(nullptr, ExplicitSpecKind::Unresolved);
  1558. }
  1559. };
  1560. /// Represents a C++ deduction guide declaration.
  1561. ///
  1562. /// \code
  1563. /// template<typename T> struct A { A(); A(T); };
  1564. /// A() -> A<int>;
  1565. /// \endcode
  1566. ///
  1567. /// In this example, there will be an explicit deduction guide from the
  1568. /// second line, and implicit deduction guide templates synthesized from
  1569. /// the constructors of \c A.
  1570. class CXXDeductionGuideDecl : public FunctionDecl {
  1571. void anchor() override;
  1572. private:
  1573. CXXDeductionGuideDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
  1574. ExplicitSpecifier ES,
  1575. const DeclarationNameInfo &NameInfo, QualType T,
  1576. TypeSourceInfo *TInfo, SourceLocation EndLocation,
  1577. CXXConstructorDecl *Ctor)
  1578. : FunctionDecl(CXXDeductionGuide, C, DC, StartLoc, NameInfo, T, TInfo,
  1579. SC_None, false, false, ConstexprSpecKind::Unspecified),
  1580. Ctor(Ctor), ExplicitSpec(ES) {
  1581. if (EndLocation.isValid())
  1582. setRangeEnd(EndLocation);
  1583. setIsCopyDeductionCandidate(false);
  1584. }
  1585. CXXConstructorDecl *Ctor;
  1586. ExplicitSpecifier ExplicitSpec;
  1587. void setExplicitSpecifier(ExplicitSpecifier ES) { ExplicitSpec = ES; }
  1588. public:
  1589. friend class ASTDeclReader;
  1590. friend class ASTDeclWriter;
  1591. static CXXDeductionGuideDecl *
  1592. Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
  1593. ExplicitSpecifier ES, const DeclarationNameInfo &NameInfo, QualType T,
  1594. TypeSourceInfo *TInfo, SourceLocation EndLocation,
  1595. CXXConstructorDecl *Ctor = nullptr);
  1596. static CXXDeductionGuideDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  1597. ExplicitSpecifier getExplicitSpecifier() { return ExplicitSpec; }
  1598. const ExplicitSpecifier getExplicitSpecifier() const { return ExplicitSpec; }
  1599. /// Return true if the declartion is already resolved to be explicit.
  1600. bool isExplicit() const { return ExplicitSpec.isExplicit(); }
  1601. /// Get the template for which this guide performs deduction.
  1602. TemplateDecl *getDeducedTemplate() const {
  1603. return getDeclName().getCXXDeductionGuideTemplate();
  1604. }
  1605. /// Get the constructor from which this deduction guide was generated, if
  1606. /// this is an implicit deduction guide.
  1607. CXXConstructorDecl *getCorrespondingConstructor() const {
  1608. return Ctor;
  1609. }
  1610. void setIsCopyDeductionCandidate(bool isCDC = true) {
  1611. FunctionDeclBits.IsCopyDeductionCandidate = isCDC;
  1612. }
  1613. bool isCopyDeductionCandidate() const {
  1614. return FunctionDeclBits.IsCopyDeductionCandidate;
  1615. }
  1616. // Implement isa/cast/dyncast/etc.
  1617. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1618. static bool classofKind(Kind K) { return K == CXXDeductionGuide; }
  1619. };
  1620. /// \brief Represents the body of a requires-expression.
  1621. ///
  1622. /// This decl exists merely to serve as the DeclContext for the local
  1623. /// parameters of the requires expression as well as other declarations inside
  1624. /// it.
  1625. ///
  1626. /// \code
  1627. /// template<typename T> requires requires (T t) { {t++} -> regular; }
  1628. /// \endcode
  1629. ///
  1630. /// In this example, a RequiresExpr object will be generated for the expression,
  1631. /// and a RequiresExprBodyDecl will be created to hold the parameter t and the
  1632. /// template argument list imposed by the compound requirement.
  1633. class RequiresExprBodyDecl : public Decl, public DeclContext {
  1634. RequiresExprBodyDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc)
  1635. : Decl(RequiresExprBody, DC, StartLoc), DeclContext(RequiresExprBody) {}
  1636. public:
  1637. friend class ASTDeclReader;
  1638. friend class ASTDeclWriter;
  1639. static RequiresExprBodyDecl *Create(ASTContext &C, DeclContext *DC,
  1640. SourceLocation StartLoc);
  1641. static RequiresExprBodyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  1642. // Implement isa/cast/dyncast/etc.
  1643. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1644. static bool classofKind(Kind K) { return K == RequiresExprBody; }
  1645. };
  1646. /// Represents a static or instance method of a struct/union/class.
  1647. ///
  1648. /// In the terminology of the C++ Standard, these are the (static and
  1649. /// non-static) member functions, whether virtual or not.
  1650. class CXXMethodDecl : public FunctionDecl {
  1651. void anchor() override;
  1652. protected:
  1653. CXXMethodDecl(Kind DK, ASTContext &C, CXXRecordDecl *RD,
  1654. SourceLocation StartLoc, const DeclarationNameInfo &NameInfo,
  1655. QualType T, TypeSourceInfo *TInfo, StorageClass SC,
  1656. bool UsesFPIntrin, bool isInline,
  1657. ConstexprSpecKind ConstexprKind, SourceLocation EndLocation,
  1658. Expr *TrailingRequiresClause = nullptr)
  1659. : FunctionDecl(DK, C, RD, StartLoc, NameInfo, T, TInfo, SC, UsesFPIntrin,
  1660. isInline, ConstexprKind, TrailingRequiresClause) {
  1661. if (EndLocation.isValid())
  1662. setRangeEnd(EndLocation);
  1663. }
  1664. public:
  1665. static CXXMethodDecl *
  1666. Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
  1667. const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
  1668. StorageClass SC, bool UsesFPIntrin, bool isInline,
  1669. ConstexprSpecKind ConstexprKind, SourceLocation EndLocation,
  1670. Expr *TrailingRequiresClause = nullptr);
  1671. static CXXMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  1672. bool isStatic() const;
  1673. bool isInstance() const { return !isStatic(); }
  1674. /// Returns true if the given operator is implicitly static in a record
  1675. /// context.
  1676. static bool isStaticOverloadedOperator(OverloadedOperatorKind OOK) {
  1677. // [class.free]p1:
  1678. // Any allocation function for a class T is a static member
  1679. // (even if not explicitly declared static).
  1680. // [class.free]p6 Any deallocation function for a class X is a static member
  1681. // (even if not explicitly declared static).
  1682. return OOK == OO_New || OOK == OO_Array_New || OOK == OO_Delete ||
  1683. OOK == OO_Array_Delete;
  1684. }
  1685. bool isConst() const { return getType()->castAs<FunctionType>()->isConst(); }
  1686. bool isVolatile() const { return getType()->castAs<FunctionType>()->isVolatile(); }
  1687. bool isVirtual() const {
  1688. CXXMethodDecl *CD = const_cast<CXXMethodDecl*>(this)->getCanonicalDecl();
  1689. // Member function is virtual if it is marked explicitly so, or if it is
  1690. // declared in __interface -- then it is automatically pure virtual.
  1691. if (CD->isVirtualAsWritten() || CD->isPure())
  1692. return true;
  1693. return CD->size_overridden_methods() != 0;
  1694. }
  1695. /// If it's possible to devirtualize a call to this method, return the called
  1696. /// function. Otherwise, return null.
  1697. /// \param Base The object on which this virtual function is called.
  1698. /// \param IsAppleKext True if we are compiling for Apple kext.
  1699. CXXMethodDecl *getDevirtualizedMethod(const Expr *Base, bool IsAppleKext);
  1700. const CXXMethodDecl *getDevirtualizedMethod(const Expr *Base,
  1701. bool IsAppleKext) const {
  1702. return const_cast<CXXMethodDecl *>(this)->getDevirtualizedMethod(
  1703. Base, IsAppleKext);
  1704. }
  1705. /// Determine whether this is a usual deallocation function (C++
  1706. /// [basic.stc.dynamic.deallocation]p2), which is an overloaded delete or
  1707. /// delete[] operator with a particular signature. Populates \p PreventedBy
  1708. /// with the declarations of the functions of the same kind if they were the
  1709. /// reason for this function returning false. This is used by
  1710. /// Sema::isUsualDeallocationFunction to reconsider the answer based on the
  1711. /// context.
  1712. bool isUsualDeallocationFunction(
  1713. SmallVectorImpl<const FunctionDecl *> &PreventedBy) const;
  1714. /// Determine whether this is a copy-assignment operator, regardless
  1715. /// of whether it was declared implicitly or explicitly.
  1716. bool isCopyAssignmentOperator() const;
  1717. /// Determine whether this is a move assignment operator.
  1718. bool isMoveAssignmentOperator() const;
  1719. CXXMethodDecl *getCanonicalDecl() override {
  1720. return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl());
  1721. }
  1722. const CXXMethodDecl *getCanonicalDecl() const {
  1723. return const_cast<CXXMethodDecl*>(this)->getCanonicalDecl();
  1724. }
  1725. CXXMethodDecl *getMostRecentDecl() {
  1726. return cast<CXXMethodDecl>(
  1727. static_cast<FunctionDecl *>(this)->getMostRecentDecl());
  1728. }
  1729. const CXXMethodDecl *getMostRecentDecl() const {
  1730. return const_cast<CXXMethodDecl*>(this)->getMostRecentDecl();
  1731. }
  1732. void addOverriddenMethod(const CXXMethodDecl *MD);
  1733. using method_iterator = const CXXMethodDecl *const *;
  1734. method_iterator begin_overridden_methods() const;
  1735. method_iterator end_overridden_methods() const;
  1736. unsigned size_overridden_methods() const;
  1737. using overridden_method_range = llvm::iterator_range<
  1738. llvm::TinyPtrVector<const CXXMethodDecl *>::const_iterator>;
  1739. overridden_method_range overridden_methods() const;
  1740. /// Return the parent of this method declaration, which
  1741. /// is the class in which this method is defined.
  1742. const CXXRecordDecl *getParent() const {
  1743. return cast<CXXRecordDecl>(FunctionDecl::getParent());
  1744. }
  1745. /// Return the parent of this method declaration, which
  1746. /// is the class in which this method is defined.
  1747. CXXRecordDecl *getParent() {
  1748. return const_cast<CXXRecordDecl *>(
  1749. cast<CXXRecordDecl>(FunctionDecl::getParent()));
  1750. }
  1751. /// Return the type of the \c this pointer.
  1752. ///
  1753. /// Should only be called for instance (i.e., non-static) methods. Note
  1754. /// that for the call operator of a lambda closure type, this returns the
  1755. /// desugared 'this' type (a pointer to the closure type), not the captured
  1756. /// 'this' type.
  1757. QualType getThisType() const;
  1758. /// Return the type of the object pointed by \c this.
  1759. ///
  1760. /// See getThisType() for usage restriction.
  1761. QualType getThisObjectType() const;
  1762. static QualType getThisType(const FunctionProtoType *FPT,
  1763. const CXXRecordDecl *Decl);
  1764. static QualType getThisObjectType(const FunctionProtoType *FPT,
  1765. const CXXRecordDecl *Decl);
  1766. Qualifiers getMethodQualifiers() const {
  1767. return getType()->castAs<FunctionProtoType>()->getMethodQuals();
  1768. }
  1769. /// Retrieve the ref-qualifier associated with this method.
  1770. ///
  1771. /// In the following example, \c f() has an lvalue ref-qualifier, \c g()
  1772. /// has an rvalue ref-qualifier, and \c h() has no ref-qualifier.
  1773. /// @code
  1774. /// struct X {
  1775. /// void f() &;
  1776. /// void g() &&;
  1777. /// void h();
  1778. /// };
  1779. /// @endcode
  1780. RefQualifierKind getRefQualifier() const {
  1781. return getType()->castAs<FunctionProtoType>()->getRefQualifier();
  1782. }
  1783. bool hasInlineBody() const;
  1784. /// Determine whether this is a lambda closure type's static member
  1785. /// function that is used for the result of the lambda's conversion to
  1786. /// function pointer (for a lambda with no captures).
  1787. ///
  1788. /// The function itself, if used, will have a placeholder body that will be
  1789. /// supplied by IR generation to either forward to the function call operator
  1790. /// or clone the function call operator.
  1791. bool isLambdaStaticInvoker() const;
  1792. /// Find the method in \p RD that corresponds to this one.
  1793. ///
  1794. /// Find if \p RD or one of the classes it inherits from override this method.
  1795. /// If so, return it. \p RD is assumed to be a subclass of the class defining
  1796. /// this method (or be the class itself), unless \p MayBeBase is set to true.
  1797. CXXMethodDecl *
  1798. getCorrespondingMethodInClass(const CXXRecordDecl *RD,
  1799. bool MayBeBase = false);
  1800. const CXXMethodDecl *
  1801. getCorrespondingMethodInClass(const CXXRecordDecl *RD,
  1802. bool MayBeBase = false) const {
  1803. return const_cast<CXXMethodDecl *>(this)
  1804. ->getCorrespondingMethodInClass(RD, MayBeBase);
  1805. }
  1806. /// Find if \p RD declares a function that overrides this function, and if so,
  1807. /// return it. Does not search base classes.
  1808. CXXMethodDecl *getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD,
  1809. bool MayBeBase = false);
  1810. const CXXMethodDecl *
  1811. getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD,
  1812. bool MayBeBase = false) const {
  1813. return const_cast<CXXMethodDecl *>(this)
  1814. ->getCorrespondingMethodDeclaredInClass(RD, MayBeBase);
  1815. }
  1816. // Implement isa/cast/dyncast/etc.
  1817. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1818. static bool classofKind(Kind K) {
  1819. return K >= firstCXXMethod && K <= lastCXXMethod;
  1820. }
  1821. };
  1822. /// Represents a C++ base or member initializer.
  1823. ///
  1824. /// This is part of a constructor initializer that
  1825. /// initializes one non-static member variable or one base class. For
  1826. /// example, in the following, both 'A(a)' and 'f(3.14159)' are member
  1827. /// initializers:
  1828. ///
  1829. /// \code
  1830. /// class A { };
  1831. /// class B : public A {
  1832. /// float f;
  1833. /// public:
  1834. /// B(A& a) : A(a), f(3.14159) { }
  1835. /// };
  1836. /// \endcode
  1837. class CXXCtorInitializer final {
  1838. /// Either the base class name/delegating constructor type (stored as
  1839. /// a TypeSourceInfo*), an normal field (FieldDecl), or an anonymous field
  1840. /// (IndirectFieldDecl*) being initialized.
  1841. llvm::PointerUnion<TypeSourceInfo *, FieldDecl *, IndirectFieldDecl *>
  1842. Initializee;
  1843. /// The argument used to initialize the base or member, which may
  1844. /// end up constructing an object (when multiple arguments are involved).
  1845. Stmt *Init;
  1846. /// The source location for the field name or, for a base initializer
  1847. /// pack expansion, the location of the ellipsis.
  1848. ///
  1849. /// In the case of a delegating
  1850. /// constructor, it will still include the type's source location as the
  1851. /// Initializee points to the CXXConstructorDecl (to allow loop detection).
  1852. SourceLocation MemberOrEllipsisLocation;
  1853. /// Location of the left paren of the ctor-initializer.
  1854. SourceLocation LParenLoc;
  1855. /// Location of the right paren of the ctor-initializer.
  1856. SourceLocation RParenLoc;
  1857. /// If the initializee is a type, whether that type makes this
  1858. /// a delegating initialization.
  1859. unsigned IsDelegating : 1;
  1860. /// If the initializer is a base initializer, this keeps track
  1861. /// of whether the base is virtual or not.
  1862. unsigned IsVirtual : 1;
  1863. /// Whether or not the initializer is explicitly written
  1864. /// in the sources.
  1865. unsigned IsWritten : 1;
  1866. /// If IsWritten is true, then this number keeps track of the textual order
  1867. /// of this initializer in the original sources, counting from 0.
  1868. unsigned SourceOrder : 13;
  1869. public:
  1870. /// Creates a new base-class initializer.
  1871. explicit
  1872. CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo, bool IsVirtual,
  1873. SourceLocation L, Expr *Init, SourceLocation R,
  1874. SourceLocation EllipsisLoc);
  1875. /// Creates a new member initializer.
  1876. explicit
  1877. CXXCtorInitializer(ASTContext &Context, FieldDecl *Member,
  1878. SourceLocation MemberLoc, SourceLocation L, Expr *Init,
  1879. SourceLocation R);
  1880. /// Creates a new anonymous field initializer.
  1881. explicit
  1882. CXXCtorInitializer(ASTContext &Context, IndirectFieldDecl *Member,
  1883. SourceLocation MemberLoc, SourceLocation L, Expr *Init,
  1884. SourceLocation R);
  1885. /// Creates a new delegating initializer.
  1886. explicit
  1887. CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo,
  1888. SourceLocation L, Expr *Init, SourceLocation R);
  1889. /// \return Unique reproducible object identifier.
  1890. int64_t getID(const ASTContext &Context) const;
  1891. /// Determine whether this initializer is initializing a base class.
  1892. bool isBaseInitializer() const {
  1893. return Initializee.is<TypeSourceInfo*>() && !IsDelegating;
  1894. }
  1895. /// Determine whether this initializer is initializing a non-static
  1896. /// data member.
  1897. bool isMemberInitializer() const { return Initializee.is<FieldDecl*>(); }
  1898. bool isAnyMemberInitializer() const {
  1899. return isMemberInitializer() || isIndirectMemberInitializer();
  1900. }
  1901. bool isIndirectMemberInitializer() const {
  1902. return Initializee.is<IndirectFieldDecl*>();
  1903. }
  1904. /// Determine whether this initializer is an implicit initializer
  1905. /// generated for a field with an initializer defined on the member
  1906. /// declaration.
  1907. ///
  1908. /// In-class member initializers (also known as "non-static data member
  1909. /// initializations", NSDMIs) were introduced in C++11.
  1910. bool isInClassMemberInitializer() const {
  1911. return Init->getStmtClass() == Stmt::CXXDefaultInitExprClass;
  1912. }
  1913. /// Determine whether this initializer is creating a delegating
  1914. /// constructor.
  1915. bool isDelegatingInitializer() const {
  1916. return Initializee.is<TypeSourceInfo*>() && IsDelegating;
  1917. }
  1918. /// Determine whether this initializer is a pack expansion.
  1919. bool isPackExpansion() const {
  1920. return isBaseInitializer() && MemberOrEllipsisLocation.isValid();
  1921. }
  1922. // For a pack expansion, returns the location of the ellipsis.
  1923. SourceLocation getEllipsisLoc() const {
  1924. if (!isPackExpansion())
  1925. return {};
  1926. return MemberOrEllipsisLocation;
  1927. }
  1928. /// If this is a base class initializer, returns the type of the
  1929. /// base class with location information. Otherwise, returns an NULL
  1930. /// type location.
  1931. TypeLoc getBaseClassLoc() const;
  1932. /// If this is a base class initializer, returns the type of the base class.
  1933. /// Otherwise, returns null.
  1934. const Type *getBaseClass() const;
  1935. /// Returns whether the base is virtual or not.
  1936. bool isBaseVirtual() const {
  1937. assert(isBaseInitializer() && "Must call this on base initializer!");
  1938. return IsVirtual;
  1939. }
  1940. /// Returns the declarator information for a base class or delegating
  1941. /// initializer.
  1942. TypeSourceInfo *getTypeSourceInfo() const {
  1943. return Initializee.dyn_cast<TypeSourceInfo *>();
  1944. }
  1945. /// If this is a member initializer, returns the declaration of the
  1946. /// non-static data member being initialized. Otherwise, returns null.
  1947. FieldDecl *getMember() const {
  1948. if (isMemberInitializer())
  1949. return Initializee.get<FieldDecl*>();
  1950. return nullptr;
  1951. }
  1952. FieldDecl *getAnyMember() const {
  1953. if (isMemberInitializer())
  1954. return Initializee.get<FieldDecl*>();
  1955. if (isIndirectMemberInitializer())
  1956. return Initializee.get<IndirectFieldDecl*>()->getAnonField();
  1957. return nullptr;
  1958. }
  1959. IndirectFieldDecl *getIndirectMember() const {
  1960. if (isIndirectMemberInitializer())
  1961. return Initializee.get<IndirectFieldDecl*>();
  1962. return nullptr;
  1963. }
  1964. SourceLocation getMemberLocation() const {
  1965. return MemberOrEllipsisLocation;
  1966. }
  1967. /// Determine the source location of the initializer.
  1968. SourceLocation getSourceLocation() const;
  1969. /// Determine the source range covering the entire initializer.
  1970. SourceRange getSourceRange() const LLVM_READONLY;
  1971. /// Determine whether this initializer is explicitly written
  1972. /// in the source code.
  1973. bool isWritten() const { return IsWritten; }
  1974. /// Return the source position of the initializer, counting from 0.
  1975. /// If the initializer was implicit, -1 is returned.
  1976. int getSourceOrder() const {
  1977. return IsWritten ? static_cast<int>(SourceOrder) : -1;
  1978. }
  1979. /// Set the source order of this initializer.
  1980. ///
  1981. /// This can only be called once for each initializer; it cannot be called
  1982. /// on an initializer having a positive number of (implicit) array indices.
  1983. ///
  1984. /// This assumes that the initializer was written in the source code, and
  1985. /// ensures that isWritten() returns true.
  1986. void setSourceOrder(int Pos) {
  1987. assert(!IsWritten &&
  1988. "setSourceOrder() used on implicit initializer");
  1989. assert(SourceOrder == 0 &&
  1990. "calling twice setSourceOrder() on the same initializer");
  1991. assert(Pos >= 0 &&
  1992. "setSourceOrder() used to make an initializer implicit");
  1993. IsWritten = true;
  1994. SourceOrder = static_cast<unsigned>(Pos);
  1995. }
  1996. SourceLocation getLParenLoc() const { return LParenLoc; }
  1997. SourceLocation getRParenLoc() const { return RParenLoc; }
  1998. /// Get the initializer.
  1999. Expr *getInit() const { return static_cast<Expr *>(Init); }
  2000. };
  2001. /// Description of a constructor that was inherited from a base class.
  2002. class InheritedConstructor {
  2003. ConstructorUsingShadowDecl *Shadow = nullptr;
  2004. CXXConstructorDecl *BaseCtor = nullptr;
  2005. public:
  2006. InheritedConstructor() = default;
  2007. InheritedConstructor(ConstructorUsingShadowDecl *Shadow,
  2008. CXXConstructorDecl *BaseCtor)
  2009. : Shadow(Shadow), BaseCtor(BaseCtor) {}
  2010. explicit operator bool() const { return Shadow; }
  2011. ConstructorUsingShadowDecl *getShadowDecl() const { return Shadow; }
  2012. CXXConstructorDecl *getConstructor() const { return BaseCtor; }
  2013. };
  2014. /// Represents a C++ constructor within a class.
  2015. ///
  2016. /// For example:
  2017. ///
  2018. /// \code
  2019. /// class X {
  2020. /// public:
  2021. /// explicit X(int); // represented by a CXXConstructorDecl.
  2022. /// };
  2023. /// \endcode
  2024. class CXXConstructorDecl final
  2025. : public CXXMethodDecl,
  2026. private llvm::TrailingObjects<CXXConstructorDecl, InheritedConstructor,
  2027. ExplicitSpecifier> {
  2028. // This class stores some data in DeclContext::CXXConstructorDeclBits
  2029. // to save some space. Use the provided accessors to access it.
  2030. /// \name Support for base and member initializers.
  2031. /// \{
  2032. /// The arguments used to initialize the base or member.
  2033. LazyCXXCtorInitializersPtr CtorInitializers;
  2034. CXXConstructorDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
  2035. const DeclarationNameInfo &NameInfo, QualType T,
  2036. TypeSourceInfo *TInfo, ExplicitSpecifier ES,
  2037. bool UsesFPIntrin, bool isInline,
  2038. bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind,
  2039. InheritedConstructor Inherited,
  2040. Expr *TrailingRequiresClause);
  2041. void anchor() override;
  2042. size_t numTrailingObjects(OverloadToken<InheritedConstructor>) const {
  2043. return CXXConstructorDeclBits.IsInheritingConstructor;
  2044. }
  2045. size_t numTrailingObjects(OverloadToken<ExplicitSpecifier>) const {
  2046. return CXXConstructorDeclBits.HasTrailingExplicitSpecifier;
  2047. }
  2048. ExplicitSpecifier getExplicitSpecifierInternal() const {
  2049. if (CXXConstructorDeclBits.HasTrailingExplicitSpecifier)
  2050. return *getTrailingObjects<ExplicitSpecifier>();
  2051. return ExplicitSpecifier(
  2052. nullptr, CXXConstructorDeclBits.IsSimpleExplicit
  2053. ? ExplicitSpecKind::ResolvedTrue
  2054. : ExplicitSpecKind::ResolvedFalse);
  2055. }
  2056. enum TrailingAllocKind {
  2057. TAKInheritsConstructor = 1,
  2058. TAKHasTailExplicit = 1 << 1,
  2059. };
  2060. uint64_t getTrailingAllocKind() const {
  2061. return numTrailingObjects(OverloadToken<InheritedConstructor>()) |
  2062. (numTrailingObjects(OverloadToken<ExplicitSpecifier>()) << 1);
  2063. }
  2064. public:
  2065. friend class ASTDeclReader;
  2066. friend class ASTDeclWriter;
  2067. friend TrailingObjects;
  2068. static CXXConstructorDecl *CreateDeserialized(ASTContext &C, unsigned ID,
  2069. uint64_t AllocKind);
  2070. static CXXConstructorDecl *
  2071. Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
  2072. const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
  2073. ExplicitSpecifier ES, bool UsesFPIntrin, bool isInline,
  2074. bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind,
  2075. InheritedConstructor Inherited = InheritedConstructor(),
  2076. Expr *TrailingRequiresClause = nullptr);
  2077. void setExplicitSpecifier(ExplicitSpecifier ES) {
  2078. assert((!ES.getExpr() ||
  2079. CXXConstructorDeclBits.HasTrailingExplicitSpecifier) &&
  2080. "cannot set this explicit specifier. no trail-allocated space for "
  2081. "explicit");
  2082. if (ES.getExpr())
  2083. *getCanonicalDecl()->getTrailingObjects<ExplicitSpecifier>() = ES;
  2084. else
  2085. CXXConstructorDeclBits.IsSimpleExplicit = ES.isExplicit();
  2086. }
  2087. ExplicitSpecifier getExplicitSpecifier() {
  2088. return getCanonicalDecl()->getExplicitSpecifierInternal();
  2089. }
  2090. const ExplicitSpecifier getExplicitSpecifier() const {
  2091. return getCanonicalDecl()->getExplicitSpecifierInternal();
  2092. }
  2093. /// Return true if the declartion is already resolved to be explicit.
  2094. bool isExplicit() const { return getExplicitSpecifier().isExplicit(); }
  2095. /// Iterates through the member/base initializer list.
  2096. using init_iterator = CXXCtorInitializer **;
  2097. /// Iterates through the member/base initializer list.
  2098. using init_const_iterator = CXXCtorInitializer *const *;
  2099. using init_range = llvm::iterator_range<init_iterator>;
  2100. using init_const_range = llvm::iterator_range<init_const_iterator>;
  2101. init_range inits() { return init_range(init_begin(), init_end()); }
  2102. init_const_range inits() const {
  2103. return init_const_range(init_begin(), init_end());
  2104. }
  2105. /// Retrieve an iterator to the first initializer.
  2106. init_iterator init_begin() {
  2107. const auto *ConstThis = this;
  2108. return const_cast<init_iterator>(ConstThis->init_begin());
  2109. }
  2110. /// Retrieve an iterator to the first initializer.
  2111. init_const_iterator init_begin() const;
  2112. /// Retrieve an iterator past the last initializer.
  2113. init_iterator init_end() {
  2114. return init_begin() + getNumCtorInitializers();
  2115. }
  2116. /// Retrieve an iterator past the last initializer.
  2117. init_const_iterator init_end() const {
  2118. return init_begin() + getNumCtorInitializers();
  2119. }
  2120. using init_reverse_iterator = std::reverse_iterator<init_iterator>;
  2121. using init_const_reverse_iterator =
  2122. std::reverse_iterator<init_const_iterator>;
  2123. init_reverse_iterator init_rbegin() {
  2124. return init_reverse_iterator(init_end());
  2125. }
  2126. init_const_reverse_iterator init_rbegin() const {
  2127. return init_const_reverse_iterator(init_end());
  2128. }
  2129. init_reverse_iterator init_rend() {
  2130. return init_reverse_iterator(init_begin());
  2131. }
  2132. init_const_reverse_iterator init_rend() const {
  2133. return init_const_reverse_iterator(init_begin());
  2134. }
  2135. /// Determine the number of arguments used to initialize the member
  2136. /// or base.
  2137. unsigned getNumCtorInitializers() const {
  2138. return CXXConstructorDeclBits.NumCtorInitializers;
  2139. }
  2140. void setNumCtorInitializers(unsigned numCtorInitializers) {
  2141. CXXConstructorDeclBits.NumCtorInitializers = numCtorInitializers;
  2142. // This assert added because NumCtorInitializers is stored
  2143. // in CXXConstructorDeclBits as a bitfield and its width has
  2144. // been shrunk from 32 bits to fit into CXXConstructorDeclBitfields.
  2145. assert(CXXConstructorDeclBits.NumCtorInitializers ==
  2146. numCtorInitializers && "NumCtorInitializers overflow!");
  2147. }
  2148. void setCtorInitializers(CXXCtorInitializer **Initializers) {
  2149. CtorInitializers = Initializers;
  2150. }
  2151. /// Determine whether this constructor is a delegating constructor.
  2152. bool isDelegatingConstructor() const {
  2153. return (getNumCtorInitializers() == 1) &&
  2154. init_begin()[0]->isDelegatingInitializer();
  2155. }
  2156. /// When this constructor delegates to another, retrieve the target.
  2157. CXXConstructorDecl *getTargetConstructor() const;
  2158. /// Whether this constructor is a default
  2159. /// constructor (C++ [class.ctor]p5), which can be used to
  2160. /// default-initialize a class of this type.
  2161. bool isDefaultConstructor() const;
  2162. /// Whether this constructor is a copy constructor (C++ [class.copy]p2,
  2163. /// which can be used to copy the class.
  2164. ///
  2165. /// \p TypeQuals will be set to the qualifiers on the
  2166. /// argument type. For example, \p TypeQuals would be set to \c
  2167. /// Qualifiers::Const for the following copy constructor:
  2168. ///
  2169. /// \code
  2170. /// class X {
  2171. /// public:
  2172. /// X(const X&);
  2173. /// };
  2174. /// \endcode
  2175. bool isCopyConstructor(unsigned &TypeQuals) const;
  2176. /// Whether this constructor is a copy
  2177. /// constructor (C++ [class.copy]p2, which can be used to copy the
  2178. /// class.
  2179. bool isCopyConstructor() const {
  2180. unsigned TypeQuals = 0;
  2181. return isCopyConstructor(TypeQuals);
  2182. }
  2183. /// Determine whether this constructor is a move constructor
  2184. /// (C++11 [class.copy]p3), which can be used to move values of the class.
  2185. ///
  2186. /// \param TypeQuals If this constructor is a move constructor, will be set
  2187. /// to the type qualifiers on the referent of the first parameter's type.
  2188. bool isMoveConstructor(unsigned &TypeQuals) const;
  2189. /// Determine whether this constructor is a move constructor
  2190. /// (C++11 [class.copy]p3), which can be used to move values of the class.
  2191. bool isMoveConstructor() const {
  2192. unsigned TypeQuals = 0;
  2193. return isMoveConstructor(TypeQuals);
  2194. }
  2195. /// Determine whether this is a copy or move constructor.
  2196. ///
  2197. /// \param TypeQuals Will be set to the type qualifiers on the reference
  2198. /// parameter, if in fact this is a copy or move constructor.
  2199. bool isCopyOrMoveConstructor(unsigned &TypeQuals) const;
  2200. /// Determine whether this a copy or move constructor.
  2201. bool isCopyOrMoveConstructor() const {
  2202. unsigned Quals;
  2203. return isCopyOrMoveConstructor(Quals);
  2204. }
  2205. /// Whether this constructor is a
  2206. /// converting constructor (C++ [class.conv.ctor]), which can be
  2207. /// used for user-defined conversions.
  2208. bool isConvertingConstructor(bool AllowExplicit) const;
  2209. /// Determine whether this is a member template specialization that
  2210. /// would copy the object to itself. Such constructors are never used to copy
  2211. /// an object.
  2212. bool isSpecializationCopyingObject() const;
  2213. /// Determine whether this is an implicit constructor synthesized to
  2214. /// model a call to a constructor inherited from a base class.
  2215. bool isInheritingConstructor() const {
  2216. return CXXConstructorDeclBits.IsInheritingConstructor;
  2217. }
  2218. /// State that this is an implicit constructor synthesized to
  2219. /// model a call to a constructor inherited from a base class.
  2220. void setInheritingConstructor(bool isIC = true) {
  2221. CXXConstructorDeclBits.IsInheritingConstructor = isIC;
  2222. }
  2223. /// Get the constructor that this inheriting constructor is based on.
  2224. InheritedConstructor getInheritedConstructor() const {
  2225. return isInheritingConstructor() ?
  2226. *getTrailingObjects<InheritedConstructor>() : InheritedConstructor();
  2227. }
  2228. CXXConstructorDecl *getCanonicalDecl() override {
  2229. return cast<CXXConstructorDecl>(FunctionDecl::getCanonicalDecl());
  2230. }
  2231. const CXXConstructorDecl *getCanonicalDecl() const {
  2232. return const_cast<CXXConstructorDecl*>(this)->getCanonicalDecl();
  2233. }
  2234. // Implement isa/cast/dyncast/etc.
  2235. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2236. static bool classofKind(Kind K) { return K == CXXConstructor; }
  2237. };
  2238. /// Represents a C++ destructor within a class.
  2239. ///
  2240. /// For example:
  2241. ///
  2242. /// \code
  2243. /// class X {
  2244. /// public:
  2245. /// ~X(); // represented by a CXXDestructorDecl.
  2246. /// };
  2247. /// \endcode
  2248. class CXXDestructorDecl : public CXXMethodDecl {
  2249. friend class ASTDeclReader;
  2250. friend class ASTDeclWriter;
  2251. // FIXME: Don't allocate storage for these except in the first declaration
  2252. // of a virtual destructor.
  2253. FunctionDecl *OperatorDelete = nullptr;
  2254. Expr *OperatorDeleteThisArg = nullptr;
  2255. CXXDestructorDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
  2256. const DeclarationNameInfo &NameInfo, QualType T,
  2257. TypeSourceInfo *TInfo, bool UsesFPIntrin, bool isInline,
  2258. bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind,
  2259. Expr *TrailingRequiresClause = nullptr)
  2260. : CXXMethodDecl(CXXDestructor, C, RD, StartLoc, NameInfo, T, TInfo,
  2261. SC_None, UsesFPIntrin, isInline, ConstexprKind,
  2262. SourceLocation(), TrailingRequiresClause) {
  2263. setImplicit(isImplicitlyDeclared);
  2264. }
  2265. void anchor() override;
  2266. public:
  2267. static CXXDestructorDecl *
  2268. Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
  2269. const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
  2270. bool UsesFPIntrin, bool isInline, bool isImplicitlyDeclared,
  2271. ConstexprSpecKind ConstexprKind,
  2272. Expr *TrailingRequiresClause = nullptr);
  2273. static CXXDestructorDecl *CreateDeserialized(ASTContext & C, unsigned ID);
  2274. void setOperatorDelete(FunctionDecl *OD, Expr *ThisArg);
  2275. const FunctionDecl *getOperatorDelete() const {
  2276. return getCanonicalDecl()->OperatorDelete;
  2277. }
  2278. Expr *getOperatorDeleteThisArg() const {
  2279. return getCanonicalDecl()->OperatorDeleteThisArg;
  2280. }
  2281. CXXDestructorDecl *getCanonicalDecl() override {
  2282. return cast<CXXDestructorDecl>(FunctionDecl::getCanonicalDecl());
  2283. }
  2284. const CXXDestructorDecl *getCanonicalDecl() const {
  2285. return const_cast<CXXDestructorDecl*>(this)->getCanonicalDecl();
  2286. }
  2287. // Implement isa/cast/dyncast/etc.
  2288. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2289. static bool classofKind(Kind K) { return K == CXXDestructor; }
  2290. };
  2291. /// Represents a C++ conversion function within a class.
  2292. ///
  2293. /// For example:
  2294. ///
  2295. /// \code
  2296. /// class X {
  2297. /// public:
  2298. /// operator bool();
  2299. /// };
  2300. /// \endcode
  2301. class CXXConversionDecl : public CXXMethodDecl {
  2302. CXXConversionDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
  2303. const DeclarationNameInfo &NameInfo, QualType T,
  2304. TypeSourceInfo *TInfo, bool UsesFPIntrin, bool isInline,
  2305. ExplicitSpecifier ES, ConstexprSpecKind ConstexprKind,
  2306. SourceLocation EndLocation,
  2307. Expr *TrailingRequiresClause = nullptr)
  2308. : CXXMethodDecl(CXXConversion, C, RD, StartLoc, NameInfo, T, TInfo,
  2309. SC_None, UsesFPIntrin, isInline, ConstexprKind,
  2310. EndLocation, TrailingRequiresClause),
  2311. ExplicitSpec(ES) {}
  2312. void anchor() override;
  2313. ExplicitSpecifier ExplicitSpec;
  2314. public:
  2315. friend class ASTDeclReader;
  2316. friend class ASTDeclWriter;
  2317. static CXXConversionDecl *
  2318. Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
  2319. const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
  2320. bool UsesFPIntrin, bool isInline, ExplicitSpecifier ES,
  2321. ConstexprSpecKind ConstexprKind, SourceLocation EndLocation,
  2322. Expr *TrailingRequiresClause = nullptr);
  2323. static CXXConversionDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2324. ExplicitSpecifier getExplicitSpecifier() {
  2325. return getCanonicalDecl()->ExplicitSpec;
  2326. }
  2327. const ExplicitSpecifier getExplicitSpecifier() const {
  2328. return getCanonicalDecl()->ExplicitSpec;
  2329. }
  2330. /// Return true if the declartion is already resolved to be explicit.
  2331. bool isExplicit() const { return getExplicitSpecifier().isExplicit(); }
  2332. void setExplicitSpecifier(ExplicitSpecifier ES) { ExplicitSpec = ES; }
  2333. /// Returns the type that this conversion function is converting to.
  2334. QualType getConversionType() const {
  2335. return getType()->castAs<FunctionType>()->getReturnType();
  2336. }
  2337. /// Determine whether this conversion function is a conversion from
  2338. /// a lambda closure type to a block pointer.
  2339. bool isLambdaToBlockPointerConversion() const;
  2340. CXXConversionDecl *getCanonicalDecl() override {
  2341. return cast<CXXConversionDecl>(FunctionDecl::getCanonicalDecl());
  2342. }
  2343. const CXXConversionDecl *getCanonicalDecl() const {
  2344. return const_cast<CXXConversionDecl*>(this)->getCanonicalDecl();
  2345. }
  2346. // Implement isa/cast/dyncast/etc.
  2347. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2348. static bool classofKind(Kind K) { return K == CXXConversion; }
  2349. };
  2350. /// Represents a linkage specification.
  2351. ///
  2352. /// For example:
  2353. /// \code
  2354. /// extern "C" void foo();
  2355. /// \endcode
  2356. class LinkageSpecDecl : public Decl, public DeclContext {
  2357. virtual void anchor();
  2358. // This class stores some data in DeclContext::LinkageSpecDeclBits to save
  2359. // some space. Use the provided accessors to access it.
  2360. public:
  2361. /// Represents the language in a linkage specification.
  2362. ///
  2363. /// The values are part of the serialization ABI for
  2364. /// ASTs and cannot be changed without altering that ABI.
  2365. enum LanguageIDs { lang_c = 1, lang_cxx = 2 };
  2366. private:
  2367. /// The source location for the extern keyword.
  2368. SourceLocation ExternLoc;
  2369. /// The source location for the right brace (if valid).
  2370. SourceLocation RBraceLoc;
  2371. LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
  2372. SourceLocation LangLoc, LanguageIDs lang, bool HasBraces);
  2373. public:
  2374. static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC,
  2375. SourceLocation ExternLoc,
  2376. SourceLocation LangLoc, LanguageIDs Lang,
  2377. bool HasBraces);
  2378. static LinkageSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2379. /// Return the language specified by this linkage specification.
  2380. LanguageIDs getLanguage() const {
  2381. return static_cast<LanguageIDs>(LinkageSpecDeclBits.Language);
  2382. }
  2383. /// Set the language specified by this linkage specification.
  2384. void setLanguage(LanguageIDs L) { LinkageSpecDeclBits.Language = L; }
  2385. /// Determines whether this linkage specification had braces in
  2386. /// its syntactic form.
  2387. bool hasBraces() const {
  2388. assert(!RBraceLoc.isValid() || LinkageSpecDeclBits.HasBraces);
  2389. return LinkageSpecDeclBits.HasBraces;
  2390. }
  2391. SourceLocation getExternLoc() const { return ExternLoc; }
  2392. SourceLocation getRBraceLoc() const { return RBraceLoc; }
  2393. void setExternLoc(SourceLocation L) { ExternLoc = L; }
  2394. void setRBraceLoc(SourceLocation L) {
  2395. RBraceLoc = L;
  2396. LinkageSpecDeclBits.HasBraces = RBraceLoc.isValid();
  2397. }
  2398. SourceLocation getEndLoc() const LLVM_READONLY {
  2399. if (hasBraces())
  2400. return getRBraceLoc();
  2401. // No braces: get the end location of the (only) declaration in context
  2402. // (if present).
  2403. return decls_empty() ? getLocation() : decls_begin()->getEndLoc();
  2404. }
  2405. SourceRange getSourceRange() const override LLVM_READONLY {
  2406. return SourceRange(ExternLoc, getEndLoc());
  2407. }
  2408. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2409. static bool classofKind(Kind K) { return K == LinkageSpec; }
  2410. static DeclContext *castToDeclContext(const LinkageSpecDecl *D) {
  2411. return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D));
  2412. }
  2413. static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) {
  2414. return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC));
  2415. }
  2416. };
  2417. /// Represents C++ using-directive.
  2418. ///
  2419. /// For example:
  2420. /// \code
  2421. /// using namespace std;
  2422. /// \endcode
  2423. ///
  2424. /// \note UsingDirectiveDecl should be Decl not NamedDecl, but we provide
  2425. /// artificial names for all using-directives in order to store
  2426. /// them in DeclContext effectively.
  2427. class UsingDirectiveDecl : public NamedDecl {
  2428. /// The location of the \c using keyword.
  2429. SourceLocation UsingLoc;
  2430. /// The location of the \c namespace keyword.
  2431. SourceLocation NamespaceLoc;
  2432. /// The nested-name-specifier that precedes the namespace.
  2433. NestedNameSpecifierLoc QualifierLoc;
  2434. /// The namespace nominated by this using-directive.
  2435. NamedDecl *NominatedNamespace;
  2436. /// Enclosing context containing both using-directive and nominated
  2437. /// namespace.
  2438. DeclContext *CommonAncestor;
  2439. UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc,
  2440. SourceLocation NamespcLoc,
  2441. NestedNameSpecifierLoc QualifierLoc,
  2442. SourceLocation IdentLoc,
  2443. NamedDecl *Nominated,
  2444. DeclContext *CommonAncestor)
  2445. : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc),
  2446. NamespaceLoc(NamespcLoc), QualifierLoc(QualifierLoc),
  2447. NominatedNamespace(Nominated), CommonAncestor(CommonAncestor) {}
  2448. /// Returns special DeclarationName used by using-directives.
  2449. ///
  2450. /// This is only used by DeclContext for storing UsingDirectiveDecls in
  2451. /// its lookup structure.
  2452. static DeclarationName getName() {
  2453. return DeclarationName::getUsingDirectiveName();
  2454. }
  2455. void anchor() override;
  2456. public:
  2457. friend class ASTDeclReader;
  2458. // Friend for getUsingDirectiveName.
  2459. friend class DeclContext;
  2460. /// Retrieve the nested-name-specifier that qualifies the
  2461. /// name of the namespace, with source-location information.
  2462. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  2463. /// Retrieve the nested-name-specifier that qualifies the
  2464. /// name of the namespace.
  2465. NestedNameSpecifier *getQualifier() const {
  2466. return QualifierLoc.getNestedNameSpecifier();
  2467. }
  2468. NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; }
  2469. const NamedDecl *getNominatedNamespaceAsWritten() const {
  2470. return NominatedNamespace;
  2471. }
  2472. /// Returns the namespace nominated by this using-directive.
  2473. NamespaceDecl *getNominatedNamespace();
  2474. const NamespaceDecl *getNominatedNamespace() const {
  2475. return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
  2476. }
  2477. /// Returns the common ancestor context of this using-directive and
  2478. /// its nominated namespace.
  2479. DeclContext *getCommonAncestor() { return CommonAncestor; }
  2480. const DeclContext *getCommonAncestor() const { return CommonAncestor; }
  2481. /// Return the location of the \c using keyword.
  2482. SourceLocation getUsingLoc() const { return UsingLoc; }
  2483. // FIXME: Could omit 'Key' in name.
  2484. /// Returns the location of the \c namespace keyword.
  2485. SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; }
  2486. /// Returns the location of this using declaration's identifier.
  2487. SourceLocation getIdentLocation() const { return getLocation(); }
  2488. static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
  2489. SourceLocation UsingLoc,
  2490. SourceLocation NamespaceLoc,
  2491. NestedNameSpecifierLoc QualifierLoc,
  2492. SourceLocation IdentLoc,
  2493. NamedDecl *Nominated,
  2494. DeclContext *CommonAncestor);
  2495. static UsingDirectiveDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2496. SourceRange getSourceRange() const override LLVM_READONLY {
  2497. return SourceRange(UsingLoc, getLocation());
  2498. }
  2499. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2500. static bool classofKind(Kind K) { return K == UsingDirective; }
  2501. };
  2502. /// Represents a C++ namespace alias.
  2503. ///
  2504. /// For example:
  2505. ///
  2506. /// \code
  2507. /// namespace Foo = Bar;
  2508. /// \endcode
  2509. class NamespaceAliasDecl : public NamedDecl,
  2510. public Redeclarable<NamespaceAliasDecl> {
  2511. friend class ASTDeclReader;
  2512. /// The location of the \c namespace keyword.
  2513. SourceLocation NamespaceLoc;
  2514. /// The location of the namespace's identifier.
  2515. ///
  2516. /// This is accessed by TargetNameLoc.
  2517. SourceLocation IdentLoc;
  2518. /// The nested-name-specifier that precedes the namespace.
  2519. NestedNameSpecifierLoc QualifierLoc;
  2520. /// The Decl that this alias points to, either a NamespaceDecl or
  2521. /// a NamespaceAliasDecl.
  2522. NamedDecl *Namespace;
  2523. NamespaceAliasDecl(ASTContext &C, DeclContext *DC,
  2524. SourceLocation NamespaceLoc, SourceLocation AliasLoc,
  2525. IdentifierInfo *Alias, NestedNameSpecifierLoc QualifierLoc,
  2526. SourceLocation IdentLoc, NamedDecl *Namespace)
  2527. : NamedDecl(NamespaceAlias, DC, AliasLoc, Alias), redeclarable_base(C),
  2528. NamespaceLoc(NamespaceLoc), IdentLoc(IdentLoc),
  2529. QualifierLoc(QualifierLoc), Namespace(Namespace) {}
  2530. void anchor() override;
  2531. using redeclarable_base = Redeclarable<NamespaceAliasDecl>;
  2532. NamespaceAliasDecl *getNextRedeclarationImpl() override;
  2533. NamespaceAliasDecl *getPreviousDeclImpl() override;
  2534. NamespaceAliasDecl *getMostRecentDeclImpl() override;
  2535. public:
  2536. static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC,
  2537. SourceLocation NamespaceLoc,
  2538. SourceLocation AliasLoc,
  2539. IdentifierInfo *Alias,
  2540. NestedNameSpecifierLoc QualifierLoc,
  2541. SourceLocation IdentLoc,
  2542. NamedDecl *Namespace);
  2543. static NamespaceAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2544. using redecl_range = redeclarable_base::redecl_range;
  2545. using redecl_iterator = redeclarable_base::redecl_iterator;
  2546. using redeclarable_base::redecls_begin;
  2547. using redeclarable_base::redecls_end;
  2548. using redeclarable_base::redecls;
  2549. using redeclarable_base::getPreviousDecl;
  2550. using redeclarable_base::getMostRecentDecl;
  2551. NamespaceAliasDecl *getCanonicalDecl() override {
  2552. return getFirstDecl();
  2553. }
  2554. const NamespaceAliasDecl *getCanonicalDecl() const {
  2555. return getFirstDecl();
  2556. }
  2557. /// Retrieve the nested-name-specifier that qualifies the
  2558. /// name of the namespace, with source-location information.
  2559. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  2560. /// Retrieve the nested-name-specifier that qualifies the
  2561. /// name of the namespace.
  2562. NestedNameSpecifier *getQualifier() const {
  2563. return QualifierLoc.getNestedNameSpecifier();
  2564. }
  2565. /// Retrieve the namespace declaration aliased by this directive.
  2566. NamespaceDecl *getNamespace() {
  2567. if (auto *AD = dyn_cast<NamespaceAliasDecl>(Namespace))
  2568. return AD->getNamespace();
  2569. return cast<NamespaceDecl>(Namespace);
  2570. }
  2571. const NamespaceDecl *getNamespace() const {
  2572. return const_cast<NamespaceAliasDecl *>(this)->getNamespace();
  2573. }
  2574. /// Returns the location of the alias name, i.e. 'foo' in
  2575. /// "namespace foo = ns::bar;".
  2576. SourceLocation getAliasLoc() const { return getLocation(); }
  2577. /// Returns the location of the \c namespace keyword.
  2578. SourceLocation getNamespaceLoc() const { return NamespaceLoc; }
  2579. /// Returns the location of the identifier in the named namespace.
  2580. SourceLocation getTargetNameLoc() const { return IdentLoc; }
  2581. /// Retrieve the namespace that this alias refers to, which
  2582. /// may either be a NamespaceDecl or a NamespaceAliasDecl.
  2583. NamedDecl *getAliasedNamespace() const { return Namespace; }
  2584. SourceRange getSourceRange() const override LLVM_READONLY {
  2585. return SourceRange(NamespaceLoc, IdentLoc);
  2586. }
  2587. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2588. static bool classofKind(Kind K) { return K == NamespaceAlias; }
  2589. };
  2590. /// Implicit declaration of a temporary that was materialized by
  2591. /// a MaterializeTemporaryExpr and lifetime-extended by a declaration
  2592. class LifetimeExtendedTemporaryDecl final
  2593. : public Decl,
  2594. public Mergeable<LifetimeExtendedTemporaryDecl> {
  2595. friend class MaterializeTemporaryExpr;
  2596. friend class ASTDeclReader;
  2597. Stmt *ExprWithTemporary = nullptr;
  2598. /// The declaration which lifetime-extended this reference, if any.
  2599. /// Either a VarDecl, or (for a ctor-initializer) a FieldDecl.
  2600. ValueDecl *ExtendingDecl = nullptr;
  2601. unsigned ManglingNumber;
  2602. mutable APValue *Value = nullptr;
  2603. virtual void anchor();
  2604. LifetimeExtendedTemporaryDecl(Expr *Temp, ValueDecl *EDecl, unsigned Mangling)
  2605. : Decl(Decl::LifetimeExtendedTemporary, EDecl->getDeclContext(),
  2606. EDecl->getLocation()),
  2607. ExprWithTemporary(Temp), ExtendingDecl(EDecl),
  2608. ManglingNumber(Mangling) {}
  2609. LifetimeExtendedTemporaryDecl(EmptyShell)
  2610. : Decl(Decl::LifetimeExtendedTemporary, EmptyShell{}) {}
  2611. public:
  2612. static LifetimeExtendedTemporaryDecl *Create(Expr *Temp, ValueDecl *EDec,
  2613. unsigned Mangling) {
  2614. return new (EDec->getASTContext(), EDec->getDeclContext())
  2615. LifetimeExtendedTemporaryDecl(Temp, EDec, Mangling);
  2616. }
  2617. static LifetimeExtendedTemporaryDecl *CreateDeserialized(ASTContext &C,
  2618. unsigned ID) {
  2619. return new (C, ID) LifetimeExtendedTemporaryDecl(EmptyShell{});
  2620. }
  2621. ValueDecl *getExtendingDecl() { return ExtendingDecl; }
  2622. const ValueDecl *getExtendingDecl() const { return ExtendingDecl; }
  2623. /// Retrieve the storage duration for the materialized temporary.
  2624. StorageDuration getStorageDuration() const;
  2625. /// Retrieve the expression to which the temporary materialization conversion
  2626. /// was applied. This isn't necessarily the initializer of the temporary due
  2627. /// to the C++98 delayed materialization rules, but
  2628. /// skipRValueSubobjectAdjustments can be used to find said initializer within
  2629. /// the subexpression.
  2630. Expr *getTemporaryExpr() { return cast<Expr>(ExprWithTemporary); }
  2631. const Expr *getTemporaryExpr() const { return cast<Expr>(ExprWithTemporary); }
  2632. unsigned getManglingNumber() const { return ManglingNumber; }
  2633. /// Get the storage for the constant value of a materialized temporary
  2634. /// of static storage duration.
  2635. APValue *getOrCreateValue(bool MayCreate) const;
  2636. APValue *getValue() const { return Value; }
  2637. // Iterators
  2638. Stmt::child_range childrenExpr() {
  2639. return Stmt::child_range(&ExprWithTemporary, &ExprWithTemporary + 1);
  2640. }
  2641. Stmt::const_child_range childrenExpr() const {
  2642. return Stmt::const_child_range(&ExprWithTemporary, &ExprWithTemporary + 1);
  2643. }
  2644. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2645. static bool classofKind(Kind K) {
  2646. return K == Decl::LifetimeExtendedTemporary;
  2647. }
  2648. };
  2649. /// Represents a shadow declaration implicitly introduced into a scope by a
  2650. /// (resolved) using-declaration or using-enum-declaration to achieve
  2651. /// the desired lookup semantics.
  2652. ///
  2653. /// For example:
  2654. /// \code
  2655. /// namespace A {
  2656. /// void foo();
  2657. /// void foo(int);
  2658. /// struct foo {};
  2659. /// enum bar { bar1, bar2 };
  2660. /// }
  2661. /// namespace B {
  2662. /// // add a UsingDecl and three UsingShadowDecls (named foo) to B.
  2663. /// using A::foo;
  2664. /// // adds UsingEnumDecl and two UsingShadowDecls (named bar1 and bar2) to B.
  2665. /// using enum A::bar;
  2666. /// }
  2667. /// \endcode
  2668. class UsingShadowDecl : public NamedDecl, public Redeclarable<UsingShadowDecl> {
  2669. friend class BaseUsingDecl;
  2670. /// The referenced declaration.
  2671. NamedDecl *Underlying = nullptr;
  2672. /// The using declaration which introduced this decl or the next using
  2673. /// shadow declaration contained in the aforementioned using declaration.
  2674. NamedDecl *UsingOrNextShadow = nullptr;
  2675. void anchor() override;
  2676. using redeclarable_base = Redeclarable<UsingShadowDecl>;
  2677. UsingShadowDecl *getNextRedeclarationImpl() override {
  2678. return getNextRedeclaration();
  2679. }
  2680. UsingShadowDecl *getPreviousDeclImpl() override {
  2681. return getPreviousDecl();
  2682. }
  2683. UsingShadowDecl *getMostRecentDeclImpl() override {
  2684. return getMostRecentDecl();
  2685. }
  2686. protected:
  2687. UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC, SourceLocation Loc,
  2688. DeclarationName Name, BaseUsingDecl *Introducer,
  2689. NamedDecl *Target);
  2690. UsingShadowDecl(Kind K, ASTContext &C, EmptyShell);
  2691. public:
  2692. friend class ASTDeclReader;
  2693. friend class ASTDeclWriter;
  2694. static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
  2695. SourceLocation Loc, DeclarationName Name,
  2696. BaseUsingDecl *Introducer, NamedDecl *Target) {
  2697. return new (C, DC)
  2698. UsingShadowDecl(UsingShadow, C, DC, Loc, Name, Introducer, Target);
  2699. }
  2700. static UsingShadowDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2701. using redecl_range = redeclarable_base::redecl_range;
  2702. using redecl_iterator = redeclarable_base::redecl_iterator;
  2703. using redeclarable_base::redecls_begin;
  2704. using redeclarable_base::redecls_end;
  2705. using redeclarable_base::redecls;
  2706. using redeclarable_base::getPreviousDecl;
  2707. using redeclarable_base::getMostRecentDecl;
  2708. using redeclarable_base::isFirstDecl;
  2709. UsingShadowDecl *getCanonicalDecl() override {
  2710. return getFirstDecl();
  2711. }
  2712. const UsingShadowDecl *getCanonicalDecl() const {
  2713. return getFirstDecl();
  2714. }
  2715. /// Gets the underlying declaration which has been brought into the
  2716. /// local scope.
  2717. NamedDecl *getTargetDecl() const { return Underlying; }
  2718. /// Sets the underlying declaration which has been brought into the
  2719. /// local scope.
  2720. void setTargetDecl(NamedDecl *ND) {
  2721. assert(ND && "Target decl is null!");
  2722. Underlying = ND;
  2723. // A UsingShadowDecl is never a friend or local extern declaration, even
  2724. // if it is a shadow declaration for one.
  2725. IdentifierNamespace =
  2726. ND->getIdentifierNamespace() &
  2727. ~(IDNS_OrdinaryFriend | IDNS_TagFriend | IDNS_LocalExtern);
  2728. }
  2729. /// Gets the (written or instantiated) using declaration that introduced this
  2730. /// declaration.
  2731. BaseUsingDecl *getIntroducer() const;
  2732. /// The next using shadow declaration contained in the shadow decl
  2733. /// chain of the using declaration which introduced this decl.
  2734. UsingShadowDecl *getNextUsingShadowDecl() const {
  2735. return dyn_cast_or_null<UsingShadowDecl>(UsingOrNextShadow);
  2736. }
  2737. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2738. static bool classofKind(Kind K) {
  2739. return K == Decl::UsingShadow || K == Decl::ConstructorUsingShadow;
  2740. }
  2741. };
  2742. /// Represents a C++ declaration that introduces decls from somewhere else. It
  2743. /// provides a set of the shadow decls so introduced.
  2744. class BaseUsingDecl : public NamedDecl {
  2745. /// The first shadow declaration of the shadow decl chain associated
  2746. /// with this using declaration.
  2747. ///
  2748. /// The bool member of the pair is a bool flag a derived type may use
  2749. /// (UsingDecl makes use of it).
  2750. llvm::PointerIntPair<UsingShadowDecl *, 1, bool> FirstUsingShadow;
  2751. protected:
  2752. BaseUsingDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N)
  2753. : NamedDecl(DK, DC, L, N), FirstUsingShadow(nullptr, false) {}
  2754. private:
  2755. void anchor() override;
  2756. protected:
  2757. /// A bool flag for use by a derived type
  2758. bool getShadowFlag() const { return FirstUsingShadow.getInt(); }
  2759. /// A bool flag a derived type may set
  2760. void setShadowFlag(bool V) { FirstUsingShadow.setInt(V); }
  2761. public:
  2762. friend class ASTDeclReader;
  2763. friend class ASTDeclWriter;
  2764. /// Iterates through the using shadow declarations associated with
  2765. /// this using declaration.
  2766. class shadow_iterator {
  2767. /// The current using shadow declaration.
  2768. UsingShadowDecl *Current = nullptr;
  2769. public:
  2770. using value_type = UsingShadowDecl *;
  2771. using reference = UsingShadowDecl *;
  2772. using pointer = UsingShadowDecl *;
  2773. using iterator_category = std::forward_iterator_tag;
  2774. using difference_type = std::ptrdiff_t;
  2775. shadow_iterator() = default;
  2776. explicit shadow_iterator(UsingShadowDecl *C) : Current(C) {}
  2777. reference operator*() const { return Current; }
  2778. pointer operator->() const { return Current; }
  2779. shadow_iterator &operator++() {
  2780. Current = Current->getNextUsingShadowDecl();
  2781. return *this;
  2782. }
  2783. shadow_iterator operator++(int) {
  2784. shadow_iterator tmp(*this);
  2785. ++(*this);
  2786. return tmp;
  2787. }
  2788. friend bool operator==(shadow_iterator x, shadow_iterator y) {
  2789. return x.Current == y.Current;
  2790. }
  2791. friend bool operator!=(shadow_iterator x, shadow_iterator y) {
  2792. return x.Current != y.Current;
  2793. }
  2794. };
  2795. using shadow_range = llvm::iterator_range<shadow_iterator>;
  2796. shadow_range shadows() const {
  2797. return shadow_range(shadow_begin(), shadow_end());
  2798. }
  2799. shadow_iterator shadow_begin() const {
  2800. return shadow_iterator(FirstUsingShadow.getPointer());
  2801. }
  2802. shadow_iterator shadow_end() const { return shadow_iterator(); }
  2803. /// Return the number of shadowed declarations associated with this
  2804. /// using declaration.
  2805. unsigned shadow_size() const {
  2806. return std::distance(shadow_begin(), shadow_end());
  2807. }
  2808. void addShadowDecl(UsingShadowDecl *S);
  2809. void removeShadowDecl(UsingShadowDecl *S);
  2810. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2811. static bool classofKind(Kind K) { return K == Using || K == UsingEnum; }
  2812. };
  2813. /// Represents a C++ using-declaration.
  2814. ///
  2815. /// For example:
  2816. /// \code
  2817. /// using someNameSpace::someIdentifier;
  2818. /// \endcode
  2819. class UsingDecl : public BaseUsingDecl, public Mergeable<UsingDecl> {
  2820. /// The source location of the 'using' keyword itself.
  2821. SourceLocation UsingLocation;
  2822. /// The nested-name-specifier that precedes the name.
  2823. NestedNameSpecifierLoc QualifierLoc;
  2824. /// Provides source/type location info for the declaration name
  2825. /// embedded in the ValueDecl base class.
  2826. DeclarationNameLoc DNLoc;
  2827. UsingDecl(DeclContext *DC, SourceLocation UL,
  2828. NestedNameSpecifierLoc QualifierLoc,
  2829. const DeclarationNameInfo &NameInfo, bool HasTypenameKeyword)
  2830. : BaseUsingDecl(Using, DC, NameInfo.getLoc(), NameInfo.getName()),
  2831. UsingLocation(UL), QualifierLoc(QualifierLoc),
  2832. DNLoc(NameInfo.getInfo()) {
  2833. setShadowFlag(HasTypenameKeyword);
  2834. }
  2835. void anchor() override;
  2836. public:
  2837. friend class ASTDeclReader;
  2838. friend class ASTDeclWriter;
  2839. /// Return the source location of the 'using' keyword.
  2840. SourceLocation getUsingLoc() const { return UsingLocation; }
  2841. /// Set the source location of the 'using' keyword.
  2842. void setUsingLoc(SourceLocation L) { UsingLocation = L; }
  2843. /// Retrieve the nested-name-specifier that qualifies the name,
  2844. /// with source-location information.
  2845. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  2846. /// Retrieve the nested-name-specifier that qualifies the name.
  2847. NestedNameSpecifier *getQualifier() const {
  2848. return QualifierLoc.getNestedNameSpecifier();
  2849. }
  2850. DeclarationNameInfo getNameInfo() const {
  2851. return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
  2852. }
  2853. /// Return true if it is a C++03 access declaration (no 'using').
  2854. bool isAccessDeclaration() const { return UsingLocation.isInvalid(); }
  2855. /// Return true if the using declaration has 'typename'.
  2856. bool hasTypename() const { return getShadowFlag(); }
  2857. /// Sets whether the using declaration has 'typename'.
  2858. void setTypename(bool TN) { setShadowFlag(TN); }
  2859. static UsingDecl *Create(ASTContext &C, DeclContext *DC,
  2860. SourceLocation UsingL,
  2861. NestedNameSpecifierLoc QualifierLoc,
  2862. const DeclarationNameInfo &NameInfo,
  2863. bool HasTypenameKeyword);
  2864. static UsingDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2865. SourceRange getSourceRange() const override LLVM_READONLY;
  2866. /// Retrieves the canonical declaration of this declaration.
  2867. UsingDecl *getCanonicalDecl() override {
  2868. return cast<UsingDecl>(getFirstDecl());
  2869. }
  2870. const UsingDecl *getCanonicalDecl() const {
  2871. return cast<UsingDecl>(getFirstDecl());
  2872. }
  2873. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2874. static bool classofKind(Kind K) { return K == Using; }
  2875. };
  2876. /// Represents a shadow constructor declaration introduced into a
  2877. /// class by a C++11 using-declaration that names a constructor.
  2878. ///
  2879. /// For example:
  2880. /// \code
  2881. /// struct Base { Base(int); };
  2882. /// struct Derived {
  2883. /// using Base::Base; // creates a UsingDecl and a ConstructorUsingShadowDecl
  2884. /// };
  2885. /// \endcode
  2886. class ConstructorUsingShadowDecl final : public UsingShadowDecl {
  2887. /// If this constructor using declaration inherted the constructor
  2888. /// from an indirect base class, this is the ConstructorUsingShadowDecl
  2889. /// in the named direct base class from which the declaration was inherited.
  2890. ConstructorUsingShadowDecl *NominatedBaseClassShadowDecl = nullptr;
  2891. /// If this constructor using declaration inherted the constructor
  2892. /// from an indirect base class, this is the ConstructorUsingShadowDecl
  2893. /// that will be used to construct the unique direct or virtual base class
  2894. /// that receives the constructor arguments.
  2895. ConstructorUsingShadowDecl *ConstructedBaseClassShadowDecl = nullptr;
  2896. /// \c true if the constructor ultimately named by this using shadow
  2897. /// declaration is within a virtual base class subobject of the class that
  2898. /// contains this declaration.
  2899. unsigned IsVirtual : 1;
  2900. ConstructorUsingShadowDecl(ASTContext &C, DeclContext *DC, SourceLocation Loc,
  2901. UsingDecl *Using, NamedDecl *Target,
  2902. bool TargetInVirtualBase)
  2903. : UsingShadowDecl(ConstructorUsingShadow, C, DC, Loc,
  2904. Using->getDeclName(), Using,
  2905. Target->getUnderlyingDecl()),
  2906. NominatedBaseClassShadowDecl(
  2907. dyn_cast<ConstructorUsingShadowDecl>(Target)),
  2908. ConstructedBaseClassShadowDecl(NominatedBaseClassShadowDecl),
  2909. IsVirtual(TargetInVirtualBase) {
  2910. // If we found a constructor that chains to a constructor for a virtual
  2911. // base, we should directly call that virtual base constructor instead.
  2912. // FIXME: This logic belongs in Sema.
  2913. if (NominatedBaseClassShadowDecl &&
  2914. NominatedBaseClassShadowDecl->constructsVirtualBase()) {
  2915. ConstructedBaseClassShadowDecl =
  2916. NominatedBaseClassShadowDecl->ConstructedBaseClassShadowDecl;
  2917. IsVirtual = true;
  2918. }
  2919. }
  2920. ConstructorUsingShadowDecl(ASTContext &C, EmptyShell Empty)
  2921. : UsingShadowDecl(ConstructorUsingShadow, C, Empty), IsVirtual(false) {}
  2922. void anchor() override;
  2923. public:
  2924. friend class ASTDeclReader;
  2925. friend class ASTDeclWriter;
  2926. static ConstructorUsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
  2927. SourceLocation Loc,
  2928. UsingDecl *Using, NamedDecl *Target,
  2929. bool IsVirtual);
  2930. static ConstructorUsingShadowDecl *CreateDeserialized(ASTContext &C,
  2931. unsigned ID);
  2932. /// Override the UsingShadowDecl's getIntroducer, returning the UsingDecl that
  2933. /// introduced this.
  2934. UsingDecl *getIntroducer() const {
  2935. return cast<UsingDecl>(UsingShadowDecl::getIntroducer());
  2936. }
  2937. /// Returns the parent of this using shadow declaration, which
  2938. /// is the class in which this is declared.
  2939. //@{
  2940. const CXXRecordDecl *getParent() const {
  2941. return cast<CXXRecordDecl>(getDeclContext());
  2942. }
  2943. CXXRecordDecl *getParent() {
  2944. return cast<CXXRecordDecl>(getDeclContext());
  2945. }
  2946. //@}
  2947. /// Get the inheriting constructor declaration for the direct base
  2948. /// class from which this using shadow declaration was inherited, if there is
  2949. /// one. This can be different for each redeclaration of the same shadow decl.
  2950. ConstructorUsingShadowDecl *getNominatedBaseClassShadowDecl() const {
  2951. return NominatedBaseClassShadowDecl;
  2952. }
  2953. /// Get the inheriting constructor declaration for the base class
  2954. /// for which we don't have an explicit initializer, if there is one.
  2955. ConstructorUsingShadowDecl *getConstructedBaseClassShadowDecl() const {
  2956. return ConstructedBaseClassShadowDecl;
  2957. }
  2958. /// Get the base class that was named in the using declaration. This
  2959. /// can be different for each redeclaration of this same shadow decl.
  2960. CXXRecordDecl *getNominatedBaseClass() const;
  2961. /// Get the base class whose constructor or constructor shadow
  2962. /// declaration is passed the constructor arguments.
  2963. CXXRecordDecl *getConstructedBaseClass() const {
  2964. return cast<CXXRecordDecl>((ConstructedBaseClassShadowDecl
  2965. ? ConstructedBaseClassShadowDecl
  2966. : getTargetDecl())
  2967. ->getDeclContext());
  2968. }
  2969. /// Returns \c true if the constructed base class is a virtual base
  2970. /// class subobject of this declaration's class.
  2971. bool constructsVirtualBase() const {
  2972. return IsVirtual;
  2973. }
  2974. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2975. static bool classofKind(Kind K) { return K == ConstructorUsingShadow; }
  2976. };
  2977. /// Represents a C++ using-enum-declaration.
  2978. ///
  2979. /// For example:
  2980. /// \code
  2981. /// using enum SomeEnumTag ;
  2982. /// \endcode
  2983. class UsingEnumDecl : public BaseUsingDecl, public Mergeable<UsingEnumDecl> {
  2984. /// The source location of the 'using' keyword itself.
  2985. SourceLocation UsingLocation;
  2986. /// Location of the 'enum' keyword.
  2987. SourceLocation EnumLocation;
  2988. /// The enum
  2989. EnumDecl *Enum;
  2990. UsingEnumDecl(DeclContext *DC, DeclarationName DN, SourceLocation UL,
  2991. SourceLocation EL, SourceLocation NL, EnumDecl *ED)
  2992. : BaseUsingDecl(UsingEnum, DC, NL, DN), UsingLocation(UL),
  2993. EnumLocation(EL), Enum(ED) {}
  2994. void anchor() override;
  2995. public:
  2996. friend class ASTDeclReader;
  2997. friend class ASTDeclWriter;
  2998. /// The source location of the 'using' keyword.
  2999. SourceLocation getUsingLoc() const { return UsingLocation; }
  3000. void setUsingLoc(SourceLocation L) { UsingLocation = L; }
  3001. /// The source location of the 'enum' keyword.
  3002. SourceLocation getEnumLoc() const { return EnumLocation; }
  3003. void setEnumLoc(SourceLocation L) { EnumLocation = L; }
  3004. public:
  3005. EnumDecl *getEnumDecl() const { return Enum; }
  3006. static UsingEnumDecl *Create(ASTContext &C, DeclContext *DC,
  3007. SourceLocation UsingL, SourceLocation EnumL,
  3008. SourceLocation NameL, EnumDecl *ED);
  3009. static UsingEnumDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  3010. SourceRange getSourceRange() const override LLVM_READONLY;
  3011. /// Retrieves the canonical declaration of this declaration.
  3012. UsingEnumDecl *getCanonicalDecl() override {
  3013. return cast<UsingEnumDecl>(getFirstDecl());
  3014. }
  3015. const UsingEnumDecl *getCanonicalDecl() const {
  3016. return cast<UsingEnumDecl>(getFirstDecl());
  3017. }
  3018. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  3019. static bool classofKind(Kind K) { return K == UsingEnum; }
  3020. };
  3021. /// Represents a pack of using declarations that a single
  3022. /// using-declarator pack-expanded into.
  3023. ///
  3024. /// \code
  3025. /// template<typename ...T> struct X : T... {
  3026. /// using T::operator()...;
  3027. /// using T::operator T...;
  3028. /// };
  3029. /// \endcode
  3030. ///
  3031. /// In the second case above, the UsingPackDecl will have the name
  3032. /// 'operator T' (which contains an unexpanded pack), but the individual
  3033. /// UsingDecls and UsingShadowDecls will have more reasonable names.
  3034. class UsingPackDecl final
  3035. : public NamedDecl, public Mergeable<UsingPackDecl>,
  3036. private llvm::TrailingObjects<UsingPackDecl, NamedDecl *> {
  3037. /// The UnresolvedUsingValueDecl or UnresolvedUsingTypenameDecl from
  3038. /// which this waas instantiated.
  3039. NamedDecl *InstantiatedFrom;
  3040. /// The number of using-declarations created by this pack expansion.
  3041. unsigned NumExpansions;
  3042. UsingPackDecl(DeclContext *DC, NamedDecl *InstantiatedFrom,
  3043. ArrayRef<NamedDecl *> UsingDecls)
  3044. : NamedDecl(UsingPack, DC,
  3045. InstantiatedFrom ? InstantiatedFrom->getLocation()
  3046. : SourceLocation(),
  3047. InstantiatedFrom ? InstantiatedFrom->getDeclName()
  3048. : DeclarationName()),
  3049. InstantiatedFrom(InstantiatedFrom), NumExpansions(UsingDecls.size()) {
  3050. std::uninitialized_copy(UsingDecls.begin(), UsingDecls.end(),
  3051. getTrailingObjects<NamedDecl *>());
  3052. }
  3053. void anchor() override;
  3054. public:
  3055. friend class ASTDeclReader;
  3056. friend class ASTDeclWriter;
  3057. friend TrailingObjects;
  3058. /// Get the using declaration from which this was instantiated. This will
  3059. /// always be an UnresolvedUsingValueDecl or an UnresolvedUsingTypenameDecl
  3060. /// that is a pack expansion.
  3061. NamedDecl *getInstantiatedFromUsingDecl() const { return InstantiatedFrom; }
  3062. /// Get the set of using declarations that this pack expanded into. Note that
  3063. /// some of these may still be unresolved.
  3064. ArrayRef<NamedDecl *> expansions() const {
  3065. return llvm::makeArrayRef(getTrailingObjects<NamedDecl *>(), NumExpansions);
  3066. }
  3067. static UsingPackDecl *Create(ASTContext &C, DeclContext *DC,
  3068. NamedDecl *InstantiatedFrom,
  3069. ArrayRef<NamedDecl *> UsingDecls);
  3070. static UsingPackDecl *CreateDeserialized(ASTContext &C, unsigned ID,
  3071. unsigned NumExpansions);
  3072. SourceRange getSourceRange() const override LLVM_READONLY {
  3073. return InstantiatedFrom->getSourceRange();
  3074. }
  3075. UsingPackDecl *getCanonicalDecl() override { return getFirstDecl(); }
  3076. const UsingPackDecl *getCanonicalDecl() const { return getFirstDecl(); }
  3077. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  3078. static bool classofKind(Kind K) { return K == UsingPack; }
  3079. };
  3080. /// Represents a dependent using declaration which was not marked with
  3081. /// \c typename.
  3082. ///
  3083. /// Unlike non-dependent using declarations, these *only* bring through
  3084. /// non-types; otherwise they would break two-phase lookup.
  3085. ///
  3086. /// \code
  3087. /// template \<class T> class A : public Base<T> {
  3088. /// using Base<T>::foo;
  3089. /// };
  3090. /// \endcode
  3091. class UnresolvedUsingValueDecl : public ValueDecl,
  3092. public Mergeable<UnresolvedUsingValueDecl> {
  3093. /// The source location of the 'using' keyword
  3094. SourceLocation UsingLocation;
  3095. /// If this is a pack expansion, the location of the '...'.
  3096. SourceLocation EllipsisLoc;
  3097. /// The nested-name-specifier that precedes the name.
  3098. NestedNameSpecifierLoc QualifierLoc;
  3099. /// Provides source/type location info for the declaration name
  3100. /// embedded in the ValueDecl base class.
  3101. DeclarationNameLoc DNLoc;
  3102. UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty,
  3103. SourceLocation UsingLoc,
  3104. NestedNameSpecifierLoc QualifierLoc,
  3105. const DeclarationNameInfo &NameInfo,
  3106. SourceLocation EllipsisLoc)
  3107. : ValueDecl(UnresolvedUsingValue, DC,
  3108. NameInfo.getLoc(), NameInfo.getName(), Ty),
  3109. UsingLocation(UsingLoc), EllipsisLoc(EllipsisLoc),
  3110. QualifierLoc(QualifierLoc), DNLoc(NameInfo.getInfo()) {}
  3111. void anchor() override;
  3112. public:
  3113. friend class ASTDeclReader;
  3114. friend class ASTDeclWriter;
  3115. /// Returns the source location of the 'using' keyword.
  3116. SourceLocation getUsingLoc() const { return UsingLocation; }
  3117. /// Set the source location of the 'using' keyword.
  3118. void setUsingLoc(SourceLocation L) { UsingLocation = L; }
  3119. /// Return true if it is a C++03 access declaration (no 'using').
  3120. bool isAccessDeclaration() const { return UsingLocation.isInvalid(); }
  3121. /// Retrieve the nested-name-specifier that qualifies the name,
  3122. /// with source-location information.
  3123. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  3124. /// Retrieve the nested-name-specifier that qualifies the name.
  3125. NestedNameSpecifier *getQualifier() const {
  3126. return QualifierLoc.getNestedNameSpecifier();
  3127. }
  3128. DeclarationNameInfo getNameInfo() const {
  3129. return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
  3130. }
  3131. /// Determine whether this is a pack expansion.
  3132. bool isPackExpansion() const {
  3133. return EllipsisLoc.isValid();
  3134. }
  3135. /// Get the location of the ellipsis if this is a pack expansion.
  3136. SourceLocation getEllipsisLoc() const {
  3137. return EllipsisLoc;
  3138. }
  3139. static UnresolvedUsingValueDecl *
  3140. Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
  3141. NestedNameSpecifierLoc QualifierLoc,
  3142. const DeclarationNameInfo &NameInfo, SourceLocation EllipsisLoc);
  3143. static UnresolvedUsingValueDecl *
  3144. CreateDeserialized(ASTContext &C, unsigned ID);
  3145. SourceRange getSourceRange() const override LLVM_READONLY;
  3146. /// Retrieves the canonical declaration of this declaration.
  3147. UnresolvedUsingValueDecl *getCanonicalDecl() override {
  3148. return getFirstDecl();
  3149. }
  3150. const UnresolvedUsingValueDecl *getCanonicalDecl() const {
  3151. return getFirstDecl();
  3152. }
  3153. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  3154. static bool classofKind(Kind K) { return K == UnresolvedUsingValue; }
  3155. };
  3156. /// Represents a dependent using declaration which was marked with
  3157. /// \c typename.
  3158. ///
  3159. /// \code
  3160. /// template \<class T> class A : public Base<T> {
  3161. /// using typename Base<T>::foo;
  3162. /// };
  3163. /// \endcode
  3164. ///
  3165. /// The type associated with an unresolved using typename decl is
  3166. /// currently always a typename type.
  3167. class UnresolvedUsingTypenameDecl
  3168. : public TypeDecl,
  3169. public Mergeable<UnresolvedUsingTypenameDecl> {
  3170. friend class ASTDeclReader;
  3171. /// The source location of the 'typename' keyword
  3172. SourceLocation TypenameLocation;
  3173. /// If this is a pack expansion, the location of the '...'.
  3174. SourceLocation EllipsisLoc;
  3175. /// The nested-name-specifier that precedes the name.
  3176. NestedNameSpecifierLoc QualifierLoc;
  3177. UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc,
  3178. SourceLocation TypenameLoc,
  3179. NestedNameSpecifierLoc QualifierLoc,
  3180. SourceLocation TargetNameLoc,
  3181. IdentifierInfo *TargetName,
  3182. SourceLocation EllipsisLoc)
  3183. : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName,
  3184. UsingLoc),
  3185. TypenameLocation(TypenameLoc), EllipsisLoc(EllipsisLoc),
  3186. QualifierLoc(QualifierLoc) {}
  3187. void anchor() override;
  3188. public:
  3189. /// Returns the source location of the 'using' keyword.
  3190. SourceLocation getUsingLoc() const { return getBeginLoc(); }
  3191. /// Returns the source location of the 'typename' keyword.
  3192. SourceLocation getTypenameLoc() const { return TypenameLocation; }
  3193. /// Retrieve the nested-name-specifier that qualifies the name,
  3194. /// with source-location information.
  3195. NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
  3196. /// Retrieve the nested-name-specifier that qualifies the name.
  3197. NestedNameSpecifier *getQualifier() const {
  3198. return QualifierLoc.getNestedNameSpecifier();
  3199. }
  3200. DeclarationNameInfo getNameInfo() const {
  3201. return DeclarationNameInfo(getDeclName(), getLocation());
  3202. }
  3203. /// Determine whether this is a pack expansion.
  3204. bool isPackExpansion() const {
  3205. return EllipsisLoc.isValid();
  3206. }
  3207. /// Get the location of the ellipsis if this is a pack expansion.
  3208. SourceLocation getEllipsisLoc() const {
  3209. return EllipsisLoc;
  3210. }
  3211. static UnresolvedUsingTypenameDecl *
  3212. Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
  3213. SourceLocation TypenameLoc, NestedNameSpecifierLoc QualifierLoc,
  3214. SourceLocation TargetNameLoc, DeclarationName TargetName,
  3215. SourceLocation EllipsisLoc);
  3216. static UnresolvedUsingTypenameDecl *
  3217. CreateDeserialized(ASTContext &C, unsigned ID);
  3218. /// Retrieves the canonical declaration of this declaration.
  3219. UnresolvedUsingTypenameDecl *getCanonicalDecl() override {
  3220. return getFirstDecl();
  3221. }
  3222. const UnresolvedUsingTypenameDecl *getCanonicalDecl() const {
  3223. return getFirstDecl();
  3224. }
  3225. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  3226. static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; }
  3227. };
  3228. /// This node is generated when a using-declaration that was annotated with
  3229. /// __attribute__((using_if_exists)) failed to resolve to a known declaration.
  3230. /// In that case, Sema builds a UsingShadowDecl whose target is an instance of
  3231. /// this declaration, adding it to the current scope. Referring to this
  3232. /// declaration in any way is an error.
  3233. class UnresolvedUsingIfExistsDecl final : public NamedDecl {
  3234. UnresolvedUsingIfExistsDecl(DeclContext *DC, SourceLocation Loc,
  3235. DeclarationName Name);
  3236. void anchor() override;
  3237. public:
  3238. static UnresolvedUsingIfExistsDecl *Create(ASTContext &Ctx, DeclContext *DC,
  3239. SourceLocation Loc,
  3240. DeclarationName Name);
  3241. static UnresolvedUsingIfExistsDecl *CreateDeserialized(ASTContext &Ctx,
  3242. unsigned ID);
  3243. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  3244. static bool classofKind(Kind K) { return K == Decl::UnresolvedUsingIfExists; }
  3245. };
  3246. /// Represents a C++11 static_assert declaration.
  3247. class StaticAssertDecl : public Decl {
  3248. llvm::PointerIntPair<Expr *, 1, bool> AssertExprAndFailed;
  3249. StringLiteral *Message;
  3250. SourceLocation RParenLoc;
  3251. StaticAssertDecl(DeclContext *DC, SourceLocation StaticAssertLoc,
  3252. Expr *AssertExpr, StringLiteral *Message,
  3253. SourceLocation RParenLoc, bool Failed)
  3254. : Decl(StaticAssert, DC, StaticAssertLoc),
  3255. AssertExprAndFailed(AssertExpr, Failed), Message(Message),
  3256. RParenLoc(RParenLoc) {}
  3257. virtual void anchor();
  3258. public:
  3259. friend class ASTDeclReader;
  3260. static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC,
  3261. SourceLocation StaticAssertLoc,
  3262. Expr *AssertExpr, StringLiteral *Message,
  3263. SourceLocation RParenLoc, bool Failed);
  3264. static StaticAssertDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  3265. Expr *getAssertExpr() { return AssertExprAndFailed.getPointer(); }
  3266. const Expr *getAssertExpr() const { return AssertExprAndFailed.getPointer(); }
  3267. StringLiteral *getMessage() { return Message; }
  3268. const StringLiteral *getMessage() const { return Message; }
  3269. bool isFailed() const { return AssertExprAndFailed.getInt(); }
  3270. SourceLocation getRParenLoc() const { return RParenLoc; }
  3271. SourceRange getSourceRange() const override LLVM_READONLY {
  3272. return SourceRange(getLocation(), getRParenLoc());
  3273. }
  3274. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  3275. static bool classofKind(Kind K) { return K == StaticAssert; }
  3276. };
  3277. /// A binding in a decomposition declaration. For instance, given:
  3278. ///
  3279. /// int n[3];
  3280. /// auto &[a, b, c] = n;
  3281. ///
  3282. /// a, b, and c are BindingDecls, whose bindings are the expressions
  3283. /// x[0], x[1], and x[2] respectively, where x is the implicit
  3284. /// DecompositionDecl of type 'int (&)[3]'.
  3285. class BindingDecl : public ValueDecl {
  3286. /// The declaration that this binding binds to part of.
  3287. ValueDecl *Decomp;
  3288. /// The binding represented by this declaration. References to this
  3289. /// declaration are effectively equivalent to this expression (except
  3290. /// that it is only evaluated once at the point of declaration of the
  3291. /// binding).
  3292. Expr *Binding = nullptr;
  3293. BindingDecl(DeclContext *DC, SourceLocation IdLoc, IdentifierInfo *Id)
  3294. : ValueDecl(Decl::Binding, DC, IdLoc, Id, QualType()) {}
  3295. void anchor() override;
  3296. public:
  3297. friend class ASTDeclReader;
  3298. static BindingDecl *Create(ASTContext &C, DeclContext *DC,
  3299. SourceLocation IdLoc, IdentifierInfo *Id);
  3300. static BindingDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  3301. /// Get the expression to which this declaration is bound. This may be null
  3302. /// in two different cases: while parsing the initializer for the
  3303. /// decomposition declaration, and when the initializer is type-dependent.
  3304. Expr *getBinding() const { return Binding; }
  3305. /// Get the decomposition declaration that this binding represents a
  3306. /// decomposition of.
  3307. ValueDecl *getDecomposedDecl() const { return Decomp; }
  3308. /// Get the variable (if any) that holds the value of evaluating the binding.
  3309. /// Only present for user-defined bindings for tuple-like types.
  3310. VarDecl *getHoldingVar() const;
  3311. /// Set the binding for this BindingDecl, along with its declared type (which
  3312. /// should be a possibly-cv-qualified form of the type of the binding, or a
  3313. /// reference to such a type).
  3314. void setBinding(QualType DeclaredType, Expr *Binding) {
  3315. setType(DeclaredType);
  3316. this->Binding = Binding;
  3317. }
  3318. /// Set the decomposed variable for this BindingDecl.
  3319. void setDecomposedDecl(ValueDecl *Decomposed) { Decomp = Decomposed; }
  3320. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  3321. static bool classofKind(Kind K) { return K == Decl::Binding; }
  3322. };
  3323. /// A decomposition declaration. For instance, given:
  3324. ///
  3325. /// int n[3];
  3326. /// auto &[a, b, c] = n;
  3327. ///
  3328. /// the second line declares a DecompositionDecl of type 'int (&)[3]', and
  3329. /// three BindingDecls (named a, b, and c). An instance of this class is always
  3330. /// unnamed, but behaves in almost all other respects like a VarDecl.
  3331. class DecompositionDecl final
  3332. : public VarDecl,
  3333. private llvm::TrailingObjects<DecompositionDecl, BindingDecl *> {
  3334. /// The number of BindingDecl*s following this object.
  3335. unsigned NumBindings;
  3336. DecompositionDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
  3337. SourceLocation LSquareLoc, QualType T,
  3338. TypeSourceInfo *TInfo, StorageClass SC,
  3339. ArrayRef<BindingDecl *> Bindings)
  3340. : VarDecl(Decomposition, C, DC, StartLoc, LSquareLoc, nullptr, T, TInfo,
  3341. SC),
  3342. NumBindings(Bindings.size()) {
  3343. std::uninitialized_copy(Bindings.begin(), Bindings.end(),
  3344. getTrailingObjects<BindingDecl *>());
  3345. for (auto *B : Bindings)
  3346. B->setDecomposedDecl(this);
  3347. }
  3348. void anchor() override;
  3349. public:
  3350. friend class ASTDeclReader;
  3351. friend TrailingObjects;
  3352. static DecompositionDecl *Create(ASTContext &C, DeclContext *DC,
  3353. SourceLocation StartLoc,
  3354. SourceLocation LSquareLoc,
  3355. QualType T, TypeSourceInfo *TInfo,
  3356. StorageClass S,
  3357. ArrayRef<BindingDecl *> Bindings);
  3358. static DecompositionDecl *CreateDeserialized(ASTContext &C, unsigned ID,
  3359. unsigned NumBindings);
  3360. ArrayRef<BindingDecl *> bindings() const {
  3361. return llvm::makeArrayRef(getTrailingObjects<BindingDecl *>(), NumBindings);
  3362. }
  3363. void printName(raw_ostream &os) const override;
  3364. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  3365. static bool classofKind(Kind K) { return K == Decomposition; }
  3366. };
  3367. /// An instance of this class represents the declaration of a property
  3368. /// member. This is a Microsoft extension to C++, first introduced in
  3369. /// Visual Studio .NET 2003 as a parallel to similar features in C#
  3370. /// and Managed C++.
  3371. ///
  3372. /// A property must always be a non-static class member.
  3373. ///
  3374. /// A property member superficially resembles a non-static data
  3375. /// member, except preceded by a property attribute:
  3376. /// __declspec(property(get=GetX, put=PutX)) int x;
  3377. /// Either (but not both) of the 'get' and 'put' names may be omitted.
  3378. ///
  3379. /// A reference to a property is always an lvalue. If the lvalue
  3380. /// undergoes lvalue-to-rvalue conversion, then a getter name is
  3381. /// required, and that member is called with no arguments.
  3382. /// If the lvalue is assigned into, then a setter name is required,
  3383. /// and that member is called with one argument, the value assigned.
  3384. /// Both operations are potentially overloaded. Compound assignments
  3385. /// are permitted, as are the increment and decrement operators.
  3386. ///
  3387. /// The getter and putter methods are permitted to be overloaded,
  3388. /// although their return and parameter types are subject to certain
  3389. /// restrictions according to the type of the property.
  3390. ///
  3391. /// A property declared using an incomplete array type may
  3392. /// additionally be subscripted, adding extra parameters to the getter
  3393. /// and putter methods.
  3394. class MSPropertyDecl : public DeclaratorDecl {
  3395. IdentifierInfo *GetterId, *SetterId;
  3396. MSPropertyDecl(DeclContext *DC, SourceLocation L, DeclarationName N,
  3397. QualType T, TypeSourceInfo *TInfo, SourceLocation StartL,
  3398. IdentifierInfo *Getter, IdentifierInfo *Setter)
  3399. : DeclaratorDecl(MSProperty, DC, L, N, T, TInfo, StartL),
  3400. GetterId(Getter), SetterId(Setter) {}
  3401. void anchor() override;
  3402. public:
  3403. friend class ASTDeclReader;
  3404. static MSPropertyDecl *Create(ASTContext &C, DeclContext *DC,
  3405. SourceLocation L, DeclarationName N, QualType T,
  3406. TypeSourceInfo *TInfo, SourceLocation StartL,
  3407. IdentifierInfo *Getter, IdentifierInfo *Setter);
  3408. static MSPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  3409. static bool classof(const Decl *D) { return D->getKind() == MSProperty; }
  3410. bool hasGetter() const { return GetterId != nullptr; }
  3411. IdentifierInfo* getGetterId() const { return GetterId; }
  3412. bool hasSetter() const { return SetterId != nullptr; }
  3413. IdentifierInfo* getSetterId() const { return SetterId; }
  3414. };
  3415. /// Parts of a decomposed MSGuidDecl. Factored out to avoid unnecessary
  3416. /// dependencies on DeclCXX.h.
  3417. struct MSGuidDeclParts {
  3418. /// {01234567-...
  3419. uint32_t Part1;
  3420. /// ...-89ab-...
  3421. uint16_t Part2;
  3422. /// ...-cdef-...
  3423. uint16_t Part3;
  3424. /// ...-0123-456789abcdef}
  3425. uint8_t Part4And5[8];
  3426. uint64_t getPart4And5AsUint64() const {
  3427. uint64_t Val;
  3428. memcpy(&Val, &Part4And5, sizeof(Part4And5));
  3429. return Val;
  3430. }
  3431. };
  3432. /// A global _GUID constant. These are implicitly created by UuidAttrs.
  3433. ///
  3434. /// struct _declspec(uuid("01234567-89ab-cdef-0123-456789abcdef")) X{};
  3435. ///
  3436. /// X is a CXXRecordDecl that contains a UuidAttr that references the (unique)
  3437. /// MSGuidDecl for the specified UUID.
  3438. class MSGuidDecl : public ValueDecl,
  3439. public Mergeable<MSGuidDecl>,
  3440. public llvm::FoldingSetNode {
  3441. public:
  3442. using Parts = MSGuidDeclParts;
  3443. private:
  3444. /// The decomposed form of the UUID.
  3445. Parts PartVal;
  3446. /// The resolved value of the UUID as an APValue. Computed on demand and
  3447. /// cached.
  3448. mutable APValue APVal;
  3449. void anchor() override;
  3450. MSGuidDecl(DeclContext *DC, QualType T, Parts P);
  3451. static MSGuidDecl *Create(const ASTContext &C, QualType T, Parts P);
  3452. static MSGuidDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  3453. // Only ASTContext::getMSGuidDecl and deserialization create these.
  3454. friend class ASTContext;
  3455. friend class ASTReader;
  3456. friend class ASTDeclReader;
  3457. public:
  3458. /// Print this UUID in a human-readable format.
  3459. void printName(llvm::raw_ostream &OS) const override;
  3460. /// Get the decomposed parts of this declaration.
  3461. Parts getParts() const { return PartVal; }
  3462. /// Get the value of this MSGuidDecl as an APValue. This may fail and return
  3463. /// an absent APValue if the type of the declaration is not of the expected
  3464. /// shape.
  3465. APValue &getAsAPValue() const;
  3466. static void Profile(llvm::FoldingSetNodeID &ID, Parts P) {
  3467. ID.AddInteger(P.Part1);
  3468. ID.AddInteger(P.Part2);
  3469. ID.AddInteger(P.Part3);
  3470. ID.AddInteger(P.getPart4And5AsUint64());
  3471. }
  3472. void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, PartVal); }
  3473. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  3474. static bool classofKind(Kind K) { return K == Decl::MSGuid; }
  3475. };
  3476. /// Insertion operator for diagnostics. This allows sending an AccessSpecifier
  3477. /// into a diagnostic with <<.
  3478. const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
  3479. AccessSpecifier AS);
  3480. } // namespace clang
  3481. #endif // LLVM_CLANG_AST_DECLCXX_H
  3482. #ifdef __GNUC__
  3483. #pragma GCC diagnostic pop
  3484. #endif