Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as
Fibonacci<N - 1>::value
where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.
Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.
We can now process a simple Fibonacci computation described as a
template metaprogram.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67308 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 3755744..df1e7c3 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -1455,6 +1455,7 @@
bool RequireCompleteDeclContext(const CXXScopeSpec &SS);
DeclContext *computeDeclContext(const CXXScopeSpec &SS);
+ bool isDependentScopeSpecifier(const CXXScopeSpec &SS);
/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
/// global scope ('::').
@@ -1880,6 +1881,12 @@
ClassTemplateSpecializationDecl *ClassTemplateSpec,
bool ExplicitInstantiation);
+ CXXScopeSpec InstantiateScopeSpecifier(const NestedNameSpecifier *Components,
+ unsigned NumComponents,
+ SourceRange Range,
+ const TemplateArgument *TemplateArgs,
+ unsigned NumTemplateArgs);
+
// Simple function for cloning expressions.
template<typename T>
OwningExprResult Clone(T *E) {
diff --git a/lib/Sema/SemaCXXScopeSpec.cpp b/lib/Sema/SemaCXXScopeSpec.cpp
index 156f3a1..9e11961 100644
--- a/lib/Sema/SemaCXXScopeSpec.cpp
+++ b/lib/Sema/SemaCXXScopeSpec.cpp
@@ -29,6 +29,21 @@
return NNS.computeDeclContext(Context);
}
+bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
+ if (!SS.isSet() || SS.isInvalid())
+ return false;
+
+ NestedNameSpecifier NNS
+ = NestedNameSpecifier::getFromOpaquePtr(SS.getCurrentScopeRep());
+
+ if (Type *T = NNS.getAsType())
+ return T->isDependentType();
+
+ // FIXME: What about the injected-class-name of a class template? It
+ // is dependent, but we represent it as a declaration.
+ return false;
+}
+
/// \brief Require that the context specified by SS be complete.
///
/// If SS refers to a type, this routine checks whether the type is
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 483b79a..18d55d9 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -52,6 +52,20 @@
/// and then return NULL.
Sema::TypeTy *Sema::getTypeName(IdentifierInfo &II, SourceLocation NameLoc,
Scope *S, const CXXScopeSpec *SS) {
+ // C++ [temp.res]p3:
+ // A qualified-id that refers to a type and in which the
+ // nested-name-specifier depends on a template-parameter (14.6.2)
+ // shall be prefixed by the keyword typename to indicate that the
+ // qualified-id denotes a type, forming an
+ // elaborated-type-specifier (7.1.5.3).
+ //
+ // We therefore do not perform any name lookup up SS is a dependent
+ // scope name. FIXME: we will need to perform a special kind of
+ // lookup if the scope specifier names a member of the current
+ // instantiation.
+ if (SS && isDependentScopeSpecifier(*SS))
+ return 0;
+
NamedDecl *IIDecl = 0;
LookupResult Result = LookupParsedName(S, SS, &II, LookupOrdinaryName,
false, false);
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index b15f6ab..e43d4b8 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -613,6 +613,21 @@
// Could be enum-constant, value decl, instance variable, etc.
if (SS && SS->isInvalid())
return ExprError();
+
+ // C++ [temp.dep.expr]p3:
+ // An id-expression is type-dependent if it contains:
+ // -- a nested-name-specifier that contains a class-name that
+ // names a dependent type.
+ if (SS && isDependentScopeSpecifier(*SS)) {
+ llvm::SmallVector<NestedNameSpecifier, 16> Specs;
+ for (CXXScopeSpec::iterator Spec = SS->begin(), SpecEnd = SS->end();
+ Spec != SpecEnd; ++Spec)
+ Specs.push_back(NestedNameSpecifier::getFromOpaquePtr(*Spec));
+ return Owned(UnresolvedDeclRefExpr::Create(Context, Name, Loc,
+ SS->getRange(), &Specs[0],
+ Specs.size()));
+ }
+
LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName,
false, true, Loc);
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index b4e505e..fd39a91 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -477,7 +477,7 @@
ClassTemplateDecl *NewTemplate
= ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
DeclarationName(Name), TemplateParams,
- NewClass);
+ NewClass, PrevClassTemplate);
// Set the lexical context of these templates
NewClass->setLexicalDeclContext(CurContext);
diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp
index 7df9941..10f9926 100644
--- a/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/lib/Sema/SemaTemplateInstantiate.cpp
@@ -625,6 +625,7 @@
OwningExprResult VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
OwningExprResult VisitConditionalOperator(ConditionalOperator *E);
OwningExprResult VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
+ OwningExprResult VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *E);
OwningExprResult VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
OwningExprResult VisitImplicitCastExpr(ImplicitCastExpr *E);
@@ -897,6 +898,26 @@
}
Sema::OwningExprResult
+TemplateExprInstantiator::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *E) {
+ CXXScopeSpec SS = SemaRef.InstantiateScopeSpecifier(E->begin(), E->size(),
+ E->getQualifierRange(),
+ TemplateArgs,
+ NumTemplateArgs);
+ if (SS.isInvalid() || SS.isEmpty())
+ return SemaRef.ExprError();
+
+ // FIXME: We're passing in a NULL scope, because
+ // ActOnDeclarationNameExpr doesn't actually use the scope when we
+ // give it a non-empty scope specifier. Investigate whether it would
+ // be better to refactor ActOnDeclarationNameExpr.
+ return SemaRef.ActOnDeclarationNameExpr(/*Scope=*/0, E->getLocation(),
+ E->getDeclName(),
+ /*HasTrailingLParen=*/false,
+ &SS,
+ /*FIXME:isAddressOfOperand=*/false);
+}
+
+Sema::OwningExprResult
TemplateExprInstantiator::VisitCXXTemporaryObjectExpr(
CXXTemporaryObjectExpr *E) {
QualType T = E->getType();
@@ -1047,7 +1068,9 @@
// the best template.
ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
- if (!Template->getTemplatedDecl()->getDefinition(Context)) {
+ RecordDecl *Pattern = cast_or_null<RecordDecl>(
+ Template->getTemplatedDecl()->getDefinition(Context));
+ if (!Pattern) {
Diag(ClassTemplateSpec->getLocation(),
diag::err_template_implicit_instantiate_undefined)
<< Context.getTypeDeclType(ClassTemplateSpec);
@@ -1084,7 +1107,6 @@
// FIXME: Create the injected-class-name for the
// instantiation. Should this be a typedef or something like it?
- RecordDecl *Pattern = Template->getTemplatedDecl();
llvm::SmallVector<DeclTy *, 32> Fields;
for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
MemberEnd = Pattern->decls_end();
@@ -1118,3 +1140,34 @@
return Invalid;
}
+
+/// \brief Instantiate a sequence of nested-name-specifiers into a
+/// scope specifier.
+CXXScopeSpec
+Sema::InstantiateScopeSpecifier(const NestedNameSpecifier *Components,
+ unsigned NumComponents,
+ SourceRange Range,
+ const TemplateArgument *TemplateArgs,
+ unsigned NumTemplateArgs) {
+ CXXScopeSpec SS;
+ for (unsigned Comp = 0; Comp < NumComponents; ++Comp) {
+ if (Type *T = Components[Comp].getAsType()) {
+ QualType NewT = InstantiateType(QualType(T, 0), TemplateArgs,
+ NumTemplateArgs, Range.getBegin(),
+ DeclarationName());
+ if (NewT.isNull())
+ return SS;
+ NestedNameSpecifier NNS(NewT.getTypePtr());
+ SS.addScopeRep(NNS.getAsOpaquePtr());
+ } else {
+ DeclContext *DC = Components[Comp].getAsDeclContext();
+ // FIXME: injected-class-name might be dependent, and therefore
+ // would need instantiation.
+ NestedNameSpecifier NNS(DC);
+ SS.addScopeRep(NNS.getAsOpaquePtr());
+ }
+ }
+
+ SS.setRange(Range);
+ return SS;
+}