tbaeder updated this revision to Diff 519434.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147621/new/

https://reviews.llvm.org/D147621

Files:
  clang/lib/AST/Interp/Interp.cpp
  clang/lib/AST/Interp/Interp.h
  clang/test/AST/Interp/records.cpp


Index: clang/test/AST/Interp/records.cpp
===================================================================
--- clang/test/AST/Interp/records.cpp
+++ clang/test/AST/Interp/records.cpp
@@ -700,3 +700,26 @@
 
 };
 #endif
+
+namespace MutableFields {
+  class Foo {
+  public:
+    constexpr Foo() : I(1) {}
+    mutable int I; // ref-note {{declared here}}
+  };
+
+
+  constexpr int foo() {
+    const Foo F;
+    F.I = 12;
+    return F.I;
+  }
+  static_assert(foo() == 12, "");
+
+  /// FIXME: This should also be rejected in the new interpreter.
+  constexpr Foo GlobalF;
+  constexpr int foo2() { // ref-error {{never produces a constant expression}}
+    return GlobalF.I; // ref-note {{read of mutable member 'I' is not allowed 
in a constant expression}}
+  }
+}
+
Index: clang/lib/AST/Interp/Interp.h
===================================================================
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -1057,7 +1057,12 @@
     return false;
   if (!CheckRange(S, OpPC, Ptr, CSK_Field))
     return false;
-  S.Stk.push<Pointer>(Ptr.atField(Off));
+
+  const Pointer &FieldPtr = Ptr.atField(Off);
+  if (!CheckMutable(S, OpPC, FieldPtr))
+    return false;
+
+  S.Stk.push<Pointer>(FieldPtr);
   return true;
 }
 
Index: clang/lib/AST/Interp/Interp.cpp
===================================================================
--- clang/lib/AST/Interp/Interp.cpp
+++ clang/lib/AST/Interp/Interp.cpp
@@ -224,6 +224,14 @@
     return true;
   }
 
+  // Writing to mutable fields is fine even if the parent is
+  // declared const.
+  if (S.getLangOpts().CPlusPlus14) {
+    if (const auto *FD = dyn_cast<FieldDecl>(Ptr.getFieldDesc()->asDecl());
+        FD && FD->isMutable())
+      return true;
+  }
+
   const QualType Ty = Ptr.getType();
   const SourceInfo &Loc = S.Current->getSource(OpPC);
   S.FFDiag(Loc, diag::note_constexpr_modify_const_type) << Ty;
@@ -236,6 +244,11 @@
     return true;
   }
 
+  // In C++14 onwards, it is permitted to read a mutable member whose
+  // lifetime began within the evaluation.
+  if (S.getLangOpts().CPlusPlus14)
+    return true;
+
   const SourceInfo &Loc = S.Current->getSource(OpPC);
   const FieldDecl *Field = Ptr.getField();
   S.FFDiag(Loc, diag::note_constexpr_access_mutable, 1) << AK_Read << Field;


Index: clang/test/AST/Interp/records.cpp
===================================================================
--- clang/test/AST/Interp/records.cpp
+++ clang/test/AST/Interp/records.cpp
@@ -700,3 +700,26 @@
 
 };
 #endif
+
+namespace MutableFields {
+  class Foo {
+  public:
+    constexpr Foo() : I(1) {}
+    mutable int I; // ref-note {{declared here}}
+  };
+
+
+  constexpr int foo() {
+    const Foo F;
+    F.I = 12;
+    return F.I;
+  }
+  static_assert(foo() == 12, "");
+
+  /// FIXME: This should also be rejected in the new interpreter.
+  constexpr Foo GlobalF;
+  constexpr int foo2() { // ref-error {{never produces a constant expression}}
+    return GlobalF.I; // ref-note {{read of mutable member 'I' is not allowed in a constant expression}}
+  }
+}
+
Index: clang/lib/AST/Interp/Interp.h
===================================================================
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -1057,7 +1057,12 @@
     return false;
   if (!CheckRange(S, OpPC, Ptr, CSK_Field))
     return false;
-  S.Stk.push<Pointer>(Ptr.atField(Off));
+
+  const Pointer &FieldPtr = Ptr.atField(Off);
+  if (!CheckMutable(S, OpPC, FieldPtr))
+    return false;
+
+  S.Stk.push<Pointer>(FieldPtr);
   return true;
 }
 
Index: clang/lib/AST/Interp/Interp.cpp
===================================================================
--- clang/lib/AST/Interp/Interp.cpp
+++ clang/lib/AST/Interp/Interp.cpp
@@ -224,6 +224,14 @@
     return true;
   }
 
+  // Writing to mutable fields is fine even if the parent is
+  // declared const.
+  if (S.getLangOpts().CPlusPlus14) {
+    if (const auto *FD = dyn_cast<FieldDecl>(Ptr.getFieldDesc()->asDecl());
+        FD && FD->isMutable())
+      return true;
+  }
+
   const QualType Ty = Ptr.getType();
   const SourceInfo &Loc = S.Current->getSource(OpPC);
   S.FFDiag(Loc, diag::note_constexpr_modify_const_type) << Ty;
@@ -236,6 +244,11 @@
     return true;
   }
 
+  // In C++14 onwards, it is permitted to read a mutable member whose
+  // lifetime began within the evaluation.
+  if (S.getLangOpts().CPlusPlus14)
+    return true;
+
   const SourceInfo &Loc = S.Current->getSource(OpPC);
   const FieldDecl *Field = Ptr.getField();
   S.FFDiag(Loc, diag::note_constexpr_access_mutable, 1) << AK_Read << Field;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to