Douglas Gregor wrote:

On Nov 13, 2008, at 2:40 PM, Sebastian Redl wrote:

Douglas Gregor wrote:
Other than that, it looks good. Are you planning to submit a follow-up patch that deals with the constness of MemberExprs for mutable members?
Do we even have MemberExprs yet? If we do, of course I will.

Yes, we do.
OK, here you go. This one is almost trivial.

Sebastian
Index: test/SemaCXX/class.cpp
===================================================================
--- test/SemaCXX/class.cpp      (revision 59331)
+++ test/SemaCXX/class.cpp      (working copy)
@@ -82,6 +82,18 @@
   }
 };
 
+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}}
Index: include/clang/AST/DeclCXX.h
===================================================================
--- include/clang/AST/DeclCXX.h (revision 59330)
+++ include/clang/AST/DeclCXX.h (working copy)
@@ -107,17 +107,20 @@
 /// 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: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp       (revision 59331)
+++ lib/Sema/SemaDecl.cpp       (working copy)
@@ -2555,7 +2555,9 @@
   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: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp       (revision 59330)
+++ lib/Sema/SemaExpr.cpp       (working copy)
@@ -951,6 +951,10 @@
     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: lib/AST/DeclCXX.cpp
===================================================================
--- lib/AST/DeclCXX.cpp (revision 59330)
+++ lib/AST/DeclCXX.cpp (working copy)
@@ -22,9 +22,9 @@
  
 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(ASTContext &C, TagKind TK, DeclContext *DC,
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to