/// 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; }
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,
// an error here
Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember);
InvalidDecl = true;
+ break;
}
IdentifierInfo *II = Name.getAsIdentifierInfo();
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);
}
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);
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();
}
}
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,
}
};
+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}}