Hi,

This patch implements parsing and semantic analysis of the mutable keyword in C++.

Sebastian
Index: test/Parser/cxx-class.cpp
===================================================================
--- test/Parser/cxx-class.cpp   (revision 58917)
+++ test/Parser/cxx-class.cpp   (working copy)
@@ -21,6 +21,7 @@
   int x,f(),y,g();
   inline int h();
   static const int sci = 10;
+  mutable int mi;
 };
 void glo()
 {
Index: test/SemaCXX/class.cpp
===================================================================
--- test/SemaCXX/class.cpp      (revision 58917)
+++ test/SemaCXX/class.cpp      (working copy)
@@ -61,6 +61,11 @@
   int x,y;
   static int sx;
 
+  mutable int mi;
+  mutable int &mir; // expected-error {{error: 'mutable' can only be applied 
to non-const, non-static, non-reference class data members}}
+  mutable void mfn(); // expected-error {{error: 'mutable' can only be applied 
to non-const, non-static, non-reference class data members}}
+  mutable const int mci; // expected-error {{error: 'mutable' can only be 
applied to non-const, non-static, non-reference class data members}}
+
   static const int number = 50;
   static int arr[number];
 };
@@ -76,3 +81,11 @@
     };
   }
 };
+
+// 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 
non-const, non-static, non-reference class data members}}
+mutable void gfn(); // expected-error {{illegal storage class on function}}
+void ogfn()
+{
+  mutable int ml; // expected-error {{error: 'mutable' can only be applied to 
non-const, non-static, non-reference class data members}}
+}
Index: include/clang/Basic/DiagnosticKinds.def
===================================================================
--- include/clang/Basic/DiagnosticKinds.def     (revision 58917)
+++ include/clang/Basic/DiagnosticKinds.def     (working copy)
@@ -669,6 +669,9 @@
 // C++ class members
 DIAG(err_storageclass_invalid_for_member, ERROR,
      "storage class specified for a member declaration")
+DIAG(err_bad_mutable, ERROR,
+     "'mutable' can only be applied to non-const, non-static, non-reference "
+     "class data members")
 DIAG(err_virtual_non_function, ERROR,
      "'virtual' can only appear on non-static member functions")
 DIAG(err_not_bitfield_type, ERROR,
Index: include/clang/Parse/DeclSpec.h
===================================================================
--- include/clang/Parse/DeclSpec.h      (revision 58917)
+++ include/clang/Parse/DeclSpec.h      (working copy)
@@ -36,7 +36,8 @@
     SCS_static,
     SCS_auto,
     SCS_register,
-    SCS_private_extern
+    SCS_private_extern,
+    SCS_mutable
   };
   
   // type-specifier
Index: www/cxx_status.html
===================================================================
--- www/cxx_status.html (revision 58917)
+++ www/cxx_status.html (working copy)
@@ -607,7 +607,7 @@
   <td></td>
   <td></td>
   <td></td>
-  <td>No parser support for nested names, mutable members, using declarations, 
or templates.</td>
+  <td>No parser support for nested names, using declarations, or 
templates.</td>
 </tr>
 <tr>
   <td>&nbsp;&nbsp;9.3 [class.mfct]</td>
Index: lib/Sema/SemaDeclCXX.cpp
===================================================================
--- lib/Sema/SemaDeclCXX.cpp    (revision 58917)
+++ lib/Sema/SemaDeclCXX.cpp    (working copy)
@@ -429,14 +429,41 @@
   Expr *Init = static_cast<Expr*>(InitExpr);
   SourceLocation Loc = D.getIdentifierLoc();
 
+  bool isFunc = D.isFunctionDeclarator();
+
   // C++ 9.2p6: A member shall not be declared to have automatic storage
   // duration (auto, register) or with the extern storage-class-specifier.
+  // C++ 7.1.1p8: The mutable specifier can be applied only to names of class
+  // data members and cannot be applied to names declared const or static,
+  // and cannot be applied to reference members.
   switch (DS.getStorageClassSpec()) {
     case DeclSpec::SCS_unspecified:
     case DeclSpec::SCS_typedef:
     case DeclSpec::SCS_static:
       // FALL THROUGH.
       break;
+    case DeclSpec::SCS_mutable:
+      if (isFunc) {
+        if (DS.getStorageClassSpecLoc().isValid())
+          Diag(DS.getStorageClassSpecLoc(),
+               diag::err_bad_mutable);
+        else
+          Diag(DS.getThreadSpecLoc(),
+               diag::err_bad_mutable);
+        D.getMutableDeclSpec().ClearStorageClassSpecs();
+      } else {
+        QualType T = GetTypeForDeclarator(D, S);
+        if (T->isReferenceType() || T.isConstQualified()) {
+          if (DS.getStorageClassSpecLoc().isValid())
+            Diag(DS.getStorageClassSpecLoc(),
+                 diag::err_bad_mutable);
+          else
+            Diag(DS.getThreadSpecLoc(),
+                 diag::err_bad_mutable);
+          D.getMutableDeclSpec().ClearStorageClassSpecs();
+        }
+      }
+      break;
     default:
       if (DS.getStorageClassSpecLoc().isValid())
         Diag(DS.getStorageClassSpecLoc(),
@@ -446,7 +473,6 @@
       D.getMutableDeclSpec().ClearStorageClassSpecs();
   }
 
-  bool isFunc = D.isFunctionDeclarator();
   if (!isFunc &&
       D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typedef &&
       D.getNumTypeObjects() == 0) {
@@ -459,7 +485,8 @@
     isFunc = Context.getTypeDeclType(cast<TypeDecl>(TD))->isFunctionType();
   }
 
-  bool isInstField = (DS.getStorageClassSpec() == DeclSpec::SCS_unspecified &&
+  bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified ||
+                       DS.getStorageClassSpec() == DeclSpec::SCS_mutable) &&
                       !isFunc);
 
   Decl *Member;
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp       (revision 58917)
+++ lib/Sema/SemaDecl.cpp       (working copy)
@@ -845,6 +845,7 @@
       default: assert(0 && "Unknown storage class!");
       case DeclSpec::SCS_auto:        
       case DeclSpec::SCS_register:
+      case DeclSpec::SCS_mutable:
         Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
              R.getAsString());
         InvalidDecl = true;
@@ -1095,7 +1096,12 @@
     case DeclSpec::SCS_auto:           SC = VarDecl::Auto; break;
     case DeclSpec::SCS_register:       SC = VarDecl::Register; break;
     case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
-    }    
+    case DeclSpec::SCS_mutable:
+      // mutable can only appear on non-static class members, so it's always
+      // an error here
+      Diag(D.getIdentifierLoc(), diag::err_bad_mutable);
+      InvalidDecl = true;
+    }
     if (DC->isCXXRecord()) {
       assert(SC == VarDecl::Static && "Invalid storage class for member!");
       // This is a static data member for a C++ class.
@@ -1113,11 +1119,11 @@
           InvalidDecl = true;
         }
       }
-        NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(), 
-                                II, R, SC, LastDeclarator,
-                                // FIXME: Move to DeclGroup...
-                                D.getDeclSpec().getSourceRange().getBegin());
-        NewVD->setThreadSpecified(ThreadSpecified);
+      NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(), 
+                              II, R, SC, LastDeclarator,
+                              // FIXME: Move to DeclGroup...
+                              D.getDeclSpec().getSourceRange().getBegin());
+      NewVD->setThreadSpecified(ThreadSpecified);
     }
     // Handle attributes prior to checking for duplicates in MergeVarDecl
     ProcessDeclAttributes(NewVD, D);
Index: lib/Parse/ParseDecl.cpp
===================================================================
--- lib/Parse/ParseDecl.cpp     (revision 58917)
+++ lib/Parse/ParseDecl.cpp     (working copy)
@@ -405,6 +405,7 @@
 ///         'static'
 ///         'auto'
 ///         'register'
+/// [C++]   'mutable'
 /// [GNU]   '__thread'
 ///       function-specifier: [C99 6.7.4]
 /// [C99]   'inline'
@@ -550,6 +551,9 @@
     case tok::kw_register:
       isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, 
PrevSpec);
       break;
+    case tok::kw_mutable:
+      isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
+      break;
     case tok::kw___thread:
       isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
       break;
Index: lib/Parse/DeclSpec.cpp
===================================================================
--- lib/Parse/DeclSpec.cpp      (revision 58917)
+++ lib/Parse/DeclSpec.cpp      (working copy)
@@ -44,6 +44,7 @@
   case DeclSpec::SCS_static:      return "static";
   case DeclSpec::SCS_auto:        return "auto";
   case DeclSpec::SCS_register:    return "register";
+  case DeclSpec::SCS_mutable:     return "mutable";
   }
 }
 
@@ -126,6 +127,7 @@
     return BadSpecifier( (SCS)StorageClassSpec, PrevSpec);
   StorageClassSpec = S;
   StorageClassSpecLoc = Loc;
+  assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
   return false;
 }
 
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to