[cfe-commits] r60597 - in /cfe/trunk: include/clang/AST/ include/clang/Basic/ include/clang/Parse/ lib/AST/ lib/CodeGen/ lib/Parse/ lib/Sema/ test/Parser/
Douglas Gregor
dgregor at apple.com
Fri Dec 5 10:15:26 PST 2008
Author: dgregor
Date: Fri Dec 5 12:15:24 2008
New Revision: 60597
URL: http://llvm.org/viewvc/llvm-project?rev=60597&view=rev
Log:
Representation of template type parameters and non-type template
parameters, with some semantic analysis:
- Template parameters are introduced into template parameter scope
- Complain about template parameter shadowing (except in Microsoft mode)
Note that we leak template parameter declarations like crazy, a
problem we'll remedy once we actually create proper declarations for
templates.
Next up: dependent types and value-dependent/type-dependent
expressions.
Added:
cfe/trunk/lib/Sema/SemaTemplate.cpp
Modified:
cfe/trunk/include/clang/AST/ASTContext.h
cfe/trunk/include/clang/AST/Decl.h
cfe/trunk/include/clang/AST/DeclBase.h
cfe/trunk/include/clang/AST/DeclCXX.h
cfe/trunk/include/clang/AST/Type.h
cfe/trunk/include/clang/Basic/DiagnosticKinds.def
cfe/trunk/include/clang/Parse/Scope.h
cfe/trunk/lib/AST/ASTContext.cpp
cfe/trunk/lib/AST/DeclBase.cpp
cfe/trunk/lib/AST/DeclCXX.cpp
cfe/trunk/lib/AST/DeclSerialization.cpp
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/lib/AST/Type.cpp
cfe/trunk/lib/AST/TypeSerialization.cpp
cfe/trunk/lib/CodeGen/CodeGenTypes.cpp
cfe/trunk/lib/Parse/ParseTemplate.cpp
cfe/trunk/lib/Parse/Parser.cpp
cfe/trunk/lib/Sema/CMakeLists.txt
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/lib/Sema/SemaDeclObjC.cpp
cfe/trunk/test/Parser/cxx-template-decl.cpp
Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Fri Dec 5 12:15:24 2008
@@ -46,6 +46,7 @@
class TranslationUnitDecl;
class TypeDecl;
class TypedefDecl;
+ class TemplateTypeParmDecl;
/// ASTContext - This class holds long-lived AST nodes (such as types and
/// decls) that can be referred to throughout the semantic analysis of a file.
@@ -221,6 +222,7 @@
/// getTypedefType - Return the unique reference to the type for the
/// specified typename decl.
QualType getTypedefType(TypedefDecl *Decl);
+ QualType getTemplateTypeParmType(TemplateTypeParmDecl *Decl);
QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl);
/// getObjCQualifiedInterfaceType - Return a
Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Fri Dec 5 12:15:24 2008
@@ -746,8 +746,8 @@
///
class TypeDecl : public ScopedDecl {
/// TypeForDecl - This indicates the Type object that represents this
- /// TypeDecl. It is a cache maintained by ASTContext::getTypedefType and
- /// ASTContext::getTagDeclType.
+ /// TypeDecl. It is a cache maintained by ASTContext::getTypedefType,
+ /// ASTContext::getTagDeclType, and ASTContext::getTemplateTypeParmType.
Type *TypeForDecl;
friend class ASTContext;
protected:
@@ -794,8 +794,7 @@
/// CreateImpl - Deserialize a TypedefDecl. Called by Decl::Create.
static TypedefDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
- friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
-
+ friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
};
Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Fri Dec 5 12:15:24 2008
@@ -63,6 +63,7 @@
Enum, // [DeclContext]
Record,
CXXRecord, // [DeclContext]
+ TemplateTypeParm,
// ValueDecl
EnumConstant,
Function, // [DeclContext]
@@ -74,6 +75,7 @@
ImplicitParam,
CXXClassVar,
ParmVar,
+ NonTypeTemplateParm,
ObjCInterface, // [DeclContext]
ObjCCompatibleAlias,
ObjCClass,
@@ -85,15 +87,15 @@
// For each non-leaf class, we now define a mapping to the first/last member
// of the class, to allow efficient classof.
- NamedFirst = Field , NamedLast = ParmVar,
+ NamedFirst = Field , NamedLast = NonTypeTemplateParm,
FieldFirst = Field , FieldLast = ObjCAtDefsField,
- ScopedFirst = Namespace , ScopedLast = ParmVar,
- TypeFirst = Typedef , TypeLast = CXXRecord,
+ ScopedFirst = Namespace , ScopedLast = NonTypeTemplateParm,
+ TypeFirst = Typedef , TypeLast = TemplateTypeParm,
TagFirst = Enum , TagLast = CXXRecord,
RecordFirst = Record , RecordLast = CXXRecord,
- ValueFirst = EnumConstant , ValueLast = ParmVar,
+ ValueFirst = EnumConstant , ValueLast = NonTypeTemplateParm,
FunctionFirst = Function , FunctionLast = CXXConversion,
- VarFirst = Var , VarLast = ParmVar
+ VarFirst = Var , VarLast = NonTypeTemplateParm
};
/// IdentifierNamespace - According to C99 6.2.3, there are four namespaces,
@@ -180,6 +182,7 @@
case Var:
case ParmVar:
case EnumConstant:
+ case NonTypeTemplateParm:
case ObjCInterface:
case ObjCCompatibleAlias:
case OverloadedFunction:
@@ -190,6 +193,7 @@
return IDNS_Ordinary;
case Record:
case CXXRecord:
+ case TemplateTypeParm:
case Enum:
return IDNS_Tag;
case Namespace:
Modified: cfe/trunk/include/clang/AST/DeclCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclCXX.h?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclCXX.h (original)
+++ cfe/trunk/include/clang/AST/DeclCXX.h Fri Dec 5 12:15:24 2008
@@ -23,6 +23,70 @@
class CXXDestructorDecl;
class CXXConversionDecl;
+/// TemplateTypeParmDecl - Declaration of a template type parameter,
+/// e.g., "T" in
+/// @code
+/// template<typename T> class vector;
+/// @endcode
+class TemplateTypeParmDecl : public TypeDecl {
+ /// Typename - Whether this template type parameter was declaration
+ /// with the 'typename' keyword. If false, it was declared with the
+ /// 'class' keyword.
+ bool Typename : 1;
+
+ TemplateTypeParmDecl(DeclContext *DC, SourceLocation L,
+ IdentifierInfo *Id, bool Typename)
+ : TypeDecl(TemplateTypeParm, DC, L, Id, 0), Typename(Typename) { }
+
+public:
+ static TemplateTypeParmDecl *Create(ASTContext &C, DeclContext *DC,
+ SourceLocation L, IdentifierInfo *Id,
+ bool Typename);
+
+ /// wasDeclarationWithTypename - Whether this template type
+ /// parameter was declared with the 'typename' keyword. If not, it
+ /// was declared with the 'class' keyword.
+ bool wasDeclaredWithTypename() const { return Typename; }
+
+ // Implement isa/cast/dyncast/etc.
+ static bool classof(const Decl *D) {
+ return D->getKind() == TemplateTypeParm;
+ }
+ static bool classof(const TemplateTypeParmDecl *D) { return true; }
+
+protected:
+ /// EmitImpl - Serialize this TemplateTypeParmDecl. Called by Decl::Emit.
+ virtual void EmitImpl(llvm::Serializer& S) const;
+
+ /// CreateImpl - Deserialize a TemplateTypeParmDecl. Called by Decl::Create.
+ static TemplateTypeParmDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
+
+ friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
+};
+
+/// NonTypeTemplateParmDecl - Declares a non-type template parameter,
+/// e.g., "Size" in
+/// @code
+/// template<int Size> class array { };
+/// @endcode
+class NonTypeTemplateParmDecl : public VarDecl {
+ NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation L,
+ IdentifierInfo *Id, QualType T,
+ SourceLocation TSSL = SourceLocation())
+ : VarDecl(NonTypeTemplateParm, DC, L, Id, T, VarDecl::None, 0, TSSL) { }
+
+public:
+ static NonTypeTemplateParmDecl *
+ Create(ASTContext &C, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
+ QualType T, SourceLocation TypeSpecStartLoc = SourceLocation());
+
+ // Implement isa/cast/dyncast/etc.
+ static bool classof(const Decl *D) {
+ return D->getKind() == NonTypeTemplateParm;
+ }
+ static bool classof(const NonTypeTemplateParmDecl *D) { return true; }
+};
+
/// OverloadedFunctionDecl - An instance of this class represents a
/// set of overloaded functions. All of the functions have the same
/// name and occur within the same scope.
Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Fri Dec 5 12:15:24 2008
@@ -30,6 +30,7 @@
class ASTContext;
class Type;
class TypedefDecl;
+ class TemplateTypeParmDecl;
class TagDecl;
class RecordDecl;
class CXXRecordDecl;
@@ -55,6 +56,7 @@
class ComplexType;
class TagType;
class TypedefType;
+ class TemplateTypeParmType;
class FunctionType;
class FunctionTypeProto;
class ExtVectorType;
@@ -238,6 +240,7 @@
Vector, ExtVector,
FunctionNoProto, FunctionProto,
TypeName, Tagged, ASQual,
+ TemplateTypeParm,
ObjCInterface, ObjCQualifiedInterface,
ObjCQualifiedId,
TypeOfExp, TypeOfTyp, // GNU typeof extension.
@@ -339,8 +342,9 @@
bool isObjCInterfaceType() const; // NSString or NSString<foo>
bool isObjCQualifiedInterfaceType() const; // NSString<foo>
bool isObjCQualifiedIdType() const; // id<foo>
+ bool isTemplateTypeParmType() const; // C++ template type parameter
bool isOverloadType() const; // C++ overloaded function
-
+
// Type Checking Functions: Check to see if this type is structurally the
// specified type, ignoring typedefs and qualifiers, and return a pointer to
// the best type we can.
@@ -364,7 +368,8 @@
const ObjCInterfaceType *getAsObjCInterfaceType() const;
const ObjCQualifiedInterfaceType *getAsObjCQualifiedInterfaceType() const;
const ObjCQualifiedIdType *getAsObjCQualifiedIdType() const;
-
+ const TemplateTypeParmType *getAsTemplateTypeParmType() const;
+
/// getAsPointerToObjCInterfaceType - If this is a pointer to an ObjC
/// interface, return the interface type, otherwise return null.
const ObjCInterfaceType *getAsPointerToObjCInterfaceType() const;
@@ -1174,7 +1179,30 @@
static bool classof(const EnumType *) { return true; }
};
+class TemplateTypeParmType : public Type {
+ TemplateTypeParmDecl *Decl;
+
+protected:
+ TemplateTypeParmType(TemplateTypeParmDecl *D)
+ : Type(TemplateTypeParm, QualType(this, 0)), Decl(D) { }
+
+ friend class ASTContext; // ASTContext creates these
+
+public:
+ TemplateTypeParmDecl *getDecl() const { return Decl; }
+
+ virtual void getAsStringInternal(std::string &InnerString) const;
+ static bool classof(const Type *T) {
+ return T->getTypeClass() == TemplateTypeParm;
+ }
+ static bool classof(const TemplateTypeParmType *T) { return true; }
+
+protected:
+ virtual void EmitImpl(llvm::Serializer& S) const;
+ static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
+ friend class Type;
+};
/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for
/// object oriented design. They basically correspond to C++ classes. There
@@ -1462,6 +1490,10 @@
inline bool Type::isObjCQualifiedIdType() const {
return isa<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
}
+inline bool Type::isTemplateTypeParmType() const {
+ return isa<TemplateTypeParmType>(CanonicalType.getUnqualifiedType());
+}
+
inline bool Type::isOverloadType() const {
if (const BuiltinType *BT = getAsBuiltinType())
return BT->getKind() == BuiltinType::Overload;
Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Fri Dec 5 12:15:24 2008
@@ -944,6 +944,12 @@
DIAG(err_ovl_surrogate_cand, NOTE,
"conversion candidate of type %0")
+/// C++ Templates Semantic Analysis
+DIAG(err_template_param_shadow, ERROR,
+ "declaration of %0 shadows template parameter")
+DIAG(note_template_param_here, NOTE,
+ "template parameter is declared here")
+
DIAG(err_unexpected_typedef, ERROR,
"unexpected type name %0: expected expression")
DIAG(err_unexpected_namespace, ERROR,
Modified: cfe/trunk/include/clang/Parse/Scope.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Scope.h?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Scope.h (original)
+++ cfe/trunk/include/clang/Parse/Scope.h Fri Dec 5 12:15:24 2008
@@ -87,7 +87,13 @@
/// BlockParent - This is a direct link to the immediately containing
/// BlockScope if this scope is not one, or null if there is none.
Scope *BlockParent;
-
+
+ /// TemplateParamParent - This is a direct link to the
+ /// immediately containing template parameter scope. In the
+ /// case of nested templates, template parameter scopes can have
+ /// other template parameter scopes as parents.
+ Scope *TemplateParamParent;
+
/// DeclsInScope - This keeps track of all declarations in this scope. When
/// the declaration is added to the scope, it is set as the current
/// declaration for the identifier in the IdentifierTable. When the scope is
@@ -147,6 +153,9 @@
Scope *getBlockParent() { return BlockParent; }
const Scope *getBlockParent() const { return BlockParent; }
+
+ Scope *getTemplateParamParent() { return TemplateParamParent; }
+ const Scope *getTemplateParamParent() const { return TemplateParamParent; }
typedef DeclSetTy::iterator decl_iterator;
decl_iterator decl_begin() const { return DeclsInScope.begin(); }
@@ -196,18 +205,20 @@
BreakParent = AnyParent->BreakParent;
ContinueParent = AnyParent->ContinueParent;
BlockParent = AnyParent->BlockParent;
+ TemplateParamParent = AnyParent->TemplateParamParent;
} else {
FnParent = BreakParent = ContinueParent = BlockParent = 0;
+ TemplateParamParent = 0;
}
// If this scope is a function or contains breaks/continues, remember it.
- if (Flags & FnScope) FnParent = this;
- if (Flags & BreakScope) BreakParent = this;
- if (Flags & ContinueScope) ContinueParent = this;
- if (Flags & BlockScope) BlockParent = this;
-
+ if (Flags & FnScope) FnParent = this;
+ if (Flags & BreakScope) BreakParent = this;
+ if (Flags & ContinueScope) ContinueParent = this;
+ if (Flags & BlockScope) BlockParent = this;
+ if (Flags & TemplateParamScope) TemplateParamParent = this;
DeclsInScope.clear();
- }
+ }
};
} // end namespace clang
Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Fri Dec 5 12:15:24 2008
@@ -943,6 +943,8 @@
if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
return getTypedefType(Typedef);
+ else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
+ return getTemplateTypeParmType(TP);
else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
return getObjCInterfaceType(ObjCInterface);
@@ -982,6 +984,16 @@
return QualType(Decl->TypeForDecl, 0);
}
+/// getTemplateTypeParmType - Return the unique reference to the type
+/// for the specified template type parameter declaration.
+QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
+ if (!Decl->TypeForDecl) {
+ Decl->TypeForDecl = new TemplateTypeParmType(Decl);
+ Types.push_back(Decl->TypeForDecl);
+ }
+ return QualType(Decl->TypeForDecl, 0);
+}
+
/// getObjCInterfaceType - Return the unique reference to the type for the
/// specified ObjC interface decl.
QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Modified: cfe/trunk/lib/AST/DeclBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Fri Dec 5 12:15:24 2008
@@ -239,6 +239,8 @@
case CXXField: nCXXFieldDecls++; break;
case CXXRecord: nCXXSUC++; break;
// FIXME: Statistics for C++ decls.
+ case TemplateTypeParm:
+ case NonTypeTemplateParm:
case CXXMethod:
case CXXConstructor:
case CXXDestructor:
Modified: cfe/trunk/lib/AST/DeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclCXX.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclCXX.cpp (original)
+++ cfe/trunk/lib/AST/DeclCXX.cpp Fri Dec 5 12:15:24 2008
@@ -19,7 +19,23 @@
//===----------------------------------------------------------------------===//
// Decl Allocation/Deallocation Method Implementations
//===----------------------------------------------------------------------===//
-
+
+TemplateTypeParmDecl *
+TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
+ SourceLocation L, IdentifierInfo *Id,
+ bool Typename) {
+ void *Mem = C.getAllocator().Allocate<TemplateTypeParmDecl>();
+ return new (Mem) TemplateTypeParmDecl(DC, L, Id, Typename);
+}
+
+NonTypeTemplateParmDecl *
+NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
+ SourceLocation L, IdentifierInfo *Id,
+ QualType T, SourceLocation TypeSpecStartLoc) {
+ void *Mem = C.getAllocator().Allocate<NonTypeTemplateParmDecl>();
+ return new (Mem) NonTypeTemplateParmDecl(DC, L, Id, T, TypeSpecStartLoc);
+}
+
CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,
SourceLocation L, IdentifierInfo *Id,
QualType T, bool Mut, Expr *BW) {
Modified: cfe/trunk/lib/AST/DeclSerialization.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclSerialization.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclSerialization.cpp (original)
+++ cfe/trunk/lib/AST/DeclSerialization.cpp Fri Dec 5 12:15:24 2008
@@ -88,6 +88,10 @@
Dcl = TypedefDecl::CreateImpl(D, C);
break;
+ case TemplateTypeParm:
+ Dcl = TemplateTypeParmDecl::CreateImpl(D, C);
+ break;
+
case FileScopeAsm:
Dcl = FileScopeAsmDecl::CreateImpl(D, C);
break;
@@ -630,6 +634,27 @@
}
//===----------------------------------------------------------------------===//
+// TemplateTypeParmDecl Serialization.
+//===----------------------------------------------------------------------===//
+
+void TemplateTypeParmDecl::EmitImpl(Serializer& S) const {
+ S.EmitBool(Typename);
+ ScopedDecl::EmitInRec(S);
+ ScopedDecl::EmitOutRec(S);
+}
+
+TemplateTypeParmDecl *
+TemplateTypeParmDecl::CreateImpl(Deserializer& D, ASTContext& C) {
+ bool Typename = D.ReadBool();
+ void *Mem = C.getAllocator().Allocate<TemplateTypeParmDecl>();
+ TemplateTypeParmDecl *decl
+ = new (Mem) TemplateTypeParmDecl(0, SourceLocation(), NULL, Typename);
+ decl->ScopedDecl::ReadInRec(D, C);
+ decl->ScopedDecl::ReadOutRec(D, C);
+ return decl;
+}
+
+//===----------------------------------------------------------------------===//
// LinkageSpec Serialization.
//===----------------------------------------------------------------------===//
Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Fri Dec 5 12:15:24 2008
@@ -341,6 +341,12 @@
/// DeclCanBeLvalue - Determine whether the given declaration can be
/// an lvalue. This is a helper routine for isLvalue.
static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
+ // C++ [temp.param]p6:
+ // A non-type non-reference template-parameter is not an lvalue.
+ if (const NonTypeTemplateParmDecl *NTTParm
+ = dyn_cast<NonTypeTemplateParmDecl>(Decl))
+ return NTTParm->getType()->isReferenceType();
+
return isa<VarDecl>(Decl) || isa<CXXFieldDecl>(Decl) ||
// C++ 3.10p2: An lvalue refers to an object or function.
(Ctx.getLangOptions().CPlusPlus &&
Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Fri Dec 5 12:15:24 2008
@@ -443,6 +443,11 @@
return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
}
+const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
+ // There is no sugar for template type parameters, so just return
+ // the canonical type pointer if it is the right class.
+ return dyn_cast<TemplateTypeParmType>(CanonicalType);
+}
bool Type::isIntegerType() const {
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
@@ -1002,6 +1007,12 @@
InnerString = getDecl()->getIdentifier()->getName() + InnerString;
}
+void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
+ if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
+ InnerString = ' ' + InnerString;
+ InnerString = getDecl()->getIdentifier()->getName() + InnerString;
+}
+
void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
InnerString = ' ' + InnerString;
Modified: cfe/trunk/lib/AST/TypeSerialization.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/TypeSerialization.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/AST/TypeSerialization.cpp (original)
+++ cfe/trunk/lib/AST/TypeSerialization.cpp Fri Dec 5 12:15:24 2008
@@ -110,6 +110,10 @@
D.RegisterPtr(PtrID,TypedefType::CreateImpl(Context,D));
break;
+ case Type::TemplateTypeParm:
+ D.RegisterPtr(PtrID,TemplateTypeParmType::CreateImpl(Context, D));
+ break;
+
case Type::VariableArray:
D.RegisterPtr(PtrID,VariableArrayType::CreateImpl(Context,D));
break;
@@ -272,6 +276,25 @@
}
//===----------------------------------------------------------------------===//
+// TemplateTypeParmType
+//===----------------------------------------------------------------------===//
+
+void TemplateTypeParmType::EmitImpl(Serializer& S) const {
+ S.EmitPtr(getDecl());
+}
+
+Type* TemplateTypeParmType::CreateImpl(ASTContext& Context, Deserializer& D) {
+ std::vector<Type*>& Types =
+ const_cast<std::vector<Type*>&>(Context.getTypes());
+
+ TemplateTypeParmType* T = new TemplateTypeParmType(NULL);
+ Types.push_back(T);
+
+ D.ReadPtr(T->Decl); // May be backpatched.
+ return T;
+}
+
+//===----------------------------------------------------------------------===//
// VariableArrayType
//===----------------------------------------------------------------------===//
Modified: cfe/trunk/lib/CodeGen/CodeGenTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenTypes.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenTypes.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenTypes.cpp Fri Dec 5 12:15:24 2008
@@ -190,6 +190,7 @@
switch (Ty.getTypeClass()) {
case Type::TypeName: // typedef isn't canonical.
+ case Type::TemplateTypeParm:// template type parameters never generated
case Type::TypeOfExp: // typeof isn't canonical.
case Type::TypeOfTyp: // typeof isn't canonical.
assert(0 && "Non-canonical type, shouldn't happen");
Modified: cfe/trunk/lib/Parse/ParseTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseTemplate.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseTemplate.cpp (original)
+++ cfe/trunk/lib/Parse/ParseTemplate.cpp Fri Dec 5 12:15:24 2008
@@ -41,7 +41,7 @@
SourceLocation TemplateLoc = ConsumeToken();
// Enter template-parameter scope.
- EnterScope(Scope::TemplateParamScope|Scope::DeclScope);
+ EnterScope(Scope::TemplateParamScope);
// Try to parse the template parameters, and the declaration if
// successful.
Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Fri Dec 5 12:15:24 2008
@@ -441,7 +441,7 @@
if (Tok.is(tok::l_brace)) {
// This recovery skips the entire function body. It would be nice
- // to simply call ParseFunctionDefintion() below, however Sema
+ // to simply call ParseFunctionDefinition() below, however Sema
// assumes the declarator represents a function, not a typedef.
ConsumeBrace();
SkipUntil(tok::r_brace, true);
Modified: cfe/trunk/lib/Sema/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/CMakeLists.txt?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/CMakeLists.txt (original)
+++ cfe/trunk/lib/Sema/CMakeLists.txt Fri Dec 5 12:15:24 2008
@@ -18,5 +18,6 @@
SemaNamedCast.cpp
SemaOverload.cpp
SemaStmt.cpp
+ SemaTemplate.cpp
SemaType.cpp
)
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Fri Dec 5 12:15:24 2008
@@ -988,6 +988,17 @@
bool CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl);
+ //===--------------------------------------------------------------------===//
+ // C++ Templates [C++ 14]
+ //
+ bool isTemplateParameterDecl(Decl *D);
+ bool DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl);
+ virtual DeclTy *ActOnTypeParameter(Scope *S, bool Typename,
+ SourceLocation KeyLoc,
+ IdentifierInfo *ParamName,
+ SourceLocation ParamNameLoc);
+ virtual DeclTy *ActOnNonTypeTemplateParameter(Scope *S, Declarator &D);
+
// Objective-C declarations.
virtual DeclTy *ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
IdentifierInfo *ClassName,
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Dec 5 12:15:24 2008
@@ -39,7 +39,8 @@
if (IIDecl && (isa<TypedefDecl>(IIDecl) ||
isa<ObjCInterfaceDecl>(IIDecl) ||
- isa<TagDecl>(IIDecl)))
+ isa<TagDecl>(IIDecl) ||
+ isa<TemplateTypeParmDecl>(IIDecl)))
return IIDecl;
return 0;
}
@@ -148,7 +149,8 @@
void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
if (S->decl_empty()) return;
- assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
+ assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) &&
+ "Scope shouldn't contain decls!");
for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
I != E; ++I) {
@@ -165,7 +167,10 @@
// We only want to remove the decls from the identifier decl chains for
// local scopes, when inside a function/method.
- if (S->getFnParent() != 0)
+ // However, we *always* remove template parameters, since they are
+ // purely lexically scoped (and can never be found by qualified
+ // name lookup).
+ if (S->getFnParent() != 0 || isa<TemplateTypeParmDecl>(D))
IdResolver.RemoveDecl(D);
// Chain this decl to the containing DeclContext.
@@ -863,6 +868,15 @@
}
}
+ if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+ // Maybe we will complain about the shadowed template parameter.
+ InvalidDecl
+ = InvalidDecl || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
+ PrevDecl);
+ // Just pretend that we didn't see the previous declaration.
+ PrevDecl = 0;
+ }
+
// In C++, the previous declaration we find might be a tag type
// (class or enum). In this case, the new declaration will hide the
// tag type.
@@ -1991,7 +2005,12 @@
// among each other. Here they can only shadow globals, which is ok.
IdentifierInfo *II = D.getIdentifier();
if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) {
- if (S->isDeclScope(PrevDecl)) {
+ if (isTemplateParameterDecl(PrevDecl)) {
+ // Maybe we will complain about the shadowed template parameter.
+ DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
+ // Just pretend that we didn't see the previous declaration.
+ PrevDecl = 0;
+ } else if (S->isDeclScope(PrevDecl)) {
Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II;
// Recover by removing the name
@@ -2250,6 +2269,13 @@
PrevDecl = dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S));
}
+ if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+ // Maybe we will complain about the shadowed template parameter.
+ DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
+ // Just pretend that we didn't see the previous declaration.
+ PrevDecl = 0;
+ }
+
if (PrevDecl) {
assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) &&
"unexpected Decl type");
@@ -2386,6 +2412,13 @@
PrevDecl = dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S));
}
+ if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+ // Maybe we will complain about the shadowed template parameter.
+ DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
+ // Just pretend that we didn't see the previous declaration.
+ PrevDecl = 0;
+ }
+
if (PrevDecl) {
assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) &&
"unexpected Decl type");
@@ -2875,7 +2908,15 @@
// Verify that there isn't already something declared with this name in this
// scope.
- if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) {
+ Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S);
+ if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+ // Maybe we will complain about the shadowed template parameter.
+ DiagnoseTemplateParameterShadow(IdLoc, PrevDecl);
+ // Just pretend that we didn't see the previous declaration.
+ PrevDecl = 0;
+ }
+
+ if (PrevDecl) {
// When in C++, we may get a TagDecl with the same name; in this case the
// enum constant will 'hide' the tag.
assert((getLangOptions().CPlusPlus || !isa<TagDecl>(PrevDecl)) &&
Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Fri Dec 5 12:15:24 2008
@@ -66,6 +66,13 @@
// Check for another declaration kind with the same name.
Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
+ if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+ // Maybe we will complain about the shadowed template parameter.
+ DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
+ // Just pretend that we didn't see the previous declaration.
+ PrevDecl = 0;
+ }
+
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Diag(PrevDecl->getLocation(), diag::note_previous_definition);
@@ -742,6 +749,13 @@
for (unsigned i = 0; i != NumElts; ++i) {
// Check for another declaration kind with the same name.
Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
+ if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+ // Maybe we will complain about the shadowed template parameter.
+ DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
+ // Just pretend that we didn't see the previous declaration.
+ PrevDecl = 0;
+ }
+
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
// GCC apparently allows the following idiom:
//
Added: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=60597&view=auto
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (added)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Fri Dec 5 12:15:24 2008
@@ -0,0 +1,116 @@
+//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
+
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//+//===----------------------------------------------------------------------===/
+
+//
+// This file implements semantic analysis for C++ templates.
+//+//===----------------------------------------------------------------------===/
+
+#include "Sema.h"
+#include "clang/Parse/DeclSpec.h"
+#include "clang/Basic/LangOptions.h"
+
+using namespace clang;
+
+/// isTemplateParameterDecl - Determines whether the given declaration
+/// 'D' names a template parameter.
+bool Sema::isTemplateParameterDecl(Decl *D) {
+ return isa<TemplateTypeParmDecl>(D) || isa<NonTypeTemplateParmDecl>(D);
+}
+
+/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
+/// that the template parameter 'PrevDecl' is being shadowed by a new
+/// declaration at location Loc. Returns true to indicate that this is
+/// an error, and false otherwise.
+bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
+ assert(isTemplateParameterDecl(PrevDecl) && "Not a template parameter");
+
+ // Microsoft Visual C++ permits template parameters to be shadowed.
+ if (getLangOptions().Microsoft)
+ return false;
+
+ // C++ [temp.local]p4:
+ // A template-parameter shall not be redeclared within its
+ // scope (including nested scopes).
+ Diag(Loc, diag::err_template_param_shadow)
+ << cast<NamedDecl>(PrevDecl)->getDeclName();
+ Diag(PrevDecl->getLocation(), diag::note_template_param_here);
+ return true;
+}
+
+/// ActOnTypeParameter - Called when a C++ template type parameter
+/// (e.g., "typename T") has been parsed. Typename specifies whether
+/// the keyword "typename" was used to declare the type parameter
+/// (otherwise, "class" was used), and KeyLoc is the location of the
+/// "class" or "typename" keyword. ParamName is the name of the
+/// parameter (NULL indicates an unnamed template parameter) and
+/// ParamName is the location of the parameter name (if any).
+/// If the type parameter has a default argument, it will be added
+/// later via ActOnTypeParameterDefault.
+Sema::DeclTy *Sema::ActOnTypeParameter(Scope *S, bool Typename,
+ SourceLocation KeyLoc,
+ IdentifierInfo *ParamName,
+ SourceLocation ParamNameLoc) {
+ assert(S->isTemplateParamScope() &&
+ "Template type parameter not in template parameter scope!");
+ bool Invalid = false;
+
+ if (ParamName) {
+ Decl *PrevDecl = LookupDecl(ParamName, Decl::IDNS_Tag, S);
+ if (PrevDecl && isTemplateParameterDecl(PrevDecl))
+ Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
+ PrevDecl);
+ }
+
+ TemplateTypeParmDecl *Param
+ = TemplateTypeParmDecl::Create(Context, CurContext,
+ ParamNameLoc, ParamName, Typename);
+ if (Invalid)
+ Param->setInvalidDecl();
+
+ if (ParamName) {
+ // Add the template parameter into the current scope.
+ S->AddDecl(Param);
+ IdResolver.AddDecl(Param);
+ }
+
+ return Param;
+}
+
+/// ActOnNonTypeTemplateParameter - Called when a C++ non-type
+/// template parameter (e.g., "int Size" in "template<int Size>
+/// class Array") has been parsed. S is the current scope and D is
+/// the parsed declarator.
+Sema::DeclTy *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D) {
+ QualType T = GetTypeForDeclarator(D, S);
+
+ assert(S->isTemplateParamScope() &&
+ "Template type parameter not in template parameter scope!");
+ bool Invalid = false;
+
+ IdentifierInfo *ParamName = D.getIdentifier();
+ if (ParamName) {
+ Decl *PrevDecl = LookupDecl(ParamName, Decl::IDNS_Tag, S);
+ if (PrevDecl && isTemplateParameterDecl(PrevDecl))
+ Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
+ PrevDecl);
+ }
+
+ NonTypeTemplateParmDecl *Param
+ = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(),
+ ParamName, T);
+ if (Invalid)
+ Param->setInvalidDecl();
+
+ if (D.getIdentifier()) {
+ // Add the template parameter into the current scope.
+ S->AddDecl(Param);
+ IdResolver.AddDecl(Param);
+ }
+ return Param;
+}
Modified: cfe/trunk/test/Parser/cxx-template-decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/cxx-template-decl.cpp?rev=60597&r1=60596&r2=60597&view=diff
==============================================================================
--- cfe/trunk/test/Parser/cxx-template-decl.cpp (original)
+++ cfe/trunk/test/Parser/cxx-template-decl.cpp Fri Dec 5 12:15:24 2008
@@ -41,9 +41,33 @@
template <unsigned int N = 12u> NTP4;;
template <unsigned int = 12u> NTP5;
template <unsigned = 15u> NTP6;
-template <typename T, T Obj> NTP7; // expected-error {{parse error}}
+template <typename T, T Obj> NTP7;
// Template class declarations
template <typename T> struct A { };
template <typename T, typename U> struct B { };
+// Template parameter shadowing
+template<typename T, // expected-note{{template parameter is declared here}}
+ typename T> // expected-error{{declaration of 'T' shadows template parameter}}
+ void shadow1();
+
+template<typename T> // expected-note{{template parameter is declared here}}
+void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
+
+template<typename T> // expected-note{{template parameter is declared here}}
+class T { // expected-error{{declaration of 'T' shadows template parameter}}
+};
+
+template<int Size> // expected-note{{template parameter is declared here}}
+void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
+
+// Non-type template parameters in scope
+template<int Size>
+void f(int& i) {
+ i = Size;
+ Size = i; // expected-error{{expression is not assignable}}
+}
+
+template<typename T>
+const T& min(const T&, const T&);
More information about the cfe-commits
mailing list