[cfe-commits] r67720 - in /cfe/trunk: include/clang/AST/ASTContext.h lib/Sema/SemaDecl.cpp lib/Sema/SemaTemplate.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp test/SemaTemplate/fibonacci.cpp test/SemaTemplate/injected-class-name.cpp
Douglas Gregor
dgregor at apple.com
Wed Mar 25 17:10:35 PDT 2009
Author: dgregor
Date: Wed Mar 25 19:10:35 2009
New Revision: 67720
URL: http://llvm.org/viewvc/llvm-project?rev=67720&view=rev
Log:
The injected-class-name of class templates and class template
specializations can be treated as a template. Finally, we can parse
and process the first implementation of Fibonacci I wrote!
Note that this code does not handle all of the cases where
injected-class-names can be treated as templates. In particular,
there's an ambiguity case that we should be able to handle (but
can't), e.g.,
template <class T> struct Base { };
template <class T> struct Derived : Base<int>, Base<char> {
typename Derived::Base b; // error: ambiguous
typename Derived::Base<double> d; // OK
};
Added:
cfe/trunk/test/SemaTemplate/injected-class-name.cpp
Modified:
cfe/trunk/include/clang/AST/ASTContext.h
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/lib/Sema/SemaTemplate.cpp
cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
cfe/trunk/test/SemaTemplate/fibonacci.cpp
Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=67720&r1=67719&r2=67720&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Wed Mar 25 19:10:35 2009
@@ -515,7 +515,8 @@
/// the first declaration of that tag.
TagDecl *getCanonicalDecl(TagDecl *Tag) {
QualType T = getTagDeclType(Tag);
- return cast<TagDecl>(cast<TagType>(T)->getDecl());
+ return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
+ ->getDecl());
}
/// Type Query functions. If the type is an instance of the specified class,
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=67720&r1=67719&r2=67720&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Mar 25 19:10:35 2009
@@ -3425,6 +3425,8 @@
Record->getIdentifier(), Record);
InjectedClassName->setImplicit();
InjectedClassName->setAccess(AS_public);
+ if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate())
+ InjectedClassName->setDescribedClassTemplate(Template);
PushOnScopeChains(InjectedClassName, S);
assert(InjectedClassName->isInjectedClassName() &&
"Broken injected-class-name");
Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=67720&r1=67719&r2=67720&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Wed Mar 25 19:10:35 2009
@@ -42,6 +42,28 @@
return TNK_Template_template_parm;
else
assert(false && "Unknown TemplateDecl");
+ } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(IIDecl)) {
+ // C++ [temp.local]p1:
+ // Like normal (non-template) classes, class templates have an
+ // injected-class-name (Clause 9). The injected-class-name
+ // can be used with or without a template-argument-list. When
+ // it is used without a template-argument-list, it is
+ // equivalent to the injected-class-name followed by the
+ // template-parameters of the class template enclosed in
+ // <>. When it is used with a template-argument-list, it
+ // refers to the specified class template specialization,
+ // which could be the current specialization or another
+ // specialization.
+ if (Record->isInjectedClassName()) {
+ Record = cast<CXXRecordDecl>(Context.getCanonicalDecl(Record));
+ if ((Template = Record->getDescribedClassTemplate()))
+ return TNK_Class_template;
+ else if (ClassTemplateSpecializationDecl *Spec
+ = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
+ Template = Spec->getSpecializedTemplate();
+ return TNK_Class_template;
+ }
+ }
}
// FIXME: What follows is a gross hack.
@@ -469,7 +491,7 @@
// If we had a scope specifier, we better have a previous template
// declaration!
- TagDecl *NewClass =
+ CXXRecordDecl *NewClass =
CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name,
PrevClassTemplate?
PrevClassTemplate->getTemplatedDecl() : 0);
@@ -478,7 +500,8 @@
= ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
DeclarationName(Name), TemplateParams,
NewClass, PrevClassTemplate);
-
+ NewClass->setDescribedClassTemplate(NewTemplate);
+
// Set the lexical context of these templates
NewClass->setLexicalDeclContext(CurContext);
NewTemplate->setLexicalDeclContext(CurContext);
Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=67720&r1=67719&r2=67720&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Wed Mar 25 19:10:35 2009
@@ -279,6 +279,8 @@
if (!D->isInjectedClassName())
Record->setInstantiationOfMemberClass(D);
+ else
+ Record->setDescribedClassTemplate(D->getDescribedClassTemplate());
Owner->addDecl(Record);
return Record;
Modified: cfe/trunk/test/SemaTemplate/fibonacci.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/fibonacci.cpp?rev=67720&r1=67719&r2=67720&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/fibonacci.cpp (original)
+++ cfe/trunk/test/SemaTemplate/fibonacci.cpp Wed Mar 25 19:10:35 2009
@@ -1,7 +1,5 @@
// RUN: clang-cc -fsyntax-only %s
-// FIXME: The Fibonacci/FibonacciEval dance is here to work around our
-// inability to parse injected-class-name<template-argument-list>.
template<unsigned I>
struct FibonacciEval;
@@ -50,3 +48,19 @@
int array5_2[Fibonacci2<5>::value == 5? 1 : -1];
int array10_2[Fibonacci2<10>::value == 55? 1 : -1];
+
+template<unsigned I>
+struct Fibonacci3 {
+ static const unsigned value = Fibonacci3<I-1>::value + Fibonacci3<I-2>::value;
+};
+
+template<> struct Fibonacci3<0> {
+ static const unsigned value = 0;
+};
+
+template<> struct Fibonacci3<1> {
+ static const unsigned value = 1;
+};
+
+int array5_3[Fibonacci3<5>::value == 5? 1 : -1];
+int array10_3[Fibonacci3<10>::value == 55? 1 : -1];
Added: cfe/trunk/test/SemaTemplate/injected-class-name.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/injected-class-name.cpp?rev=67720&view=auto
==============================================================================
--- cfe/trunk/test/SemaTemplate/injected-class-name.cpp (added)
+++ cfe/trunk/test/SemaTemplate/injected-class-name.cpp Wed Mar 25 19:10:35 2009
@@ -0,0 +1,17 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+template<typename T>
+struct X {
+ X<T*> *ptr;
+};
+
+X<int> x;
+
+template<>
+struct X<int***> {
+ typedef X<int***> *ptr;
+};
+
+// FIXME: EDG rejects this in their strict-conformance mode, but I
+// don't see any wording making this ill-formed.
+X<float>::X<int> xi = x;
More information about the cfe-commits
mailing list