DIAG(err_missing_param, ERROR,
"expected parameter declarator")
-DIAG(err_invalid_reference_qualifier_application, ERROR,
- "'%0' qualifier may not be applied to a reference")
DIAG(err_declarator_need_ident, ERROR,
"declarator requires an identifier")
DIAG(err_bad_language, ERROR,
"'%0' declared as a pointer to a reference")
DIAG(err_illegal_decl_reference_to_reference, ERROR,
"'%0' declared as a reference to a reference")
+DIAG(err_invalid_reference_qualifier_application, ERROR,
+ "'%0' qualifier may not be applied to a reference")
+DIAG(err_reference_to_void, ERROR,
+ "cannot form a reference to 'void'")
DIAG(err_qualified_block_pointer_type, ERROR,
"qualifier specification on block pointer type not allowed")
DIAG(err_nonfunction_block_type, ERROR,
// Recursively parse the declarator.
ParseDeclaratorInternal(D);
+ if (D.getNumTypeObjects() > 0) {
+ // C++ [dcl.ref]p4: There shall be no references to references.
+ DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
+ if (InnerChunk.Kind == DeclaratorChunk::Reference) {
+ Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference,
+ D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
+
+ // Once we've complained about the reference-to-referwnce, we
+ // can go ahead and build the (technically ill-formed)
+ // declarator: reference collapsing will take care of it.
+ }
+ }
+
// Remember that we parsed a reference type. It doesn't have type-quals.
D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
DS.TakeAttributes()));
Result.getAsString(), DS.getSourceRange());
}
+ // C++ [dcl.ref]p1:
+ // Cv-qualified references are ill-formed except when the
+ // cv-qualifiers are introduced through the use of a typedef
+ // (7.1.3) or of a template type argument (14.3), in which
+ // case the cv-qualifiers are ignored.
+ if (DS.getTypeSpecType() == DeclSpec::TST_typedef &&
+ TypeQuals && Result->isReferenceType()) {
+ TypeQuals &= ~QualType::Const;
+ TypeQuals &= ~QualType::Volatile;
+ }
+
Result = Result.getQualifiedType(TypeQuals);
}
return Result;
// Apply the pointer typequals to the pointer object.
T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
break;
- case DeclaratorChunk::Reference:
- if (const ReferenceType *RT = T->getAsReferenceType()) {
- // C++ 8.3.2p4: There shall be no references to references.
- Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
- D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
+ case DeclaratorChunk::Reference: {
+ // Whether we should suppress the creation of the reference.
+ bool SuppressReference = false;
+ if (T->isReferenceType()) {
+ // C++ [dcl.ref]p4: There shall be no references to references.
+ //
+ // According to C++ DR 106, references to references are only
+ // diagnosed when they are written directly (e.g., "int & &"),
+ // but not when they happen via a typedef:
+ //
+ // typedef int& intref;
+ // typedef intref& intref2;
+ //
+ // Parser::ParserDeclaratorInternal diagnoses the case where
+ // references are written directly; here, we handle the
+ // collapsing of references-to-references as described in C++
+ // DR 106 and amended by C++ DR 540.
+ SuppressReference = true;
+ }
+
+ // C++ [dcl.ref]p1:
+ // A declarator that specifies the type “reference to cv void”
+ // is ill-formed.
+ if (T->isVoidType()) {
+ Diag(DeclType.Loc, diag::err_reference_to_void);
D.setInvalidType(true);
- T = RT->getPointeeType();
+ T = Context.IntTy;
}
// Enforce C99 6.7.3p2: "Types other than pointer types derived from
DeclType.Ref.HasRestrict = false;
}
- T = Context.getReferenceType(T);
+ if (!SuppressReference)
+ T = Context.getReferenceType(T);
// Handle restrict on references.
if (DeclType.Ref.HasRestrict)
T.addRestrict();
break;
+ }
case DeclaratorChunk::Array: {
DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
void test7(C& c) {
A& a1 = c; // expected-error {{ambiguous conversion from derived class 'struct C' to base class 'struct A':}}
}
+
+// C++ [dcl.ref]p1, C++ [dcl.ref]p4
+void test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}}
+
+ void&, // expected-error{{cannot form a reference to 'void'}}
+ int& &) // expected-error{{'type name' declared as a reference to a reference}}
+{
+ typedef int& intref;
+ typedef intref& intrefref; // C++ DR 106: reference collapsing
+
+ typedef intref const intref_c; // okay. FIXME: how do we verify that this is the same type as intref?
+}