]> granicus.if.org Git - clang/commitdiff
Implement effects of 'mutable', and a few comments from Chris on its parsing.
authorSebastian Redl <sebastian.redl@getdesigned.at>
Mon, 17 Nov 2008 23:24:37 +0000 (23:24 +0000)
committerSebastian Redl <sebastian.redl@getdesigned.at>
Mon, 17 Nov 2008 23:24:37 +0000 (23:24 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59470 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclCXX.h
lib/AST/DeclCXX.cpp
lib/Sema/SemaDecl.cpp
lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaExpr.cpp
test/SemaCXX/class.cpp

index 86cd68ac8e8ec086fd8be42e4716da892461f4d9..77eb8fade422540f7a5159d82285ee9e6337aac1 100644 (file)
@@ -112,17 +112,20 @@ protected:
 /// CXXFieldDecl - Represents an instance field of a C++ struct/union/class.
 class CXXFieldDecl : public FieldDecl {
   CXXRecordDecl *Parent;
+  bool Mutable;
 
   CXXFieldDecl(CXXRecordDecl *RD, SourceLocation L, IdentifierInfo *Id,
-               QualType T, Expr *BW = NULL)
-    : FieldDecl(CXXField, L, Id, T, BW), Parent(RD) {}
+               QualType T, bool Mut, Expr *BW = NULL)
+    : FieldDecl(CXXField, L, Id, T, BW), Parent(RD), Mutable(Mut) {}
 public:
   static CXXFieldDecl *Create(ASTContext &C, CXXRecordDecl *RD,SourceLocation L,
-                              IdentifierInfo *Id, QualType T, Expr *BW = NULL);
+                              IdentifierInfo *Id, QualType T, bool Mut,
+                              Expr *BW = NULL);
 
   void setAccess(AccessSpecifier AS) { Access = AS; }
   AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
   CXXRecordDecl *getParent() const { return Parent; }
+  bool isMutable() const { return Mutable; }
     
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return D->getKind() == CXXField; }
index 4037404124d323c6ef13f30646b123139734e1d8..6be9eaf1a34287a303ade0b381bb99e2ea06e686 100644 (file)
@@ -22,9 +22,9 @@ using namespace clang;
  
 CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,
                                    SourceLocation L, IdentifierInfo *Id,
-                                   QualType T, Expr *BW) {
+                                   QualType T, bool Mut, Expr *BW) {
   void *Mem = C.getAllocator().Allocate<CXXFieldDecl>();
-  return new (Mem) CXXFieldDecl(RD, L, Id, T, BW);
+  return new (Mem) CXXFieldDecl(RD, L, Id, T, Mut, BW);
 }
 
 CXXRecordDecl::CXXRecordDecl(TagKind TK, DeclContext *DC,
index 9c0a40e74abe89d341bc83c779e0e4bfd2e50b9a..fdc4aff031cd92d2c7cdb4bbe5acbacedbf0dc0b 100644 (file)
@@ -1149,6 +1149,7 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
       // an error here
       Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember);
       InvalidDecl = true;
+      break;
     }
 
     IdentifierInfo *II = Name.getAsIdentifierInfo();
@@ -2603,7 +2604,9 @@ Sema::DeclTy *Sema::ActOnField(Scope *S,
   if (getLangOptions().CPlusPlus) {
     // FIXME: Replace CXXFieldDecls with FieldDecls for simple structs.
     NewFD = CXXFieldDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
-                                 Loc, II, T, BitWidth);
+                                 Loc, II, T,
+                                 D.getDeclSpec().getStorageClassSpec() ==
+                                   DeclSpec::SCS_mutable, BitWidth);
     if (II)
       PushOnScopeChains(NewFD, S);
   }
index f601fe4dc6a9629209d94f526b7ab1f2eb57c58d..68dcdc8d2badfb4e5fb15260e9e3ec6e820d34f8 100644 (file)
@@ -447,6 +447,8 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
         else
           Diag(DS.getThreadSpecLoc(),
                diag::err_mutable_function);
+        // FIXME: It would be nicer if the keyword was ignored only for this
+        // declarator. Otherwise we could get follow-up errors.
         D.getMutableDeclSpec().ClearStorageClassSpecs();
       } else {
         QualType T = GetTypeForDeclarator(D, S);
@@ -460,6 +462,8 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
             Diag(DS.getStorageClassSpecLoc(), err);
           else
             Diag(DS.getThreadSpecLoc(), err);
+          // FIXME: It would be nicer if the keyword was ignored only for this
+          // declarator. Otherwise we could get follow-up errors.
           D.getMutableDeclSpec().ClearStorageClassSpecs();
         }
       }
index 0506b4076eff98873a8bb8c2fd1805ef67c6833e..2b621f2a2e2c7e6c9d9e62c7375696154762b72d 100644 (file)
@@ -951,6 +951,10 @@ ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
     QualType MemberType = MemberDecl->getType();
     unsigned combinedQualifiers =
         MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
+    if (CXXFieldDecl *CXXMember = dyn_cast<CXXFieldDecl>(MemberDecl)) {
+      if (CXXMember->isMutable())
+        combinedQualifiers &= ~QualType::Const;
+    }
     MemberType = MemberType.getQualifiedType(combinedQualifiers);
 
     return new MemberExpr(BaseExpr, OpKind == tok::arrow, MemberDecl,
index ada508ac3225fe648b1aa3ace4cf6ff941c13ed4..b7d1ca34dcfad799818f191c2ca87b3ce896a02d 100644 (file)
@@ -82,6 +82,18 @@ class C2 {
   }
 };
 
+struct C3 {
+  int i;
+  mutable int j;
+};
+void f()
+{
+  const C3 c3 = { 1, 2 };
+  (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'int const *' to 'int *' is not allowed}}
+  // but no error here
+  (void)static_cast<int*>(&c3.j);
+}
+
 // Play with mutable a bit more, to make sure it doesn't crash anything.
 mutable int gi; // expected-error {{error: 'mutable' can only be applied to member variables}}
 mutable void gfn(); // expected-error {{illegal storage class on function}}