]> granicus.if.org Git - clang/commitdiff
Added AccessSpecDecl node.
authorAbramo Bagnara <abramo.bagnara@gmail.com>
Sat, 5 Jun 2010 05:09:32 +0000 (05:09 +0000)
committerAbramo Bagnara <abramo.bagnara@gmail.com>
Sat, 5 Jun 2010 05:09:32 +0000 (05:09 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105525 91177308-0d34-0410-b5e6-96231b3b80d8

15 files changed:
include/clang/AST/DeclCXX.h
include/clang/Basic/DeclNodes.td
include/clang/Frontend/PCHBitCodes.h
include/clang/Parse/Action.h
lib/AST/DeclBase.cpp
lib/AST/DeclPrinter.cpp
lib/CodeGen/CGDecl.cpp
lib/Frontend/PCHReaderDecl.cpp
lib/Frontend/PCHWriterDecl.cpp
lib/Parse/ParseDeclCXX.cpp
lib/Sema/Sema.h
lib/Sema/SemaDecl.cpp
lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaTemplateInstantiateDecl.cpp
tools/libclang/CIndex.cpp

index f672433c8922cf938cf4d657fac4524084fd5329..2caba65d78227ff2e90c0eb7379995625e5e717e 100644 (file)
@@ -92,6 +92,53 @@ namespace llvm {
 
 namespace clang {
 
+/// AccessSpecDecl - An access specifier followed by colon ':'.
+///
+/// An objects of this class represents sugar for the syntactic occurrence
+/// of an access specifier followed by a colon in the list of member
+/// specifiers of a C++ class definition.
+///
+/// Note that they do not represent other uses of access specifiers,
+/// such as those occurring in a list of base specifiers.
+/// Also note that this class has nothing to do with so-called
+/// "access declarations" (C++98 11.3 [class.access.dcl]).
+class AccessSpecDecl : public Decl {
+  /// ColonLoc - The location of the ':'.
+  SourceLocation ColonLoc;
+
+  AccessSpecDecl(AccessSpecifier AS, DeclContext *DC,
+                 SourceLocation ASLoc, SourceLocation ColonLoc)
+    : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) {
+    setAccess(AS);
+  }
+public:
+  /// getAccessSpecifierLoc - The location of the access specifier.
+  SourceLocation getAccessSpecifierLoc() const { return getLocation(); }
+  /// setAccessSpecifierLoc - Sets the location of the access specifier.
+  void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); }
+
+  /// getColonLoc - The location of the colon following the access specifier.
+  SourceLocation getColonLoc() const { return ColonLoc; }
+  /// setColonLoc - Sets the location of the colon.
+  void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; }
+
+  SourceRange getSourceRange() const {
+    return SourceRange(getAccessSpecifierLoc(), getColonLoc());
+  }
+
+  static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS,
+                                DeclContext *DC, SourceLocation ASLoc,
+                                SourceLocation ColonLoc) {
+    return new (C) AccessSpecDecl(AS, DC, ASLoc, ColonLoc);
+  }
+
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
+  static bool classof(const AccessSpecDecl *D) { return true; }
+  static bool classofKind(Kind K) { return K == AccessSpec; }
+};
+
+
 /// CXXBaseSpecifier - A base class of a C++ class.
 ///
 /// Each CXXBaseSpecifier represents a single, direct base class (or
index 8025394f3c34414f5b47c5c3395350e3c711c83c..a25f3ee10b0fe2b40aaa7c2da0f9ecc8f7c1acb4 100644 (file)
@@ -61,6 +61,7 @@ def ObjCPropertyImpl : Decl;
 def ObjCForwardProtocol : Decl;
 def ObjCClass : Decl;
 def FileScopeAsm : Decl;
+def AccessSpec : Decl;
 def Friend : Decl;
 def FriendTemplate : Decl;
 def StaticAssert : Decl;
index 2493cfd47d1e9e67f54ec0b70d9f6b3113e0c21f..32643de2613f59676f959e2be5363966e633d279 100644 (file)
@@ -562,6 +562,8 @@ namespace clang {
       DECL_CXX_DESTRUCTOR,
       /// \brief A CXXConversionDecl record.
       DECL_CXX_CONVERSION,
+      /// \brief An AccessSpecDecl record.
+      DECL_ACCESS_SPEC,
 
       // FIXME: Implement serialization for these decl types. This just
       // allocates the order in which
index 74b3b5b7641b428fbc134cceb8fe016f1bd44986..0b07ded05d389ee1457d031ccd2c2d2604e25b81 100644 (file)
@@ -1769,6 +1769,15 @@ public:
                                    unsigned NumBases) {
   }
 
+  /// ActOnAccessSpecifier - This is invoked when an access specifier
+  /// (and the colon following it) is found during the parsing of a
+  /// C++ class member declarator.
+  virtual DeclPtrTy ActOnAccessSpecifier(AccessSpecifier AS,
+                                         SourceLocation ASLoc,
+                                         SourceLocation ColonLoc) {
+    return DeclPtrTy();
+  }
+
   /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member
   /// declarator is parsed. 'AS' is the access specifier, 'BitfieldWidth'
   /// specifies the bitfield width if there is one and 'Init' specifies the
index a73d17a5ca2eefb199c0bd2ca2593db06c0f6df8..8f6fdbc956f8a1bd2d4eae3204563ea3d0073720 100644 (file)
@@ -291,6 +291,7 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
     // Never have names.
     case Friend:
     case FriendTemplate:
+    case AccessSpec:
     case LinkageSpec:
     case FileScopeAsm:
     case StaticAssert:
index 539492479707c9ebeeb9bdccde27d0cf5680d09c..2fb6cb1d311dd96f28d573a992e6a7a2c5c9bda3 100644 (file)
@@ -183,7 +183,7 @@ void DeclPrinter::Print(AccessSpecifier AS) {
   case AS_none:      assert(0 && "No access specifier!"); break;
   case AS_public:    Out << "public"; break;
   case AS_protected: Out << "protected"; break;
-  case AS_private:   Out << " private"; break;
+  case AS_private:   Out << "private"; break;
   }
 }
 
@@ -195,9 +195,6 @@ void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
   if (Indent)
     Indentation += Policy.Indentation;
 
-  bool PrintAccess = isa<CXXRecordDecl>(DC);
-  AccessSpecifier CurAS = AS_none;
-
   llvm::SmallVector<Decl*, 2> Decls;
   for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
        D != DEnd; ++D) {
@@ -211,18 +208,6 @@ void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
         continue;
     }
 
-    if (PrintAccess) {
-      AccessSpecifier AS = D->getAccess();
-
-      if (AS != CurAS) {
-        if (Indent)
-          this->Indent(Indentation - Policy.Indentation);
-        Print(AS);
-        Out << ":\n";
-        CurAS = AS;
-      }
-    }
-
     // The next bits of code handles stuff like "struct {int x;} a,b"; we're
     // forced to merge the declarations because there's no other way to
     // refer to the struct in question.  This limited merging is safe without
@@ -251,6 +236,16 @@ void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
       Decls.push_back(*D);
       continue;
     }
+
+    if (isa<AccessSpecDecl>(*D)) {
+      Indentation -= Policy.Indentation;
+      this->Indent();
+      Print(D->getAccess());
+      Out << ":\n";
+      Indentation += Policy.Indentation;
+      continue;
+    }
+
     this->Indent();
     Visit(*D);
 
index 5b0cc00cd767ce8d57f51fd2d6d22680243a2144..e8b67b8864b74f5819da34a483918cb31b6d83ed 100644 (file)
@@ -59,6 +59,7 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
   case Decl::ObjCImplementation:
   case Decl::ObjCProperty:
   case Decl::ObjCCompatibleAlias:
+  case Decl::AccessSpec:
   case Decl::LinkageSpec:
   case Decl::ObjCPropertyImpl:
   case Decl::ObjCClass:
index 5056f93377c79bcb1e913f7b4866e04458d16a35..7ccf14362f9d8b16fb1c1c24345c90931db5c55e 100644 (file)
@@ -80,6 +80,7 @@ namespace {
     void VisitUsingShadow(UsingShadowDecl *D);
     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
     void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
+    void VisitAccessSpecDecl(AccessSpecDecl *D);
     void VisitFriendTemplateDecl(FriendTemplateDecl *D);
     void VisitStaticAssertDecl(StaticAssertDecl *D);
     void VisitBlockDecl(BlockDecl *BD);
@@ -607,6 +608,11 @@ void PCHDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
   VisitCXXMethodDecl(D);
 }
 
+void PCHDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
+  VisitDecl(D);
+  D->setColonLoc(Reader.ReadSourceLocation(Record, Idx));
+}
+
 void PCHDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
   assert(false && "cannot read FriendTemplateDecl");
 }
@@ -971,6 +977,10 @@ Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
   case pch::DECL_CXX_CONVERSION:
     D = CXXConversionDecl::Create(*Context, Decl::EmptyShell());
     break;
+  case pch::DECL_ACCESS_SPEC:
+    D = AccessSpecDecl::Create(*Context, AS_none, 0, SourceLocation(),
+                               SourceLocation());
+    break;
   case pch::DECL_FRIEND:
     assert(false && "cannot read FriendDecl");
     break;
index 499a16adb1eff1035847d108101978a795794e57..6b88dfcae416239d251f904a1f0cc2ae50185e19 100644 (file)
@@ -81,6 +81,7 @@ namespace {
     void VisitUsingShadow(UsingShadowDecl *D);
     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
     void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
+    void VisitAccessSpecDecl(AccessSpecDecl *D);
     void VisitFriendTemplateDecl(FriendTemplateDecl *D);
     void VisitStaticAssertDecl(StaticAssertDecl *D);
     void VisitBlockDecl(BlockDecl *D);
@@ -607,6 +608,12 @@ void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
   Code = pch::DECL_CXX_CONVERSION;
 }
 
+void PCHDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
+  VisitDecl(D);
+  Writer.AddSourceLocation(D->getColonLoc(), Record);
+  Code = pch::DECL_ACCESS_SPEC;
+}
+
 void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
   assert(false && "cannot write FriendTemplateDecl");
 }
index 7e59170b370e9cd96c5c19ce4b8596e6099d8ee0..eda490d918cff9f029a34bde6cb2d7b0bfbd23cf 100644 (file)
@@ -1570,8 +1570,13 @@ void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
     if (AS != AS_none) {
       // Current token is a C++ access specifier.
       CurAS = AS;
+      SourceLocation ASLoc = Tok.getLocation();
+      ConsumeToken();
+      if (Tok.is(tok::colon))
+        Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
+      else
+        Diag(Tok, diag::err_expected_colon);
       ConsumeToken();
-      ExpectAndConsume(tok::colon, diag::err_expected_colon);
       continue;
     }
 
index 5d4b39e2925f94a05ded242288ced80fd3009b42..064d34316ea50c45943ea1a2cdf4fb59a3065609 100644 (file)
@@ -2544,6 +2544,10 @@ public:
   virtual bool isCurrentClassName(const IdentifierInfo &II, Scope *S,
                                   const CXXScopeSpec *SS);
 
+  virtual DeclPtrTy ActOnAccessSpecifier(AccessSpecifier Access,
+                                         SourceLocation ASLoc,
+                                         SourceLocation ColonLoc);
+
   virtual DeclPtrTy ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS,
                                              Declarator &D,
                                  MultiTemplateParamsArg TemplateParameterLists,
index 501d878f574b413e8206fe4bc5982ff783fe0b18..03b2ef56430571dcdaa18a018b8086f3d57ce1b2 100644 (file)
@@ -1770,6 +1770,8 @@ Sema::DeclPtrTy Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS,
             << (int)Record->isUnion();
           Invalid = true;
         }
+      } else if (isa<AccessSpecDecl>(*Mem)) {
+        // Any access specifier is fine.
       } else {
         // We have something that isn't a non-static data
         // member. Complain about it.
index a6a1943ecd0aca8f3675ea899acf8a95cc606fa2..7dcda884076a04fc0738b68fa5fed68aaf2d39f1 100644 (file)
@@ -871,6 +871,17 @@ std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) {
 // C++ class member Handling
 //===----------------------------------------------------------------------===//
 
+/// ActOnAccessSpecifier - Parsed an access specifier followed by a colon.
+Sema::DeclPtrTy
+Sema::ActOnAccessSpecifier(AccessSpecifier Access,
+                           SourceLocation ASLoc, SourceLocation ColonLoc) {
+  assert(Access != AS_none && "Invalid kind for syntactic access specifier!");
+  AccessSpecDecl* ASDecl = AccessSpecDecl::Create(Context, Access, CurContext,
+                                                  ASLoc, ColonLoc);
+  CurContext->addHiddenDecl(ASDecl);
+  return DeclPtrTy::make(ASDecl);
+}
+
 /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member
 /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the
 /// bitfield width if there is one and 'InitExpr' specifies the initializer if
index 939f6caa96a5a79a16d20f8282d6baeddd925d5d..0af3ed9095ee388555296aa62f0e4c6bf7896666 100644 (file)
@@ -48,6 +48,7 @@ namespace {
     Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
     Decl *VisitTypedefDecl(TypedefDecl *D);
     Decl *VisitVarDecl(VarDecl *D);
+    Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
     Decl *VisitFieldDecl(FieldDecl *D);
     Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
     Decl *VisitEnumDecl(EnumDecl *D);
@@ -437,6 +438,14 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
   return Var;
 }
 
+Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
+  AccessSpecDecl* AD
+    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
+                             D->getAccessSpecifierLoc(), D->getColonLoc());
+  Owner->addHiddenDecl(AD);
+  return AD;
+}
+
 Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
   bool Invalid = false;
   TypeSourceInfo *DI = D->getTypeSourceInfo();
index 903d341a79c4aefbe8eef9d11323b75f38ccba5f..6410d3715f6b594930797f2f80638c971ae5b1cf 100644 (file)
@@ -2041,6 +2041,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
   case Decl::TemplateTemplateParm:
   case Decl::ObjCCategoryImpl:
   case Decl::ObjCImplementation:
+  case Decl::AccessSpec:
   case Decl::LinkageSpec:
   case Decl::ObjCPropertyImpl:
   case Decl::FileScopeAsm: