[cfe-commits] r58612 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Parse/ParseDecl.cpp lib/Sema/SemaType.cpp test/SemaCXX/references.cpp
Douglas Gregor
doug.gregor at gmail.com
Mon Nov 3 07:51:30 PST 2008
Author: dgregor
Date: Mon Nov 3 09:51:28 2008
New Revision: 58612
URL: http://llvm.org/viewvc/llvm-project?rev=58612&view=rev
Log:
Implement C++ DR 106 and C++ DR 540, both of which deal with
reference-collapsing.
Implement diagnostic for formation of a reference to cv void.
Drop cv-qualifiers added to a reference type when the reference type
comes from a typedef.
Modified:
cfe/trunk/include/clang/Basic/DiagnosticKinds.def
cfe/trunk/lib/Parse/ParseDecl.cpp
cfe/trunk/lib/Sema/SemaType.cpp
cfe/trunk/test/SemaCXX/references.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=58612&r1=58611&r2=58612&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Mon Nov 3 09:51:28 2008
@@ -657,8 +657,6 @@
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,
@@ -959,6 +957,10 @@
"'%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,
Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=58612&r1=58611&r2=58612&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Mon Nov 3 09:51:28 2008
@@ -1222,6 +1222,19 @@
// 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()));
Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=58612&r1=58611&r2=58612&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Mon Nov 3 09:51:28 2008
@@ -232,6 +232,17 @@
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;
@@ -283,13 +294,33 @@
// 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
@@ -302,12 +333,14 @@
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);
Modified: cfe/trunk/test/SemaCXX/references.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/references.cpp?rev=58612&r1=58611&r2=58612&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/references.cpp (original)
+++ cfe/trunk/test/SemaCXX/references.cpp Mon Nov 3 09:51:28 2008
@@ -75,3 +75,15 @@
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?
+}
More information about the cfe-commits
mailing list