1f4a2713aSLionel Sambuc //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
8f4a2713aSLionel Sambuc //
9f4a2713aSLionel Sambuc // This file implements semantic analysis for C++ templates.
10f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
11f4a2713aSLionel Sambuc
12f4a2713aSLionel Sambuc #include "TreeTransform.h"
13f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
14*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTContext.h"
15f4a2713aSLionel Sambuc #include "clang/AST/DeclFriend.h"
16f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
17f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
19f4a2713aSLionel Sambuc #include "clang/AST/RecursiveASTVisitor.h"
20f4a2713aSLionel Sambuc #include "clang/AST/TypeVisitor.h"
21f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h"
22f4a2713aSLionel Sambuc #include "clang/Basic/PartialDiagnostic.h"
23*0a6a1f1dSLionel Sambuc #include "clang/Basic/TargetInfo.h"
24f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
25f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
26f4a2713aSLionel Sambuc #include "clang/Sema/ParsedTemplate.h"
27f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
28f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
29f4a2713aSLionel Sambuc #include "clang/Sema/Template.h"
30f4a2713aSLionel Sambuc #include "clang/Sema/TemplateDeduction.h"
31f4a2713aSLionel Sambuc #include "llvm/ADT/SmallBitVector.h"
32f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
33f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
34f4a2713aSLionel Sambuc using namespace clang;
35f4a2713aSLionel Sambuc using namespace sema;
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc // Exported for use by Parser.
38f4a2713aSLionel Sambuc SourceRange
getTemplateParamsRange(TemplateParameterList const * const * Ps,unsigned N)39f4a2713aSLionel Sambuc clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
40f4a2713aSLionel Sambuc unsigned N) {
41f4a2713aSLionel Sambuc if (!N) return SourceRange();
42f4a2713aSLionel Sambuc return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
43f4a2713aSLionel Sambuc }
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc /// \brief Determine whether the declaration found is acceptable as the name
46f4a2713aSLionel Sambuc /// of a template and, if so, return that template declaration. Otherwise,
47f4a2713aSLionel Sambuc /// returns NULL.
isAcceptableTemplateName(ASTContext & Context,NamedDecl * Orig,bool AllowFunctionTemplates)48f4a2713aSLionel Sambuc static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
49f4a2713aSLionel Sambuc NamedDecl *Orig,
50f4a2713aSLionel Sambuc bool AllowFunctionTemplates) {
51f4a2713aSLionel Sambuc NamedDecl *D = Orig->getUnderlyingDecl();
52f4a2713aSLionel Sambuc
53f4a2713aSLionel Sambuc if (isa<TemplateDecl>(D)) {
54f4a2713aSLionel Sambuc if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D))
55*0a6a1f1dSLionel Sambuc return nullptr;
56f4a2713aSLionel Sambuc
57f4a2713aSLionel Sambuc return Orig;
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
61f4a2713aSLionel Sambuc // C++ [temp.local]p1:
62f4a2713aSLionel Sambuc // Like normal (non-template) classes, class templates have an
63f4a2713aSLionel Sambuc // injected-class-name (Clause 9). The injected-class-name
64f4a2713aSLionel Sambuc // can be used with or without a template-argument-list. When
65f4a2713aSLionel Sambuc // it is used without a template-argument-list, it is
66f4a2713aSLionel Sambuc // equivalent to the injected-class-name followed by the
67f4a2713aSLionel Sambuc // template-parameters of the class template enclosed in
68f4a2713aSLionel Sambuc // <>. When it is used with a template-argument-list, it
69f4a2713aSLionel Sambuc // refers to the specified class template specialization,
70f4a2713aSLionel Sambuc // which could be the current specialization or another
71f4a2713aSLionel Sambuc // specialization.
72f4a2713aSLionel Sambuc if (Record->isInjectedClassName()) {
73f4a2713aSLionel Sambuc Record = cast<CXXRecordDecl>(Record->getDeclContext());
74f4a2713aSLionel Sambuc if (Record->getDescribedClassTemplate())
75f4a2713aSLionel Sambuc return Record->getDescribedClassTemplate();
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc if (ClassTemplateSpecializationDecl *Spec
78f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(Record))
79f4a2713aSLionel Sambuc return Spec->getSpecializedTemplate();
80f4a2713aSLionel Sambuc }
81f4a2713aSLionel Sambuc
82*0a6a1f1dSLionel Sambuc return nullptr;
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc
85*0a6a1f1dSLionel Sambuc return nullptr;
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc
FilterAcceptableTemplateNames(LookupResult & R,bool AllowFunctionTemplates)88f4a2713aSLionel Sambuc void Sema::FilterAcceptableTemplateNames(LookupResult &R,
89f4a2713aSLionel Sambuc bool AllowFunctionTemplates) {
90f4a2713aSLionel Sambuc // The set of class templates we've already seen.
91f4a2713aSLionel Sambuc llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
92f4a2713aSLionel Sambuc LookupResult::Filter filter = R.makeFilter();
93f4a2713aSLionel Sambuc while (filter.hasNext()) {
94f4a2713aSLionel Sambuc NamedDecl *Orig = filter.next();
95f4a2713aSLionel Sambuc NamedDecl *Repl = isAcceptableTemplateName(Context, Orig,
96f4a2713aSLionel Sambuc AllowFunctionTemplates);
97f4a2713aSLionel Sambuc if (!Repl)
98f4a2713aSLionel Sambuc filter.erase();
99f4a2713aSLionel Sambuc else if (Repl != Orig) {
100f4a2713aSLionel Sambuc
101f4a2713aSLionel Sambuc // C++ [temp.local]p3:
102f4a2713aSLionel Sambuc // A lookup that finds an injected-class-name (10.2) can result in an
103f4a2713aSLionel Sambuc // ambiguity in certain cases (for example, if it is found in more than
104f4a2713aSLionel Sambuc // one base class). If all of the injected-class-names that are found
105f4a2713aSLionel Sambuc // refer to specializations of the same class template, and if the name
106f4a2713aSLionel Sambuc // is used as a template-name, the reference refers to the class
107f4a2713aSLionel Sambuc // template itself and not a specialization thereof, and is not
108f4a2713aSLionel Sambuc // ambiguous.
109f4a2713aSLionel Sambuc if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
110*0a6a1f1dSLionel Sambuc if (!ClassTemplates.insert(ClassTmpl).second) {
111f4a2713aSLionel Sambuc filter.erase();
112f4a2713aSLionel Sambuc continue;
113f4a2713aSLionel Sambuc }
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc // FIXME: we promote access to public here as a workaround to
116f4a2713aSLionel Sambuc // the fact that LookupResult doesn't let us remember that we
117f4a2713aSLionel Sambuc // found this template through a particular injected class name,
118f4a2713aSLionel Sambuc // which means we end up doing nasty things to the invariants.
119f4a2713aSLionel Sambuc // Pretending that access is public is *much* safer.
120f4a2713aSLionel Sambuc filter.replace(Repl, AS_public);
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc filter.done();
124f4a2713aSLionel Sambuc }
125f4a2713aSLionel Sambuc
hasAnyAcceptableTemplateNames(LookupResult & R,bool AllowFunctionTemplates)126f4a2713aSLionel Sambuc bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R,
127f4a2713aSLionel Sambuc bool AllowFunctionTemplates) {
128f4a2713aSLionel Sambuc for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
129f4a2713aSLionel Sambuc if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates))
130f4a2713aSLionel Sambuc return true;
131f4a2713aSLionel Sambuc
132f4a2713aSLionel Sambuc return false;
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc
isTemplateName(Scope * S,CXXScopeSpec & SS,bool hasTemplateKeyword,UnqualifiedId & Name,ParsedType ObjectTypePtr,bool EnteringContext,TemplateTy & TemplateResult,bool & MemberOfUnknownSpecialization)135f4a2713aSLionel Sambuc TemplateNameKind Sema::isTemplateName(Scope *S,
136f4a2713aSLionel Sambuc CXXScopeSpec &SS,
137f4a2713aSLionel Sambuc bool hasTemplateKeyword,
138f4a2713aSLionel Sambuc UnqualifiedId &Name,
139f4a2713aSLionel Sambuc ParsedType ObjectTypePtr,
140f4a2713aSLionel Sambuc bool EnteringContext,
141f4a2713aSLionel Sambuc TemplateTy &TemplateResult,
142f4a2713aSLionel Sambuc bool &MemberOfUnknownSpecialization) {
143f4a2713aSLionel Sambuc assert(getLangOpts().CPlusPlus && "No template names in C!");
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc DeclarationName TName;
146f4a2713aSLionel Sambuc MemberOfUnknownSpecialization = false;
147f4a2713aSLionel Sambuc
148f4a2713aSLionel Sambuc switch (Name.getKind()) {
149f4a2713aSLionel Sambuc case UnqualifiedId::IK_Identifier:
150f4a2713aSLionel Sambuc TName = DeclarationName(Name.Identifier);
151f4a2713aSLionel Sambuc break;
152f4a2713aSLionel Sambuc
153f4a2713aSLionel Sambuc case UnqualifiedId::IK_OperatorFunctionId:
154f4a2713aSLionel Sambuc TName = Context.DeclarationNames.getCXXOperatorName(
155f4a2713aSLionel Sambuc Name.OperatorFunctionId.Operator);
156f4a2713aSLionel Sambuc break;
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc case UnqualifiedId::IK_LiteralOperatorId:
159f4a2713aSLionel Sambuc TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
160f4a2713aSLionel Sambuc break;
161f4a2713aSLionel Sambuc
162f4a2713aSLionel Sambuc default:
163f4a2713aSLionel Sambuc return TNK_Non_template;
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc
166f4a2713aSLionel Sambuc QualType ObjectType = ObjectTypePtr.get();
167f4a2713aSLionel Sambuc
168f4a2713aSLionel Sambuc LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName);
169f4a2713aSLionel Sambuc LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
170f4a2713aSLionel Sambuc MemberOfUnknownSpecialization);
171f4a2713aSLionel Sambuc if (R.empty()) return TNK_Non_template;
172f4a2713aSLionel Sambuc if (R.isAmbiguous()) {
173f4a2713aSLionel Sambuc // Suppress diagnostics; we'll redo this lookup later.
174f4a2713aSLionel Sambuc R.suppressDiagnostics();
175f4a2713aSLionel Sambuc
176f4a2713aSLionel Sambuc // FIXME: we might have ambiguous templates, in which case we
177f4a2713aSLionel Sambuc // should at least parse them properly!
178f4a2713aSLionel Sambuc return TNK_Non_template;
179f4a2713aSLionel Sambuc }
180f4a2713aSLionel Sambuc
181f4a2713aSLionel Sambuc TemplateName Template;
182f4a2713aSLionel Sambuc TemplateNameKind TemplateKind;
183f4a2713aSLionel Sambuc
184f4a2713aSLionel Sambuc unsigned ResultCount = R.end() - R.begin();
185f4a2713aSLionel Sambuc if (ResultCount > 1) {
186f4a2713aSLionel Sambuc // We assume that we'll preserve the qualifier from a function
187f4a2713aSLionel Sambuc // template name in other ways.
188f4a2713aSLionel Sambuc Template = Context.getOverloadedTemplateName(R.begin(), R.end());
189f4a2713aSLionel Sambuc TemplateKind = TNK_Function_template;
190f4a2713aSLionel Sambuc
191f4a2713aSLionel Sambuc // We'll do this lookup again later.
192f4a2713aSLionel Sambuc R.suppressDiagnostics();
193f4a2713aSLionel Sambuc } else {
194f4a2713aSLionel Sambuc TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
195f4a2713aSLionel Sambuc
196f4a2713aSLionel Sambuc if (SS.isSet() && !SS.isInvalid()) {
197*0a6a1f1dSLionel Sambuc NestedNameSpecifier *Qualifier = SS.getScopeRep();
198f4a2713aSLionel Sambuc Template = Context.getQualifiedTemplateName(Qualifier,
199f4a2713aSLionel Sambuc hasTemplateKeyword, TD);
200f4a2713aSLionel Sambuc } else {
201f4a2713aSLionel Sambuc Template = TemplateName(TD);
202f4a2713aSLionel Sambuc }
203f4a2713aSLionel Sambuc
204f4a2713aSLionel Sambuc if (isa<FunctionTemplateDecl>(TD)) {
205f4a2713aSLionel Sambuc TemplateKind = TNK_Function_template;
206f4a2713aSLionel Sambuc
207f4a2713aSLionel Sambuc // We'll do this lookup again later.
208f4a2713aSLionel Sambuc R.suppressDiagnostics();
209f4a2713aSLionel Sambuc } else {
210f4a2713aSLionel Sambuc assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
211f4a2713aSLionel Sambuc isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD));
212f4a2713aSLionel Sambuc TemplateKind =
213f4a2713aSLionel Sambuc isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template;
214f4a2713aSLionel Sambuc }
215f4a2713aSLionel Sambuc }
216f4a2713aSLionel Sambuc
217f4a2713aSLionel Sambuc TemplateResult = TemplateTy::make(Template);
218f4a2713aSLionel Sambuc return TemplateKind;
219f4a2713aSLionel Sambuc }
220f4a2713aSLionel Sambuc
DiagnoseUnknownTemplateName(const IdentifierInfo & II,SourceLocation IILoc,Scope * S,const CXXScopeSpec * SS,TemplateTy & SuggestedTemplate,TemplateNameKind & SuggestedKind)221f4a2713aSLionel Sambuc bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
222f4a2713aSLionel Sambuc SourceLocation IILoc,
223f4a2713aSLionel Sambuc Scope *S,
224f4a2713aSLionel Sambuc const CXXScopeSpec *SS,
225f4a2713aSLionel Sambuc TemplateTy &SuggestedTemplate,
226f4a2713aSLionel Sambuc TemplateNameKind &SuggestedKind) {
227f4a2713aSLionel Sambuc // We can't recover unless there's a dependent scope specifier preceding the
228f4a2713aSLionel Sambuc // template name.
229f4a2713aSLionel Sambuc // FIXME: Typo correction?
230f4a2713aSLionel Sambuc if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
231f4a2713aSLionel Sambuc computeDeclContext(*SS))
232f4a2713aSLionel Sambuc return false;
233f4a2713aSLionel Sambuc
234f4a2713aSLionel Sambuc // The code is missing a 'template' keyword prior to the dependent template
235f4a2713aSLionel Sambuc // name.
236f4a2713aSLionel Sambuc NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
237f4a2713aSLionel Sambuc Diag(IILoc, diag::err_template_kw_missing)
238f4a2713aSLionel Sambuc << Qualifier << II.getName()
239f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(IILoc, "template ");
240f4a2713aSLionel Sambuc SuggestedTemplate
241f4a2713aSLionel Sambuc = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
242f4a2713aSLionel Sambuc SuggestedKind = TNK_Dependent_template_name;
243f4a2713aSLionel Sambuc return true;
244f4a2713aSLionel Sambuc }
245f4a2713aSLionel Sambuc
LookupTemplateName(LookupResult & Found,Scope * S,CXXScopeSpec & SS,QualType ObjectType,bool EnteringContext,bool & MemberOfUnknownSpecialization)246f4a2713aSLionel Sambuc void Sema::LookupTemplateName(LookupResult &Found,
247f4a2713aSLionel Sambuc Scope *S, CXXScopeSpec &SS,
248f4a2713aSLionel Sambuc QualType ObjectType,
249f4a2713aSLionel Sambuc bool EnteringContext,
250f4a2713aSLionel Sambuc bool &MemberOfUnknownSpecialization) {
251f4a2713aSLionel Sambuc // Determine where to perform name lookup
252f4a2713aSLionel Sambuc MemberOfUnknownSpecialization = false;
253*0a6a1f1dSLionel Sambuc DeclContext *LookupCtx = nullptr;
254f4a2713aSLionel Sambuc bool isDependent = false;
255f4a2713aSLionel Sambuc if (!ObjectType.isNull()) {
256f4a2713aSLionel Sambuc // This nested-name-specifier occurs in a member access expression, e.g.,
257f4a2713aSLionel Sambuc // x->B::f, and we are looking into the type of the object.
258f4a2713aSLionel Sambuc assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
259f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(ObjectType);
260f4a2713aSLionel Sambuc isDependent = ObjectType->isDependentType();
261f4a2713aSLionel Sambuc assert((isDependent || !ObjectType->isIncompleteType() ||
262f4a2713aSLionel Sambuc ObjectType->castAs<TagType>()->isBeingDefined()) &&
263f4a2713aSLionel Sambuc "Caller should have completed object type");
264f4a2713aSLionel Sambuc
265f4a2713aSLionel Sambuc // Template names cannot appear inside an Objective-C class or object type.
266f4a2713aSLionel Sambuc if (ObjectType->isObjCObjectOrInterfaceType()) {
267f4a2713aSLionel Sambuc Found.clear();
268f4a2713aSLionel Sambuc return;
269f4a2713aSLionel Sambuc }
270f4a2713aSLionel Sambuc } else if (SS.isSet()) {
271f4a2713aSLionel Sambuc // This nested-name-specifier occurs after another nested-name-specifier,
272f4a2713aSLionel Sambuc // so long into the context associated with the prior nested-name-specifier.
273f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(SS, EnteringContext);
274f4a2713aSLionel Sambuc isDependent = isDependentScopeSpecifier(SS);
275f4a2713aSLionel Sambuc
276f4a2713aSLionel Sambuc // The declaration context must be complete.
277f4a2713aSLionel Sambuc if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
278f4a2713aSLionel Sambuc return;
279f4a2713aSLionel Sambuc }
280f4a2713aSLionel Sambuc
281f4a2713aSLionel Sambuc bool ObjectTypeSearchedInScope = false;
282f4a2713aSLionel Sambuc bool AllowFunctionTemplatesInLookup = true;
283f4a2713aSLionel Sambuc if (LookupCtx) {
284f4a2713aSLionel Sambuc // Perform "qualified" name lookup into the declaration context we
285f4a2713aSLionel Sambuc // computed, which is either the type of the base of a member access
286f4a2713aSLionel Sambuc // expression or the declaration context associated with a prior
287f4a2713aSLionel Sambuc // nested-name-specifier.
288f4a2713aSLionel Sambuc LookupQualifiedName(Found, LookupCtx);
289f4a2713aSLionel Sambuc if (!ObjectType.isNull() && Found.empty()) {
290f4a2713aSLionel Sambuc // C++ [basic.lookup.classref]p1:
291f4a2713aSLionel Sambuc // In a class member access expression (5.2.5), if the . or -> token is
292f4a2713aSLionel Sambuc // immediately followed by an identifier followed by a <, the
293f4a2713aSLionel Sambuc // identifier must be looked up to determine whether the < is the
294f4a2713aSLionel Sambuc // beginning of a template argument list (14.2) or a less-than operator.
295f4a2713aSLionel Sambuc // The identifier is first looked up in the class of the object
296f4a2713aSLionel Sambuc // expression. If the identifier is not found, it is then looked up in
297f4a2713aSLionel Sambuc // the context of the entire postfix-expression and shall name a class
298f4a2713aSLionel Sambuc // or function template.
299f4a2713aSLionel Sambuc if (S) LookupName(Found, S);
300f4a2713aSLionel Sambuc ObjectTypeSearchedInScope = true;
301f4a2713aSLionel Sambuc AllowFunctionTemplatesInLookup = false;
302f4a2713aSLionel Sambuc }
303f4a2713aSLionel Sambuc } else if (isDependent && (!S || ObjectType.isNull())) {
304f4a2713aSLionel Sambuc // We cannot look into a dependent object type or nested nme
305f4a2713aSLionel Sambuc // specifier.
306f4a2713aSLionel Sambuc MemberOfUnknownSpecialization = true;
307f4a2713aSLionel Sambuc return;
308f4a2713aSLionel Sambuc } else {
309f4a2713aSLionel Sambuc // Perform unqualified name lookup in the current scope.
310f4a2713aSLionel Sambuc LookupName(Found, S);
311f4a2713aSLionel Sambuc
312f4a2713aSLionel Sambuc if (!ObjectType.isNull())
313f4a2713aSLionel Sambuc AllowFunctionTemplatesInLookup = false;
314f4a2713aSLionel Sambuc }
315f4a2713aSLionel Sambuc
316f4a2713aSLionel Sambuc if (Found.empty() && !isDependent) {
317f4a2713aSLionel Sambuc // If we did not find any names, attempt to correct any typos.
318f4a2713aSLionel Sambuc DeclarationName Name = Found.getLookupName();
319f4a2713aSLionel Sambuc Found.clear();
320f4a2713aSLionel Sambuc // Simple filter callback that, for keywords, only accepts the C++ *_cast
321*0a6a1f1dSLionel Sambuc auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>();
322*0a6a1f1dSLionel Sambuc FilterCCC->WantTypeSpecifiers = false;
323*0a6a1f1dSLionel Sambuc FilterCCC->WantExpressionKeywords = false;
324*0a6a1f1dSLionel Sambuc FilterCCC->WantRemainingKeywords = false;
325*0a6a1f1dSLionel Sambuc FilterCCC->WantCXXNamedCasts = true;
326*0a6a1f1dSLionel Sambuc if (TypoCorrection Corrected = CorrectTypo(
327*0a6a1f1dSLionel Sambuc Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS,
328*0a6a1f1dSLionel Sambuc std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) {
329f4a2713aSLionel Sambuc Found.setLookupName(Corrected.getCorrection());
330f4a2713aSLionel Sambuc if (Corrected.getCorrectionDecl())
331f4a2713aSLionel Sambuc Found.addDecl(Corrected.getCorrectionDecl());
332f4a2713aSLionel Sambuc FilterAcceptableTemplateNames(Found);
333f4a2713aSLionel Sambuc if (!Found.empty()) {
334f4a2713aSLionel Sambuc if (LookupCtx) {
335f4a2713aSLionel Sambuc std::string CorrectedStr(Corrected.getAsString(getLangOpts()));
336f4a2713aSLionel Sambuc bool DroppedSpecifier = Corrected.WillReplaceSpecifier() &&
337f4a2713aSLionel Sambuc Name.getAsString() == CorrectedStr;
338f4a2713aSLionel Sambuc diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest)
339f4a2713aSLionel Sambuc << Name << LookupCtx << DroppedSpecifier
340f4a2713aSLionel Sambuc << SS.getRange());
341f4a2713aSLionel Sambuc } else {
342f4a2713aSLionel Sambuc diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name);
343f4a2713aSLionel Sambuc }
344f4a2713aSLionel Sambuc }
345f4a2713aSLionel Sambuc } else {
346f4a2713aSLionel Sambuc Found.setLookupName(Name);
347f4a2713aSLionel Sambuc }
348f4a2713aSLionel Sambuc }
349f4a2713aSLionel Sambuc
350f4a2713aSLionel Sambuc FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup);
351f4a2713aSLionel Sambuc if (Found.empty()) {
352f4a2713aSLionel Sambuc if (isDependent)
353f4a2713aSLionel Sambuc MemberOfUnknownSpecialization = true;
354f4a2713aSLionel Sambuc return;
355f4a2713aSLionel Sambuc }
356f4a2713aSLionel Sambuc
357f4a2713aSLionel Sambuc if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope &&
358f4a2713aSLionel Sambuc !getLangOpts().CPlusPlus11) {
359f4a2713aSLionel Sambuc // C++03 [basic.lookup.classref]p1:
360f4a2713aSLionel Sambuc // [...] If the lookup in the class of the object expression finds a
361f4a2713aSLionel Sambuc // template, the name is also looked up in the context of the entire
362f4a2713aSLionel Sambuc // postfix-expression and [...]
363f4a2713aSLionel Sambuc //
364f4a2713aSLionel Sambuc // Note: C++11 does not perform this second lookup.
365f4a2713aSLionel Sambuc LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
366f4a2713aSLionel Sambuc LookupOrdinaryName);
367f4a2713aSLionel Sambuc LookupName(FoundOuter, S);
368f4a2713aSLionel Sambuc FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false);
369f4a2713aSLionel Sambuc
370f4a2713aSLionel Sambuc if (FoundOuter.empty()) {
371f4a2713aSLionel Sambuc // - if the name is not found, the name found in the class of the
372f4a2713aSLionel Sambuc // object expression is used, otherwise
373f4a2713aSLionel Sambuc } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
374f4a2713aSLionel Sambuc FoundOuter.isAmbiguous()) {
375f4a2713aSLionel Sambuc // - if the name is found in the context of the entire
376f4a2713aSLionel Sambuc // postfix-expression and does not name a class template, the name
377f4a2713aSLionel Sambuc // found in the class of the object expression is used, otherwise
378f4a2713aSLionel Sambuc FoundOuter.clear();
379f4a2713aSLionel Sambuc } else if (!Found.isSuppressingDiagnostics()) {
380f4a2713aSLionel Sambuc // - if the name found is a class template, it must refer to the same
381f4a2713aSLionel Sambuc // entity as the one found in the class of the object expression,
382f4a2713aSLionel Sambuc // otherwise the program is ill-formed.
383f4a2713aSLionel Sambuc if (!Found.isSingleResult() ||
384f4a2713aSLionel Sambuc Found.getFoundDecl()->getCanonicalDecl()
385f4a2713aSLionel Sambuc != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
386f4a2713aSLionel Sambuc Diag(Found.getNameLoc(),
387f4a2713aSLionel Sambuc diag::ext_nested_name_member_ref_lookup_ambiguous)
388f4a2713aSLionel Sambuc << Found.getLookupName()
389f4a2713aSLionel Sambuc << ObjectType;
390f4a2713aSLionel Sambuc Diag(Found.getRepresentativeDecl()->getLocation(),
391f4a2713aSLionel Sambuc diag::note_ambig_member_ref_object_type)
392f4a2713aSLionel Sambuc << ObjectType;
393f4a2713aSLionel Sambuc Diag(FoundOuter.getFoundDecl()->getLocation(),
394f4a2713aSLionel Sambuc diag::note_ambig_member_ref_scope);
395f4a2713aSLionel Sambuc
396f4a2713aSLionel Sambuc // Recover by taking the template that we found in the object
397f4a2713aSLionel Sambuc // expression's type.
398f4a2713aSLionel Sambuc }
399f4a2713aSLionel Sambuc }
400f4a2713aSLionel Sambuc }
401f4a2713aSLionel Sambuc }
402f4a2713aSLionel Sambuc
403f4a2713aSLionel Sambuc /// ActOnDependentIdExpression - Handle a dependent id-expression that
404f4a2713aSLionel Sambuc /// was just parsed. This is only possible with an explicit scope
405f4a2713aSLionel Sambuc /// specifier naming a dependent type.
406f4a2713aSLionel Sambuc ExprResult
ActOnDependentIdExpression(const CXXScopeSpec & SS,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,bool isAddressOfOperand,const TemplateArgumentListInfo * TemplateArgs)407f4a2713aSLionel Sambuc Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
408f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
409f4a2713aSLionel Sambuc const DeclarationNameInfo &NameInfo,
410f4a2713aSLionel Sambuc bool isAddressOfOperand,
411f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs) {
412f4a2713aSLionel Sambuc DeclContext *DC = getFunctionLevelDeclContext();
413f4a2713aSLionel Sambuc
414f4a2713aSLionel Sambuc if (!isAddressOfOperand &&
415f4a2713aSLionel Sambuc isa<CXXMethodDecl>(DC) &&
416f4a2713aSLionel Sambuc cast<CXXMethodDecl>(DC)->isInstance()) {
417f4a2713aSLionel Sambuc QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
418f4a2713aSLionel Sambuc
419f4a2713aSLionel Sambuc // Since the 'this' expression is synthesized, we don't need to
420f4a2713aSLionel Sambuc // perform the double-lookup check.
421*0a6a1f1dSLionel Sambuc NamedDecl *FirstQualifierInScope = nullptr;
422f4a2713aSLionel Sambuc
423*0a6a1f1dSLionel Sambuc return CXXDependentScopeMemberExpr::Create(
424*0a6a1f1dSLionel Sambuc Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true,
425*0a6a1f1dSLionel Sambuc /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc,
426*0a6a1f1dSLionel Sambuc FirstQualifierInScope, NameInfo, TemplateArgs);
427f4a2713aSLionel Sambuc }
428f4a2713aSLionel Sambuc
429f4a2713aSLionel Sambuc return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
430f4a2713aSLionel Sambuc }
431f4a2713aSLionel Sambuc
432f4a2713aSLionel Sambuc ExprResult
BuildDependentDeclRefExpr(const CXXScopeSpec & SS,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * TemplateArgs)433f4a2713aSLionel Sambuc Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
434f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
435f4a2713aSLionel Sambuc const DeclarationNameInfo &NameInfo,
436f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs) {
437*0a6a1f1dSLionel Sambuc return DependentScopeDeclRefExpr::Create(
438*0a6a1f1dSLionel Sambuc Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo,
439*0a6a1f1dSLionel Sambuc TemplateArgs);
440f4a2713aSLionel Sambuc }
441f4a2713aSLionel Sambuc
442f4a2713aSLionel Sambuc /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
443f4a2713aSLionel Sambuc /// that the template parameter 'PrevDecl' is being shadowed by a new
444f4a2713aSLionel Sambuc /// declaration at location Loc. Returns true to indicate that this is
445f4a2713aSLionel Sambuc /// an error, and false otherwise.
DiagnoseTemplateParameterShadow(SourceLocation Loc,Decl * PrevDecl)446f4a2713aSLionel Sambuc void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
447f4a2713aSLionel Sambuc assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
448f4a2713aSLionel Sambuc
449f4a2713aSLionel Sambuc // Microsoft Visual C++ permits template parameters to be shadowed.
450f4a2713aSLionel Sambuc if (getLangOpts().MicrosoftExt)
451f4a2713aSLionel Sambuc return;
452f4a2713aSLionel Sambuc
453f4a2713aSLionel Sambuc // C++ [temp.local]p4:
454f4a2713aSLionel Sambuc // A template-parameter shall not be redeclared within its
455f4a2713aSLionel Sambuc // scope (including nested scopes).
456f4a2713aSLionel Sambuc Diag(Loc, diag::err_template_param_shadow)
457f4a2713aSLionel Sambuc << cast<NamedDecl>(PrevDecl)->getDeclName();
458f4a2713aSLionel Sambuc Diag(PrevDecl->getLocation(), diag::note_template_param_here);
459f4a2713aSLionel Sambuc return;
460f4a2713aSLionel Sambuc }
461f4a2713aSLionel Sambuc
462f4a2713aSLionel Sambuc /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
463f4a2713aSLionel Sambuc /// the parameter D to reference the templated declaration and return a pointer
464f4a2713aSLionel Sambuc /// to the template declaration. Otherwise, do nothing to D and return null.
AdjustDeclIfTemplate(Decl * & D)465f4a2713aSLionel Sambuc TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
466f4a2713aSLionel Sambuc if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
467f4a2713aSLionel Sambuc D = Temp->getTemplatedDecl();
468f4a2713aSLionel Sambuc return Temp;
469f4a2713aSLionel Sambuc }
470*0a6a1f1dSLionel Sambuc return nullptr;
471f4a2713aSLionel Sambuc }
472f4a2713aSLionel Sambuc
getTemplatePackExpansion(SourceLocation EllipsisLoc) const473f4a2713aSLionel Sambuc ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
474f4a2713aSLionel Sambuc SourceLocation EllipsisLoc) const {
475f4a2713aSLionel Sambuc assert(Kind == Template &&
476f4a2713aSLionel Sambuc "Only template template arguments can be pack expansions here");
477f4a2713aSLionel Sambuc assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
478f4a2713aSLionel Sambuc "Template template argument pack expansion without packs");
479f4a2713aSLionel Sambuc ParsedTemplateArgument Result(*this);
480f4a2713aSLionel Sambuc Result.EllipsisLoc = EllipsisLoc;
481f4a2713aSLionel Sambuc return Result;
482f4a2713aSLionel Sambuc }
483f4a2713aSLionel Sambuc
translateTemplateArgument(Sema & SemaRef,const ParsedTemplateArgument & Arg)484f4a2713aSLionel Sambuc static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
485f4a2713aSLionel Sambuc const ParsedTemplateArgument &Arg) {
486f4a2713aSLionel Sambuc
487f4a2713aSLionel Sambuc switch (Arg.getKind()) {
488f4a2713aSLionel Sambuc case ParsedTemplateArgument::Type: {
489f4a2713aSLionel Sambuc TypeSourceInfo *DI;
490f4a2713aSLionel Sambuc QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
491f4a2713aSLionel Sambuc if (!DI)
492f4a2713aSLionel Sambuc DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
493f4a2713aSLionel Sambuc return TemplateArgumentLoc(TemplateArgument(T), DI);
494f4a2713aSLionel Sambuc }
495f4a2713aSLionel Sambuc
496f4a2713aSLionel Sambuc case ParsedTemplateArgument::NonType: {
497f4a2713aSLionel Sambuc Expr *E = static_cast<Expr *>(Arg.getAsExpr());
498f4a2713aSLionel Sambuc return TemplateArgumentLoc(TemplateArgument(E), E);
499f4a2713aSLionel Sambuc }
500f4a2713aSLionel Sambuc
501f4a2713aSLionel Sambuc case ParsedTemplateArgument::Template: {
502f4a2713aSLionel Sambuc TemplateName Template = Arg.getAsTemplate().get();
503f4a2713aSLionel Sambuc TemplateArgument TArg;
504f4a2713aSLionel Sambuc if (Arg.getEllipsisLoc().isValid())
505f4a2713aSLionel Sambuc TArg = TemplateArgument(Template, Optional<unsigned int>());
506f4a2713aSLionel Sambuc else
507f4a2713aSLionel Sambuc TArg = Template;
508f4a2713aSLionel Sambuc return TemplateArgumentLoc(TArg,
509f4a2713aSLionel Sambuc Arg.getScopeSpec().getWithLocInContext(
510f4a2713aSLionel Sambuc SemaRef.Context),
511f4a2713aSLionel Sambuc Arg.getLocation(),
512f4a2713aSLionel Sambuc Arg.getEllipsisLoc());
513f4a2713aSLionel Sambuc }
514f4a2713aSLionel Sambuc }
515f4a2713aSLionel Sambuc
516f4a2713aSLionel Sambuc llvm_unreachable("Unhandled parsed template argument");
517f4a2713aSLionel Sambuc }
518f4a2713aSLionel Sambuc
519f4a2713aSLionel Sambuc /// \brief Translates template arguments as provided by the parser
520f4a2713aSLionel Sambuc /// into template arguments used by semantic analysis.
translateTemplateArguments(const ASTTemplateArgsPtr & TemplateArgsIn,TemplateArgumentListInfo & TemplateArgs)521f4a2713aSLionel Sambuc void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
522f4a2713aSLionel Sambuc TemplateArgumentListInfo &TemplateArgs) {
523f4a2713aSLionel Sambuc for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
524f4a2713aSLionel Sambuc TemplateArgs.addArgument(translateTemplateArgument(*this,
525f4a2713aSLionel Sambuc TemplateArgsIn[I]));
526f4a2713aSLionel Sambuc }
527f4a2713aSLionel Sambuc
maybeDiagnoseTemplateParameterShadow(Sema & SemaRef,Scope * S,SourceLocation Loc,IdentifierInfo * Name)528f4a2713aSLionel Sambuc static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S,
529f4a2713aSLionel Sambuc SourceLocation Loc,
530f4a2713aSLionel Sambuc IdentifierInfo *Name) {
531f4a2713aSLionel Sambuc NamedDecl *PrevDecl = SemaRef.LookupSingleName(
532f4a2713aSLionel Sambuc S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration);
533f4a2713aSLionel Sambuc if (PrevDecl && PrevDecl->isTemplateParameter())
534f4a2713aSLionel Sambuc SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl);
535f4a2713aSLionel Sambuc }
536f4a2713aSLionel Sambuc
537f4a2713aSLionel Sambuc /// ActOnTypeParameter - Called when a C++ template type parameter
538f4a2713aSLionel Sambuc /// (e.g., "typename T") has been parsed. Typename specifies whether
539f4a2713aSLionel Sambuc /// the keyword "typename" was used to declare the type parameter
540f4a2713aSLionel Sambuc /// (otherwise, "class" was used), and KeyLoc is the location of the
541f4a2713aSLionel Sambuc /// "class" or "typename" keyword. ParamName is the name of the
542f4a2713aSLionel Sambuc /// parameter (NULL indicates an unnamed template parameter) and
543f4a2713aSLionel Sambuc /// ParamNameLoc is the location of the parameter name (if any).
544f4a2713aSLionel Sambuc /// If the type parameter has a default argument, it will be added
545f4a2713aSLionel Sambuc /// later via ActOnTypeParameterDefault.
ActOnTypeParameter(Scope * S,bool Typename,SourceLocation EllipsisLoc,SourceLocation KeyLoc,IdentifierInfo * ParamName,SourceLocation ParamNameLoc,unsigned Depth,unsigned Position,SourceLocation EqualLoc,ParsedType DefaultArg)546*0a6a1f1dSLionel Sambuc Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename,
547f4a2713aSLionel Sambuc SourceLocation EllipsisLoc,
548f4a2713aSLionel Sambuc SourceLocation KeyLoc,
549f4a2713aSLionel Sambuc IdentifierInfo *ParamName,
550f4a2713aSLionel Sambuc SourceLocation ParamNameLoc,
551f4a2713aSLionel Sambuc unsigned Depth, unsigned Position,
552f4a2713aSLionel Sambuc SourceLocation EqualLoc,
553f4a2713aSLionel Sambuc ParsedType DefaultArg) {
554f4a2713aSLionel Sambuc assert(S->isTemplateParamScope() &&
555f4a2713aSLionel Sambuc "Template type parameter not in template parameter scope!");
556f4a2713aSLionel Sambuc bool Invalid = false;
557f4a2713aSLionel Sambuc
558f4a2713aSLionel Sambuc SourceLocation Loc = ParamNameLoc;
559f4a2713aSLionel Sambuc if (!ParamName)
560f4a2713aSLionel Sambuc Loc = KeyLoc;
561f4a2713aSLionel Sambuc
562*0a6a1f1dSLionel Sambuc bool IsParameterPack = EllipsisLoc.isValid();
563f4a2713aSLionel Sambuc TemplateTypeParmDecl *Param
564f4a2713aSLionel Sambuc = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
565f4a2713aSLionel Sambuc KeyLoc, Loc, Depth, Position, ParamName,
566*0a6a1f1dSLionel Sambuc Typename, IsParameterPack);
567f4a2713aSLionel Sambuc Param->setAccess(AS_public);
568f4a2713aSLionel Sambuc if (Invalid)
569f4a2713aSLionel Sambuc Param->setInvalidDecl();
570f4a2713aSLionel Sambuc
571f4a2713aSLionel Sambuc if (ParamName) {
572f4a2713aSLionel Sambuc maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName);
573f4a2713aSLionel Sambuc
574f4a2713aSLionel Sambuc // Add the template parameter into the current scope.
575f4a2713aSLionel Sambuc S->AddDecl(Param);
576f4a2713aSLionel Sambuc IdResolver.AddDecl(Param);
577f4a2713aSLionel Sambuc }
578f4a2713aSLionel Sambuc
579f4a2713aSLionel Sambuc // C++0x [temp.param]p9:
580f4a2713aSLionel Sambuc // A default template-argument may be specified for any kind of
581f4a2713aSLionel Sambuc // template-parameter that is not a template parameter pack.
582*0a6a1f1dSLionel Sambuc if (DefaultArg && IsParameterPack) {
583f4a2713aSLionel Sambuc Diag(EqualLoc, diag::err_template_param_pack_default_arg);
584f4a2713aSLionel Sambuc DefaultArg = ParsedType();
585f4a2713aSLionel Sambuc }
586f4a2713aSLionel Sambuc
587f4a2713aSLionel Sambuc // Handle the default argument, if provided.
588f4a2713aSLionel Sambuc if (DefaultArg) {
589f4a2713aSLionel Sambuc TypeSourceInfo *DefaultTInfo;
590f4a2713aSLionel Sambuc GetTypeFromParser(DefaultArg, &DefaultTInfo);
591f4a2713aSLionel Sambuc
592f4a2713aSLionel Sambuc assert(DefaultTInfo && "expected source information for type");
593f4a2713aSLionel Sambuc
594f4a2713aSLionel Sambuc // Check for unexpanded parameter packs.
595f4a2713aSLionel Sambuc if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
596f4a2713aSLionel Sambuc UPPC_DefaultArgument))
597f4a2713aSLionel Sambuc return Param;
598f4a2713aSLionel Sambuc
599f4a2713aSLionel Sambuc // Check the template argument itself.
600f4a2713aSLionel Sambuc if (CheckTemplateArgument(Param, DefaultTInfo)) {
601f4a2713aSLionel Sambuc Param->setInvalidDecl();
602f4a2713aSLionel Sambuc return Param;
603f4a2713aSLionel Sambuc }
604f4a2713aSLionel Sambuc
605f4a2713aSLionel Sambuc Param->setDefaultArgument(DefaultTInfo, false);
606f4a2713aSLionel Sambuc }
607f4a2713aSLionel Sambuc
608f4a2713aSLionel Sambuc return Param;
609f4a2713aSLionel Sambuc }
610f4a2713aSLionel Sambuc
611f4a2713aSLionel Sambuc /// \brief Check that the type of a non-type template parameter is
612f4a2713aSLionel Sambuc /// well-formed.
613f4a2713aSLionel Sambuc ///
614f4a2713aSLionel Sambuc /// \returns the (possibly-promoted) parameter type if valid;
615f4a2713aSLionel Sambuc /// otherwise, produces a diagnostic and returns a NULL type.
616f4a2713aSLionel Sambuc QualType
CheckNonTypeTemplateParameterType(QualType T,SourceLocation Loc)617f4a2713aSLionel Sambuc Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
618f4a2713aSLionel Sambuc // We don't allow variably-modified types as the type of non-type template
619f4a2713aSLionel Sambuc // parameters.
620f4a2713aSLionel Sambuc if (T->isVariablyModifiedType()) {
621f4a2713aSLionel Sambuc Diag(Loc, diag::err_variably_modified_nontype_template_param)
622f4a2713aSLionel Sambuc << T;
623f4a2713aSLionel Sambuc return QualType();
624f4a2713aSLionel Sambuc }
625f4a2713aSLionel Sambuc
626f4a2713aSLionel Sambuc // C++ [temp.param]p4:
627f4a2713aSLionel Sambuc //
628f4a2713aSLionel Sambuc // A non-type template-parameter shall have one of the following
629f4a2713aSLionel Sambuc // (optionally cv-qualified) types:
630f4a2713aSLionel Sambuc //
631f4a2713aSLionel Sambuc // -- integral or enumeration type,
632f4a2713aSLionel Sambuc if (T->isIntegralOrEnumerationType() ||
633f4a2713aSLionel Sambuc // -- pointer to object or pointer to function,
634f4a2713aSLionel Sambuc T->isPointerType() ||
635f4a2713aSLionel Sambuc // -- reference to object or reference to function,
636f4a2713aSLionel Sambuc T->isReferenceType() ||
637f4a2713aSLionel Sambuc // -- pointer to member,
638f4a2713aSLionel Sambuc T->isMemberPointerType() ||
639f4a2713aSLionel Sambuc // -- std::nullptr_t.
640f4a2713aSLionel Sambuc T->isNullPtrType() ||
641f4a2713aSLionel Sambuc // If T is a dependent type, we can't do the check now, so we
642f4a2713aSLionel Sambuc // assume that it is well-formed.
643f4a2713aSLionel Sambuc T->isDependentType()) {
644f4a2713aSLionel Sambuc // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter
645f4a2713aSLionel Sambuc // are ignored when determining its type.
646f4a2713aSLionel Sambuc return T.getUnqualifiedType();
647f4a2713aSLionel Sambuc }
648f4a2713aSLionel Sambuc
649f4a2713aSLionel Sambuc // C++ [temp.param]p8:
650f4a2713aSLionel Sambuc //
651f4a2713aSLionel Sambuc // A non-type template-parameter of type "array of T" or
652f4a2713aSLionel Sambuc // "function returning T" is adjusted to be of type "pointer to
653f4a2713aSLionel Sambuc // T" or "pointer to function returning T", respectively.
654*0a6a1f1dSLionel Sambuc else if (T->isArrayType() || T->isFunctionType())
655*0a6a1f1dSLionel Sambuc return Context.getDecayedType(T);
656f4a2713aSLionel Sambuc
657f4a2713aSLionel Sambuc Diag(Loc, diag::err_template_nontype_parm_bad_type)
658f4a2713aSLionel Sambuc << T;
659f4a2713aSLionel Sambuc
660f4a2713aSLionel Sambuc return QualType();
661f4a2713aSLionel Sambuc }
662f4a2713aSLionel Sambuc
ActOnNonTypeTemplateParameter(Scope * S,Declarator & D,unsigned Depth,unsigned Position,SourceLocation EqualLoc,Expr * Default)663f4a2713aSLionel Sambuc Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
664f4a2713aSLionel Sambuc unsigned Depth,
665f4a2713aSLionel Sambuc unsigned Position,
666f4a2713aSLionel Sambuc SourceLocation EqualLoc,
667f4a2713aSLionel Sambuc Expr *Default) {
668f4a2713aSLionel Sambuc TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
669f4a2713aSLionel Sambuc QualType T = TInfo->getType();
670f4a2713aSLionel Sambuc
671f4a2713aSLionel Sambuc assert(S->isTemplateParamScope() &&
672f4a2713aSLionel Sambuc "Non-type template parameter not in template parameter scope!");
673f4a2713aSLionel Sambuc bool Invalid = false;
674f4a2713aSLionel Sambuc
675f4a2713aSLionel Sambuc T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
676f4a2713aSLionel Sambuc if (T.isNull()) {
677f4a2713aSLionel Sambuc T = Context.IntTy; // Recover with an 'int' type.
678f4a2713aSLionel Sambuc Invalid = true;
679f4a2713aSLionel Sambuc }
680f4a2713aSLionel Sambuc
681f4a2713aSLionel Sambuc IdentifierInfo *ParamName = D.getIdentifier();
682f4a2713aSLionel Sambuc bool IsParameterPack = D.hasEllipsis();
683f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *Param
684f4a2713aSLionel Sambuc = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
685f4a2713aSLionel Sambuc D.getLocStart(),
686f4a2713aSLionel Sambuc D.getIdentifierLoc(),
687f4a2713aSLionel Sambuc Depth, Position, ParamName, T,
688f4a2713aSLionel Sambuc IsParameterPack, TInfo);
689f4a2713aSLionel Sambuc Param->setAccess(AS_public);
690f4a2713aSLionel Sambuc
691f4a2713aSLionel Sambuc if (Invalid)
692f4a2713aSLionel Sambuc Param->setInvalidDecl();
693f4a2713aSLionel Sambuc
694f4a2713aSLionel Sambuc if (ParamName) {
695f4a2713aSLionel Sambuc maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(),
696f4a2713aSLionel Sambuc ParamName);
697f4a2713aSLionel Sambuc
698f4a2713aSLionel Sambuc // Add the template parameter into the current scope.
699f4a2713aSLionel Sambuc S->AddDecl(Param);
700f4a2713aSLionel Sambuc IdResolver.AddDecl(Param);
701f4a2713aSLionel Sambuc }
702f4a2713aSLionel Sambuc
703f4a2713aSLionel Sambuc // C++0x [temp.param]p9:
704f4a2713aSLionel Sambuc // A default template-argument may be specified for any kind of
705f4a2713aSLionel Sambuc // template-parameter that is not a template parameter pack.
706f4a2713aSLionel Sambuc if (Default && IsParameterPack) {
707f4a2713aSLionel Sambuc Diag(EqualLoc, diag::err_template_param_pack_default_arg);
708*0a6a1f1dSLionel Sambuc Default = nullptr;
709f4a2713aSLionel Sambuc }
710f4a2713aSLionel Sambuc
711f4a2713aSLionel Sambuc // Check the well-formedness of the default template argument, if provided.
712f4a2713aSLionel Sambuc if (Default) {
713f4a2713aSLionel Sambuc // Check for unexpanded parameter packs.
714f4a2713aSLionel Sambuc if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
715f4a2713aSLionel Sambuc return Param;
716f4a2713aSLionel Sambuc
717f4a2713aSLionel Sambuc TemplateArgument Converted;
718*0a6a1f1dSLionel Sambuc ExprResult DefaultRes =
719*0a6a1f1dSLionel Sambuc CheckTemplateArgument(Param, Param->getType(), Default, Converted);
720f4a2713aSLionel Sambuc if (DefaultRes.isInvalid()) {
721f4a2713aSLionel Sambuc Param->setInvalidDecl();
722f4a2713aSLionel Sambuc return Param;
723f4a2713aSLionel Sambuc }
724*0a6a1f1dSLionel Sambuc Default = DefaultRes.get();
725f4a2713aSLionel Sambuc
726f4a2713aSLionel Sambuc Param->setDefaultArgument(Default, false);
727f4a2713aSLionel Sambuc }
728f4a2713aSLionel Sambuc
729f4a2713aSLionel Sambuc return Param;
730f4a2713aSLionel Sambuc }
731f4a2713aSLionel Sambuc
732f4a2713aSLionel Sambuc /// ActOnTemplateTemplateParameter - Called when a C++ template template
733f4a2713aSLionel Sambuc /// parameter (e.g. T in template <template \<typename> class T> class array)
734f4a2713aSLionel Sambuc /// has been parsed. S is the current scope.
ActOnTemplateTemplateParameter(Scope * S,SourceLocation TmpLoc,TemplateParameterList * Params,SourceLocation EllipsisLoc,IdentifierInfo * Name,SourceLocation NameLoc,unsigned Depth,unsigned Position,SourceLocation EqualLoc,ParsedTemplateArgument Default)735f4a2713aSLionel Sambuc Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
736f4a2713aSLionel Sambuc SourceLocation TmpLoc,
737f4a2713aSLionel Sambuc TemplateParameterList *Params,
738f4a2713aSLionel Sambuc SourceLocation EllipsisLoc,
739f4a2713aSLionel Sambuc IdentifierInfo *Name,
740f4a2713aSLionel Sambuc SourceLocation NameLoc,
741f4a2713aSLionel Sambuc unsigned Depth,
742f4a2713aSLionel Sambuc unsigned Position,
743f4a2713aSLionel Sambuc SourceLocation EqualLoc,
744f4a2713aSLionel Sambuc ParsedTemplateArgument Default) {
745f4a2713aSLionel Sambuc assert(S->isTemplateParamScope() &&
746f4a2713aSLionel Sambuc "Template template parameter not in template parameter scope!");
747f4a2713aSLionel Sambuc
748f4a2713aSLionel Sambuc // Construct the parameter object.
749f4a2713aSLionel Sambuc bool IsParameterPack = EllipsisLoc.isValid();
750f4a2713aSLionel Sambuc TemplateTemplateParmDecl *Param =
751f4a2713aSLionel Sambuc TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
752f4a2713aSLionel Sambuc NameLoc.isInvalid()? TmpLoc : NameLoc,
753f4a2713aSLionel Sambuc Depth, Position, IsParameterPack,
754f4a2713aSLionel Sambuc Name, Params);
755f4a2713aSLionel Sambuc Param->setAccess(AS_public);
756f4a2713aSLionel Sambuc
757f4a2713aSLionel Sambuc // If the template template parameter has a name, then link the identifier
758f4a2713aSLionel Sambuc // into the scope and lookup mechanisms.
759f4a2713aSLionel Sambuc if (Name) {
760f4a2713aSLionel Sambuc maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name);
761f4a2713aSLionel Sambuc
762f4a2713aSLionel Sambuc S->AddDecl(Param);
763f4a2713aSLionel Sambuc IdResolver.AddDecl(Param);
764f4a2713aSLionel Sambuc }
765f4a2713aSLionel Sambuc
766f4a2713aSLionel Sambuc if (Params->size() == 0) {
767f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
768f4a2713aSLionel Sambuc << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
769f4a2713aSLionel Sambuc Param->setInvalidDecl();
770f4a2713aSLionel Sambuc }
771f4a2713aSLionel Sambuc
772f4a2713aSLionel Sambuc // C++0x [temp.param]p9:
773f4a2713aSLionel Sambuc // A default template-argument may be specified for any kind of
774f4a2713aSLionel Sambuc // template-parameter that is not a template parameter pack.
775f4a2713aSLionel Sambuc if (IsParameterPack && !Default.isInvalid()) {
776f4a2713aSLionel Sambuc Diag(EqualLoc, diag::err_template_param_pack_default_arg);
777f4a2713aSLionel Sambuc Default = ParsedTemplateArgument();
778f4a2713aSLionel Sambuc }
779f4a2713aSLionel Sambuc
780f4a2713aSLionel Sambuc if (!Default.isInvalid()) {
781f4a2713aSLionel Sambuc // Check only that we have a template template argument. We don't want to
782f4a2713aSLionel Sambuc // try to check well-formedness now, because our template template parameter
783f4a2713aSLionel Sambuc // might have dependent types in its template parameters, which we wouldn't
784f4a2713aSLionel Sambuc // be able to match now.
785f4a2713aSLionel Sambuc //
786f4a2713aSLionel Sambuc // If none of the template template parameter's template arguments mention
787f4a2713aSLionel Sambuc // other template parameters, we could actually perform more checking here.
788f4a2713aSLionel Sambuc // However, it isn't worth doing.
789f4a2713aSLionel Sambuc TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
790f4a2713aSLionel Sambuc if (DefaultArg.getArgument().getAsTemplate().isNull()) {
791f4a2713aSLionel Sambuc Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
792f4a2713aSLionel Sambuc << DefaultArg.getSourceRange();
793f4a2713aSLionel Sambuc return Param;
794f4a2713aSLionel Sambuc }
795f4a2713aSLionel Sambuc
796f4a2713aSLionel Sambuc // Check for unexpanded parameter packs.
797f4a2713aSLionel Sambuc if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
798f4a2713aSLionel Sambuc DefaultArg.getArgument().getAsTemplate(),
799f4a2713aSLionel Sambuc UPPC_DefaultArgument))
800f4a2713aSLionel Sambuc return Param;
801f4a2713aSLionel Sambuc
802f4a2713aSLionel Sambuc Param->setDefaultArgument(DefaultArg, false);
803f4a2713aSLionel Sambuc }
804f4a2713aSLionel Sambuc
805f4a2713aSLionel Sambuc return Param;
806f4a2713aSLionel Sambuc }
807f4a2713aSLionel Sambuc
808f4a2713aSLionel Sambuc /// ActOnTemplateParameterList - Builds a TemplateParameterList that
809f4a2713aSLionel Sambuc /// contains the template parameters in Params/NumParams.
810f4a2713aSLionel Sambuc TemplateParameterList *
ActOnTemplateParameterList(unsigned Depth,SourceLocation ExportLoc,SourceLocation TemplateLoc,SourceLocation LAngleLoc,Decl ** Params,unsigned NumParams,SourceLocation RAngleLoc)811f4a2713aSLionel Sambuc Sema::ActOnTemplateParameterList(unsigned Depth,
812f4a2713aSLionel Sambuc SourceLocation ExportLoc,
813f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
814f4a2713aSLionel Sambuc SourceLocation LAngleLoc,
815f4a2713aSLionel Sambuc Decl **Params, unsigned NumParams,
816f4a2713aSLionel Sambuc SourceLocation RAngleLoc) {
817f4a2713aSLionel Sambuc if (ExportLoc.isValid())
818f4a2713aSLionel Sambuc Diag(ExportLoc, diag::warn_template_export_unsupported);
819f4a2713aSLionel Sambuc
820f4a2713aSLionel Sambuc return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
821f4a2713aSLionel Sambuc (NamedDecl**)Params, NumParams,
822f4a2713aSLionel Sambuc RAngleLoc);
823f4a2713aSLionel Sambuc }
824f4a2713aSLionel Sambuc
SetNestedNameSpecifier(TagDecl * T,const CXXScopeSpec & SS)825f4a2713aSLionel Sambuc static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
826f4a2713aSLionel Sambuc if (SS.isSet())
827f4a2713aSLionel Sambuc T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
828f4a2713aSLionel Sambuc }
829f4a2713aSLionel Sambuc
830f4a2713aSLionel Sambuc DeclResult
CheckClassTemplate(Scope * S,unsigned TagSpec,TagUseKind TUK,SourceLocation KWLoc,CXXScopeSpec & SS,IdentifierInfo * Name,SourceLocation NameLoc,AttributeList * Attr,TemplateParameterList * TemplateParams,AccessSpecifier AS,SourceLocation ModulePrivateLoc,SourceLocation FriendLoc,unsigned NumOuterTemplateParamLists,TemplateParameterList ** OuterTemplateParamLists)831f4a2713aSLionel Sambuc Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
832f4a2713aSLionel Sambuc SourceLocation KWLoc, CXXScopeSpec &SS,
833f4a2713aSLionel Sambuc IdentifierInfo *Name, SourceLocation NameLoc,
834f4a2713aSLionel Sambuc AttributeList *Attr,
835f4a2713aSLionel Sambuc TemplateParameterList *TemplateParams,
836f4a2713aSLionel Sambuc AccessSpecifier AS, SourceLocation ModulePrivateLoc,
837*0a6a1f1dSLionel Sambuc SourceLocation FriendLoc,
838f4a2713aSLionel Sambuc unsigned NumOuterTemplateParamLists,
839f4a2713aSLionel Sambuc TemplateParameterList** OuterTemplateParamLists) {
840f4a2713aSLionel Sambuc assert(TemplateParams && TemplateParams->size() > 0 &&
841f4a2713aSLionel Sambuc "No template parameters");
842f4a2713aSLionel Sambuc assert(TUK != TUK_Reference && "Can only declare or define class templates");
843f4a2713aSLionel Sambuc bool Invalid = false;
844f4a2713aSLionel Sambuc
845f4a2713aSLionel Sambuc // Check that we can declare a template here.
846f4a2713aSLionel Sambuc if (CheckTemplateDeclScope(S, TemplateParams))
847f4a2713aSLionel Sambuc return true;
848f4a2713aSLionel Sambuc
849f4a2713aSLionel Sambuc TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
850f4a2713aSLionel Sambuc assert(Kind != TTK_Enum && "can't build template of enumerated type");
851f4a2713aSLionel Sambuc
852f4a2713aSLionel Sambuc // There is no such thing as an unnamed class template.
853f4a2713aSLionel Sambuc if (!Name) {
854f4a2713aSLionel Sambuc Diag(KWLoc, diag::err_template_unnamed_class);
855f4a2713aSLionel Sambuc return true;
856f4a2713aSLionel Sambuc }
857f4a2713aSLionel Sambuc
858f4a2713aSLionel Sambuc // Find any previous declaration with this name. For a friend with no
859f4a2713aSLionel Sambuc // scope explicitly specified, we only look for tag declarations (per
860f4a2713aSLionel Sambuc // C++11 [basic.lookup.elab]p2).
861f4a2713aSLionel Sambuc DeclContext *SemanticContext;
862f4a2713aSLionel Sambuc LookupResult Previous(*this, Name, NameLoc,
863f4a2713aSLionel Sambuc (SS.isEmpty() && TUK == TUK_Friend)
864f4a2713aSLionel Sambuc ? LookupTagName : LookupOrdinaryName,
865f4a2713aSLionel Sambuc ForRedeclaration);
866f4a2713aSLionel Sambuc if (SS.isNotEmpty() && !SS.isInvalid()) {
867f4a2713aSLionel Sambuc SemanticContext = computeDeclContext(SS, true);
868f4a2713aSLionel Sambuc if (!SemanticContext) {
869f4a2713aSLionel Sambuc // FIXME: Horrible, horrible hack! We can't currently represent this
870f4a2713aSLionel Sambuc // in the AST, and historically we have just ignored such friend
871f4a2713aSLionel Sambuc // class templates, so don't complain here.
872f4a2713aSLionel Sambuc Diag(NameLoc, TUK == TUK_Friend
873f4a2713aSLionel Sambuc ? diag::warn_template_qualified_friend_ignored
874f4a2713aSLionel Sambuc : diag::err_template_qualified_declarator_no_match)
875f4a2713aSLionel Sambuc << SS.getScopeRep() << SS.getRange();
876f4a2713aSLionel Sambuc return TUK != TUK_Friend;
877f4a2713aSLionel Sambuc }
878f4a2713aSLionel Sambuc
879f4a2713aSLionel Sambuc if (RequireCompleteDeclContext(SS, SemanticContext))
880f4a2713aSLionel Sambuc return true;
881f4a2713aSLionel Sambuc
882f4a2713aSLionel Sambuc // If we're adding a template to a dependent context, we may need to
883f4a2713aSLionel Sambuc // rebuilding some of the types used within the template parameter list,
884f4a2713aSLionel Sambuc // now that we know what the current instantiation is.
885f4a2713aSLionel Sambuc if (SemanticContext->isDependentContext()) {
886f4a2713aSLionel Sambuc ContextRAII SavedContext(*this, SemanticContext);
887f4a2713aSLionel Sambuc if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
888f4a2713aSLionel Sambuc Invalid = true;
889f4a2713aSLionel Sambuc } else if (TUK != TUK_Friend && TUK != TUK_Reference)
890f4a2713aSLionel Sambuc diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc);
891f4a2713aSLionel Sambuc
892f4a2713aSLionel Sambuc LookupQualifiedName(Previous, SemanticContext);
893f4a2713aSLionel Sambuc } else {
894f4a2713aSLionel Sambuc SemanticContext = CurContext;
895f4a2713aSLionel Sambuc LookupName(Previous, S);
896f4a2713aSLionel Sambuc }
897f4a2713aSLionel Sambuc
898f4a2713aSLionel Sambuc if (Previous.isAmbiguous())
899f4a2713aSLionel Sambuc return true;
900f4a2713aSLionel Sambuc
901*0a6a1f1dSLionel Sambuc NamedDecl *PrevDecl = nullptr;
902f4a2713aSLionel Sambuc if (Previous.begin() != Previous.end())
903f4a2713aSLionel Sambuc PrevDecl = (*Previous.begin())->getUnderlyingDecl();
904f4a2713aSLionel Sambuc
905f4a2713aSLionel Sambuc // If there is a previous declaration with the same name, check
906f4a2713aSLionel Sambuc // whether this is a valid redeclaration.
907f4a2713aSLionel Sambuc ClassTemplateDecl *PrevClassTemplate
908f4a2713aSLionel Sambuc = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
909f4a2713aSLionel Sambuc
910f4a2713aSLionel Sambuc // We may have found the injected-class-name of a class template,
911f4a2713aSLionel Sambuc // class template partial specialization, or class template specialization.
912f4a2713aSLionel Sambuc // In these cases, grab the template that is being defined or specialized.
913f4a2713aSLionel Sambuc if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
914f4a2713aSLionel Sambuc cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
915f4a2713aSLionel Sambuc PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
916f4a2713aSLionel Sambuc PrevClassTemplate
917f4a2713aSLionel Sambuc = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
918f4a2713aSLionel Sambuc if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
919f4a2713aSLionel Sambuc PrevClassTemplate
920f4a2713aSLionel Sambuc = cast<ClassTemplateSpecializationDecl>(PrevDecl)
921f4a2713aSLionel Sambuc ->getSpecializedTemplate();
922f4a2713aSLionel Sambuc }
923f4a2713aSLionel Sambuc }
924f4a2713aSLionel Sambuc
925f4a2713aSLionel Sambuc if (TUK == TUK_Friend) {
926f4a2713aSLionel Sambuc // C++ [namespace.memdef]p3:
927f4a2713aSLionel Sambuc // [...] When looking for a prior declaration of a class or a function
928f4a2713aSLionel Sambuc // declared as a friend, and when the name of the friend class or
929f4a2713aSLionel Sambuc // function is neither a qualified name nor a template-id, scopes outside
930f4a2713aSLionel Sambuc // the innermost enclosing namespace scope are not considered.
931f4a2713aSLionel Sambuc if (!SS.isSet()) {
932f4a2713aSLionel Sambuc DeclContext *OutermostContext = CurContext;
933f4a2713aSLionel Sambuc while (!OutermostContext->isFileContext())
934f4a2713aSLionel Sambuc OutermostContext = OutermostContext->getLookupParent();
935f4a2713aSLionel Sambuc
936f4a2713aSLionel Sambuc if (PrevDecl &&
937f4a2713aSLionel Sambuc (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
938f4a2713aSLionel Sambuc OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
939f4a2713aSLionel Sambuc SemanticContext = PrevDecl->getDeclContext();
940f4a2713aSLionel Sambuc } else {
941f4a2713aSLionel Sambuc // Declarations in outer scopes don't matter. However, the outermost
942f4a2713aSLionel Sambuc // context we computed is the semantic context for our new
943f4a2713aSLionel Sambuc // declaration.
944*0a6a1f1dSLionel Sambuc PrevDecl = PrevClassTemplate = nullptr;
945f4a2713aSLionel Sambuc SemanticContext = OutermostContext;
946f4a2713aSLionel Sambuc
947f4a2713aSLionel Sambuc // Check that the chosen semantic context doesn't already contain a
948f4a2713aSLionel Sambuc // declaration of this name as a non-tag type.
949f4a2713aSLionel Sambuc LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
950f4a2713aSLionel Sambuc ForRedeclaration);
951f4a2713aSLionel Sambuc DeclContext *LookupContext = SemanticContext;
952f4a2713aSLionel Sambuc while (LookupContext->isTransparentContext())
953f4a2713aSLionel Sambuc LookupContext = LookupContext->getLookupParent();
954f4a2713aSLionel Sambuc LookupQualifiedName(Previous, LookupContext);
955f4a2713aSLionel Sambuc
956f4a2713aSLionel Sambuc if (Previous.isAmbiguous())
957f4a2713aSLionel Sambuc return true;
958f4a2713aSLionel Sambuc
959f4a2713aSLionel Sambuc if (Previous.begin() != Previous.end())
960f4a2713aSLionel Sambuc PrevDecl = (*Previous.begin())->getUnderlyingDecl();
961f4a2713aSLionel Sambuc }
962f4a2713aSLionel Sambuc }
963*0a6a1f1dSLionel Sambuc } else if (PrevDecl &&
964*0a6a1f1dSLionel Sambuc !isDeclInScope(PrevDecl, SemanticContext, S, SS.isValid()))
965*0a6a1f1dSLionel Sambuc PrevDecl = PrevClassTemplate = nullptr;
966f4a2713aSLionel Sambuc
967f4a2713aSLionel Sambuc if (PrevClassTemplate) {
968f4a2713aSLionel Sambuc // Ensure that the template parameter lists are compatible. Skip this check
969f4a2713aSLionel Sambuc // for a friend in a dependent context: the template parameter list itself
970f4a2713aSLionel Sambuc // could be dependent.
971f4a2713aSLionel Sambuc if (!(TUK == TUK_Friend && CurContext->isDependentContext()) &&
972f4a2713aSLionel Sambuc !TemplateParameterListsAreEqual(TemplateParams,
973f4a2713aSLionel Sambuc PrevClassTemplate->getTemplateParameters(),
974f4a2713aSLionel Sambuc /*Complain=*/true,
975f4a2713aSLionel Sambuc TPL_TemplateMatch))
976f4a2713aSLionel Sambuc return true;
977f4a2713aSLionel Sambuc
978f4a2713aSLionel Sambuc // C++ [temp.class]p4:
979f4a2713aSLionel Sambuc // In a redeclaration, partial specialization, explicit
980f4a2713aSLionel Sambuc // specialization or explicit instantiation of a class template,
981f4a2713aSLionel Sambuc // the class-key shall agree in kind with the original class
982f4a2713aSLionel Sambuc // template declaration (7.1.5.3).
983f4a2713aSLionel Sambuc RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
984f4a2713aSLionel Sambuc if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
985f4a2713aSLionel Sambuc TUK == TUK_Definition, KWLoc, *Name)) {
986f4a2713aSLionel Sambuc Diag(KWLoc, diag::err_use_with_wrong_tag)
987f4a2713aSLionel Sambuc << Name
988f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
989f4a2713aSLionel Sambuc Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
990f4a2713aSLionel Sambuc Kind = PrevRecordDecl->getTagKind();
991f4a2713aSLionel Sambuc }
992f4a2713aSLionel Sambuc
993f4a2713aSLionel Sambuc // Check for redefinition of this class template.
994f4a2713aSLionel Sambuc if (TUK == TUK_Definition) {
995f4a2713aSLionel Sambuc if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
996f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_redefinition) << Name;
997f4a2713aSLionel Sambuc Diag(Def->getLocation(), diag::note_previous_definition);
998f4a2713aSLionel Sambuc // FIXME: Would it make sense to try to "forget" the previous
999f4a2713aSLionel Sambuc // definition, as part of error recovery?
1000f4a2713aSLionel Sambuc return true;
1001f4a2713aSLionel Sambuc }
1002f4a2713aSLionel Sambuc }
1003f4a2713aSLionel Sambuc } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
1004f4a2713aSLionel Sambuc // Maybe we will complain about the shadowed template parameter.
1005f4a2713aSLionel Sambuc DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
1006f4a2713aSLionel Sambuc // Just pretend that we didn't see the previous declaration.
1007*0a6a1f1dSLionel Sambuc PrevDecl = nullptr;
1008f4a2713aSLionel Sambuc } else if (PrevDecl) {
1009f4a2713aSLionel Sambuc // C++ [temp]p5:
1010f4a2713aSLionel Sambuc // A class template shall not have the same name as any other
1011f4a2713aSLionel Sambuc // template, class, function, object, enumeration, enumerator,
1012f4a2713aSLionel Sambuc // namespace, or type in the same scope (3.3), except as specified
1013f4a2713aSLionel Sambuc // in (14.5.4).
1014f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
1015f4a2713aSLionel Sambuc Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1016f4a2713aSLionel Sambuc return true;
1017f4a2713aSLionel Sambuc }
1018f4a2713aSLionel Sambuc
1019f4a2713aSLionel Sambuc // Check the template parameter list of this declaration, possibly
1020f4a2713aSLionel Sambuc // merging in the template parameter list from the previous class
1021f4a2713aSLionel Sambuc // template declaration. Skip this check for a friend in a dependent
1022f4a2713aSLionel Sambuc // context, because the template parameter list might be dependent.
1023f4a2713aSLionel Sambuc if (!(TUK == TUK_Friend && CurContext->isDependentContext()) &&
1024f4a2713aSLionel Sambuc CheckTemplateParameterList(
1025f4a2713aSLionel Sambuc TemplateParams,
1026*0a6a1f1dSLionel Sambuc PrevClassTemplate ? PrevClassTemplate->getTemplateParameters()
1027*0a6a1f1dSLionel Sambuc : nullptr,
1028f4a2713aSLionel Sambuc (SS.isSet() && SemanticContext && SemanticContext->isRecord() &&
1029f4a2713aSLionel Sambuc SemanticContext->isDependentContext())
1030f4a2713aSLionel Sambuc ? TPC_ClassTemplateMember
1031f4a2713aSLionel Sambuc : TUK == TUK_Friend ? TPC_FriendClassTemplate
1032f4a2713aSLionel Sambuc : TPC_ClassTemplate))
1033f4a2713aSLionel Sambuc Invalid = true;
1034f4a2713aSLionel Sambuc
1035f4a2713aSLionel Sambuc if (SS.isSet()) {
1036f4a2713aSLionel Sambuc // If the name of the template was qualified, we must be defining the
1037f4a2713aSLionel Sambuc // template out-of-line.
1038f4a2713aSLionel Sambuc if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) {
1039f4a2713aSLionel Sambuc Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match
1040f4a2713aSLionel Sambuc : diag::err_member_decl_does_not_match)
1041f4a2713aSLionel Sambuc << Name << SemanticContext << /*IsDefinition*/true << SS.getRange();
1042f4a2713aSLionel Sambuc Invalid = true;
1043f4a2713aSLionel Sambuc }
1044f4a2713aSLionel Sambuc }
1045f4a2713aSLionel Sambuc
1046f4a2713aSLionel Sambuc CXXRecordDecl *NewClass =
1047f4a2713aSLionel Sambuc CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
1048f4a2713aSLionel Sambuc PrevClassTemplate?
1049*0a6a1f1dSLionel Sambuc PrevClassTemplate->getTemplatedDecl() : nullptr,
1050f4a2713aSLionel Sambuc /*DelayTypeCreation=*/true);
1051f4a2713aSLionel Sambuc SetNestedNameSpecifier(NewClass, SS);
1052f4a2713aSLionel Sambuc if (NumOuterTemplateParamLists > 0)
1053f4a2713aSLionel Sambuc NewClass->setTemplateParameterListsInfo(Context,
1054f4a2713aSLionel Sambuc NumOuterTemplateParamLists,
1055f4a2713aSLionel Sambuc OuterTemplateParamLists);
1056f4a2713aSLionel Sambuc
1057f4a2713aSLionel Sambuc // Add alignment attributes if necessary; these attributes are checked when
1058f4a2713aSLionel Sambuc // the ASTContext lays out the structure.
1059f4a2713aSLionel Sambuc if (TUK == TUK_Definition) {
1060f4a2713aSLionel Sambuc AddAlignmentAttributesForRecord(NewClass);
1061f4a2713aSLionel Sambuc AddMsStructLayoutForRecord(NewClass);
1062f4a2713aSLionel Sambuc }
1063f4a2713aSLionel Sambuc
1064f4a2713aSLionel Sambuc ClassTemplateDecl *NewTemplate
1065f4a2713aSLionel Sambuc = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1066f4a2713aSLionel Sambuc DeclarationName(Name), TemplateParams,
1067f4a2713aSLionel Sambuc NewClass, PrevClassTemplate);
1068f4a2713aSLionel Sambuc NewClass->setDescribedClassTemplate(NewTemplate);
1069f4a2713aSLionel Sambuc
1070f4a2713aSLionel Sambuc if (ModulePrivateLoc.isValid())
1071f4a2713aSLionel Sambuc NewTemplate->setModulePrivate();
1072f4a2713aSLionel Sambuc
1073f4a2713aSLionel Sambuc // Build the type for the class template declaration now.
1074f4a2713aSLionel Sambuc QualType T = NewTemplate->getInjectedClassNameSpecialization();
1075f4a2713aSLionel Sambuc T = Context.getInjectedClassNameType(NewClass, T);
1076f4a2713aSLionel Sambuc assert(T->isDependentType() && "Class template type is not dependent?");
1077f4a2713aSLionel Sambuc (void)T;
1078f4a2713aSLionel Sambuc
1079f4a2713aSLionel Sambuc // If we are providing an explicit specialization of a member that is a
1080f4a2713aSLionel Sambuc // class template, make a note of that.
1081f4a2713aSLionel Sambuc if (PrevClassTemplate &&
1082f4a2713aSLionel Sambuc PrevClassTemplate->getInstantiatedFromMemberTemplate())
1083f4a2713aSLionel Sambuc PrevClassTemplate->setMemberSpecialization();
1084f4a2713aSLionel Sambuc
1085f4a2713aSLionel Sambuc // Set the access specifier.
1086f4a2713aSLionel Sambuc if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord())
1087f4a2713aSLionel Sambuc SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
1088f4a2713aSLionel Sambuc
1089f4a2713aSLionel Sambuc // Set the lexical context of these templates
1090f4a2713aSLionel Sambuc NewClass->setLexicalDeclContext(CurContext);
1091f4a2713aSLionel Sambuc NewTemplate->setLexicalDeclContext(CurContext);
1092f4a2713aSLionel Sambuc
1093f4a2713aSLionel Sambuc if (TUK == TUK_Definition)
1094f4a2713aSLionel Sambuc NewClass->startDefinition();
1095f4a2713aSLionel Sambuc
1096f4a2713aSLionel Sambuc if (Attr)
1097f4a2713aSLionel Sambuc ProcessDeclAttributeList(S, NewClass, Attr);
1098f4a2713aSLionel Sambuc
1099f4a2713aSLionel Sambuc if (PrevClassTemplate)
1100f4a2713aSLionel Sambuc mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl());
1101f4a2713aSLionel Sambuc
1102f4a2713aSLionel Sambuc AddPushedVisibilityAttribute(NewClass);
1103f4a2713aSLionel Sambuc
1104*0a6a1f1dSLionel Sambuc if (TUK != TUK_Friend) {
1105*0a6a1f1dSLionel Sambuc // Per C++ [basic.scope.temp]p2, skip the template parameter scopes.
1106*0a6a1f1dSLionel Sambuc Scope *Outer = S;
1107*0a6a1f1dSLionel Sambuc while ((Outer->getFlags() & Scope::TemplateParamScope) != 0)
1108*0a6a1f1dSLionel Sambuc Outer = Outer->getParent();
1109*0a6a1f1dSLionel Sambuc PushOnScopeChains(NewTemplate, Outer);
1110*0a6a1f1dSLionel Sambuc } else {
1111f4a2713aSLionel Sambuc if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
1112f4a2713aSLionel Sambuc NewTemplate->setAccess(PrevClassTemplate->getAccess());
1113f4a2713aSLionel Sambuc NewClass->setAccess(PrevClassTemplate->getAccess());
1114f4a2713aSLionel Sambuc }
1115f4a2713aSLionel Sambuc
1116f4a2713aSLionel Sambuc NewTemplate->setObjectOfFriendDecl();
1117f4a2713aSLionel Sambuc
1118f4a2713aSLionel Sambuc // Friend templates are visible in fairly strange ways.
1119f4a2713aSLionel Sambuc if (!CurContext->isDependentContext()) {
1120f4a2713aSLionel Sambuc DeclContext *DC = SemanticContext->getRedeclContext();
1121f4a2713aSLionel Sambuc DC->makeDeclVisibleInContext(NewTemplate);
1122f4a2713aSLionel Sambuc if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1123f4a2713aSLionel Sambuc PushOnScopeChains(NewTemplate, EnclosingScope,
1124f4a2713aSLionel Sambuc /* AddToContext = */ false);
1125f4a2713aSLionel Sambuc }
1126f4a2713aSLionel Sambuc
1127*0a6a1f1dSLionel Sambuc FriendDecl *Friend = FriendDecl::Create(
1128*0a6a1f1dSLionel Sambuc Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc);
1129f4a2713aSLionel Sambuc Friend->setAccess(AS_public);
1130f4a2713aSLionel Sambuc CurContext->addDecl(Friend);
1131f4a2713aSLionel Sambuc }
1132f4a2713aSLionel Sambuc
1133f4a2713aSLionel Sambuc if (Invalid) {
1134f4a2713aSLionel Sambuc NewTemplate->setInvalidDecl();
1135f4a2713aSLionel Sambuc NewClass->setInvalidDecl();
1136f4a2713aSLionel Sambuc }
1137f4a2713aSLionel Sambuc
1138f4a2713aSLionel Sambuc ActOnDocumentableDecl(NewTemplate);
1139f4a2713aSLionel Sambuc
1140f4a2713aSLionel Sambuc return NewTemplate;
1141f4a2713aSLionel Sambuc }
1142f4a2713aSLionel Sambuc
1143f4a2713aSLionel Sambuc /// \brief Diagnose the presence of a default template argument on a
1144f4a2713aSLionel Sambuc /// template parameter, which is ill-formed in certain contexts.
1145f4a2713aSLionel Sambuc ///
1146f4a2713aSLionel Sambuc /// \returns true if the default template argument should be dropped.
DiagnoseDefaultTemplateArgument(Sema & S,Sema::TemplateParamListContext TPC,SourceLocation ParamLoc,SourceRange DefArgRange)1147f4a2713aSLionel Sambuc static bool DiagnoseDefaultTemplateArgument(Sema &S,
1148f4a2713aSLionel Sambuc Sema::TemplateParamListContext TPC,
1149f4a2713aSLionel Sambuc SourceLocation ParamLoc,
1150f4a2713aSLionel Sambuc SourceRange DefArgRange) {
1151f4a2713aSLionel Sambuc switch (TPC) {
1152f4a2713aSLionel Sambuc case Sema::TPC_ClassTemplate:
1153f4a2713aSLionel Sambuc case Sema::TPC_VarTemplate:
1154f4a2713aSLionel Sambuc case Sema::TPC_TypeAliasTemplate:
1155f4a2713aSLionel Sambuc return false;
1156f4a2713aSLionel Sambuc
1157f4a2713aSLionel Sambuc case Sema::TPC_FunctionTemplate:
1158f4a2713aSLionel Sambuc case Sema::TPC_FriendFunctionTemplateDefinition:
1159f4a2713aSLionel Sambuc // C++ [temp.param]p9:
1160f4a2713aSLionel Sambuc // A default template-argument shall not be specified in a
1161f4a2713aSLionel Sambuc // function template declaration or a function template
1162f4a2713aSLionel Sambuc // definition [...]
1163f4a2713aSLionel Sambuc // If a friend function template declaration specifies a default
1164f4a2713aSLionel Sambuc // template-argument, that declaration shall be a definition and shall be
1165f4a2713aSLionel Sambuc // the only declaration of the function template in the translation unit.
1166f4a2713aSLionel Sambuc // (C++98/03 doesn't have this wording; see DR226).
1167f4a2713aSLionel Sambuc S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ?
1168f4a2713aSLionel Sambuc diag::warn_cxx98_compat_template_parameter_default_in_function_template
1169f4a2713aSLionel Sambuc : diag::ext_template_parameter_default_in_function_template)
1170f4a2713aSLionel Sambuc << DefArgRange;
1171f4a2713aSLionel Sambuc return false;
1172f4a2713aSLionel Sambuc
1173f4a2713aSLionel Sambuc case Sema::TPC_ClassTemplateMember:
1174f4a2713aSLionel Sambuc // C++0x [temp.param]p9:
1175f4a2713aSLionel Sambuc // A default template-argument shall not be specified in the
1176f4a2713aSLionel Sambuc // template-parameter-lists of the definition of a member of a
1177f4a2713aSLionel Sambuc // class template that appears outside of the member's class.
1178f4a2713aSLionel Sambuc S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1179f4a2713aSLionel Sambuc << DefArgRange;
1180f4a2713aSLionel Sambuc return true;
1181f4a2713aSLionel Sambuc
1182f4a2713aSLionel Sambuc case Sema::TPC_FriendClassTemplate:
1183f4a2713aSLionel Sambuc case Sema::TPC_FriendFunctionTemplate:
1184f4a2713aSLionel Sambuc // C++ [temp.param]p9:
1185f4a2713aSLionel Sambuc // A default template-argument shall not be specified in a
1186f4a2713aSLionel Sambuc // friend template declaration.
1187f4a2713aSLionel Sambuc S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1188f4a2713aSLionel Sambuc << DefArgRange;
1189f4a2713aSLionel Sambuc return true;
1190f4a2713aSLionel Sambuc
1191f4a2713aSLionel Sambuc // FIXME: C++0x [temp.param]p9 allows default template-arguments
1192f4a2713aSLionel Sambuc // for friend function templates if there is only a single
1193f4a2713aSLionel Sambuc // declaration (and it is a definition). Strange!
1194f4a2713aSLionel Sambuc }
1195f4a2713aSLionel Sambuc
1196f4a2713aSLionel Sambuc llvm_unreachable("Invalid TemplateParamListContext!");
1197f4a2713aSLionel Sambuc }
1198f4a2713aSLionel Sambuc
1199f4a2713aSLionel Sambuc /// \brief Check for unexpanded parameter packs within the template parameters
1200f4a2713aSLionel Sambuc /// of a template template parameter, recursively.
DiagnoseUnexpandedParameterPacks(Sema & S,TemplateTemplateParmDecl * TTP)1201f4a2713aSLionel Sambuc static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1202f4a2713aSLionel Sambuc TemplateTemplateParmDecl *TTP) {
1203f4a2713aSLionel Sambuc // A template template parameter which is a parameter pack is also a pack
1204f4a2713aSLionel Sambuc // expansion.
1205f4a2713aSLionel Sambuc if (TTP->isParameterPack())
1206f4a2713aSLionel Sambuc return false;
1207f4a2713aSLionel Sambuc
1208f4a2713aSLionel Sambuc TemplateParameterList *Params = TTP->getTemplateParameters();
1209f4a2713aSLionel Sambuc for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1210f4a2713aSLionel Sambuc NamedDecl *P = Params->getParam(I);
1211f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
1212f4a2713aSLionel Sambuc if (!NTTP->isParameterPack() &&
1213f4a2713aSLionel Sambuc S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
1214f4a2713aSLionel Sambuc NTTP->getTypeSourceInfo(),
1215f4a2713aSLionel Sambuc Sema::UPPC_NonTypeTemplateParameterType))
1216f4a2713aSLionel Sambuc return true;
1217f4a2713aSLionel Sambuc
1218f4a2713aSLionel Sambuc continue;
1219f4a2713aSLionel Sambuc }
1220f4a2713aSLionel Sambuc
1221f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *InnerTTP
1222f4a2713aSLionel Sambuc = dyn_cast<TemplateTemplateParmDecl>(P))
1223f4a2713aSLionel Sambuc if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1224f4a2713aSLionel Sambuc return true;
1225f4a2713aSLionel Sambuc }
1226f4a2713aSLionel Sambuc
1227f4a2713aSLionel Sambuc return false;
1228f4a2713aSLionel Sambuc }
1229f4a2713aSLionel Sambuc
1230f4a2713aSLionel Sambuc /// \brief Checks the validity of a template parameter list, possibly
1231f4a2713aSLionel Sambuc /// considering the template parameter list from a previous
1232f4a2713aSLionel Sambuc /// declaration.
1233f4a2713aSLionel Sambuc ///
1234f4a2713aSLionel Sambuc /// If an "old" template parameter list is provided, it must be
1235f4a2713aSLionel Sambuc /// equivalent (per TemplateParameterListsAreEqual) to the "new"
1236f4a2713aSLionel Sambuc /// template parameter list.
1237f4a2713aSLionel Sambuc ///
1238f4a2713aSLionel Sambuc /// \param NewParams Template parameter list for a new template
1239f4a2713aSLionel Sambuc /// declaration. This template parameter list will be updated with any
1240f4a2713aSLionel Sambuc /// default arguments that are carried through from the previous
1241f4a2713aSLionel Sambuc /// template parameter list.
1242f4a2713aSLionel Sambuc ///
1243f4a2713aSLionel Sambuc /// \param OldParams If provided, template parameter list from a
1244f4a2713aSLionel Sambuc /// previous declaration of the same template. Default template
1245f4a2713aSLionel Sambuc /// arguments will be merged from the old template parameter list to
1246f4a2713aSLionel Sambuc /// the new template parameter list.
1247f4a2713aSLionel Sambuc ///
1248f4a2713aSLionel Sambuc /// \param TPC Describes the context in which we are checking the given
1249f4a2713aSLionel Sambuc /// template parameter list.
1250f4a2713aSLionel Sambuc ///
1251f4a2713aSLionel Sambuc /// \returns true if an error occurred, false otherwise.
CheckTemplateParameterList(TemplateParameterList * NewParams,TemplateParameterList * OldParams,TemplateParamListContext TPC)1252f4a2713aSLionel Sambuc bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
1253f4a2713aSLionel Sambuc TemplateParameterList *OldParams,
1254f4a2713aSLionel Sambuc TemplateParamListContext TPC) {
1255f4a2713aSLionel Sambuc bool Invalid = false;
1256f4a2713aSLionel Sambuc
1257f4a2713aSLionel Sambuc // C++ [temp.param]p10:
1258f4a2713aSLionel Sambuc // The set of default template-arguments available for use with a
1259f4a2713aSLionel Sambuc // template declaration or definition is obtained by merging the
1260f4a2713aSLionel Sambuc // default arguments from the definition (if in scope) and all
1261f4a2713aSLionel Sambuc // declarations in scope in the same way default function
1262f4a2713aSLionel Sambuc // arguments are (8.3.6).
1263f4a2713aSLionel Sambuc bool SawDefaultArgument = false;
1264f4a2713aSLionel Sambuc SourceLocation PreviousDefaultArgLoc;
1265f4a2713aSLionel Sambuc
1266f4a2713aSLionel Sambuc // Dummy initialization to avoid warnings.
1267f4a2713aSLionel Sambuc TemplateParameterList::iterator OldParam = NewParams->end();
1268f4a2713aSLionel Sambuc if (OldParams)
1269f4a2713aSLionel Sambuc OldParam = OldParams->begin();
1270f4a2713aSLionel Sambuc
1271f4a2713aSLionel Sambuc bool RemoveDefaultArguments = false;
1272f4a2713aSLionel Sambuc for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1273f4a2713aSLionel Sambuc NewParamEnd = NewParams->end();
1274f4a2713aSLionel Sambuc NewParam != NewParamEnd; ++NewParam) {
1275f4a2713aSLionel Sambuc // Variables used to diagnose redundant default arguments
1276f4a2713aSLionel Sambuc bool RedundantDefaultArg = false;
1277f4a2713aSLionel Sambuc SourceLocation OldDefaultLoc;
1278f4a2713aSLionel Sambuc SourceLocation NewDefaultLoc;
1279f4a2713aSLionel Sambuc
1280f4a2713aSLionel Sambuc // Variable used to diagnose missing default arguments
1281f4a2713aSLionel Sambuc bool MissingDefaultArg = false;
1282f4a2713aSLionel Sambuc
1283f4a2713aSLionel Sambuc // Variable used to diagnose non-final parameter packs
1284f4a2713aSLionel Sambuc bool SawParameterPack = false;
1285f4a2713aSLionel Sambuc
1286f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *NewTypeParm
1287f4a2713aSLionel Sambuc = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
1288f4a2713aSLionel Sambuc // Check the presence of a default argument here.
1289f4a2713aSLionel Sambuc if (NewTypeParm->hasDefaultArgument() &&
1290f4a2713aSLionel Sambuc DiagnoseDefaultTemplateArgument(*this, TPC,
1291f4a2713aSLionel Sambuc NewTypeParm->getLocation(),
1292f4a2713aSLionel Sambuc NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
1293f4a2713aSLionel Sambuc .getSourceRange()))
1294f4a2713aSLionel Sambuc NewTypeParm->removeDefaultArgument();
1295f4a2713aSLionel Sambuc
1296f4a2713aSLionel Sambuc // Merge default arguments for template type parameters.
1297f4a2713aSLionel Sambuc TemplateTypeParmDecl *OldTypeParm
1298*0a6a1f1dSLionel Sambuc = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr;
1299f4a2713aSLionel Sambuc
1300f4a2713aSLionel Sambuc if (NewTypeParm->isParameterPack()) {
1301f4a2713aSLionel Sambuc assert(!NewTypeParm->hasDefaultArgument() &&
1302f4a2713aSLionel Sambuc "Parameter packs can't have a default argument!");
1303f4a2713aSLionel Sambuc SawParameterPack = true;
1304f4a2713aSLionel Sambuc } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
1305f4a2713aSLionel Sambuc NewTypeParm->hasDefaultArgument()) {
1306f4a2713aSLionel Sambuc OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1307f4a2713aSLionel Sambuc NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1308f4a2713aSLionel Sambuc SawDefaultArgument = true;
1309f4a2713aSLionel Sambuc RedundantDefaultArg = true;
1310f4a2713aSLionel Sambuc PreviousDefaultArgLoc = NewDefaultLoc;
1311f4a2713aSLionel Sambuc } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1312f4a2713aSLionel Sambuc // Merge the default argument from the old declaration to the
1313f4a2713aSLionel Sambuc // new declaration.
1314f4a2713aSLionel Sambuc NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
1315f4a2713aSLionel Sambuc true);
1316f4a2713aSLionel Sambuc PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1317f4a2713aSLionel Sambuc } else if (NewTypeParm->hasDefaultArgument()) {
1318f4a2713aSLionel Sambuc SawDefaultArgument = true;
1319f4a2713aSLionel Sambuc PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1320f4a2713aSLionel Sambuc } else if (SawDefaultArgument)
1321f4a2713aSLionel Sambuc MissingDefaultArg = true;
1322f4a2713aSLionel Sambuc } else if (NonTypeTemplateParmDecl *NewNonTypeParm
1323f4a2713aSLionel Sambuc = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
1324f4a2713aSLionel Sambuc // Check for unexpanded parameter packs.
1325f4a2713aSLionel Sambuc if (!NewNonTypeParm->isParameterPack() &&
1326f4a2713aSLionel Sambuc DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
1327f4a2713aSLionel Sambuc NewNonTypeParm->getTypeSourceInfo(),
1328f4a2713aSLionel Sambuc UPPC_NonTypeTemplateParameterType)) {
1329f4a2713aSLionel Sambuc Invalid = true;
1330f4a2713aSLionel Sambuc continue;
1331f4a2713aSLionel Sambuc }
1332f4a2713aSLionel Sambuc
1333f4a2713aSLionel Sambuc // Check the presence of a default argument here.
1334f4a2713aSLionel Sambuc if (NewNonTypeParm->hasDefaultArgument() &&
1335f4a2713aSLionel Sambuc DiagnoseDefaultTemplateArgument(*this, TPC,
1336f4a2713aSLionel Sambuc NewNonTypeParm->getLocation(),
1337f4a2713aSLionel Sambuc NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
1338f4a2713aSLionel Sambuc NewNonTypeParm->removeDefaultArgument();
1339f4a2713aSLionel Sambuc }
1340f4a2713aSLionel Sambuc
1341f4a2713aSLionel Sambuc // Merge default arguments for non-type template parameters
1342f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *OldNonTypeParm
1343*0a6a1f1dSLionel Sambuc = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr;
1344f4a2713aSLionel Sambuc if (NewNonTypeParm->isParameterPack()) {
1345f4a2713aSLionel Sambuc assert(!NewNonTypeParm->hasDefaultArgument() &&
1346f4a2713aSLionel Sambuc "Parameter packs can't have a default argument!");
1347f4a2713aSLionel Sambuc if (!NewNonTypeParm->isPackExpansion())
1348f4a2713aSLionel Sambuc SawParameterPack = true;
1349f4a2713aSLionel Sambuc } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
1350f4a2713aSLionel Sambuc NewNonTypeParm->hasDefaultArgument()) {
1351f4a2713aSLionel Sambuc OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1352f4a2713aSLionel Sambuc NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1353f4a2713aSLionel Sambuc SawDefaultArgument = true;
1354f4a2713aSLionel Sambuc RedundantDefaultArg = true;
1355f4a2713aSLionel Sambuc PreviousDefaultArgLoc = NewDefaultLoc;
1356f4a2713aSLionel Sambuc } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1357f4a2713aSLionel Sambuc // Merge the default argument from the old declaration to the
1358f4a2713aSLionel Sambuc // new declaration.
1359f4a2713aSLionel Sambuc // FIXME: We need to create a new kind of "default argument"
1360f4a2713aSLionel Sambuc // expression that points to a previous non-type template
1361f4a2713aSLionel Sambuc // parameter.
1362f4a2713aSLionel Sambuc NewNonTypeParm->setDefaultArgument(
1363f4a2713aSLionel Sambuc OldNonTypeParm->getDefaultArgument(),
1364f4a2713aSLionel Sambuc /*Inherited=*/ true);
1365f4a2713aSLionel Sambuc PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1366f4a2713aSLionel Sambuc } else if (NewNonTypeParm->hasDefaultArgument()) {
1367f4a2713aSLionel Sambuc SawDefaultArgument = true;
1368f4a2713aSLionel Sambuc PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1369f4a2713aSLionel Sambuc } else if (SawDefaultArgument)
1370f4a2713aSLionel Sambuc MissingDefaultArg = true;
1371f4a2713aSLionel Sambuc } else {
1372f4a2713aSLionel Sambuc TemplateTemplateParmDecl *NewTemplateParm
1373f4a2713aSLionel Sambuc = cast<TemplateTemplateParmDecl>(*NewParam);
1374f4a2713aSLionel Sambuc
1375f4a2713aSLionel Sambuc // Check for unexpanded parameter packs, recursively.
1376f4a2713aSLionel Sambuc if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1377f4a2713aSLionel Sambuc Invalid = true;
1378f4a2713aSLionel Sambuc continue;
1379f4a2713aSLionel Sambuc }
1380f4a2713aSLionel Sambuc
1381f4a2713aSLionel Sambuc // Check the presence of a default argument here.
1382f4a2713aSLionel Sambuc if (NewTemplateParm->hasDefaultArgument() &&
1383f4a2713aSLionel Sambuc DiagnoseDefaultTemplateArgument(*this, TPC,
1384f4a2713aSLionel Sambuc NewTemplateParm->getLocation(),
1385f4a2713aSLionel Sambuc NewTemplateParm->getDefaultArgument().getSourceRange()))
1386f4a2713aSLionel Sambuc NewTemplateParm->removeDefaultArgument();
1387f4a2713aSLionel Sambuc
1388f4a2713aSLionel Sambuc // Merge default arguments for template template parameters
1389f4a2713aSLionel Sambuc TemplateTemplateParmDecl *OldTemplateParm
1390*0a6a1f1dSLionel Sambuc = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr;
1391f4a2713aSLionel Sambuc if (NewTemplateParm->isParameterPack()) {
1392f4a2713aSLionel Sambuc assert(!NewTemplateParm->hasDefaultArgument() &&
1393f4a2713aSLionel Sambuc "Parameter packs can't have a default argument!");
1394f4a2713aSLionel Sambuc if (!NewTemplateParm->isPackExpansion())
1395f4a2713aSLionel Sambuc SawParameterPack = true;
1396f4a2713aSLionel Sambuc } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
1397f4a2713aSLionel Sambuc NewTemplateParm->hasDefaultArgument()) {
1398f4a2713aSLionel Sambuc OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1399f4a2713aSLionel Sambuc NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
1400f4a2713aSLionel Sambuc SawDefaultArgument = true;
1401f4a2713aSLionel Sambuc RedundantDefaultArg = true;
1402f4a2713aSLionel Sambuc PreviousDefaultArgLoc = NewDefaultLoc;
1403f4a2713aSLionel Sambuc } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1404f4a2713aSLionel Sambuc // Merge the default argument from the old declaration to the
1405f4a2713aSLionel Sambuc // new declaration.
1406f4a2713aSLionel Sambuc // FIXME: We need to create a new kind of "default argument" expression
1407f4a2713aSLionel Sambuc // that points to a previous template template parameter.
1408f4a2713aSLionel Sambuc NewTemplateParm->setDefaultArgument(
1409f4a2713aSLionel Sambuc OldTemplateParm->getDefaultArgument(),
1410f4a2713aSLionel Sambuc /*Inherited=*/ true);
1411f4a2713aSLionel Sambuc PreviousDefaultArgLoc
1412f4a2713aSLionel Sambuc = OldTemplateParm->getDefaultArgument().getLocation();
1413f4a2713aSLionel Sambuc } else if (NewTemplateParm->hasDefaultArgument()) {
1414f4a2713aSLionel Sambuc SawDefaultArgument = true;
1415f4a2713aSLionel Sambuc PreviousDefaultArgLoc
1416f4a2713aSLionel Sambuc = NewTemplateParm->getDefaultArgument().getLocation();
1417f4a2713aSLionel Sambuc } else if (SawDefaultArgument)
1418f4a2713aSLionel Sambuc MissingDefaultArg = true;
1419f4a2713aSLionel Sambuc }
1420f4a2713aSLionel Sambuc
1421f4a2713aSLionel Sambuc // C++11 [temp.param]p11:
1422f4a2713aSLionel Sambuc // If a template parameter of a primary class template or alias template
1423f4a2713aSLionel Sambuc // is a template parameter pack, it shall be the last template parameter.
1424f4a2713aSLionel Sambuc if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1425f4a2713aSLionel Sambuc (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate ||
1426f4a2713aSLionel Sambuc TPC == TPC_TypeAliasTemplate)) {
1427f4a2713aSLionel Sambuc Diag((*NewParam)->getLocation(),
1428f4a2713aSLionel Sambuc diag::err_template_param_pack_must_be_last_template_parameter);
1429f4a2713aSLionel Sambuc Invalid = true;
1430f4a2713aSLionel Sambuc }
1431f4a2713aSLionel Sambuc
1432f4a2713aSLionel Sambuc if (RedundantDefaultArg) {
1433f4a2713aSLionel Sambuc // C++ [temp.param]p12:
1434f4a2713aSLionel Sambuc // A template-parameter shall not be given default arguments
1435f4a2713aSLionel Sambuc // by two different declarations in the same scope.
1436f4a2713aSLionel Sambuc Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1437f4a2713aSLionel Sambuc Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1438f4a2713aSLionel Sambuc Invalid = true;
1439f4a2713aSLionel Sambuc } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
1440f4a2713aSLionel Sambuc // C++ [temp.param]p11:
1441f4a2713aSLionel Sambuc // If a template-parameter of a class template has a default
1442f4a2713aSLionel Sambuc // template-argument, each subsequent template-parameter shall either
1443f4a2713aSLionel Sambuc // have a default template-argument supplied or be a template parameter
1444f4a2713aSLionel Sambuc // pack.
1445f4a2713aSLionel Sambuc Diag((*NewParam)->getLocation(),
1446f4a2713aSLionel Sambuc diag::err_template_param_default_arg_missing);
1447f4a2713aSLionel Sambuc Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1448f4a2713aSLionel Sambuc Invalid = true;
1449f4a2713aSLionel Sambuc RemoveDefaultArguments = true;
1450f4a2713aSLionel Sambuc }
1451f4a2713aSLionel Sambuc
1452f4a2713aSLionel Sambuc // If we have an old template parameter list that we're merging
1453f4a2713aSLionel Sambuc // in, move on to the next parameter.
1454f4a2713aSLionel Sambuc if (OldParams)
1455f4a2713aSLionel Sambuc ++OldParam;
1456f4a2713aSLionel Sambuc }
1457f4a2713aSLionel Sambuc
1458f4a2713aSLionel Sambuc // We were missing some default arguments at the end of the list, so remove
1459f4a2713aSLionel Sambuc // all of the default arguments.
1460f4a2713aSLionel Sambuc if (RemoveDefaultArguments) {
1461f4a2713aSLionel Sambuc for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1462f4a2713aSLionel Sambuc NewParamEnd = NewParams->end();
1463f4a2713aSLionel Sambuc NewParam != NewParamEnd; ++NewParam) {
1464f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1465f4a2713aSLionel Sambuc TTP->removeDefaultArgument();
1466f4a2713aSLionel Sambuc else if (NonTypeTemplateParmDecl *NTTP
1467f4a2713aSLionel Sambuc = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1468f4a2713aSLionel Sambuc NTTP->removeDefaultArgument();
1469f4a2713aSLionel Sambuc else
1470f4a2713aSLionel Sambuc cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1471f4a2713aSLionel Sambuc }
1472f4a2713aSLionel Sambuc }
1473f4a2713aSLionel Sambuc
1474f4a2713aSLionel Sambuc return Invalid;
1475f4a2713aSLionel Sambuc }
1476f4a2713aSLionel Sambuc
1477f4a2713aSLionel Sambuc namespace {
1478f4a2713aSLionel Sambuc
1479f4a2713aSLionel Sambuc /// A class which looks for a use of a certain level of template
1480f4a2713aSLionel Sambuc /// parameter.
1481f4a2713aSLionel Sambuc struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1482f4a2713aSLionel Sambuc typedef RecursiveASTVisitor<DependencyChecker> super;
1483f4a2713aSLionel Sambuc
1484f4a2713aSLionel Sambuc unsigned Depth;
1485f4a2713aSLionel Sambuc bool Match;
1486*0a6a1f1dSLionel Sambuc SourceLocation MatchLoc;
1487*0a6a1f1dSLionel Sambuc
DependencyChecker__anon572b50840111::DependencyChecker1488*0a6a1f1dSLionel Sambuc DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {}
1489f4a2713aSLionel Sambuc
DependencyChecker__anon572b50840111::DependencyChecker1490f4a2713aSLionel Sambuc DependencyChecker(TemplateParameterList *Params) : Match(false) {
1491f4a2713aSLionel Sambuc NamedDecl *ND = Params->getParam(0);
1492f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1493f4a2713aSLionel Sambuc Depth = PD->getDepth();
1494f4a2713aSLionel Sambuc } else if (NonTypeTemplateParmDecl *PD =
1495f4a2713aSLionel Sambuc dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1496f4a2713aSLionel Sambuc Depth = PD->getDepth();
1497f4a2713aSLionel Sambuc } else {
1498f4a2713aSLionel Sambuc Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1499f4a2713aSLionel Sambuc }
1500f4a2713aSLionel Sambuc }
1501f4a2713aSLionel Sambuc
Matches__anon572b50840111::DependencyChecker1502*0a6a1f1dSLionel Sambuc bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) {
1503f4a2713aSLionel Sambuc if (ParmDepth >= Depth) {
1504f4a2713aSLionel Sambuc Match = true;
1505*0a6a1f1dSLionel Sambuc MatchLoc = Loc;
1506f4a2713aSLionel Sambuc return true;
1507f4a2713aSLionel Sambuc }
1508f4a2713aSLionel Sambuc return false;
1509f4a2713aSLionel Sambuc }
1510f4a2713aSLionel Sambuc
VisitTemplateTypeParmTypeLoc__anon572b50840111::DependencyChecker1511*0a6a1f1dSLionel Sambuc bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
1512*0a6a1f1dSLionel Sambuc return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc());
1513*0a6a1f1dSLionel Sambuc }
1514*0a6a1f1dSLionel Sambuc
VisitTemplateTypeParmType__anon572b50840111::DependencyChecker1515f4a2713aSLionel Sambuc bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1516f4a2713aSLionel Sambuc return !Matches(T->getDepth());
1517f4a2713aSLionel Sambuc }
1518f4a2713aSLionel Sambuc
TraverseTemplateName__anon572b50840111::DependencyChecker1519f4a2713aSLionel Sambuc bool TraverseTemplateName(TemplateName N) {
1520f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *PD =
1521f4a2713aSLionel Sambuc dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1522*0a6a1f1dSLionel Sambuc if (Matches(PD->getDepth()))
1523*0a6a1f1dSLionel Sambuc return false;
1524f4a2713aSLionel Sambuc return super::TraverseTemplateName(N);
1525f4a2713aSLionel Sambuc }
1526f4a2713aSLionel Sambuc
VisitDeclRefExpr__anon572b50840111::DependencyChecker1527f4a2713aSLionel Sambuc bool VisitDeclRefExpr(DeclRefExpr *E) {
1528f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *PD =
1529*0a6a1f1dSLionel Sambuc dyn_cast<NonTypeTemplateParmDecl>(E->getDecl()))
1530*0a6a1f1dSLionel Sambuc if (Matches(PD->getDepth(), E->getExprLoc()))
1531f4a2713aSLionel Sambuc return false;
1532f4a2713aSLionel Sambuc return super::VisitDeclRefExpr(E);
1533f4a2713aSLionel Sambuc }
1534f4a2713aSLionel Sambuc
VisitSubstTemplateTypeParmType__anon572b50840111::DependencyChecker1535*0a6a1f1dSLionel Sambuc bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
1536*0a6a1f1dSLionel Sambuc return TraverseType(T->getReplacementType());
1537*0a6a1f1dSLionel Sambuc }
1538*0a6a1f1dSLionel Sambuc
1539*0a6a1f1dSLionel Sambuc bool
VisitSubstTemplateTypeParmPackType__anon572b50840111::DependencyChecker1540*0a6a1f1dSLionel Sambuc VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
1541*0a6a1f1dSLionel Sambuc return TraverseTemplateArgument(T->getArgumentPack());
1542*0a6a1f1dSLionel Sambuc }
1543*0a6a1f1dSLionel Sambuc
TraverseInjectedClassNameType__anon572b50840111::DependencyChecker1544f4a2713aSLionel Sambuc bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1545f4a2713aSLionel Sambuc return TraverseType(T->getInjectedSpecializationType());
1546f4a2713aSLionel Sambuc }
1547f4a2713aSLionel Sambuc };
1548f4a2713aSLionel Sambuc }
1549f4a2713aSLionel Sambuc
1550f4a2713aSLionel Sambuc /// Determines whether a given type depends on the given parameter
1551f4a2713aSLionel Sambuc /// list.
1552f4a2713aSLionel Sambuc static bool
DependsOnTemplateParameters(QualType T,TemplateParameterList * Params)1553f4a2713aSLionel Sambuc DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
1554f4a2713aSLionel Sambuc DependencyChecker Checker(Params);
1555f4a2713aSLionel Sambuc Checker.TraverseType(T);
1556f4a2713aSLionel Sambuc return Checker.Match;
1557f4a2713aSLionel Sambuc }
1558f4a2713aSLionel Sambuc
1559f4a2713aSLionel Sambuc // Find the source range corresponding to the named type in the given
1560f4a2713aSLionel Sambuc // nested-name-specifier, if any.
getRangeOfTypeInNestedNameSpecifier(ASTContext & Context,QualType T,const CXXScopeSpec & SS)1561f4a2713aSLionel Sambuc static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1562f4a2713aSLionel Sambuc QualType T,
1563f4a2713aSLionel Sambuc const CXXScopeSpec &SS) {
1564f4a2713aSLionel Sambuc NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1565f4a2713aSLionel Sambuc while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1566f4a2713aSLionel Sambuc if (const Type *CurType = NNS->getAsType()) {
1567f4a2713aSLionel Sambuc if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1568f4a2713aSLionel Sambuc return NNSLoc.getTypeLoc().getSourceRange();
1569f4a2713aSLionel Sambuc } else
1570f4a2713aSLionel Sambuc break;
1571f4a2713aSLionel Sambuc
1572f4a2713aSLionel Sambuc NNSLoc = NNSLoc.getPrefix();
1573f4a2713aSLionel Sambuc }
1574f4a2713aSLionel Sambuc
1575f4a2713aSLionel Sambuc return SourceRange();
1576f4a2713aSLionel Sambuc }
1577f4a2713aSLionel Sambuc
1578f4a2713aSLionel Sambuc /// \brief Match the given template parameter lists to the given scope
1579f4a2713aSLionel Sambuc /// specifier, returning the template parameter list that applies to the
1580f4a2713aSLionel Sambuc /// name.
1581f4a2713aSLionel Sambuc ///
1582f4a2713aSLionel Sambuc /// \param DeclStartLoc the start of the declaration that has a scope
1583f4a2713aSLionel Sambuc /// specifier or a template parameter list.
1584f4a2713aSLionel Sambuc ///
1585f4a2713aSLionel Sambuc /// \param DeclLoc The location of the declaration itself.
1586f4a2713aSLionel Sambuc ///
1587f4a2713aSLionel Sambuc /// \param SS the scope specifier that will be matched to the given template
1588f4a2713aSLionel Sambuc /// parameter lists. This scope specifier precedes a qualified name that is
1589f4a2713aSLionel Sambuc /// being declared.
1590f4a2713aSLionel Sambuc ///
1591*0a6a1f1dSLionel Sambuc /// \param TemplateId The template-id following the scope specifier, if there
1592*0a6a1f1dSLionel Sambuc /// is one. Used to check for a missing 'template<>'.
1593*0a6a1f1dSLionel Sambuc ///
1594f4a2713aSLionel Sambuc /// \param ParamLists the template parameter lists, from the outermost to the
1595f4a2713aSLionel Sambuc /// innermost template parameter lists.
1596f4a2713aSLionel Sambuc ///
1597f4a2713aSLionel Sambuc /// \param IsFriend Whether to apply the slightly different rules for
1598f4a2713aSLionel Sambuc /// matching template parameters to scope specifiers in friend
1599f4a2713aSLionel Sambuc /// declarations.
1600f4a2713aSLionel Sambuc ///
1601f4a2713aSLionel Sambuc /// \param IsExplicitSpecialization will be set true if the entity being
1602f4a2713aSLionel Sambuc /// declared is an explicit specialization, false otherwise.
1603f4a2713aSLionel Sambuc ///
1604f4a2713aSLionel Sambuc /// \returns the template parameter list, if any, that corresponds to the
1605f4a2713aSLionel Sambuc /// name that is preceded by the scope specifier @p SS. This template
1606f4a2713aSLionel Sambuc /// parameter list may have template parameters (if we're declaring a
1607f4a2713aSLionel Sambuc /// template) or may have no template parameters (if we're declaring a
1608f4a2713aSLionel Sambuc /// template specialization), or may be NULL (if what we're declaring isn't
1609f4a2713aSLionel Sambuc /// itself a template).
MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,SourceLocation DeclLoc,const CXXScopeSpec & SS,TemplateIdAnnotation * TemplateId,ArrayRef<TemplateParameterList * > ParamLists,bool IsFriend,bool & IsExplicitSpecialization,bool & Invalid)1610f4a2713aSLionel Sambuc TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier(
1611f4a2713aSLionel Sambuc SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS,
1612*0a6a1f1dSLionel Sambuc TemplateIdAnnotation *TemplateId,
1613f4a2713aSLionel Sambuc ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend,
1614f4a2713aSLionel Sambuc bool &IsExplicitSpecialization, bool &Invalid) {
1615f4a2713aSLionel Sambuc IsExplicitSpecialization = false;
1616f4a2713aSLionel Sambuc Invalid = false;
1617f4a2713aSLionel Sambuc
1618f4a2713aSLionel Sambuc // The sequence of nested types to which we will match up the template
1619f4a2713aSLionel Sambuc // parameter lists. We first build this list by starting with the type named
1620f4a2713aSLionel Sambuc // by the nested-name-specifier and walking out until we run out of types.
1621f4a2713aSLionel Sambuc SmallVector<QualType, 4> NestedTypes;
1622f4a2713aSLionel Sambuc QualType T;
1623f4a2713aSLionel Sambuc if (SS.getScopeRep()) {
1624f4a2713aSLionel Sambuc if (CXXRecordDecl *Record
1625f4a2713aSLionel Sambuc = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1626f4a2713aSLionel Sambuc T = Context.getTypeDeclType(Record);
1627f4a2713aSLionel Sambuc else
1628f4a2713aSLionel Sambuc T = QualType(SS.getScopeRep()->getAsType(), 0);
1629f4a2713aSLionel Sambuc }
1630f4a2713aSLionel Sambuc
1631f4a2713aSLionel Sambuc // If we found an explicit specialization that prevents us from needing
1632f4a2713aSLionel Sambuc // 'template<>' headers, this will be set to the location of that
1633f4a2713aSLionel Sambuc // explicit specialization.
1634f4a2713aSLionel Sambuc SourceLocation ExplicitSpecLoc;
1635f4a2713aSLionel Sambuc
1636f4a2713aSLionel Sambuc while (!T.isNull()) {
1637f4a2713aSLionel Sambuc NestedTypes.push_back(T);
1638f4a2713aSLionel Sambuc
1639f4a2713aSLionel Sambuc // Retrieve the parent of a record type.
1640f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1641f4a2713aSLionel Sambuc // If this type is an explicit specialization, we're done.
1642f4a2713aSLionel Sambuc if (ClassTemplateSpecializationDecl *Spec
1643f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1644f4a2713aSLionel Sambuc if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1645f4a2713aSLionel Sambuc Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1646f4a2713aSLionel Sambuc ExplicitSpecLoc = Spec->getLocation();
1647f4a2713aSLionel Sambuc break;
1648f4a2713aSLionel Sambuc }
1649f4a2713aSLionel Sambuc } else if (Record->getTemplateSpecializationKind()
1650f4a2713aSLionel Sambuc == TSK_ExplicitSpecialization) {
1651f4a2713aSLionel Sambuc ExplicitSpecLoc = Record->getLocation();
1652f4a2713aSLionel Sambuc break;
1653f4a2713aSLionel Sambuc }
1654f4a2713aSLionel Sambuc
1655f4a2713aSLionel Sambuc if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1656f4a2713aSLionel Sambuc T = Context.getTypeDeclType(Parent);
1657f4a2713aSLionel Sambuc else
1658f4a2713aSLionel Sambuc T = QualType();
1659f4a2713aSLionel Sambuc continue;
1660f4a2713aSLionel Sambuc }
1661f4a2713aSLionel Sambuc
1662f4a2713aSLionel Sambuc if (const TemplateSpecializationType *TST
1663f4a2713aSLionel Sambuc = T->getAs<TemplateSpecializationType>()) {
1664f4a2713aSLionel Sambuc if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1665f4a2713aSLionel Sambuc if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1666f4a2713aSLionel Sambuc T = Context.getTypeDeclType(Parent);
1667f4a2713aSLionel Sambuc else
1668f4a2713aSLionel Sambuc T = QualType();
1669f4a2713aSLionel Sambuc continue;
1670f4a2713aSLionel Sambuc }
1671f4a2713aSLionel Sambuc }
1672f4a2713aSLionel Sambuc
1673f4a2713aSLionel Sambuc // Look one step prior in a dependent template specialization type.
1674f4a2713aSLionel Sambuc if (const DependentTemplateSpecializationType *DependentTST
1675f4a2713aSLionel Sambuc = T->getAs<DependentTemplateSpecializationType>()) {
1676f4a2713aSLionel Sambuc if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1677f4a2713aSLionel Sambuc T = QualType(NNS->getAsType(), 0);
1678f4a2713aSLionel Sambuc else
1679f4a2713aSLionel Sambuc T = QualType();
1680f4a2713aSLionel Sambuc continue;
1681f4a2713aSLionel Sambuc }
1682f4a2713aSLionel Sambuc
1683f4a2713aSLionel Sambuc // Look one step prior in a dependent name type.
1684f4a2713aSLionel Sambuc if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1685f4a2713aSLionel Sambuc if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1686f4a2713aSLionel Sambuc T = QualType(NNS->getAsType(), 0);
1687f4a2713aSLionel Sambuc else
1688f4a2713aSLionel Sambuc T = QualType();
1689f4a2713aSLionel Sambuc continue;
1690f4a2713aSLionel Sambuc }
1691f4a2713aSLionel Sambuc
1692f4a2713aSLionel Sambuc // Retrieve the parent of an enumeration type.
1693f4a2713aSLionel Sambuc if (const EnumType *EnumT = T->getAs<EnumType>()) {
1694f4a2713aSLionel Sambuc // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1695f4a2713aSLionel Sambuc // check here.
1696f4a2713aSLionel Sambuc EnumDecl *Enum = EnumT->getDecl();
1697f4a2713aSLionel Sambuc
1698f4a2713aSLionel Sambuc // Get to the parent type.
1699f4a2713aSLionel Sambuc if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1700f4a2713aSLionel Sambuc T = Context.getTypeDeclType(Parent);
1701f4a2713aSLionel Sambuc else
1702f4a2713aSLionel Sambuc T = QualType();
1703f4a2713aSLionel Sambuc continue;
1704f4a2713aSLionel Sambuc }
1705f4a2713aSLionel Sambuc
1706f4a2713aSLionel Sambuc T = QualType();
1707f4a2713aSLionel Sambuc }
1708f4a2713aSLionel Sambuc // Reverse the nested types list, since we want to traverse from the outermost
1709f4a2713aSLionel Sambuc // to the innermost while checking template-parameter-lists.
1710f4a2713aSLionel Sambuc std::reverse(NestedTypes.begin(), NestedTypes.end());
1711f4a2713aSLionel Sambuc
1712f4a2713aSLionel Sambuc // C++0x [temp.expl.spec]p17:
1713f4a2713aSLionel Sambuc // A member or a member template may be nested within many
1714f4a2713aSLionel Sambuc // enclosing class templates. In an explicit specialization for
1715f4a2713aSLionel Sambuc // such a member, the member declaration shall be preceded by a
1716f4a2713aSLionel Sambuc // template<> for each enclosing class template that is
1717f4a2713aSLionel Sambuc // explicitly specialized.
1718f4a2713aSLionel Sambuc bool SawNonEmptyTemplateParameterList = false;
1719*0a6a1f1dSLionel Sambuc
1720*0a6a1f1dSLionel Sambuc auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) {
1721*0a6a1f1dSLionel Sambuc if (SawNonEmptyTemplateParameterList) {
1722*0a6a1f1dSLionel Sambuc Diag(DeclLoc, diag::err_specialize_member_of_template)
1723*0a6a1f1dSLionel Sambuc << !Recovery << Range;
1724*0a6a1f1dSLionel Sambuc Invalid = true;
1725*0a6a1f1dSLionel Sambuc IsExplicitSpecialization = false;
1726*0a6a1f1dSLionel Sambuc return true;
1727*0a6a1f1dSLionel Sambuc }
1728*0a6a1f1dSLionel Sambuc
1729*0a6a1f1dSLionel Sambuc return false;
1730*0a6a1f1dSLionel Sambuc };
1731*0a6a1f1dSLionel Sambuc
1732*0a6a1f1dSLionel Sambuc auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) {
1733*0a6a1f1dSLionel Sambuc // Check that we can have an explicit specialization here.
1734*0a6a1f1dSLionel Sambuc if (CheckExplicitSpecialization(Range, true))
1735*0a6a1f1dSLionel Sambuc return true;
1736*0a6a1f1dSLionel Sambuc
1737*0a6a1f1dSLionel Sambuc // We don't have a template header, but we should.
1738*0a6a1f1dSLionel Sambuc SourceLocation ExpectedTemplateLoc;
1739*0a6a1f1dSLionel Sambuc if (!ParamLists.empty())
1740*0a6a1f1dSLionel Sambuc ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1741*0a6a1f1dSLionel Sambuc else
1742*0a6a1f1dSLionel Sambuc ExpectedTemplateLoc = DeclStartLoc;
1743*0a6a1f1dSLionel Sambuc
1744*0a6a1f1dSLionel Sambuc Diag(DeclLoc, diag::err_template_spec_needs_header)
1745*0a6a1f1dSLionel Sambuc << Range
1746*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1747*0a6a1f1dSLionel Sambuc return false;
1748*0a6a1f1dSLionel Sambuc };
1749*0a6a1f1dSLionel Sambuc
1750f4a2713aSLionel Sambuc unsigned ParamIdx = 0;
1751f4a2713aSLionel Sambuc for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1752f4a2713aSLionel Sambuc ++TypeIdx) {
1753f4a2713aSLionel Sambuc T = NestedTypes[TypeIdx];
1754f4a2713aSLionel Sambuc
1755f4a2713aSLionel Sambuc // Whether we expect a 'template<>' header.
1756f4a2713aSLionel Sambuc bool NeedEmptyTemplateHeader = false;
1757f4a2713aSLionel Sambuc
1758f4a2713aSLionel Sambuc // Whether we expect a template header with parameters.
1759f4a2713aSLionel Sambuc bool NeedNonemptyTemplateHeader = false;
1760f4a2713aSLionel Sambuc
1761f4a2713aSLionel Sambuc // For a dependent type, the set of template parameters that we
1762f4a2713aSLionel Sambuc // expect to see.
1763*0a6a1f1dSLionel Sambuc TemplateParameterList *ExpectedTemplateParams = nullptr;
1764f4a2713aSLionel Sambuc
1765f4a2713aSLionel Sambuc // C++0x [temp.expl.spec]p15:
1766f4a2713aSLionel Sambuc // A member or a member template may be nested within many enclosing
1767f4a2713aSLionel Sambuc // class templates. In an explicit specialization for such a member, the
1768f4a2713aSLionel Sambuc // member declaration shall be preceded by a template<> for each
1769f4a2713aSLionel Sambuc // enclosing class template that is explicitly specialized.
1770f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1771f4a2713aSLionel Sambuc if (ClassTemplatePartialSpecializationDecl *Partial
1772f4a2713aSLionel Sambuc = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1773f4a2713aSLionel Sambuc ExpectedTemplateParams = Partial->getTemplateParameters();
1774f4a2713aSLionel Sambuc NeedNonemptyTemplateHeader = true;
1775f4a2713aSLionel Sambuc } else if (Record->isDependentType()) {
1776f4a2713aSLionel Sambuc if (Record->getDescribedClassTemplate()) {
1777f4a2713aSLionel Sambuc ExpectedTemplateParams = Record->getDescribedClassTemplate()
1778f4a2713aSLionel Sambuc ->getTemplateParameters();
1779f4a2713aSLionel Sambuc NeedNonemptyTemplateHeader = true;
1780f4a2713aSLionel Sambuc }
1781f4a2713aSLionel Sambuc } else if (ClassTemplateSpecializationDecl *Spec
1782f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1783f4a2713aSLionel Sambuc // C++0x [temp.expl.spec]p4:
1784f4a2713aSLionel Sambuc // Members of an explicitly specialized class template are defined
1785f4a2713aSLionel Sambuc // in the same manner as members of normal classes, and not using
1786f4a2713aSLionel Sambuc // the template<> syntax.
1787f4a2713aSLionel Sambuc if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1788f4a2713aSLionel Sambuc NeedEmptyTemplateHeader = true;
1789f4a2713aSLionel Sambuc else
1790f4a2713aSLionel Sambuc continue;
1791f4a2713aSLionel Sambuc } else if (Record->getTemplateSpecializationKind()) {
1792f4a2713aSLionel Sambuc if (Record->getTemplateSpecializationKind()
1793f4a2713aSLionel Sambuc != TSK_ExplicitSpecialization &&
1794f4a2713aSLionel Sambuc TypeIdx == NumTypes - 1)
1795f4a2713aSLionel Sambuc IsExplicitSpecialization = true;
1796f4a2713aSLionel Sambuc
1797f4a2713aSLionel Sambuc continue;
1798f4a2713aSLionel Sambuc }
1799f4a2713aSLionel Sambuc } else if (const TemplateSpecializationType *TST
1800f4a2713aSLionel Sambuc = T->getAs<TemplateSpecializationType>()) {
1801f4a2713aSLionel Sambuc if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1802f4a2713aSLionel Sambuc ExpectedTemplateParams = Template->getTemplateParameters();
1803f4a2713aSLionel Sambuc NeedNonemptyTemplateHeader = true;
1804f4a2713aSLionel Sambuc }
1805f4a2713aSLionel Sambuc } else if (T->getAs<DependentTemplateSpecializationType>()) {
1806f4a2713aSLionel Sambuc // FIXME: We actually could/should check the template arguments here
1807f4a2713aSLionel Sambuc // against the corresponding template parameter list.
1808f4a2713aSLionel Sambuc NeedNonemptyTemplateHeader = false;
1809f4a2713aSLionel Sambuc }
1810f4a2713aSLionel Sambuc
1811f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p16:
1812f4a2713aSLionel Sambuc // In an explicit specialization declaration for a member of a class
1813f4a2713aSLionel Sambuc // template or a member template that ap- pears in namespace scope, the
1814f4a2713aSLionel Sambuc // member template and some of its enclosing class templates may remain
1815f4a2713aSLionel Sambuc // unspecialized, except that the declaration shall not explicitly
1816f4a2713aSLionel Sambuc // specialize a class member template if its en- closing class templates
1817f4a2713aSLionel Sambuc // are not explicitly specialized as well.
1818f4a2713aSLionel Sambuc if (ParamIdx < ParamLists.size()) {
1819f4a2713aSLionel Sambuc if (ParamLists[ParamIdx]->size() == 0) {
1820*0a6a1f1dSLionel Sambuc if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(),
1821*0a6a1f1dSLionel Sambuc false))
1822*0a6a1f1dSLionel Sambuc return nullptr;
1823f4a2713aSLionel Sambuc } else
1824f4a2713aSLionel Sambuc SawNonEmptyTemplateParameterList = true;
1825f4a2713aSLionel Sambuc }
1826f4a2713aSLionel Sambuc
1827f4a2713aSLionel Sambuc if (NeedEmptyTemplateHeader) {
1828f4a2713aSLionel Sambuc // If we're on the last of the types, and we need a 'template<>' header
1829f4a2713aSLionel Sambuc // here, then it's an explicit specialization.
1830f4a2713aSLionel Sambuc if (TypeIdx == NumTypes - 1)
1831f4a2713aSLionel Sambuc IsExplicitSpecialization = true;
1832f4a2713aSLionel Sambuc
1833f4a2713aSLionel Sambuc if (ParamIdx < ParamLists.size()) {
1834f4a2713aSLionel Sambuc if (ParamLists[ParamIdx]->size() > 0) {
1835f4a2713aSLionel Sambuc // The header has template parameters when it shouldn't. Complain.
1836f4a2713aSLionel Sambuc Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1837f4a2713aSLionel Sambuc diag::err_template_param_list_matches_nontemplate)
1838f4a2713aSLionel Sambuc << T
1839f4a2713aSLionel Sambuc << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1840f4a2713aSLionel Sambuc ParamLists[ParamIdx]->getRAngleLoc())
1841f4a2713aSLionel Sambuc << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1842f4a2713aSLionel Sambuc Invalid = true;
1843*0a6a1f1dSLionel Sambuc return nullptr;
1844f4a2713aSLionel Sambuc }
1845f4a2713aSLionel Sambuc
1846f4a2713aSLionel Sambuc // Consume this template header.
1847f4a2713aSLionel Sambuc ++ParamIdx;
1848f4a2713aSLionel Sambuc continue;
1849f4a2713aSLionel Sambuc }
1850f4a2713aSLionel Sambuc
1851*0a6a1f1dSLionel Sambuc if (!IsFriend)
1852*0a6a1f1dSLionel Sambuc if (DiagnoseMissingExplicitSpecialization(
1853*0a6a1f1dSLionel Sambuc getRangeOfTypeInNestedNameSpecifier(Context, T, SS)))
1854*0a6a1f1dSLionel Sambuc return nullptr;
1855f4a2713aSLionel Sambuc
1856f4a2713aSLionel Sambuc continue;
1857f4a2713aSLionel Sambuc }
1858f4a2713aSLionel Sambuc
1859f4a2713aSLionel Sambuc if (NeedNonemptyTemplateHeader) {
1860f4a2713aSLionel Sambuc // In friend declarations we can have template-ids which don't
1861f4a2713aSLionel Sambuc // depend on the corresponding template parameter lists. But
1862f4a2713aSLionel Sambuc // assume that empty parameter lists are supposed to match this
1863f4a2713aSLionel Sambuc // template-id.
1864f4a2713aSLionel Sambuc if (IsFriend && T->isDependentType()) {
1865f4a2713aSLionel Sambuc if (ParamIdx < ParamLists.size() &&
1866f4a2713aSLionel Sambuc DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1867*0a6a1f1dSLionel Sambuc ExpectedTemplateParams = nullptr;
1868f4a2713aSLionel Sambuc else
1869f4a2713aSLionel Sambuc continue;
1870f4a2713aSLionel Sambuc }
1871f4a2713aSLionel Sambuc
1872f4a2713aSLionel Sambuc if (ParamIdx < ParamLists.size()) {
1873f4a2713aSLionel Sambuc // Check the template parameter list, if we can.
1874f4a2713aSLionel Sambuc if (ExpectedTemplateParams &&
1875f4a2713aSLionel Sambuc !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1876f4a2713aSLionel Sambuc ExpectedTemplateParams,
1877f4a2713aSLionel Sambuc true, TPL_TemplateMatch))
1878f4a2713aSLionel Sambuc Invalid = true;
1879f4a2713aSLionel Sambuc
1880f4a2713aSLionel Sambuc if (!Invalid &&
1881*0a6a1f1dSLionel Sambuc CheckTemplateParameterList(ParamLists[ParamIdx], nullptr,
1882f4a2713aSLionel Sambuc TPC_ClassTemplateMember))
1883f4a2713aSLionel Sambuc Invalid = true;
1884f4a2713aSLionel Sambuc
1885f4a2713aSLionel Sambuc ++ParamIdx;
1886f4a2713aSLionel Sambuc continue;
1887f4a2713aSLionel Sambuc }
1888f4a2713aSLionel Sambuc
1889f4a2713aSLionel Sambuc Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1890f4a2713aSLionel Sambuc << T
1891f4a2713aSLionel Sambuc << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1892f4a2713aSLionel Sambuc Invalid = true;
1893f4a2713aSLionel Sambuc continue;
1894f4a2713aSLionel Sambuc }
1895f4a2713aSLionel Sambuc }
1896f4a2713aSLionel Sambuc
1897f4a2713aSLionel Sambuc // If there were at least as many template-ids as there were template
1898f4a2713aSLionel Sambuc // parameter lists, then there are no template parameter lists remaining for
1899f4a2713aSLionel Sambuc // the declaration itself.
1900*0a6a1f1dSLionel Sambuc if (ParamIdx >= ParamLists.size()) {
1901*0a6a1f1dSLionel Sambuc if (TemplateId && !IsFriend) {
1902*0a6a1f1dSLionel Sambuc // We don't have a template header for the declaration itself, but we
1903*0a6a1f1dSLionel Sambuc // should.
1904*0a6a1f1dSLionel Sambuc IsExplicitSpecialization = true;
1905*0a6a1f1dSLionel Sambuc DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc,
1906*0a6a1f1dSLionel Sambuc TemplateId->RAngleLoc));
1907*0a6a1f1dSLionel Sambuc
1908*0a6a1f1dSLionel Sambuc // Fabricate an empty template parameter list for the invented header.
1909*0a6a1f1dSLionel Sambuc return TemplateParameterList::Create(Context, SourceLocation(),
1910*0a6a1f1dSLionel Sambuc SourceLocation(), nullptr, 0,
1911*0a6a1f1dSLionel Sambuc SourceLocation());
1912*0a6a1f1dSLionel Sambuc }
1913*0a6a1f1dSLionel Sambuc
1914*0a6a1f1dSLionel Sambuc return nullptr;
1915*0a6a1f1dSLionel Sambuc }
1916f4a2713aSLionel Sambuc
1917f4a2713aSLionel Sambuc // If there were too many template parameter lists, complain about that now.
1918f4a2713aSLionel Sambuc if (ParamIdx < ParamLists.size() - 1) {
1919f4a2713aSLionel Sambuc bool HasAnyExplicitSpecHeader = false;
1920f4a2713aSLionel Sambuc bool AllExplicitSpecHeaders = true;
1921f4a2713aSLionel Sambuc for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) {
1922f4a2713aSLionel Sambuc if (ParamLists[I]->size() == 0)
1923f4a2713aSLionel Sambuc HasAnyExplicitSpecHeader = true;
1924f4a2713aSLionel Sambuc else
1925f4a2713aSLionel Sambuc AllExplicitSpecHeaders = false;
1926f4a2713aSLionel Sambuc }
1927f4a2713aSLionel Sambuc
1928f4a2713aSLionel Sambuc Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1929f4a2713aSLionel Sambuc AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers
1930f4a2713aSLionel Sambuc : diag::err_template_spec_extra_headers)
1931f4a2713aSLionel Sambuc << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1932f4a2713aSLionel Sambuc ParamLists[ParamLists.size() - 2]->getRAngleLoc());
1933f4a2713aSLionel Sambuc
1934f4a2713aSLionel Sambuc // If there was a specialization somewhere, such that 'template<>' is
1935f4a2713aSLionel Sambuc // not required, and there were any 'template<>' headers, note where the
1936f4a2713aSLionel Sambuc // specialization occurred.
1937f4a2713aSLionel Sambuc if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1938f4a2713aSLionel Sambuc Diag(ExplicitSpecLoc,
1939f4a2713aSLionel Sambuc diag::note_explicit_template_spec_does_not_need_header)
1940f4a2713aSLionel Sambuc << NestedTypes.back();
1941f4a2713aSLionel Sambuc
1942f4a2713aSLionel Sambuc // We have a template parameter list with no corresponding scope, which
1943f4a2713aSLionel Sambuc // means that the resulting template declaration can't be instantiated
1944f4a2713aSLionel Sambuc // properly (we'll end up with dependent nodes when we shouldn't).
1945f4a2713aSLionel Sambuc if (!AllExplicitSpecHeaders)
1946f4a2713aSLionel Sambuc Invalid = true;
1947f4a2713aSLionel Sambuc }
1948f4a2713aSLionel Sambuc
1949f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p16:
1950f4a2713aSLionel Sambuc // In an explicit specialization declaration for a member of a class
1951f4a2713aSLionel Sambuc // template or a member template that ap- pears in namespace scope, the
1952f4a2713aSLionel Sambuc // member template and some of its enclosing class templates may remain
1953f4a2713aSLionel Sambuc // unspecialized, except that the declaration shall not explicitly
1954f4a2713aSLionel Sambuc // specialize a class member template if its en- closing class templates
1955f4a2713aSLionel Sambuc // are not explicitly specialized as well.
1956*0a6a1f1dSLionel Sambuc if (ParamLists.back()->size() == 0 &&
1957*0a6a1f1dSLionel Sambuc CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(),
1958*0a6a1f1dSLionel Sambuc false))
1959*0a6a1f1dSLionel Sambuc return nullptr;
1960f4a2713aSLionel Sambuc
1961f4a2713aSLionel Sambuc // Return the last template parameter list, which corresponds to the
1962f4a2713aSLionel Sambuc // entity being declared.
1963f4a2713aSLionel Sambuc return ParamLists.back();
1964f4a2713aSLionel Sambuc }
1965f4a2713aSLionel Sambuc
NoteAllFoundTemplates(TemplateName Name)1966f4a2713aSLionel Sambuc void Sema::NoteAllFoundTemplates(TemplateName Name) {
1967f4a2713aSLionel Sambuc if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1968f4a2713aSLionel Sambuc Diag(Template->getLocation(), diag::note_template_declared_here)
1969f4a2713aSLionel Sambuc << (isa<FunctionTemplateDecl>(Template)
1970f4a2713aSLionel Sambuc ? 0
1971f4a2713aSLionel Sambuc : isa<ClassTemplateDecl>(Template)
1972f4a2713aSLionel Sambuc ? 1
1973f4a2713aSLionel Sambuc : isa<VarTemplateDecl>(Template)
1974f4a2713aSLionel Sambuc ? 2
1975f4a2713aSLionel Sambuc : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4)
1976f4a2713aSLionel Sambuc << Template->getDeclName();
1977f4a2713aSLionel Sambuc return;
1978f4a2713aSLionel Sambuc }
1979f4a2713aSLionel Sambuc
1980f4a2713aSLionel Sambuc if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1981f4a2713aSLionel Sambuc for (OverloadedTemplateStorage::iterator I = OST->begin(),
1982f4a2713aSLionel Sambuc IEnd = OST->end();
1983f4a2713aSLionel Sambuc I != IEnd; ++I)
1984f4a2713aSLionel Sambuc Diag((*I)->getLocation(), diag::note_template_declared_here)
1985f4a2713aSLionel Sambuc << 0 << (*I)->getDeclName();
1986f4a2713aSLionel Sambuc
1987f4a2713aSLionel Sambuc return;
1988f4a2713aSLionel Sambuc }
1989f4a2713aSLionel Sambuc }
1990f4a2713aSLionel Sambuc
CheckTemplateIdType(TemplateName Name,SourceLocation TemplateLoc,TemplateArgumentListInfo & TemplateArgs)1991f4a2713aSLionel Sambuc QualType Sema::CheckTemplateIdType(TemplateName Name,
1992f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
1993f4a2713aSLionel Sambuc TemplateArgumentListInfo &TemplateArgs) {
1994f4a2713aSLionel Sambuc DependentTemplateName *DTN
1995f4a2713aSLionel Sambuc = Name.getUnderlying().getAsDependentTemplateName();
1996f4a2713aSLionel Sambuc if (DTN && DTN->isIdentifier())
1997f4a2713aSLionel Sambuc // When building a template-id where the template-name is dependent,
1998f4a2713aSLionel Sambuc // assume the template is a type template. Either our assumption is
1999f4a2713aSLionel Sambuc // correct, or the code is ill-formed and will be diagnosed when the
2000f4a2713aSLionel Sambuc // dependent name is substituted.
2001f4a2713aSLionel Sambuc return Context.getDependentTemplateSpecializationType(ETK_None,
2002f4a2713aSLionel Sambuc DTN->getQualifier(),
2003f4a2713aSLionel Sambuc DTN->getIdentifier(),
2004f4a2713aSLionel Sambuc TemplateArgs);
2005f4a2713aSLionel Sambuc
2006f4a2713aSLionel Sambuc TemplateDecl *Template = Name.getAsTemplateDecl();
2007*0a6a1f1dSLionel Sambuc if (!Template || isa<FunctionTemplateDecl>(Template) ||
2008*0a6a1f1dSLionel Sambuc isa<VarTemplateDecl>(Template)) {
2009f4a2713aSLionel Sambuc // We might have a substituted template template parameter pack. If so,
2010f4a2713aSLionel Sambuc // build a template specialization type for it.
2011f4a2713aSLionel Sambuc if (Name.getAsSubstTemplateTemplateParmPack())
2012f4a2713aSLionel Sambuc return Context.getTemplateSpecializationType(Name, TemplateArgs);
2013f4a2713aSLionel Sambuc
2014f4a2713aSLionel Sambuc Diag(TemplateLoc, diag::err_template_id_not_a_type)
2015f4a2713aSLionel Sambuc << Name;
2016f4a2713aSLionel Sambuc NoteAllFoundTemplates(Name);
2017f4a2713aSLionel Sambuc return QualType();
2018f4a2713aSLionel Sambuc }
2019f4a2713aSLionel Sambuc
2020f4a2713aSLionel Sambuc // Check that the template argument list is well-formed for this
2021f4a2713aSLionel Sambuc // template.
2022f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> Converted;
2023f4a2713aSLionel Sambuc if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
2024*0a6a1f1dSLionel Sambuc false, Converted))
2025f4a2713aSLionel Sambuc return QualType();
2026f4a2713aSLionel Sambuc
2027f4a2713aSLionel Sambuc QualType CanonType;
2028f4a2713aSLionel Sambuc
2029f4a2713aSLionel Sambuc bool InstantiationDependent = false;
2030*0a6a1f1dSLionel Sambuc if (TypeAliasTemplateDecl *AliasTemplate =
2031*0a6a1f1dSLionel Sambuc dyn_cast<TypeAliasTemplateDecl>(Template)) {
2032f4a2713aSLionel Sambuc // Find the canonical type for this type alias template specialization.
2033f4a2713aSLionel Sambuc TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
2034f4a2713aSLionel Sambuc if (Pattern->isInvalidDecl())
2035f4a2713aSLionel Sambuc return QualType();
2036f4a2713aSLionel Sambuc
2037f4a2713aSLionel Sambuc TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
2038f4a2713aSLionel Sambuc Converted.data(), Converted.size());
2039f4a2713aSLionel Sambuc
2040f4a2713aSLionel Sambuc // Only substitute for the innermost template argument list.
2041f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList TemplateArgLists;
2042f4a2713aSLionel Sambuc TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
2043f4a2713aSLionel Sambuc unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
2044f4a2713aSLionel Sambuc for (unsigned I = 0; I < Depth; ++I)
2045f4a2713aSLionel Sambuc TemplateArgLists.addOuterTemplateArguments(None);
2046f4a2713aSLionel Sambuc
2047f4a2713aSLionel Sambuc LocalInstantiationScope Scope(*this);
2048f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, TemplateLoc, Template);
2049f4a2713aSLionel Sambuc if (Inst.isInvalid())
2050f4a2713aSLionel Sambuc return QualType();
2051f4a2713aSLionel Sambuc
2052f4a2713aSLionel Sambuc CanonType = SubstType(Pattern->getUnderlyingType(),
2053f4a2713aSLionel Sambuc TemplateArgLists, AliasTemplate->getLocation(),
2054f4a2713aSLionel Sambuc AliasTemplate->getDeclName());
2055f4a2713aSLionel Sambuc if (CanonType.isNull())
2056f4a2713aSLionel Sambuc return QualType();
2057f4a2713aSLionel Sambuc } else if (Name.isDependent() ||
2058f4a2713aSLionel Sambuc TemplateSpecializationType::anyDependentTemplateArguments(
2059f4a2713aSLionel Sambuc TemplateArgs, InstantiationDependent)) {
2060f4a2713aSLionel Sambuc // This class template specialization is a dependent
2061f4a2713aSLionel Sambuc // type. Therefore, its canonical type is another class template
2062f4a2713aSLionel Sambuc // specialization type that contains all of the converted
2063f4a2713aSLionel Sambuc // arguments in canonical form. This ensures that, e.g., A<T> and
2064f4a2713aSLionel Sambuc // A<T, T> have identical types when A is declared as:
2065f4a2713aSLionel Sambuc //
2066f4a2713aSLionel Sambuc // template<typename T, typename U = T> struct A;
2067f4a2713aSLionel Sambuc TemplateName CanonName = Context.getCanonicalTemplateName(Name);
2068f4a2713aSLionel Sambuc CanonType = Context.getTemplateSpecializationType(CanonName,
2069f4a2713aSLionel Sambuc Converted.data(),
2070f4a2713aSLionel Sambuc Converted.size());
2071f4a2713aSLionel Sambuc
2072f4a2713aSLionel Sambuc // FIXME: CanonType is not actually the canonical type, and unfortunately
2073f4a2713aSLionel Sambuc // it is a TemplateSpecializationType that we will never use again.
2074f4a2713aSLionel Sambuc // In the future, we need to teach getTemplateSpecializationType to only
2075f4a2713aSLionel Sambuc // build the canonical type and return that to us.
2076f4a2713aSLionel Sambuc CanonType = Context.getCanonicalType(CanonType);
2077f4a2713aSLionel Sambuc
2078f4a2713aSLionel Sambuc // This might work out to be a current instantiation, in which
2079f4a2713aSLionel Sambuc // case the canonical type needs to be the InjectedClassNameType.
2080f4a2713aSLionel Sambuc //
2081f4a2713aSLionel Sambuc // TODO: in theory this could be a simple hashtable lookup; most
2082f4a2713aSLionel Sambuc // changes to CurContext don't change the set of current
2083f4a2713aSLionel Sambuc // instantiations.
2084f4a2713aSLionel Sambuc if (isa<ClassTemplateDecl>(Template)) {
2085f4a2713aSLionel Sambuc for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
2086f4a2713aSLionel Sambuc // If we get out to a namespace, we're done.
2087f4a2713aSLionel Sambuc if (Ctx->isFileContext()) break;
2088f4a2713aSLionel Sambuc
2089f4a2713aSLionel Sambuc // If this isn't a record, keep looking.
2090f4a2713aSLionel Sambuc CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
2091f4a2713aSLionel Sambuc if (!Record) continue;
2092f4a2713aSLionel Sambuc
2093f4a2713aSLionel Sambuc // Look for one of the two cases with InjectedClassNameTypes
2094f4a2713aSLionel Sambuc // and check whether it's the same template.
2095f4a2713aSLionel Sambuc if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
2096f4a2713aSLionel Sambuc !Record->getDescribedClassTemplate())
2097f4a2713aSLionel Sambuc continue;
2098f4a2713aSLionel Sambuc
2099f4a2713aSLionel Sambuc // Fetch the injected class name type and check whether its
2100f4a2713aSLionel Sambuc // injected type is equal to the type we just built.
2101f4a2713aSLionel Sambuc QualType ICNT = Context.getTypeDeclType(Record);
2102f4a2713aSLionel Sambuc QualType Injected = cast<InjectedClassNameType>(ICNT)
2103f4a2713aSLionel Sambuc ->getInjectedSpecializationType();
2104f4a2713aSLionel Sambuc
2105f4a2713aSLionel Sambuc if (CanonType != Injected->getCanonicalTypeInternal())
2106f4a2713aSLionel Sambuc continue;
2107f4a2713aSLionel Sambuc
2108f4a2713aSLionel Sambuc // If so, the canonical type of this TST is the injected
2109f4a2713aSLionel Sambuc // class name type of the record we just found.
2110f4a2713aSLionel Sambuc assert(ICNT.isCanonical());
2111f4a2713aSLionel Sambuc CanonType = ICNT;
2112f4a2713aSLionel Sambuc break;
2113f4a2713aSLionel Sambuc }
2114f4a2713aSLionel Sambuc }
2115f4a2713aSLionel Sambuc } else if (ClassTemplateDecl *ClassTemplate
2116f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateDecl>(Template)) {
2117f4a2713aSLionel Sambuc // Find the class template specialization declaration that
2118f4a2713aSLionel Sambuc // corresponds to these arguments.
2119*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2120f4a2713aSLionel Sambuc ClassTemplateSpecializationDecl *Decl
2121*0a6a1f1dSLionel Sambuc = ClassTemplate->findSpecialization(Converted, InsertPos);
2122f4a2713aSLionel Sambuc if (!Decl) {
2123f4a2713aSLionel Sambuc // This is the first time we have referenced this class template
2124f4a2713aSLionel Sambuc // specialization. Create the canonical declaration and add it to
2125f4a2713aSLionel Sambuc // the set of specializations.
2126f4a2713aSLionel Sambuc Decl = ClassTemplateSpecializationDecl::Create(Context,
2127f4a2713aSLionel Sambuc ClassTemplate->getTemplatedDecl()->getTagKind(),
2128f4a2713aSLionel Sambuc ClassTemplate->getDeclContext(),
2129f4a2713aSLionel Sambuc ClassTemplate->getTemplatedDecl()->getLocStart(),
2130f4a2713aSLionel Sambuc ClassTemplate->getLocation(),
2131f4a2713aSLionel Sambuc ClassTemplate,
2132f4a2713aSLionel Sambuc Converted.data(),
2133*0a6a1f1dSLionel Sambuc Converted.size(), nullptr);
2134f4a2713aSLionel Sambuc ClassTemplate->AddSpecialization(Decl, InsertPos);
2135f4a2713aSLionel Sambuc if (ClassTemplate->isOutOfLine())
2136f4a2713aSLionel Sambuc Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext());
2137f4a2713aSLionel Sambuc }
2138f4a2713aSLionel Sambuc
2139f4a2713aSLionel Sambuc // Diagnose uses of this specialization.
2140f4a2713aSLionel Sambuc (void)DiagnoseUseOfDecl(Decl, TemplateLoc);
2141f4a2713aSLionel Sambuc
2142f4a2713aSLionel Sambuc CanonType = Context.getTypeDeclType(Decl);
2143f4a2713aSLionel Sambuc assert(isa<RecordType>(CanonType) &&
2144f4a2713aSLionel Sambuc "type of non-dependent specialization is not a RecordType");
2145f4a2713aSLionel Sambuc }
2146f4a2713aSLionel Sambuc
2147f4a2713aSLionel Sambuc // Build the fully-sugared type for this class template
2148f4a2713aSLionel Sambuc // specialization, which refers back to the class template
2149f4a2713aSLionel Sambuc // specialization we created or found.
2150f4a2713aSLionel Sambuc return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
2151f4a2713aSLionel Sambuc }
2152f4a2713aSLionel Sambuc
2153f4a2713aSLionel Sambuc TypeResult
ActOnTemplateIdType(CXXScopeSpec & SS,SourceLocation TemplateKWLoc,TemplateTy TemplateD,SourceLocation TemplateLoc,SourceLocation LAngleLoc,ASTTemplateArgsPtr TemplateArgsIn,SourceLocation RAngleLoc,bool IsCtorOrDtorName)2154f4a2713aSLionel Sambuc Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc,
2155f4a2713aSLionel Sambuc TemplateTy TemplateD, SourceLocation TemplateLoc,
2156f4a2713aSLionel Sambuc SourceLocation LAngleLoc,
2157f4a2713aSLionel Sambuc ASTTemplateArgsPtr TemplateArgsIn,
2158f4a2713aSLionel Sambuc SourceLocation RAngleLoc,
2159f4a2713aSLionel Sambuc bool IsCtorOrDtorName) {
2160f4a2713aSLionel Sambuc if (SS.isInvalid())
2161f4a2713aSLionel Sambuc return true;
2162f4a2713aSLionel Sambuc
2163f4a2713aSLionel Sambuc TemplateName Template = TemplateD.get();
2164f4a2713aSLionel Sambuc
2165f4a2713aSLionel Sambuc // Translate the parser's template argument list in our AST format.
2166f4a2713aSLionel Sambuc TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2167f4a2713aSLionel Sambuc translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2168f4a2713aSLionel Sambuc
2169f4a2713aSLionel Sambuc if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2170f4a2713aSLionel Sambuc QualType T
2171f4a2713aSLionel Sambuc = Context.getDependentTemplateSpecializationType(ETK_None,
2172f4a2713aSLionel Sambuc DTN->getQualifier(),
2173f4a2713aSLionel Sambuc DTN->getIdentifier(),
2174f4a2713aSLionel Sambuc TemplateArgs);
2175f4a2713aSLionel Sambuc // Build type-source information.
2176f4a2713aSLionel Sambuc TypeLocBuilder TLB;
2177f4a2713aSLionel Sambuc DependentTemplateSpecializationTypeLoc SpecTL
2178f4a2713aSLionel Sambuc = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2179f4a2713aSLionel Sambuc SpecTL.setElaboratedKeywordLoc(SourceLocation());
2180f4a2713aSLionel Sambuc SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2181f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
2182f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateLoc);
2183f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc);
2184f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc);
2185f4a2713aSLionel Sambuc for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2186f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2187f4a2713aSLionel Sambuc return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2188f4a2713aSLionel Sambuc }
2189f4a2713aSLionel Sambuc
2190f4a2713aSLionel Sambuc QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2191f4a2713aSLionel Sambuc
2192f4a2713aSLionel Sambuc if (Result.isNull())
2193f4a2713aSLionel Sambuc return true;
2194f4a2713aSLionel Sambuc
2195f4a2713aSLionel Sambuc // Build type-source information.
2196f4a2713aSLionel Sambuc TypeLocBuilder TLB;
2197f4a2713aSLionel Sambuc TemplateSpecializationTypeLoc SpecTL
2198f4a2713aSLionel Sambuc = TLB.push<TemplateSpecializationTypeLoc>(Result);
2199f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
2200f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateLoc);
2201f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc);
2202f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc);
2203f4a2713aSLionel Sambuc for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2204f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
2205f4a2713aSLionel Sambuc
2206f4a2713aSLionel Sambuc // NOTE: avoid constructing an ElaboratedTypeLoc if this is a
2207f4a2713aSLionel Sambuc // constructor or destructor name (in such a case, the scope specifier
2208f4a2713aSLionel Sambuc // will be attached to the enclosing Decl or Expr node).
2209f4a2713aSLionel Sambuc if (SS.isNotEmpty() && !IsCtorOrDtorName) {
2210f4a2713aSLionel Sambuc // Create an elaborated-type-specifier containing the nested-name-specifier.
2211f4a2713aSLionel Sambuc Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2212f4a2713aSLionel Sambuc ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2213f4a2713aSLionel Sambuc ElabTL.setElaboratedKeywordLoc(SourceLocation());
2214f4a2713aSLionel Sambuc ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2215f4a2713aSLionel Sambuc }
2216f4a2713aSLionel Sambuc
2217f4a2713aSLionel Sambuc return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
2218f4a2713aSLionel Sambuc }
2219f4a2713aSLionel Sambuc
ActOnTagTemplateIdType(TagUseKind TUK,TypeSpecifierType TagSpec,SourceLocation TagLoc,CXXScopeSpec & SS,SourceLocation TemplateKWLoc,TemplateTy TemplateD,SourceLocation TemplateLoc,SourceLocation LAngleLoc,ASTTemplateArgsPtr TemplateArgsIn,SourceLocation RAngleLoc)2220f4a2713aSLionel Sambuc TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
2221f4a2713aSLionel Sambuc TypeSpecifierType TagSpec,
2222f4a2713aSLionel Sambuc SourceLocation TagLoc,
2223f4a2713aSLionel Sambuc CXXScopeSpec &SS,
2224f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
2225f4a2713aSLionel Sambuc TemplateTy TemplateD,
2226f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
2227f4a2713aSLionel Sambuc SourceLocation LAngleLoc,
2228f4a2713aSLionel Sambuc ASTTemplateArgsPtr TemplateArgsIn,
2229f4a2713aSLionel Sambuc SourceLocation RAngleLoc) {
2230f4a2713aSLionel Sambuc TemplateName Template = TemplateD.get();
2231f4a2713aSLionel Sambuc
2232f4a2713aSLionel Sambuc // Translate the parser's template argument list in our AST format.
2233f4a2713aSLionel Sambuc TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2234f4a2713aSLionel Sambuc translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2235f4a2713aSLionel Sambuc
2236f4a2713aSLionel Sambuc // Determine the tag kind
2237f4a2713aSLionel Sambuc TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
2238f4a2713aSLionel Sambuc ElaboratedTypeKeyword Keyword
2239f4a2713aSLionel Sambuc = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
2240f4a2713aSLionel Sambuc
2241f4a2713aSLionel Sambuc if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2242f4a2713aSLionel Sambuc QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2243f4a2713aSLionel Sambuc DTN->getQualifier(),
2244f4a2713aSLionel Sambuc DTN->getIdentifier(),
2245f4a2713aSLionel Sambuc TemplateArgs);
2246f4a2713aSLionel Sambuc
2247f4a2713aSLionel Sambuc // Build type-source information.
2248f4a2713aSLionel Sambuc TypeLocBuilder TLB;
2249f4a2713aSLionel Sambuc DependentTemplateSpecializationTypeLoc SpecTL
2250f4a2713aSLionel Sambuc = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2251f4a2713aSLionel Sambuc SpecTL.setElaboratedKeywordLoc(TagLoc);
2252f4a2713aSLionel Sambuc SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2253f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
2254f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateLoc);
2255f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc);
2256f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc);
2257f4a2713aSLionel Sambuc for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2258f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2259f4a2713aSLionel Sambuc return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2260f4a2713aSLionel Sambuc }
2261f4a2713aSLionel Sambuc
2262f4a2713aSLionel Sambuc if (TypeAliasTemplateDecl *TAT =
2263f4a2713aSLionel Sambuc dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2264f4a2713aSLionel Sambuc // C++0x [dcl.type.elab]p2:
2265f4a2713aSLionel Sambuc // If the identifier resolves to a typedef-name or the simple-template-id
2266f4a2713aSLionel Sambuc // resolves to an alias template specialization, the
2267f4a2713aSLionel Sambuc // elaborated-type-specifier is ill-formed.
2268f4a2713aSLionel Sambuc Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2269f4a2713aSLionel Sambuc Diag(TAT->getLocation(), diag::note_declared_at);
2270f4a2713aSLionel Sambuc }
2271f4a2713aSLionel Sambuc
2272f4a2713aSLionel Sambuc QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2273f4a2713aSLionel Sambuc if (Result.isNull())
2274f4a2713aSLionel Sambuc return TypeResult(true);
2275f4a2713aSLionel Sambuc
2276f4a2713aSLionel Sambuc // Check the tag kind
2277f4a2713aSLionel Sambuc if (const RecordType *RT = Result->getAs<RecordType>()) {
2278f4a2713aSLionel Sambuc RecordDecl *D = RT->getDecl();
2279f4a2713aSLionel Sambuc
2280f4a2713aSLionel Sambuc IdentifierInfo *Id = D->getIdentifier();
2281f4a2713aSLionel Sambuc assert(Id && "templated class must have an identifier");
2282f4a2713aSLionel Sambuc
2283f4a2713aSLionel Sambuc if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2284f4a2713aSLionel Sambuc TagLoc, *Id)) {
2285f4a2713aSLionel Sambuc Diag(TagLoc, diag::err_use_with_wrong_tag)
2286f4a2713aSLionel Sambuc << Result
2287f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
2288f4a2713aSLionel Sambuc Diag(D->getLocation(), diag::note_previous_use);
2289f4a2713aSLionel Sambuc }
2290f4a2713aSLionel Sambuc }
2291f4a2713aSLionel Sambuc
2292f4a2713aSLionel Sambuc // Provide source-location information for the template specialization.
2293f4a2713aSLionel Sambuc TypeLocBuilder TLB;
2294f4a2713aSLionel Sambuc TemplateSpecializationTypeLoc SpecTL
2295f4a2713aSLionel Sambuc = TLB.push<TemplateSpecializationTypeLoc>(Result);
2296f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
2297f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateLoc);
2298f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc);
2299f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc);
2300f4a2713aSLionel Sambuc for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2301f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
2302f4a2713aSLionel Sambuc
2303f4a2713aSLionel Sambuc // Construct an elaborated type containing the nested-name-specifier (if any)
2304f4a2713aSLionel Sambuc // and tag keyword.
2305f4a2713aSLionel Sambuc Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2306f4a2713aSLionel Sambuc ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2307f4a2713aSLionel Sambuc ElabTL.setElaboratedKeywordLoc(TagLoc);
2308f4a2713aSLionel Sambuc ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2309f4a2713aSLionel Sambuc return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
2310f4a2713aSLionel Sambuc }
2311f4a2713aSLionel Sambuc
2312f4a2713aSLionel Sambuc static bool CheckTemplatePartialSpecializationArgs(
2313*0a6a1f1dSLionel Sambuc Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams,
2314*0a6a1f1dSLionel Sambuc unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs);
2315f4a2713aSLionel Sambuc
2316f4a2713aSLionel Sambuc static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized,
2317f4a2713aSLionel Sambuc NamedDecl *PrevDecl,
2318f4a2713aSLionel Sambuc SourceLocation Loc,
2319f4a2713aSLionel Sambuc bool IsPartialSpecialization);
2320f4a2713aSLionel Sambuc
2321f4a2713aSLionel Sambuc static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D);
2322f4a2713aSLionel Sambuc
isTemplateArgumentTemplateParameter(const TemplateArgument & Arg,unsigned Depth,unsigned Index)2323f4a2713aSLionel Sambuc static bool isTemplateArgumentTemplateParameter(
2324f4a2713aSLionel Sambuc const TemplateArgument &Arg, unsigned Depth, unsigned Index) {
2325f4a2713aSLionel Sambuc switch (Arg.getKind()) {
2326f4a2713aSLionel Sambuc case TemplateArgument::Null:
2327f4a2713aSLionel Sambuc case TemplateArgument::NullPtr:
2328f4a2713aSLionel Sambuc case TemplateArgument::Integral:
2329f4a2713aSLionel Sambuc case TemplateArgument::Declaration:
2330f4a2713aSLionel Sambuc case TemplateArgument::Pack:
2331f4a2713aSLionel Sambuc case TemplateArgument::TemplateExpansion:
2332f4a2713aSLionel Sambuc return false;
2333f4a2713aSLionel Sambuc
2334f4a2713aSLionel Sambuc case TemplateArgument::Type: {
2335f4a2713aSLionel Sambuc QualType Type = Arg.getAsType();
2336f4a2713aSLionel Sambuc const TemplateTypeParmType *TPT =
2337f4a2713aSLionel Sambuc Arg.getAsType()->getAs<TemplateTypeParmType>();
2338f4a2713aSLionel Sambuc return TPT && !Type.hasQualifiers() &&
2339f4a2713aSLionel Sambuc TPT->getDepth() == Depth && TPT->getIndex() == Index;
2340f4a2713aSLionel Sambuc }
2341f4a2713aSLionel Sambuc
2342f4a2713aSLionel Sambuc case TemplateArgument::Expression: {
2343f4a2713aSLionel Sambuc DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr());
2344f4a2713aSLionel Sambuc if (!DRE || !DRE->getDecl())
2345f4a2713aSLionel Sambuc return false;
2346f4a2713aSLionel Sambuc const NonTypeTemplateParmDecl *NTTP =
2347f4a2713aSLionel Sambuc dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2348f4a2713aSLionel Sambuc return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index;
2349f4a2713aSLionel Sambuc }
2350f4a2713aSLionel Sambuc
2351f4a2713aSLionel Sambuc case TemplateArgument::Template:
2352f4a2713aSLionel Sambuc const TemplateTemplateParmDecl *TTP =
2353f4a2713aSLionel Sambuc dyn_cast_or_null<TemplateTemplateParmDecl>(
2354f4a2713aSLionel Sambuc Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl());
2355f4a2713aSLionel Sambuc return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index;
2356f4a2713aSLionel Sambuc }
2357f4a2713aSLionel Sambuc llvm_unreachable("unexpected kind of template argument");
2358f4a2713aSLionel Sambuc }
2359f4a2713aSLionel Sambuc
isSameAsPrimaryTemplate(TemplateParameterList * Params,ArrayRef<TemplateArgument> Args)2360f4a2713aSLionel Sambuc static bool isSameAsPrimaryTemplate(TemplateParameterList *Params,
2361f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> Args) {
2362f4a2713aSLionel Sambuc if (Params->size() != Args.size())
2363f4a2713aSLionel Sambuc return false;
2364f4a2713aSLionel Sambuc
2365f4a2713aSLionel Sambuc unsigned Depth = Params->getDepth();
2366f4a2713aSLionel Sambuc
2367f4a2713aSLionel Sambuc for (unsigned I = 0, N = Args.size(); I != N; ++I) {
2368f4a2713aSLionel Sambuc TemplateArgument Arg = Args[I];
2369f4a2713aSLionel Sambuc
2370f4a2713aSLionel Sambuc // If the parameter is a pack expansion, the argument must be a pack
2371f4a2713aSLionel Sambuc // whose only element is a pack expansion.
2372f4a2713aSLionel Sambuc if (Params->getParam(I)->isParameterPack()) {
2373f4a2713aSLionel Sambuc if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 ||
2374f4a2713aSLionel Sambuc !Arg.pack_begin()->isPackExpansion())
2375f4a2713aSLionel Sambuc return false;
2376f4a2713aSLionel Sambuc Arg = Arg.pack_begin()->getPackExpansionPattern();
2377f4a2713aSLionel Sambuc }
2378f4a2713aSLionel Sambuc
2379f4a2713aSLionel Sambuc if (!isTemplateArgumentTemplateParameter(Arg, Depth, I))
2380f4a2713aSLionel Sambuc return false;
2381f4a2713aSLionel Sambuc }
2382f4a2713aSLionel Sambuc
2383f4a2713aSLionel Sambuc return true;
2384f4a2713aSLionel Sambuc }
2385f4a2713aSLionel Sambuc
2386*0a6a1f1dSLionel Sambuc /// Convert the parser's template argument list representation into our form.
2387*0a6a1f1dSLionel Sambuc static TemplateArgumentListInfo
makeTemplateArgumentListInfo(Sema & S,TemplateIdAnnotation & TemplateId)2388*0a6a1f1dSLionel Sambuc makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) {
2389*0a6a1f1dSLionel Sambuc TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc,
2390*0a6a1f1dSLionel Sambuc TemplateId.RAngleLoc);
2391*0a6a1f1dSLionel Sambuc ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(),
2392*0a6a1f1dSLionel Sambuc TemplateId.NumArgs);
2393*0a6a1f1dSLionel Sambuc S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
2394*0a6a1f1dSLionel Sambuc return TemplateArgs;
2395*0a6a1f1dSLionel Sambuc }
2396f4a2713aSLionel Sambuc
ActOnVarTemplateSpecialization(Scope * S,Declarator & D,TypeSourceInfo * DI,SourceLocation TemplateKWLoc,TemplateParameterList * TemplateParams,StorageClass SC,bool IsPartialSpecialization)2397*0a6a1f1dSLionel Sambuc DeclResult Sema::ActOnVarTemplateSpecialization(
2398*0a6a1f1dSLionel Sambuc Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc,
2399*0a6a1f1dSLionel Sambuc TemplateParameterList *TemplateParams, StorageClass SC,
2400*0a6a1f1dSLionel Sambuc bool IsPartialSpecialization) {
2401f4a2713aSLionel Sambuc // D must be variable template id.
2402f4a2713aSLionel Sambuc assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId &&
2403f4a2713aSLionel Sambuc "Variable template specialization is declared with a template it.");
2404f4a2713aSLionel Sambuc
2405f4a2713aSLionel Sambuc TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
2406*0a6a1f1dSLionel Sambuc TemplateArgumentListInfo TemplateArgs =
2407*0a6a1f1dSLionel Sambuc makeTemplateArgumentListInfo(*this, *TemplateId);
2408f4a2713aSLionel Sambuc SourceLocation TemplateNameLoc = D.getIdentifierLoc();
2409f4a2713aSLionel Sambuc SourceLocation LAngleLoc = TemplateId->LAngleLoc;
2410f4a2713aSLionel Sambuc SourceLocation RAngleLoc = TemplateId->RAngleLoc;
2411*0a6a1f1dSLionel Sambuc
2412*0a6a1f1dSLionel Sambuc TemplateName Name = TemplateId->Template.get();
2413*0a6a1f1dSLionel Sambuc
2414*0a6a1f1dSLionel Sambuc // The template-id must name a variable template.
2415*0a6a1f1dSLionel Sambuc VarTemplateDecl *VarTemplate =
2416*0a6a1f1dSLionel Sambuc dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl());
2417*0a6a1f1dSLionel Sambuc if (!VarTemplate) {
2418*0a6a1f1dSLionel Sambuc NamedDecl *FnTemplate;
2419*0a6a1f1dSLionel Sambuc if (auto *OTS = Name.getAsOverloadedTemplate())
2420*0a6a1f1dSLionel Sambuc FnTemplate = *OTS->begin();
2421*0a6a1f1dSLionel Sambuc else
2422*0a6a1f1dSLionel Sambuc FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl());
2423*0a6a1f1dSLionel Sambuc if (FnTemplate)
2424*0a6a1f1dSLionel Sambuc return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method)
2425*0a6a1f1dSLionel Sambuc << FnTemplate->getDeclName();
2426*0a6a1f1dSLionel Sambuc return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template)
2427*0a6a1f1dSLionel Sambuc << IsPartialSpecialization;
2428*0a6a1f1dSLionel Sambuc }
2429f4a2713aSLionel Sambuc
2430f4a2713aSLionel Sambuc // Check for unexpanded parameter packs in any of the template arguments.
2431f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
2432f4a2713aSLionel Sambuc if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
2433f4a2713aSLionel Sambuc UPPC_PartialSpecialization))
2434f4a2713aSLionel Sambuc return true;
2435f4a2713aSLionel Sambuc
2436f4a2713aSLionel Sambuc // Check that the template argument list is well-formed for this
2437f4a2713aSLionel Sambuc // template.
2438f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> Converted;
2439f4a2713aSLionel Sambuc if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs,
2440f4a2713aSLionel Sambuc false, Converted))
2441f4a2713aSLionel Sambuc return true;
2442f4a2713aSLionel Sambuc
2443f4a2713aSLionel Sambuc // Check that the type of this variable template specialization
2444f4a2713aSLionel Sambuc // matches the expected type.
2445f4a2713aSLionel Sambuc TypeSourceInfo *ExpectedDI;
2446f4a2713aSLionel Sambuc {
2447f4a2713aSLionel Sambuc // Do substitution on the type of the declaration
2448f4a2713aSLionel Sambuc TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack,
2449f4a2713aSLionel Sambuc Converted.data(), Converted.size());
2450f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, TemplateKWLoc, VarTemplate);
2451f4a2713aSLionel Sambuc if (Inst.isInvalid())
2452f4a2713aSLionel Sambuc return true;
2453f4a2713aSLionel Sambuc VarDecl *Templated = VarTemplate->getTemplatedDecl();
2454f4a2713aSLionel Sambuc ExpectedDI =
2455f4a2713aSLionel Sambuc SubstType(Templated->getTypeSourceInfo(),
2456f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList(TemplateArgList),
2457f4a2713aSLionel Sambuc Templated->getTypeSpecStartLoc(), Templated->getDeclName());
2458f4a2713aSLionel Sambuc }
2459f4a2713aSLionel Sambuc if (!ExpectedDI)
2460f4a2713aSLionel Sambuc return true;
2461f4a2713aSLionel Sambuc
2462f4a2713aSLionel Sambuc // Find the variable template (partial) specialization declaration that
2463f4a2713aSLionel Sambuc // corresponds to these arguments.
2464f4a2713aSLionel Sambuc if (IsPartialSpecialization) {
2465f4a2713aSLionel Sambuc if (CheckTemplatePartialSpecializationArgs(
2466*0a6a1f1dSLionel Sambuc *this, TemplateNameLoc, VarTemplate->getTemplateParameters(),
2467*0a6a1f1dSLionel Sambuc TemplateArgs.size(), Converted))
2468f4a2713aSLionel Sambuc return true;
2469f4a2713aSLionel Sambuc
2470f4a2713aSLionel Sambuc bool InstantiationDependent;
2471f4a2713aSLionel Sambuc if (!Name.isDependent() &&
2472f4a2713aSLionel Sambuc !TemplateSpecializationType::anyDependentTemplateArguments(
2473f4a2713aSLionel Sambuc TemplateArgs.getArgumentArray(), TemplateArgs.size(),
2474f4a2713aSLionel Sambuc InstantiationDependent)) {
2475f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
2476f4a2713aSLionel Sambuc << VarTemplate->getDeclName();
2477f4a2713aSLionel Sambuc IsPartialSpecialization = false;
2478f4a2713aSLionel Sambuc }
2479f4a2713aSLionel Sambuc
2480f4a2713aSLionel Sambuc if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(),
2481f4a2713aSLionel Sambuc Converted)) {
2482f4a2713aSLionel Sambuc // C++ [temp.class.spec]p9b3:
2483f4a2713aSLionel Sambuc //
2484f4a2713aSLionel Sambuc // -- The argument list of the specialization shall not be identical
2485f4a2713aSLionel Sambuc // to the implicit argument list of the primary template.
2486f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
2487f4a2713aSLionel Sambuc << /*variable template*/ 1
2488f4a2713aSLionel Sambuc << /*is definition*/(SC != SC_Extern && !CurContext->isRecord())
2489f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
2490f4a2713aSLionel Sambuc // FIXME: Recover from this by treating the declaration as a redeclaration
2491f4a2713aSLionel Sambuc // of the primary template.
2492f4a2713aSLionel Sambuc return true;
2493f4a2713aSLionel Sambuc }
2494f4a2713aSLionel Sambuc }
2495f4a2713aSLionel Sambuc
2496*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2497*0a6a1f1dSLionel Sambuc VarTemplateSpecializationDecl *PrevDecl = nullptr;
2498f4a2713aSLionel Sambuc
2499f4a2713aSLionel Sambuc if (IsPartialSpecialization)
2500f4a2713aSLionel Sambuc // FIXME: Template parameter list matters too
2501*0a6a1f1dSLionel Sambuc PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos);
2502f4a2713aSLionel Sambuc else
2503*0a6a1f1dSLionel Sambuc PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos);
2504f4a2713aSLionel Sambuc
2505*0a6a1f1dSLionel Sambuc VarTemplateSpecializationDecl *Specialization = nullptr;
2506f4a2713aSLionel Sambuc
2507f4a2713aSLionel Sambuc // Check whether we can declare a variable template specialization in
2508f4a2713aSLionel Sambuc // the current scope.
2509f4a2713aSLionel Sambuc if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl,
2510f4a2713aSLionel Sambuc TemplateNameLoc,
2511f4a2713aSLionel Sambuc IsPartialSpecialization))
2512f4a2713aSLionel Sambuc return true;
2513f4a2713aSLionel Sambuc
2514f4a2713aSLionel Sambuc if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) {
2515f4a2713aSLionel Sambuc // Since the only prior variable template specialization with these
2516f4a2713aSLionel Sambuc // arguments was referenced but not declared, reuse that
2517f4a2713aSLionel Sambuc // declaration node as our own, updating its source location and
2518f4a2713aSLionel Sambuc // the list of outer template parameters to reflect our new declaration.
2519f4a2713aSLionel Sambuc Specialization = PrevDecl;
2520f4a2713aSLionel Sambuc Specialization->setLocation(TemplateNameLoc);
2521*0a6a1f1dSLionel Sambuc PrevDecl = nullptr;
2522f4a2713aSLionel Sambuc } else if (IsPartialSpecialization) {
2523f4a2713aSLionel Sambuc // Create a new class template partial specialization declaration node.
2524f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *PrevPartial =
2525f4a2713aSLionel Sambuc cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl);
2526f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *Partial =
2527f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl::Create(
2528f4a2713aSLionel Sambuc Context, VarTemplate->getDeclContext(), TemplateKWLoc,
2529f4a2713aSLionel Sambuc TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC,
2530f4a2713aSLionel Sambuc Converted.data(), Converted.size(), TemplateArgs);
2531f4a2713aSLionel Sambuc
2532f4a2713aSLionel Sambuc if (!PrevPartial)
2533f4a2713aSLionel Sambuc VarTemplate->AddPartialSpecialization(Partial, InsertPos);
2534f4a2713aSLionel Sambuc Specialization = Partial;
2535f4a2713aSLionel Sambuc
2536f4a2713aSLionel Sambuc // If we are providing an explicit specialization of a member variable
2537f4a2713aSLionel Sambuc // template specialization, make a note of that.
2538f4a2713aSLionel Sambuc if (PrevPartial && PrevPartial->getInstantiatedFromMember())
2539f4a2713aSLionel Sambuc PrevPartial->setMemberSpecialization();
2540f4a2713aSLionel Sambuc
2541f4a2713aSLionel Sambuc // Check that all of the template parameters of the variable template
2542f4a2713aSLionel Sambuc // partial specialization are deducible from the template
2543f4a2713aSLionel Sambuc // arguments. If not, this variable template partial specialization
2544f4a2713aSLionel Sambuc // will never be used.
2545f4a2713aSLionel Sambuc llvm::SmallBitVector DeducibleParams(TemplateParams->size());
2546f4a2713aSLionel Sambuc MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
2547f4a2713aSLionel Sambuc TemplateParams->getDepth(), DeducibleParams);
2548f4a2713aSLionel Sambuc
2549f4a2713aSLionel Sambuc if (!DeducibleParams.all()) {
2550f4a2713aSLionel Sambuc unsigned NumNonDeducible =
2551f4a2713aSLionel Sambuc DeducibleParams.size() - DeducibleParams.count();
2552f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
2553f4a2713aSLionel Sambuc << /*variable template*/ 1 << (NumNonDeducible > 1)
2554f4a2713aSLionel Sambuc << SourceRange(TemplateNameLoc, RAngleLoc);
2555f4a2713aSLionel Sambuc for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
2556f4a2713aSLionel Sambuc if (!DeducibleParams[I]) {
2557f4a2713aSLionel Sambuc NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
2558f4a2713aSLionel Sambuc if (Param->getDeclName())
2559f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter)
2560f4a2713aSLionel Sambuc << Param->getDeclName();
2561f4a2713aSLionel Sambuc else
2562f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter)
2563*0a6a1f1dSLionel Sambuc << "(anonymous)";
2564f4a2713aSLionel Sambuc }
2565f4a2713aSLionel Sambuc }
2566f4a2713aSLionel Sambuc }
2567f4a2713aSLionel Sambuc } else {
2568f4a2713aSLionel Sambuc // Create a new class template specialization declaration node for
2569f4a2713aSLionel Sambuc // this explicit specialization or friend declaration.
2570f4a2713aSLionel Sambuc Specialization = VarTemplateSpecializationDecl::Create(
2571f4a2713aSLionel Sambuc Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc,
2572f4a2713aSLionel Sambuc VarTemplate, DI->getType(), DI, SC, Converted.data(), Converted.size());
2573f4a2713aSLionel Sambuc Specialization->setTemplateArgsInfo(TemplateArgs);
2574f4a2713aSLionel Sambuc
2575f4a2713aSLionel Sambuc if (!PrevDecl)
2576f4a2713aSLionel Sambuc VarTemplate->AddSpecialization(Specialization, InsertPos);
2577f4a2713aSLionel Sambuc }
2578f4a2713aSLionel Sambuc
2579f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p6:
2580f4a2713aSLionel Sambuc // If a template, a member template or the member of a class template is
2581f4a2713aSLionel Sambuc // explicitly specialized then that specialization shall be declared
2582f4a2713aSLionel Sambuc // before the first use of that specialization that would cause an implicit
2583f4a2713aSLionel Sambuc // instantiation to take place, in every translation unit in which such a
2584f4a2713aSLionel Sambuc // use occurs; no diagnostic is required.
2585f4a2713aSLionel Sambuc if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
2586f4a2713aSLionel Sambuc bool Okay = false;
2587f4a2713aSLionel Sambuc for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
2588f4a2713aSLionel Sambuc // Is there any previous explicit specialization declaration?
2589f4a2713aSLionel Sambuc if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
2590f4a2713aSLionel Sambuc Okay = true;
2591f4a2713aSLionel Sambuc break;
2592f4a2713aSLionel Sambuc }
2593f4a2713aSLionel Sambuc }
2594f4a2713aSLionel Sambuc
2595f4a2713aSLionel Sambuc if (!Okay) {
2596f4a2713aSLionel Sambuc SourceRange Range(TemplateNameLoc, RAngleLoc);
2597f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
2598f4a2713aSLionel Sambuc << Name << Range;
2599f4a2713aSLionel Sambuc
2600f4a2713aSLionel Sambuc Diag(PrevDecl->getPointOfInstantiation(),
2601f4a2713aSLionel Sambuc diag::note_instantiation_required_here)
2602f4a2713aSLionel Sambuc << (PrevDecl->getTemplateSpecializationKind() !=
2603f4a2713aSLionel Sambuc TSK_ImplicitInstantiation);
2604f4a2713aSLionel Sambuc return true;
2605f4a2713aSLionel Sambuc }
2606f4a2713aSLionel Sambuc }
2607f4a2713aSLionel Sambuc
2608f4a2713aSLionel Sambuc Specialization->setTemplateKeywordLoc(TemplateKWLoc);
2609f4a2713aSLionel Sambuc Specialization->setLexicalDeclContext(CurContext);
2610f4a2713aSLionel Sambuc
2611f4a2713aSLionel Sambuc // Add the specialization into its lexical context, so that it can
2612f4a2713aSLionel Sambuc // be seen when iterating through the list of declarations in that
2613f4a2713aSLionel Sambuc // context. However, specializations are not found by name lookup.
2614f4a2713aSLionel Sambuc CurContext->addDecl(Specialization);
2615f4a2713aSLionel Sambuc
2616f4a2713aSLionel Sambuc // Note that this is an explicit specialization.
2617f4a2713aSLionel Sambuc Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
2618f4a2713aSLionel Sambuc
2619f4a2713aSLionel Sambuc if (PrevDecl) {
2620f4a2713aSLionel Sambuc // Check that this isn't a redefinition of this specialization,
2621f4a2713aSLionel Sambuc // merging with previous declarations.
2622f4a2713aSLionel Sambuc LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName,
2623f4a2713aSLionel Sambuc ForRedeclaration);
2624f4a2713aSLionel Sambuc PrevSpec.addDecl(PrevDecl);
2625f4a2713aSLionel Sambuc D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec));
2626f4a2713aSLionel Sambuc } else if (Specialization->isStaticDataMember() &&
2627f4a2713aSLionel Sambuc Specialization->isOutOfLine()) {
2628f4a2713aSLionel Sambuc Specialization->setAccess(VarTemplate->getAccess());
2629f4a2713aSLionel Sambuc }
2630f4a2713aSLionel Sambuc
2631f4a2713aSLionel Sambuc // Link instantiations of static data members back to the template from
2632f4a2713aSLionel Sambuc // which they were instantiated.
2633f4a2713aSLionel Sambuc if (Specialization->isStaticDataMember())
2634f4a2713aSLionel Sambuc Specialization->setInstantiationOfStaticDataMember(
2635f4a2713aSLionel Sambuc VarTemplate->getTemplatedDecl(),
2636f4a2713aSLionel Sambuc Specialization->getSpecializationKind());
2637f4a2713aSLionel Sambuc
2638f4a2713aSLionel Sambuc return Specialization;
2639f4a2713aSLionel Sambuc }
2640f4a2713aSLionel Sambuc
2641f4a2713aSLionel Sambuc namespace {
2642f4a2713aSLionel Sambuc /// \brief A partial specialization whose template arguments have matched
2643f4a2713aSLionel Sambuc /// a given template-id.
2644f4a2713aSLionel Sambuc struct PartialSpecMatchResult {
2645f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *Partial;
2646f4a2713aSLionel Sambuc TemplateArgumentList *Args;
2647f4a2713aSLionel Sambuc };
2648f4a2713aSLionel Sambuc }
2649f4a2713aSLionel Sambuc
2650f4a2713aSLionel Sambuc DeclResult
CheckVarTemplateId(VarTemplateDecl * Template,SourceLocation TemplateLoc,SourceLocation TemplateNameLoc,const TemplateArgumentListInfo & TemplateArgs)2651f4a2713aSLionel Sambuc Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc,
2652f4a2713aSLionel Sambuc SourceLocation TemplateNameLoc,
2653f4a2713aSLionel Sambuc const TemplateArgumentListInfo &TemplateArgs) {
2654f4a2713aSLionel Sambuc assert(Template && "A variable template id without template?");
2655f4a2713aSLionel Sambuc
2656f4a2713aSLionel Sambuc // Check that the template argument list is well-formed for this template.
2657f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> Converted;
2658f4a2713aSLionel Sambuc if (CheckTemplateArgumentList(
2659f4a2713aSLionel Sambuc Template, TemplateNameLoc,
2660f4a2713aSLionel Sambuc const_cast<TemplateArgumentListInfo &>(TemplateArgs), false,
2661*0a6a1f1dSLionel Sambuc Converted))
2662f4a2713aSLionel Sambuc return true;
2663f4a2713aSLionel Sambuc
2664f4a2713aSLionel Sambuc // Find the variable template specialization declaration that
2665f4a2713aSLionel Sambuc // corresponds to these arguments.
2666*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2667f4a2713aSLionel Sambuc if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization(
2668*0a6a1f1dSLionel Sambuc Converted, InsertPos))
2669f4a2713aSLionel Sambuc // If we already have a variable template specialization, return it.
2670f4a2713aSLionel Sambuc return Spec;
2671f4a2713aSLionel Sambuc
2672f4a2713aSLionel Sambuc // This is the first time we have referenced this variable template
2673f4a2713aSLionel Sambuc // specialization. Create the canonical declaration and add it to
2674f4a2713aSLionel Sambuc // the set of specializations, based on the closest partial specialization
2675f4a2713aSLionel Sambuc // that it represents. That is,
2676f4a2713aSLionel Sambuc VarDecl *InstantiationPattern = Template->getTemplatedDecl();
2677f4a2713aSLionel Sambuc TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack,
2678f4a2713aSLionel Sambuc Converted.data(), Converted.size());
2679f4a2713aSLionel Sambuc TemplateArgumentList *InstantiationArgs = &TemplateArgList;
2680f4a2713aSLionel Sambuc bool AmbiguousPartialSpec = false;
2681f4a2713aSLionel Sambuc typedef PartialSpecMatchResult MatchResult;
2682f4a2713aSLionel Sambuc SmallVector<MatchResult, 4> Matched;
2683f4a2713aSLionel Sambuc SourceLocation PointOfInstantiation = TemplateNameLoc;
2684f4a2713aSLionel Sambuc TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation);
2685f4a2713aSLionel Sambuc
2686f4a2713aSLionel Sambuc // 1. Attempt to find the closest partial specialization that this
2687f4a2713aSLionel Sambuc // specializes, if any.
2688f4a2713aSLionel Sambuc // If any of the template arguments is dependent, then this is probably
2689f4a2713aSLionel Sambuc // a placeholder for an incomplete declarative context; which must be
2690f4a2713aSLionel Sambuc // complete by instantiation time. Thus, do not search through the partial
2691f4a2713aSLionel Sambuc // specializations yet.
2692f4a2713aSLionel Sambuc // TODO: Unify with InstantiateClassTemplateSpecialization()?
2693f4a2713aSLionel Sambuc // Perhaps better after unification of DeduceTemplateArguments() and
2694f4a2713aSLionel Sambuc // getMoreSpecializedPartialSpecialization().
2695f4a2713aSLionel Sambuc bool InstantiationDependent = false;
2696f4a2713aSLionel Sambuc if (!TemplateSpecializationType::anyDependentTemplateArguments(
2697f4a2713aSLionel Sambuc TemplateArgs, InstantiationDependent)) {
2698f4a2713aSLionel Sambuc
2699f4a2713aSLionel Sambuc SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs;
2700f4a2713aSLionel Sambuc Template->getPartialSpecializations(PartialSpecs);
2701f4a2713aSLionel Sambuc
2702f4a2713aSLionel Sambuc for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2703f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
2704f4a2713aSLionel Sambuc TemplateDeductionInfo Info(FailedCandidates.getLocation());
2705f4a2713aSLionel Sambuc
2706f4a2713aSLionel Sambuc if (TemplateDeductionResult Result =
2707f4a2713aSLionel Sambuc DeduceTemplateArguments(Partial, TemplateArgList, Info)) {
2708f4a2713aSLionel Sambuc // Store the failed-deduction information for use in diagnostics, later.
2709f4a2713aSLionel Sambuc // TODO: Actually use the failed-deduction info?
2710f4a2713aSLionel Sambuc FailedCandidates.addCandidate()
2711f4a2713aSLionel Sambuc .set(Partial, MakeDeductionFailureInfo(Context, Result, Info));
2712f4a2713aSLionel Sambuc (void)Result;
2713f4a2713aSLionel Sambuc } else {
2714f4a2713aSLionel Sambuc Matched.push_back(PartialSpecMatchResult());
2715f4a2713aSLionel Sambuc Matched.back().Partial = Partial;
2716f4a2713aSLionel Sambuc Matched.back().Args = Info.take();
2717f4a2713aSLionel Sambuc }
2718f4a2713aSLionel Sambuc }
2719f4a2713aSLionel Sambuc
2720f4a2713aSLionel Sambuc if (Matched.size() >= 1) {
2721f4a2713aSLionel Sambuc SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
2722f4a2713aSLionel Sambuc if (Matched.size() == 1) {
2723f4a2713aSLionel Sambuc // -- If exactly one matching specialization is found, the
2724f4a2713aSLionel Sambuc // instantiation is generated from that specialization.
2725f4a2713aSLionel Sambuc // We don't need to do anything for this.
2726f4a2713aSLionel Sambuc } else {
2727f4a2713aSLionel Sambuc // -- If more than one matching specialization is found, the
2728f4a2713aSLionel Sambuc // partial order rules (14.5.4.2) are used to determine
2729f4a2713aSLionel Sambuc // whether one of the specializations is more specialized
2730f4a2713aSLionel Sambuc // than the others. If none of the specializations is more
2731f4a2713aSLionel Sambuc // specialized than all of the other matching
2732f4a2713aSLionel Sambuc // specializations, then the use of the variable template is
2733f4a2713aSLionel Sambuc // ambiguous and the program is ill-formed.
2734f4a2713aSLionel Sambuc for (SmallVector<MatchResult, 4>::iterator P = Best + 1,
2735f4a2713aSLionel Sambuc PEnd = Matched.end();
2736f4a2713aSLionel Sambuc P != PEnd; ++P) {
2737f4a2713aSLionel Sambuc if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
2738f4a2713aSLionel Sambuc PointOfInstantiation) ==
2739f4a2713aSLionel Sambuc P->Partial)
2740f4a2713aSLionel Sambuc Best = P;
2741f4a2713aSLionel Sambuc }
2742f4a2713aSLionel Sambuc
2743f4a2713aSLionel Sambuc // Determine if the best partial specialization is more specialized than
2744f4a2713aSLionel Sambuc // the others.
2745f4a2713aSLionel Sambuc for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
2746f4a2713aSLionel Sambuc PEnd = Matched.end();
2747f4a2713aSLionel Sambuc P != PEnd; ++P) {
2748f4a2713aSLionel Sambuc if (P != Best && getMoreSpecializedPartialSpecialization(
2749f4a2713aSLionel Sambuc P->Partial, Best->Partial,
2750f4a2713aSLionel Sambuc PointOfInstantiation) != Best->Partial) {
2751f4a2713aSLionel Sambuc AmbiguousPartialSpec = true;
2752f4a2713aSLionel Sambuc break;
2753f4a2713aSLionel Sambuc }
2754f4a2713aSLionel Sambuc }
2755f4a2713aSLionel Sambuc }
2756f4a2713aSLionel Sambuc
2757f4a2713aSLionel Sambuc // Instantiate using the best variable template partial specialization.
2758f4a2713aSLionel Sambuc InstantiationPattern = Best->Partial;
2759f4a2713aSLionel Sambuc InstantiationArgs = Best->Args;
2760f4a2713aSLionel Sambuc } else {
2761f4a2713aSLionel Sambuc // -- If no match is found, the instantiation is generated
2762f4a2713aSLionel Sambuc // from the primary template.
2763f4a2713aSLionel Sambuc // InstantiationPattern = Template->getTemplatedDecl();
2764f4a2713aSLionel Sambuc }
2765f4a2713aSLionel Sambuc }
2766f4a2713aSLionel Sambuc
2767f4a2713aSLionel Sambuc // 2. Create the canonical declaration.
2768f4a2713aSLionel Sambuc // Note that we do not instantiate the variable just yet, since
2769f4a2713aSLionel Sambuc // instantiation is handled in DoMarkVarDeclReferenced().
2770f4a2713aSLionel Sambuc // FIXME: LateAttrs et al.?
2771f4a2713aSLionel Sambuc VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation(
2772f4a2713aSLionel Sambuc Template, InstantiationPattern, *InstantiationArgs, TemplateArgs,
2773f4a2713aSLionel Sambuc Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/);
2774f4a2713aSLionel Sambuc if (!Decl)
2775f4a2713aSLionel Sambuc return true;
2776f4a2713aSLionel Sambuc
2777f4a2713aSLionel Sambuc if (AmbiguousPartialSpec) {
2778f4a2713aSLionel Sambuc // Partial ordering did not produce a clear winner. Complain.
2779f4a2713aSLionel Sambuc Decl->setInvalidDecl();
2780f4a2713aSLionel Sambuc Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
2781f4a2713aSLionel Sambuc << Decl;
2782f4a2713aSLionel Sambuc
2783f4a2713aSLionel Sambuc // Print the matching partial specializations.
2784f4a2713aSLionel Sambuc for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
2785f4a2713aSLionel Sambuc PEnd = Matched.end();
2786f4a2713aSLionel Sambuc P != PEnd; ++P)
2787f4a2713aSLionel Sambuc Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
2788f4a2713aSLionel Sambuc << getTemplateArgumentBindingsText(
2789f4a2713aSLionel Sambuc P->Partial->getTemplateParameters(), *P->Args);
2790f4a2713aSLionel Sambuc return true;
2791f4a2713aSLionel Sambuc }
2792f4a2713aSLionel Sambuc
2793f4a2713aSLionel Sambuc if (VarTemplatePartialSpecializationDecl *D =
2794f4a2713aSLionel Sambuc dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern))
2795f4a2713aSLionel Sambuc Decl->setInstantiationOf(D, InstantiationArgs);
2796f4a2713aSLionel Sambuc
2797f4a2713aSLionel Sambuc assert(Decl && "No variable template specialization?");
2798f4a2713aSLionel Sambuc return Decl;
2799f4a2713aSLionel Sambuc }
2800f4a2713aSLionel Sambuc
2801f4a2713aSLionel Sambuc ExprResult
CheckVarTemplateId(const CXXScopeSpec & SS,const DeclarationNameInfo & NameInfo,VarTemplateDecl * Template,SourceLocation TemplateLoc,const TemplateArgumentListInfo * TemplateArgs)2802f4a2713aSLionel Sambuc Sema::CheckVarTemplateId(const CXXScopeSpec &SS,
2803f4a2713aSLionel Sambuc const DeclarationNameInfo &NameInfo,
2804f4a2713aSLionel Sambuc VarTemplateDecl *Template, SourceLocation TemplateLoc,
2805f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs) {
2806f4a2713aSLionel Sambuc
2807f4a2713aSLionel Sambuc DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(),
2808f4a2713aSLionel Sambuc *TemplateArgs);
2809f4a2713aSLionel Sambuc if (Decl.isInvalid())
2810f4a2713aSLionel Sambuc return ExprError();
2811f4a2713aSLionel Sambuc
2812f4a2713aSLionel Sambuc VarDecl *Var = cast<VarDecl>(Decl.get());
2813f4a2713aSLionel Sambuc if (!Var->getTemplateSpecializationKind())
2814f4a2713aSLionel Sambuc Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation,
2815f4a2713aSLionel Sambuc NameInfo.getLoc());
2816f4a2713aSLionel Sambuc
2817f4a2713aSLionel Sambuc // Build an ordinary singleton decl ref.
2818f4a2713aSLionel Sambuc return BuildDeclarationNameExpr(SS, NameInfo, Var,
2819*0a6a1f1dSLionel Sambuc /*FoundD=*/nullptr, TemplateArgs);
2820f4a2713aSLionel Sambuc }
2821f4a2713aSLionel Sambuc
BuildTemplateIdExpr(const CXXScopeSpec & SS,SourceLocation TemplateKWLoc,LookupResult & R,bool RequiresADL,const TemplateArgumentListInfo * TemplateArgs)2822f4a2713aSLionel Sambuc ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
2823f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
2824f4a2713aSLionel Sambuc LookupResult &R,
2825f4a2713aSLionel Sambuc bool RequiresADL,
2826f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs) {
2827f4a2713aSLionel Sambuc // FIXME: Can we do any checking at this point? I guess we could check the
2828f4a2713aSLionel Sambuc // template arguments that we have against the template name, if the template
2829f4a2713aSLionel Sambuc // name refers to a single template. That's not a terribly common case,
2830f4a2713aSLionel Sambuc // though.
2831f4a2713aSLionel Sambuc // foo<int> could identify a single function unambiguously
2832f4a2713aSLionel Sambuc // This approach does NOT work, since f<int>(1);
2833f4a2713aSLionel Sambuc // gets resolved prior to resorting to overload resolution
2834f4a2713aSLionel Sambuc // i.e., template<class T> void f(double);
2835f4a2713aSLionel Sambuc // vs template<class T, class U> void f(U);
2836f4a2713aSLionel Sambuc
2837f4a2713aSLionel Sambuc // These should be filtered out by our callers.
2838f4a2713aSLionel Sambuc assert(!R.empty() && "empty lookup results when building templateid");
2839f4a2713aSLionel Sambuc assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2840f4a2713aSLionel Sambuc
2841f4a2713aSLionel Sambuc // In C++1y, check variable template ids.
2842*0a6a1f1dSLionel Sambuc bool InstantiationDependent;
2843*0a6a1f1dSLionel Sambuc if (R.getAsSingle<VarTemplateDecl>() &&
2844*0a6a1f1dSLionel Sambuc !TemplateSpecializationType::anyDependentTemplateArguments(
2845*0a6a1f1dSLionel Sambuc *TemplateArgs, InstantiationDependent)) {
2846*0a6a1f1dSLionel Sambuc return CheckVarTemplateId(SS, R.getLookupNameInfo(),
2847f4a2713aSLionel Sambuc R.getAsSingle<VarTemplateDecl>(),
2848*0a6a1f1dSLionel Sambuc TemplateKWLoc, TemplateArgs);
2849f4a2713aSLionel Sambuc }
2850f4a2713aSLionel Sambuc
2851f4a2713aSLionel Sambuc // We don't want lookup warnings at this point.
2852f4a2713aSLionel Sambuc R.suppressDiagnostics();
2853f4a2713aSLionel Sambuc
2854f4a2713aSLionel Sambuc UnresolvedLookupExpr *ULE
2855f4a2713aSLionel Sambuc = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
2856f4a2713aSLionel Sambuc SS.getWithLocInContext(Context),
2857f4a2713aSLionel Sambuc TemplateKWLoc,
2858f4a2713aSLionel Sambuc R.getLookupNameInfo(),
2859f4a2713aSLionel Sambuc RequiresADL, TemplateArgs,
2860f4a2713aSLionel Sambuc R.begin(), R.end());
2861f4a2713aSLionel Sambuc
2862*0a6a1f1dSLionel Sambuc return ULE;
2863f4a2713aSLionel Sambuc }
2864f4a2713aSLionel Sambuc
2865f4a2713aSLionel Sambuc // We actually only call this from template instantiation.
2866f4a2713aSLionel Sambuc ExprResult
BuildQualifiedTemplateIdExpr(CXXScopeSpec & SS,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * TemplateArgs)2867f4a2713aSLionel Sambuc Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
2868f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
2869f4a2713aSLionel Sambuc const DeclarationNameInfo &NameInfo,
2870f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs) {
2871f4a2713aSLionel Sambuc
2872f4a2713aSLionel Sambuc assert(TemplateArgs || TemplateKWLoc.isValid());
2873f4a2713aSLionel Sambuc DeclContext *DC;
2874f4a2713aSLionel Sambuc if (!(DC = computeDeclContext(SS, false)) ||
2875f4a2713aSLionel Sambuc DC->isDependentContext() ||
2876f4a2713aSLionel Sambuc RequireCompleteDeclContext(SS, DC))
2877f4a2713aSLionel Sambuc return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
2878f4a2713aSLionel Sambuc
2879f4a2713aSLionel Sambuc bool MemberOfUnknownSpecialization;
2880f4a2713aSLionel Sambuc LookupResult R(*this, NameInfo, LookupOrdinaryName);
2881*0a6a1f1dSLionel Sambuc LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false,
2882f4a2713aSLionel Sambuc MemberOfUnknownSpecialization);
2883f4a2713aSLionel Sambuc
2884f4a2713aSLionel Sambuc if (R.isAmbiguous())
2885f4a2713aSLionel Sambuc return ExprError();
2886f4a2713aSLionel Sambuc
2887f4a2713aSLionel Sambuc if (R.empty()) {
2888f4a2713aSLionel Sambuc Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2889f4a2713aSLionel Sambuc << NameInfo.getName() << SS.getRange();
2890f4a2713aSLionel Sambuc return ExprError();
2891f4a2713aSLionel Sambuc }
2892f4a2713aSLionel Sambuc
2893f4a2713aSLionel Sambuc if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
2894f4a2713aSLionel Sambuc Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2895*0a6a1f1dSLionel Sambuc << SS.getScopeRep()
2896*0a6a1f1dSLionel Sambuc << NameInfo.getName().getAsString() << SS.getRange();
2897f4a2713aSLionel Sambuc Diag(Temp->getLocation(), diag::note_referenced_class_template);
2898f4a2713aSLionel Sambuc return ExprError();
2899f4a2713aSLionel Sambuc }
2900f4a2713aSLionel Sambuc
2901f4a2713aSLionel Sambuc return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs);
2902f4a2713aSLionel Sambuc }
2903f4a2713aSLionel Sambuc
2904f4a2713aSLionel Sambuc /// \brief Form a dependent template name.
2905f4a2713aSLionel Sambuc ///
2906f4a2713aSLionel Sambuc /// This action forms a dependent template name given the template
2907f4a2713aSLionel Sambuc /// name and its (presumably dependent) scope specifier. For
2908f4a2713aSLionel Sambuc /// example, given "MetaFun::template apply", the scope specifier \p
2909f4a2713aSLionel Sambuc /// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2910f4a2713aSLionel Sambuc /// of the "template" keyword, and "apply" is the \p Name.
ActOnDependentTemplateName(Scope * S,CXXScopeSpec & SS,SourceLocation TemplateKWLoc,UnqualifiedId & Name,ParsedType ObjectType,bool EnteringContext,TemplateTy & Result)2911f4a2713aSLionel Sambuc TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
2912f4a2713aSLionel Sambuc CXXScopeSpec &SS,
2913f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
2914f4a2713aSLionel Sambuc UnqualifiedId &Name,
2915f4a2713aSLionel Sambuc ParsedType ObjectType,
2916f4a2713aSLionel Sambuc bool EnteringContext,
2917f4a2713aSLionel Sambuc TemplateTy &Result) {
2918f4a2713aSLionel Sambuc if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2919f4a2713aSLionel Sambuc Diag(TemplateKWLoc,
2920f4a2713aSLionel Sambuc getLangOpts().CPlusPlus11 ?
2921f4a2713aSLionel Sambuc diag::warn_cxx98_compat_template_outside_of_template :
2922f4a2713aSLionel Sambuc diag::ext_template_outside_of_template)
2923f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(TemplateKWLoc);
2924f4a2713aSLionel Sambuc
2925*0a6a1f1dSLionel Sambuc DeclContext *LookupCtx = nullptr;
2926f4a2713aSLionel Sambuc if (SS.isSet())
2927f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(SS, EnteringContext);
2928f4a2713aSLionel Sambuc if (!LookupCtx && ObjectType)
2929f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(ObjectType.get());
2930f4a2713aSLionel Sambuc if (LookupCtx) {
2931f4a2713aSLionel Sambuc // C++0x [temp.names]p5:
2932f4a2713aSLionel Sambuc // If a name prefixed by the keyword template is not the name of
2933f4a2713aSLionel Sambuc // a template, the program is ill-formed. [Note: the keyword
2934f4a2713aSLionel Sambuc // template may not be applied to non-template members of class
2935f4a2713aSLionel Sambuc // templates. -end note ] [ Note: as is the case with the
2936f4a2713aSLionel Sambuc // typename prefix, the template prefix is allowed in cases
2937f4a2713aSLionel Sambuc // where it is not strictly necessary; i.e., when the
2938f4a2713aSLionel Sambuc // nested-name-specifier or the expression on the left of the ->
2939f4a2713aSLionel Sambuc // or . is not dependent on a template-parameter, or the use
2940f4a2713aSLionel Sambuc // does not appear in the scope of a template. -end note]
2941f4a2713aSLionel Sambuc //
2942f4a2713aSLionel Sambuc // Note: C++03 was more strict here, because it banned the use of
2943f4a2713aSLionel Sambuc // the "template" keyword prior to a template-name that was not a
2944f4a2713aSLionel Sambuc // dependent name. C++ DR468 relaxed this requirement (the
2945f4a2713aSLionel Sambuc // "template" keyword is now permitted). We follow the C++0x
2946f4a2713aSLionel Sambuc // rules, even in C++03 mode with a warning, retroactively applying the DR.
2947f4a2713aSLionel Sambuc bool MemberOfUnknownSpecialization;
2948f4a2713aSLionel Sambuc TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name,
2949f4a2713aSLionel Sambuc ObjectType, EnteringContext, Result,
2950f4a2713aSLionel Sambuc MemberOfUnknownSpecialization);
2951f4a2713aSLionel Sambuc if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2952f4a2713aSLionel Sambuc isa<CXXRecordDecl>(LookupCtx) &&
2953f4a2713aSLionel Sambuc (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2954f4a2713aSLionel Sambuc cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
2955f4a2713aSLionel Sambuc // This is a dependent template. Handle it below.
2956f4a2713aSLionel Sambuc } else if (TNK == TNK_Non_template) {
2957f4a2713aSLionel Sambuc Diag(Name.getLocStart(),
2958f4a2713aSLionel Sambuc diag::err_template_kw_refers_to_non_template)
2959f4a2713aSLionel Sambuc << GetNameFromUnqualifiedId(Name).getName()
2960f4a2713aSLionel Sambuc << Name.getSourceRange()
2961f4a2713aSLionel Sambuc << TemplateKWLoc;
2962f4a2713aSLionel Sambuc return TNK_Non_template;
2963f4a2713aSLionel Sambuc } else {
2964f4a2713aSLionel Sambuc // We found something; return it.
2965f4a2713aSLionel Sambuc return TNK;
2966f4a2713aSLionel Sambuc }
2967f4a2713aSLionel Sambuc }
2968f4a2713aSLionel Sambuc
2969*0a6a1f1dSLionel Sambuc NestedNameSpecifier *Qualifier = SS.getScopeRep();
2970f4a2713aSLionel Sambuc
2971f4a2713aSLionel Sambuc switch (Name.getKind()) {
2972f4a2713aSLionel Sambuc case UnqualifiedId::IK_Identifier:
2973f4a2713aSLionel Sambuc Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
2974f4a2713aSLionel Sambuc Name.Identifier));
2975f4a2713aSLionel Sambuc return TNK_Dependent_template_name;
2976f4a2713aSLionel Sambuc
2977f4a2713aSLionel Sambuc case UnqualifiedId::IK_OperatorFunctionId:
2978f4a2713aSLionel Sambuc Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
2979f4a2713aSLionel Sambuc Name.OperatorFunctionId.Operator));
2980*0a6a1f1dSLionel Sambuc return TNK_Function_template;
2981f4a2713aSLionel Sambuc
2982f4a2713aSLionel Sambuc case UnqualifiedId::IK_LiteralOperatorId:
2983*0a6a1f1dSLionel Sambuc llvm_unreachable("literal operator id cannot have a dependent scope");
2984f4a2713aSLionel Sambuc
2985f4a2713aSLionel Sambuc default:
2986f4a2713aSLionel Sambuc break;
2987f4a2713aSLionel Sambuc }
2988f4a2713aSLionel Sambuc
2989f4a2713aSLionel Sambuc Diag(Name.getLocStart(),
2990f4a2713aSLionel Sambuc diag::err_template_kw_refers_to_non_template)
2991f4a2713aSLionel Sambuc << GetNameFromUnqualifiedId(Name).getName()
2992f4a2713aSLionel Sambuc << Name.getSourceRange()
2993f4a2713aSLionel Sambuc << TemplateKWLoc;
2994f4a2713aSLionel Sambuc return TNK_Non_template;
2995f4a2713aSLionel Sambuc }
2996f4a2713aSLionel Sambuc
CheckTemplateTypeArgument(TemplateTypeParmDecl * Param,TemplateArgumentLoc & AL,SmallVectorImpl<TemplateArgument> & Converted)2997f4a2713aSLionel Sambuc bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
2998*0a6a1f1dSLionel Sambuc TemplateArgumentLoc &AL,
2999f4a2713aSLionel Sambuc SmallVectorImpl<TemplateArgument> &Converted) {
3000f4a2713aSLionel Sambuc const TemplateArgument &Arg = AL.getArgument();
3001*0a6a1f1dSLionel Sambuc QualType ArgType;
3002*0a6a1f1dSLionel Sambuc TypeSourceInfo *TSI = nullptr;
3003f4a2713aSLionel Sambuc
3004f4a2713aSLionel Sambuc // Check template type parameter.
3005f4a2713aSLionel Sambuc switch(Arg.getKind()) {
3006f4a2713aSLionel Sambuc case TemplateArgument::Type:
3007f4a2713aSLionel Sambuc // C++ [temp.arg.type]p1:
3008f4a2713aSLionel Sambuc // A template-argument for a template-parameter which is a
3009f4a2713aSLionel Sambuc // type shall be a type-id.
3010*0a6a1f1dSLionel Sambuc ArgType = Arg.getAsType();
3011*0a6a1f1dSLionel Sambuc TSI = AL.getTypeSourceInfo();
3012f4a2713aSLionel Sambuc break;
3013f4a2713aSLionel Sambuc case TemplateArgument::Template: {
3014f4a2713aSLionel Sambuc // We have a template type parameter but the template argument
3015f4a2713aSLionel Sambuc // is a template without any arguments.
3016f4a2713aSLionel Sambuc SourceRange SR = AL.getSourceRange();
3017f4a2713aSLionel Sambuc TemplateName Name = Arg.getAsTemplate();
3018f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_template_missing_args)
3019f4a2713aSLionel Sambuc << Name << SR;
3020f4a2713aSLionel Sambuc if (TemplateDecl *Decl = Name.getAsTemplateDecl())
3021f4a2713aSLionel Sambuc Diag(Decl->getLocation(), diag::note_template_decl_here);
3022f4a2713aSLionel Sambuc
3023f4a2713aSLionel Sambuc return true;
3024f4a2713aSLionel Sambuc }
3025f4a2713aSLionel Sambuc case TemplateArgument::Expression: {
3026f4a2713aSLionel Sambuc // We have a template type parameter but the template argument is an
3027f4a2713aSLionel Sambuc // expression; see if maybe it is missing the "typename" keyword.
3028f4a2713aSLionel Sambuc CXXScopeSpec SS;
3029f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo;
3030f4a2713aSLionel Sambuc
3031f4a2713aSLionel Sambuc if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) {
3032f4a2713aSLionel Sambuc SS.Adopt(ArgExpr->getQualifierLoc());
3033f4a2713aSLionel Sambuc NameInfo = ArgExpr->getNameInfo();
3034f4a2713aSLionel Sambuc } else if (DependentScopeDeclRefExpr *ArgExpr =
3035f4a2713aSLionel Sambuc dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) {
3036f4a2713aSLionel Sambuc SS.Adopt(ArgExpr->getQualifierLoc());
3037f4a2713aSLionel Sambuc NameInfo = ArgExpr->getNameInfo();
3038f4a2713aSLionel Sambuc } else if (CXXDependentScopeMemberExpr *ArgExpr =
3039f4a2713aSLionel Sambuc dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) {
3040f4a2713aSLionel Sambuc if (ArgExpr->isImplicitAccess()) {
3041f4a2713aSLionel Sambuc SS.Adopt(ArgExpr->getQualifierLoc());
3042f4a2713aSLionel Sambuc NameInfo = ArgExpr->getMemberNameInfo();
3043f4a2713aSLionel Sambuc }
3044f4a2713aSLionel Sambuc }
3045f4a2713aSLionel Sambuc
3046*0a6a1f1dSLionel Sambuc if (auto *II = NameInfo.getName().getAsIdentifierInfo()) {
3047f4a2713aSLionel Sambuc LookupResult Result(*this, NameInfo, LookupOrdinaryName);
3048f4a2713aSLionel Sambuc LookupParsedName(Result, CurScope, &SS);
3049f4a2713aSLionel Sambuc
3050f4a2713aSLionel Sambuc if (Result.getAsSingle<TypeDecl>() ||
3051f4a2713aSLionel Sambuc Result.getResultKind() ==
3052f4a2713aSLionel Sambuc LookupResult::NotFoundInCurrentInstantiation) {
3053*0a6a1f1dSLionel Sambuc // Suggest that the user add 'typename' before the NNS.
3054f4a2713aSLionel Sambuc SourceLocation Loc = AL.getSourceRange().getBegin();
3055*0a6a1f1dSLionel Sambuc Diag(Loc, getLangOpts().MSVCCompat
3056*0a6a1f1dSLionel Sambuc ? diag::ext_ms_template_type_arg_missing_typename
3057*0a6a1f1dSLionel Sambuc : diag::err_template_arg_must_be_type_suggest)
3058*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(Loc, "typename ");
3059f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
3060*0a6a1f1dSLionel Sambuc
3061*0a6a1f1dSLionel Sambuc // Recover by synthesizing a type using the location information that we
3062*0a6a1f1dSLionel Sambuc // already have.
3063*0a6a1f1dSLionel Sambuc ArgType =
3064*0a6a1f1dSLionel Sambuc Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II);
3065*0a6a1f1dSLionel Sambuc TypeLocBuilder TLB;
3066*0a6a1f1dSLionel Sambuc DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType);
3067*0a6a1f1dSLionel Sambuc TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/));
3068*0a6a1f1dSLionel Sambuc TL.setQualifierLoc(SS.getWithLocInContext(Context));
3069*0a6a1f1dSLionel Sambuc TL.setNameLoc(NameInfo.getLoc());
3070*0a6a1f1dSLionel Sambuc TSI = TLB.getTypeSourceInfo(Context, ArgType);
3071*0a6a1f1dSLionel Sambuc
3072*0a6a1f1dSLionel Sambuc // Overwrite our input TemplateArgumentLoc so that we can recover
3073*0a6a1f1dSLionel Sambuc // properly.
3074*0a6a1f1dSLionel Sambuc AL = TemplateArgumentLoc(TemplateArgument(ArgType),
3075*0a6a1f1dSLionel Sambuc TemplateArgumentLocInfo(TSI));
3076*0a6a1f1dSLionel Sambuc
3077*0a6a1f1dSLionel Sambuc break;
3078f4a2713aSLionel Sambuc }
3079f4a2713aSLionel Sambuc }
3080f4a2713aSLionel Sambuc // fallthrough
3081f4a2713aSLionel Sambuc }
3082f4a2713aSLionel Sambuc default: {
3083f4a2713aSLionel Sambuc // We have a template type parameter but the template argument
3084f4a2713aSLionel Sambuc // is not a type.
3085f4a2713aSLionel Sambuc SourceRange SR = AL.getSourceRange();
3086f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
3087f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
3088f4a2713aSLionel Sambuc
3089f4a2713aSLionel Sambuc return true;
3090f4a2713aSLionel Sambuc }
3091f4a2713aSLionel Sambuc }
3092f4a2713aSLionel Sambuc
3093*0a6a1f1dSLionel Sambuc if (CheckTemplateArgument(Param, TSI))
3094f4a2713aSLionel Sambuc return true;
3095f4a2713aSLionel Sambuc
3096f4a2713aSLionel Sambuc // Add the converted template type argument.
3097*0a6a1f1dSLionel Sambuc ArgType = Context.getCanonicalType(ArgType);
3098f4a2713aSLionel Sambuc
3099f4a2713aSLionel Sambuc // Objective-C ARC:
3100f4a2713aSLionel Sambuc // If an explicitly-specified template argument type is a lifetime type
3101f4a2713aSLionel Sambuc // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
3102f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount &&
3103f4a2713aSLionel Sambuc ArgType->isObjCLifetimeType() &&
3104f4a2713aSLionel Sambuc !ArgType.getObjCLifetime()) {
3105f4a2713aSLionel Sambuc Qualifiers Qs;
3106f4a2713aSLionel Sambuc Qs.setObjCLifetime(Qualifiers::OCL_Strong);
3107f4a2713aSLionel Sambuc ArgType = Context.getQualifiedType(ArgType, Qs);
3108f4a2713aSLionel Sambuc }
3109f4a2713aSLionel Sambuc
3110f4a2713aSLionel Sambuc Converted.push_back(TemplateArgument(ArgType));
3111f4a2713aSLionel Sambuc return false;
3112f4a2713aSLionel Sambuc }
3113f4a2713aSLionel Sambuc
3114f4a2713aSLionel Sambuc /// \brief Substitute template arguments into the default template argument for
3115f4a2713aSLionel Sambuc /// the given template type parameter.
3116f4a2713aSLionel Sambuc ///
3117f4a2713aSLionel Sambuc /// \param SemaRef the semantic analysis object for which we are performing
3118f4a2713aSLionel Sambuc /// the substitution.
3119f4a2713aSLionel Sambuc ///
3120f4a2713aSLionel Sambuc /// \param Template the template that we are synthesizing template arguments
3121f4a2713aSLionel Sambuc /// for.
3122f4a2713aSLionel Sambuc ///
3123f4a2713aSLionel Sambuc /// \param TemplateLoc the location of the template name that started the
3124f4a2713aSLionel Sambuc /// template-id we are checking.
3125f4a2713aSLionel Sambuc ///
3126f4a2713aSLionel Sambuc /// \param RAngleLoc the location of the right angle bracket ('>') that
3127f4a2713aSLionel Sambuc /// terminates the template-id.
3128f4a2713aSLionel Sambuc ///
3129f4a2713aSLionel Sambuc /// \param Param the template template parameter whose default we are
3130f4a2713aSLionel Sambuc /// substituting into.
3131f4a2713aSLionel Sambuc ///
3132f4a2713aSLionel Sambuc /// \param Converted the list of template arguments provided for template
3133f4a2713aSLionel Sambuc /// parameters that precede \p Param in the template parameter list.
3134f4a2713aSLionel Sambuc /// \returns the substituted template argument, or NULL if an error occurred.
3135f4a2713aSLionel Sambuc static TypeSourceInfo *
SubstDefaultTemplateArgument(Sema & SemaRef,TemplateDecl * Template,SourceLocation TemplateLoc,SourceLocation RAngleLoc,TemplateTypeParmDecl * Param,SmallVectorImpl<TemplateArgument> & Converted)3136f4a2713aSLionel Sambuc SubstDefaultTemplateArgument(Sema &SemaRef,
3137f4a2713aSLionel Sambuc TemplateDecl *Template,
3138f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
3139f4a2713aSLionel Sambuc SourceLocation RAngleLoc,
3140f4a2713aSLionel Sambuc TemplateTypeParmDecl *Param,
3141f4a2713aSLionel Sambuc SmallVectorImpl<TemplateArgument> &Converted) {
3142f4a2713aSLionel Sambuc TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
3143f4a2713aSLionel Sambuc
3144f4a2713aSLionel Sambuc // If the argument type is dependent, instantiate it now based
3145f4a2713aSLionel Sambuc // on the previously-computed template arguments.
3146f4a2713aSLionel Sambuc if (ArgType->getType()->isDependentType()) {
3147f4a2713aSLionel Sambuc Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
3148f4a2713aSLionel Sambuc Template, Converted,
3149f4a2713aSLionel Sambuc SourceRange(TemplateLoc, RAngleLoc));
3150f4a2713aSLionel Sambuc if (Inst.isInvalid())
3151*0a6a1f1dSLionel Sambuc return nullptr;
3152f4a2713aSLionel Sambuc
3153f4a2713aSLionel Sambuc TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
3154f4a2713aSLionel Sambuc Converted.data(), Converted.size());
3155f4a2713aSLionel Sambuc
3156f4a2713aSLionel Sambuc // Only substitute for the innermost template argument list.
3157f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList TemplateArgLists;
3158f4a2713aSLionel Sambuc TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
3159f4a2713aSLionel Sambuc for (unsigned i = 0, e = Param->getDepth(); i != e; ++i)
3160f4a2713aSLionel Sambuc TemplateArgLists.addOuterTemplateArguments(None);
3161f4a2713aSLionel Sambuc
3162f4a2713aSLionel Sambuc Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext());
3163f4a2713aSLionel Sambuc ArgType =
3164f4a2713aSLionel Sambuc SemaRef.SubstType(ArgType, TemplateArgLists,
3165f4a2713aSLionel Sambuc Param->getDefaultArgumentLoc(), Param->getDeclName());
3166f4a2713aSLionel Sambuc }
3167f4a2713aSLionel Sambuc
3168f4a2713aSLionel Sambuc return ArgType;
3169f4a2713aSLionel Sambuc }
3170f4a2713aSLionel Sambuc
3171f4a2713aSLionel Sambuc /// \brief Substitute template arguments into the default template argument for
3172f4a2713aSLionel Sambuc /// the given non-type template parameter.
3173f4a2713aSLionel Sambuc ///
3174f4a2713aSLionel Sambuc /// \param SemaRef the semantic analysis object for which we are performing
3175f4a2713aSLionel Sambuc /// the substitution.
3176f4a2713aSLionel Sambuc ///
3177f4a2713aSLionel Sambuc /// \param Template the template that we are synthesizing template arguments
3178f4a2713aSLionel Sambuc /// for.
3179f4a2713aSLionel Sambuc ///
3180f4a2713aSLionel Sambuc /// \param TemplateLoc the location of the template name that started the
3181f4a2713aSLionel Sambuc /// template-id we are checking.
3182f4a2713aSLionel Sambuc ///
3183f4a2713aSLionel Sambuc /// \param RAngleLoc the location of the right angle bracket ('>') that
3184f4a2713aSLionel Sambuc /// terminates the template-id.
3185f4a2713aSLionel Sambuc ///
3186f4a2713aSLionel Sambuc /// \param Param the non-type template parameter whose default we are
3187f4a2713aSLionel Sambuc /// substituting into.
3188f4a2713aSLionel Sambuc ///
3189f4a2713aSLionel Sambuc /// \param Converted the list of template arguments provided for template
3190f4a2713aSLionel Sambuc /// parameters that precede \p Param in the template parameter list.
3191f4a2713aSLionel Sambuc ///
3192f4a2713aSLionel Sambuc /// \returns the substituted template argument, or NULL if an error occurred.
3193f4a2713aSLionel Sambuc static ExprResult
SubstDefaultTemplateArgument(Sema & SemaRef,TemplateDecl * Template,SourceLocation TemplateLoc,SourceLocation RAngleLoc,NonTypeTemplateParmDecl * Param,SmallVectorImpl<TemplateArgument> & Converted)3194f4a2713aSLionel Sambuc SubstDefaultTemplateArgument(Sema &SemaRef,
3195f4a2713aSLionel Sambuc TemplateDecl *Template,
3196f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
3197f4a2713aSLionel Sambuc SourceLocation RAngleLoc,
3198f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *Param,
3199f4a2713aSLionel Sambuc SmallVectorImpl<TemplateArgument> &Converted) {
3200f4a2713aSLionel Sambuc Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
3201f4a2713aSLionel Sambuc Template, Converted,
3202f4a2713aSLionel Sambuc SourceRange(TemplateLoc, RAngleLoc));
3203f4a2713aSLionel Sambuc if (Inst.isInvalid())
3204f4a2713aSLionel Sambuc return ExprError();
3205f4a2713aSLionel Sambuc
3206f4a2713aSLionel Sambuc TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
3207f4a2713aSLionel Sambuc Converted.data(), Converted.size());
3208f4a2713aSLionel Sambuc
3209f4a2713aSLionel Sambuc // Only substitute for the innermost template argument list.
3210f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList TemplateArgLists;
3211f4a2713aSLionel Sambuc TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
3212f4a2713aSLionel Sambuc for (unsigned i = 0, e = Param->getDepth(); i != e; ++i)
3213f4a2713aSLionel Sambuc TemplateArgLists.addOuterTemplateArguments(None);
3214f4a2713aSLionel Sambuc
3215f4a2713aSLionel Sambuc Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext());
3216f4a2713aSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
3217f4a2713aSLionel Sambuc return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists);
3218f4a2713aSLionel Sambuc }
3219f4a2713aSLionel Sambuc
3220f4a2713aSLionel Sambuc /// \brief Substitute template arguments into the default template argument for
3221f4a2713aSLionel Sambuc /// the given template template parameter.
3222f4a2713aSLionel Sambuc ///
3223f4a2713aSLionel Sambuc /// \param SemaRef the semantic analysis object for which we are performing
3224f4a2713aSLionel Sambuc /// the substitution.
3225f4a2713aSLionel Sambuc ///
3226f4a2713aSLionel Sambuc /// \param Template the template that we are synthesizing template arguments
3227f4a2713aSLionel Sambuc /// for.
3228f4a2713aSLionel Sambuc ///
3229f4a2713aSLionel Sambuc /// \param TemplateLoc the location of the template name that started the
3230f4a2713aSLionel Sambuc /// template-id we are checking.
3231f4a2713aSLionel Sambuc ///
3232f4a2713aSLionel Sambuc /// \param RAngleLoc the location of the right angle bracket ('>') that
3233f4a2713aSLionel Sambuc /// terminates the template-id.
3234f4a2713aSLionel Sambuc ///
3235f4a2713aSLionel Sambuc /// \param Param the template template parameter whose default we are
3236f4a2713aSLionel Sambuc /// substituting into.
3237f4a2713aSLionel Sambuc ///
3238f4a2713aSLionel Sambuc /// \param Converted the list of template arguments provided for template
3239f4a2713aSLionel Sambuc /// parameters that precede \p Param in the template parameter list.
3240f4a2713aSLionel Sambuc ///
3241f4a2713aSLionel Sambuc /// \param QualifierLoc Will be set to the nested-name-specifier (with
3242f4a2713aSLionel Sambuc /// source-location information) that precedes the template name.
3243f4a2713aSLionel Sambuc ///
3244f4a2713aSLionel Sambuc /// \returns the substituted template argument, or NULL if an error occurred.
3245f4a2713aSLionel Sambuc static TemplateName
SubstDefaultTemplateArgument(Sema & SemaRef,TemplateDecl * Template,SourceLocation TemplateLoc,SourceLocation RAngleLoc,TemplateTemplateParmDecl * Param,SmallVectorImpl<TemplateArgument> & Converted,NestedNameSpecifierLoc & QualifierLoc)3246f4a2713aSLionel Sambuc SubstDefaultTemplateArgument(Sema &SemaRef,
3247f4a2713aSLionel Sambuc TemplateDecl *Template,
3248f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
3249f4a2713aSLionel Sambuc SourceLocation RAngleLoc,
3250f4a2713aSLionel Sambuc TemplateTemplateParmDecl *Param,
3251f4a2713aSLionel Sambuc SmallVectorImpl<TemplateArgument> &Converted,
3252f4a2713aSLionel Sambuc NestedNameSpecifierLoc &QualifierLoc) {
3253f4a2713aSLionel Sambuc Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted,
3254f4a2713aSLionel Sambuc SourceRange(TemplateLoc, RAngleLoc));
3255f4a2713aSLionel Sambuc if (Inst.isInvalid())
3256f4a2713aSLionel Sambuc return TemplateName();
3257f4a2713aSLionel Sambuc
3258f4a2713aSLionel Sambuc TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
3259f4a2713aSLionel Sambuc Converted.data(), Converted.size());
3260f4a2713aSLionel Sambuc
3261f4a2713aSLionel Sambuc // Only substitute for the innermost template argument list.
3262f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList TemplateArgLists;
3263f4a2713aSLionel Sambuc TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
3264f4a2713aSLionel Sambuc for (unsigned i = 0, e = Param->getDepth(); i != e; ++i)
3265f4a2713aSLionel Sambuc TemplateArgLists.addOuterTemplateArguments(None);
3266f4a2713aSLionel Sambuc
3267f4a2713aSLionel Sambuc Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext());
3268f4a2713aSLionel Sambuc // Substitute into the nested-name-specifier first,
3269f4a2713aSLionel Sambuc QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
3270f4a2713aSLionel Sambuc if (QualifierLoc) {
3271f4a2713aSLionel Sambuc QualifierLoc =
3272f4a2713aSLionel Sambuc SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists);
3273f4a2713aSLionel Sambuc if (!QualifierLoc)
3274f4a2713aSLionel Sambuc return TemplateName();
3275f4a2713aSLionel Sambuc }
3276f4a2713aSLionel Sambuc
3277f4a2713aSLionel Sambuc return SemaRef.SubstTemplateName(
3278f4a2713aSLionel Sambuc QualifierLoc,
3279f4a2713aSLionel Sambuc Param->getDefaultArgument().getArgument().getAsTemplate(),
3280f4a2713aSLionel Sambuc Param->getDefaultArgument().getTemplateNameLoc(),
3281f4a2713aSLionel Sambuc TemplateArgLists);
3282f4a2713aSLionel Sambuc }
3283f4a2713aSLionel Sambuc
3284f4a2713aSLionel Sambuc /// \brief If the given template parameter has a default template
3285f4a2713aSLionel Sambuc /// argument, substitute into that default template argument and
3286f4a2713aSLionel Sambuc /// return the corresponding template argument.
3287f4a2713aSLionel Sambuc TemplateArgumentLoc
SubstDefaultTemplateArgumentIfAvailable(TemplateDecl * Template,SourceLocation TemplateLoc,SourceLocation RAngleLoc,Decl * Param,SmallVectorImpl<TemplateArgument> & Converted,bool & HasDefaultArg)3288f4a2713aSLionel Sambuc Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
3289f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
3290f4a2713aSLionel Sambuc SourceLocation RAngleLoc,
3291f4a2713aSLionel Sambuc Decl *Param,
3292f4a2713aSLionel Sambuc SmallVectorImpl<TemplateArgument>
3293f4a2713aSLionel Sambuc &Converted,
3294f4a2713aSLionel Sambuc bool &HasDefaultArg) {
3295f4a2713aSLionel Sambuc HasDefaultArg = false;
3296f4a2713aSLionel Sambuc
3297f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
3298f4a2713aSLionel Sambuc if (!TypeParm->hasDefaultArgument())
3299f4a2713aSLionel Sambuc return TemplateArgumentLoc();
3300f4a2713aSLionel Sambuc
3301f4a2713aSLionel Sambuc HasDefaultArg = true;
3302f4a2713aSLionel Sambuc TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
3303f4a2713aSLionel Sambuc TemplateLoc,
3304f4a2713aSLionel Sambuc RAngleLoc,
3305f4a2713aSLionel Sambuc TypeParm,
3306f4a2713aSLionel Sambuc Converted);
3307f4a2713aSLionel Sambuc if (DI)
3308f4a2713aSLionel Sambuc return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
3309f4a2713aSLionel Sambuc
3310f4a2713aSLionel Sambuc return TemplateArgumentLoc();
3311f4a2713aSLionel Sambuc }
3312f4a2713aSLionel Sambuc
3313f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *NonTypeParm
3314f4a2713aSLionel Sambuc = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
3315f4a2713aSLionel Sambuc if (!NonTypeParm->hasDefaultArgument())
3316f4a2713aSLionel Sambuc return TemplateArgumentLoc();
3317f4a2713aSLionel Sambuc
3318f4a2713aSLionel Sambuc HasDefaultArg = true;
3319f4a2713aSLionel Sambuc ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
3320f4a2713aSLionel Sambuc TemplateLoc,
3321f4a2713aSLionel Sambuc RAngleLoc,
3322f4a2713aSLionel Sambuc NonTypeParm,
3323f4a2713aSLionel Sambuc Converted);
3324f4a2713aSLionel Sambuc if (Arg.isInvalid())
3325f4a2713aSLionel Sambuc return TemplateArgumentLoc();
3326f4a2713aSLionel Sambuc
3327*0a6a1f1dSLionel Sambuc Expr *ArgE = Arg.getAs<Expr>();
3328f4a2713aSLionel Sambuc return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
3329f4a2713aSLionel Sambuc }
3330f4a2713aSLionel Sambuc
3331f4a2713aSLionel Sambuc TemplateTemplateParmDecl *TempTempParm
3332f4a2713aSLionel Sambuc = cast<TemplateTemplateParmDecl>(Param);
3333f4a2713aSLionel Sambuc if (!TempTempParm->hasDefaultArgument())
3334f4a2713aSLionel Sambuc return TemplateArgumentLoc();
3335f4a2713aSLionel Sambuc
3336f4a2713aSLionel Sambuc HasDefaultArg = true;
3337f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc;
3338f4a2713aSLionel Sambuc TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
3339f4a2713aSLionel Sambuc TemplateLoc,
3340f4a2713aSLionel Sambuc RAngleLoc,
3341f4a2713aSLionel Sambuc TempTempParm,
3342f4a2713aSLionel Sambuc Converted,
3343f4a2713aSLionel Sambuc QualifierLoc);
3344f4a2713aSLionel Sambuc if (TName.isNull())
3345f4a2713aSLionel Sambuc return TemplateArgumentLoc();
3346f4a2713aSLionel Sambuc
3347f4a2713aSLionel Sambuc return TemplateArgumentLoc(TemplateArgument(TName),
3348f4a2713aSLionel Sambuc TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
3349f4a2713aSLionel Sambuc TempTempParm->getDefaultArgument().getTemplateNameLoc());
3350f4a2713aSLionel Sambuc }
3351f4a2713aSLionel Sambuc
3352f4a2713aSLionel Sambuc /// \brief Check that the given template argument corresponds to the given
3353f4a2713aSLionel Sambuc /// template parameter.
3354f4a2713aSLionel Sambuc ///
3355f4a2713aSLionel Sambuc /// \param Param The template parameter against which the argument will be
3356f4a2713aSLionel Sambuc /// checked.
3357f4a2713aSLionel Sambuc ///
3358*0a6a1f1dSLionel Sambuc /// \param Arg The template argument, which may be updated due to conversions.
3359f4a2713aSLionel Sambuc ///
3360f4a2713aSLionel Sambuc /// \param Template The template in which the template argument resides.
3361f4a2713aSLionel Sambuc ///
3362f4a2713aSLionel Sambuc /// \param TemplateLoc The location of the template name for the template
3363f4a2713aSLionel Sambuc /// whose argument list we're matching.
3364f4a2713aSLionel Sambuc ///
3365f4a2713aSLionel Sambuc /// \param RAngleLoc The location of the right angle bracket ('>') that closes
3366f4a2713aSLionel Sambuc /// the template argument list.
3367f4a2713aSLionel Sambuc ///
3368f4a2713aSLionel Sambuc /// \param ArgumentPackIndex The index into the argument pack where this
3369f4a2713aSLionel Sambuc /// argument will be placed. Only valid if the parameter is a parameter pack.
3370f4a2713aSLionel Sambuc ///
3371f4a2713aSLionel Sambuc /// \param Converted The checked, converted argument will be added to the
3372f4a2713aSLionel Sambuc /// end of this small vector.
3373f4a2713aSLionel Sambuc ///
3374f4a2713aSLionel Sambuc /// \param CTAK Describes how we arrived at this particular template argument:
3375f4a2713aSLionel Sambuc /// explicitly written, deduced, etc.
3376f4a2713aSLionel Sambuc ///
3377f4a2713aSLionel Sambuc /// \returns true on error, false otherwise.
CheckTemplateArgument(NamedDecl * Param,TemplateArgumentLoc & Arg,NamedDecl * Template,SourceLocation TemplateLoc,SourceLocation RAngleLoc,unsigned ArgumentPackIndex,SmallVectorImpl<TemplateArgument> & Converted,CheckTemplateArgumentKind CTAK)3378f4a2713aSLionel Sambuc bool Sema::CheckTemplateArgument(NamedDecl *Param,
3379*0a6a1f1dSLionel Sambuc TemplateArgumentLoc &Arg,
3380f4a2713aSLionel Sambuc NamedDecl *Template,
3381f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
3382f4a2713aSLionel Sambuc SourceLocation RAngleLoc,
3383f4a2713aSLionel Sambuc unsigned ArgumentPackIndex,
3384f4a2713aSLionel Sambuc SmallVectorImpl<TemplateArgument> &Converted,
3385f4a2713aSLionel Sambuc CheckTemplateArgumentKind CTAK) {
3386f4a2713aSLionel Sambuc // Check template type parameters.
3387f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
3388f4a2713aSLionel Sambuc return CheckTemplateTypeArgument(TTP, Arg, Converted);
3389f4a2713aSLionel Sambuc
3390f4a2713aSLionel Sambuc // Check non-type template parameters.
3391f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
3392f4a2713aSLionel Sambuc // Do substitution on the type of the non-type template parameter
3393f4a2713aSLionel Sambuc // with the template arguments we've seen thus far. But if the
3394f4a2713aSLionel Sambuc // template has a dependent context then we cannot substitute yet.
3395f4a2713aSLionel Sambuc QualType NTTPType = NTTP->getType();
3396f4a2713aSLionel Sambuc if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
3397f4a2713aSLionel Sambuc NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
3398f4a2713aSLionel Sambuc
3399f4a2713aSLionel Sambuc if (NTTPType->isDependentType() &&
3400f4a2713aSLionel Sambuc !isa<TemplateTemplateParmDecl>(Template) &&
3401f4a2713aSLionel Sambuc !Template->getDeclContext()->isDependentContext()) {
3402f4a2713aSLionel Sambuc // Do substitution on the type of the non-type template parameter.
3403f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, TemplateLoc, Template,
3404f4a2713aSLionel Sambuc NTTP, Converted,
3405f4a2713aSLionel Sambuc SourceRange(TemplateLoc, RAngleLoc));
3406f4a2713aSLionel Sambuc if (Inst.isInvalid())
3407f4a2713aSLionel Sambuc return true;
3408f4a2713aSLionel Sambuc
3409f4a2713aSLionel Sambuc TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
3410f4a2713aSLionel Sambuc Converted.data(), Converted.size());
3411f4a2713aSLionel Sambuc NTTPType = SubstType(NTTPType,
3412f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList(TemplateArgs),
3413f4a2713aSLionel Sambuc NTTP->getLocation(),
3414f4a2713aSLionel Sambuc NTTP->getDeclName());
3415f4a2713aSLionel Sambuc // If that worked, check the non-type template parameter type
3416f4a2713aSLionel Sambuc // for validity.
3417f4a2713aSLionel Sambuc if (!NTTPType.isNull())
3418f4a2713aSLionel Sambuc NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
3419f4a2713aSLionel Sambuc NTTP->getLocation());
3420f4a2713aSLionel Sambuc if (NTTPType.isNull())
3421f4a2713aSLionel Sambuc return true;
3422f4a2713aSLionel Sambuc }
3423f4a2713aSLionel Sambuc
3424f4a2713aSLionel Sambuc switch (Arg.getArgument().getKind()) {
3425f4a2713aSLionel Sambuc case TemplateArgument::Null:
3426f4a2713aSLionel Sambuc llvm_unreachable("Should never see a NULL template argument here");
3427f4a2713aSLionel Sambuc
3428f4a2713aSLionel Sambuc case TemplateArgument::Expression: {
3429f4a2713aSLionel Sambuc TemplateArgument Result;
3430f4a2713aSLionel Sambuc ExprResult Res =
3431f4a2713aSLionel Sambuc CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
3432f4a2713aSLionel Sambuc Result, CTAK);
3433f4a2713aSLionel Sambuc if (Res.isInvalid())
3434f4a2713aSLionel Sambuc return true;
3435f4a2713aSLionel Sambuc
3436*0a6a1f1dSLionel Sambuc // If the resulting expression is new, then use it in place of the
3437*0a6a1f1dSLionel Sambuc // old expression in the template argument.
3438*0a6a1f1dSLionel Sambuc if (Res.get() != Arg.getArgument().getAsExpr()) {
3439*0a6a1f1dSLionel Sambuc TemplateArgument TA(Res.get());
3440*0a6a1f1dSLionel Sambuc Arg = TemplateArgumentLoc(TA, Res.get());
3441*0a6a1f1dSLionel Sambuc }
3442*0a6a1f1dSLionel Sambuc
3443f4a2713aSLionel Sambuc Converted.push_back(Result);
3444f4a2713aSLionel Sambuc break;
3445f4a2713aSLionel Sambuc }
3446f4a2713aSLionel Sambuc
3447f4a2713aSLionel Sambuc case TemplateArgument::Declaration:
3448f4a2713aSLionel Sambuc case TemplateArgument::Integral:
3449f4a2713aSLionel Sambuc case TemplateArgument::NullPtr:
3450f4a2713aSLionel Sambuc // We've already checked this template argument, so just copy
3451f4a2713aSLionel Sambuc // it to the list of converted arguments.
3452f4a2713aSLionel Sambuc Converted.push_back(Arg.getArgument());
3453f4a2713aSLionel Sambuc break;
3454f4a2713aSLionel Sambuc
3455f4a2713aSLionel Sambuc case TemplateArgument::Template:
3456f4a2713aSLionel Sambuc case TemplateArgument::TemplateExpansion:
3457f4a2713aSLionel Sambuc // We were given a template template argument. It may not be ill-formed;
3458f4a2713aSLionel Sambuc // see below.
3459f4a2713aSLionel Sambuc if (DependentTemplateName *DTN
3460f4a2713aSLionel Sambuc = Arg.getArgument().getAsTemplateOrTemplatePattern()
3461f4a2713aSLionel Sambuc .getAsDependentTemplateName()) {
3462f4a2713aSLionel Sambuc // We have a template argument such as \c T::template X, which we
3463f4a2713aSLionel Sambuc // parsed as a template template argument. However, since we now
3464f4a2713aSLionel Sambuc // know that we need a non-type template argument, convert this
3465f4a2713aSLionel Sambuc // template name into an expression.
3466f4a2713aSLionel Sambuc
3467f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo(DTN->getIdentifier(),
3468f4a2713aSLionel Sambuc Arg.getTemplateNameLoc());
3469f4a2713aSLionel Sambuc
3470f4a2713aSLionel Sambuc CXXScopeSpec SS;
3471f4a2713aSLionel Sambuc SS.Adopt(Arg.getTemplateQualifierLoc());
3472f4a2713aSLionel Sambuc // FIXME: the template-template arg was a DependentTemplateName,
3473f4a2713aSLionel Sambuc // so it was provided with a template keyword. However, its source
3474f4a2713aSLionel Sambuc // location is not stored in the template argument structure.
3475f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc;
3476*0a6a1f1dSLionel Sambuc ExprResult E = DependentScopeDeclRefExpr::Create(
3477*0a6a1f1dSLionel Sambuc Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo,
3478*0a6a1f1dSLionel Sambuc nullptr);
3479f4a2713aSLionel Sambuc
3480f4a2713aSLionel Sambuc // If we parsed the template argument as a pack expansion, create a
3481f4a2713aSLionel Sambuc // pack expansion expression.
3482f4a2713aSLionel Sambuc if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
3483*0a6a1f1dSLionel Sambuc E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc());
3484f4a2713aSLionel Sambuc if (E.isInvalid())
3485f4a2713aSLionel Sambuc return true;
3486f4a2713aSLionel Sambuc }
3487f4a2713aSLionel Sambuc
3488f4a2713aSLionel Sambuc TemplateArgument Result;
3489*0a6a1f1dSLionel Sambuc E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result);
3490f4a2713aSLionel Sambuc if (E.isInvalid())
3491f4a2713aSLionel Sambuc return true;
3492f4a2713aSLionel Sambuc
3493f4a2713aSLionel Sambuc Converted.push_back(Result);
3494f4a2713aSLionel Sambuc break;
3495f4a2713aSLionel Sambuc }
3496f4a2713aSLionel Sambuc
3497f4a2713aSLionel Sambuc // We have a template argument that actually does refer to a class
3498f4a2713aSLionel Sambuc // template, alias template, or template template parameter, and
3499f4a2713aSLionel Sambuc // therefore cannot be a non-type template argument.
3500f4a2713aSLionel Sambuc Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
3501f4a2713aSLionel Sambuc << Arg.getSourceRange();
3502f4a2713aSLionel Sambuc
3503f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
3504f4a2713aSLionel Sambuc return true;
3505f4a2713aSLionel Sambuc
3506f4a2713aSLionel Sambuc case TemplateArgument::Type: {
3507f4a2713aSLionel Sambuc // We have a non-type template parameter but the template
3508f4a2713aSLionel Sambuc // argument is a type.
3509f4a2713aSLionel Sambuc
3510f4a2713aSLionel Sambuc // C++ [temp.arg]p2:
3511f4a2713aSLionel Sambuc // In a template-argument, an ambiguity between a type-id and
3512f4a2713aSLionel Sambuc // an expression is resolved to a type-id, regardless of the
3513f4a2713aSLionel Sambuc // form of the corresponding template-parameter.
3514f4a2713aSLionel Sambuc //
3515f4a2713aSLionel Sambuc // We warn specifically about this case, since it can be rather
3516f4a2713aSLionel Sambuc // confusing for users.
3517f4a2713aSLionel Sambuc QualType T = Arg.getArgument().getAsType();
3518f4a2713aSLionel Sambuc SourceRange SR = Arg.getSourceRange();
3519f4a2713aSLionel Sambuc if (T->isFunctionType())
3520f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
3521f4a2713aSLionel Sambuc else
3522f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
3523f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
3524f4a2713aSLionel Sambuc return true;
3525f4a2713aSLionel Sambuc }
3526f4a2713aSLionel Sambuc
3527f4a2713aSLionel Sambuc case TemplateArgument::Pack:
3528f4a2713aSLionel Sambuc llvm_unreachable("Caller must expand template argument packs");
3529f4a2713aSLionel Sambuc }
3530f4a2713aSLionel Sambuc
3531f4a2713aSLionel Sambuc return false;
3532f4a2713aSLionel Sambuc }
3533f4a2713aSLionel Sambuc
3534f4a2713aSLionel Sambuc
3535f4a2713aSLionel Sambuc // Check template template parameters.
3536f4a2713aSLionel Sambuc TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
3537f4a2713aSLionel Sambuc
3538f4a2713aSLionel Sambuc // Substitute into the template parameter list of the template
3539f4a2713aSLionel Sambuc // template parameter, since previously-supplied template arguments
3540f4a2713aSLionel Sambuc // may appear within the template template parameter.
3541f4a2713aSLionel Sambuc {
3542f4a2713aSLionel Sambuc // Set up a template instantiation context.
3543f4a2713aSLionel Sambuc LocalInstantiationScope Scope(*this);
3544f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, TemplateLoc, Template,
3545f4a2713aSLionel Sambuc TempParm, Converted,
3546f4a2713aSLionel Sambuc SourceRange(TemplateLoc, RAngleLoc));
3547f4a2713aSLionel Sambuc if (Inst.isInvalid())
3548f4a2713aSLionel Sambuc return true;
3549f4a2713aSLionel Sambuc
3550f4a2713aSLionel Sambuc TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
3551f4a2713aSLionel Sambuc Converted.data(), Converted.size());
3552f4a2713aSLionel Sambuc TempParm = cast_or_null<TemplateTemplateParmDecl>(
3553f4a2713aSLionel Sambuc SubstDecl(TempParm, CurContext,
3554f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList(TemplateArgs)));
3555f4a2713aSLionel Sambuc if (!TempParm)
3556f4a2713aSLionel Sambuc return true;
3557f4a2713aSLionel Sambuc }
3558f4a2713aSLionel Sambuc
3559f4a2713aSLionel Sambuc switch (Arg.getArgument().getKind()) {
3560f4a2713aSLionel Sambuc case TemplateArgument::Null:
3561f4a2713aSLionel Sambuc llvm_unreachable("Should never see a NULL template argument here");
3562f4a2713aSLionel Sambuc
3563f4a2713aSLionel Sambuc case TemplateArgument::Template:
3564f4a2713aSLionel Sambuc case TemplateArgument::TemplateExpansion:
3565f4a2713aSLionel Sambuc if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex))
3566f4a2713aSLionel Sambuc return true;
3567f4a2713aSLionel Sambuc
3568f4a2713aSLionel Sambuc Converted.push_back(Arg.getArgument());
3569f4a2713aSLionel Sambuc break;
3570f4a2713aSLionel Sambuc
3571f4a2713aSLionel Sambuc case TemplateArgument::Expression:
3572f4a2713aSLionel Sambuc case TemplateArgument::Type:
3573f4a2713aSLionel Sambuc // We have a template template parameter but the template
3574f4a2713aSLionel Sambuc // argument does not refer to a template.
3575f4a2713aSLionel Sambuc Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
3576f4a2713aSLionel Sambuc << getLangOpts().CPlusPlus11;
3577f4a2713aSLionel Sambuc return true;
3578f4a2713aSLionel Sambuc
3579f4a2713aSLionel Sambuc case TemplateArgument::Declaration:
3580f4a2713aSLionel Sambuc llvm_unreachable("Declaration argument with template template parameter");
3581f4a2713aSLionel Sambuc case TemplateArgument::Integral:
3582f4a2713aSLionel Sambuc llvm_unreachable("Integral argument with template template parameter");
3583f4a2713aSLionel Sambuc case TemplateArgument::NullPtr:
3584f4a2713aSLionel Sambuc llvm_unreachable("Null pointer argument with template template parameter");
3585f4a2713aSLionel Sambuc
3586f4a2713aSLionel Sambuc case TemplateArgument::Pack:
3587f4a2713aSLionel Sambuc llvm_unreachable("Caller must expand template argument packs");
3588f4a2713aSLionel Sambuc }
3589f4a2713aSLionel Sambuc
3590f4a2713aSLionel Sambuc return false;
3591f4a2713aSLionel Sambuc }
3592f4a2713aSLionel Sambuc
3593f4a2713aSLionel Sambuc /// \brief Diagnose an arity mismatch in the
diagnoseArityMismatch(Sema & S,TemplateDecl * Template,SourceLocation TemplateLoc,TemplateArgumentListInfo & TemplateArgs)3594f4a2713aSLionel Sambuc static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template,
3595f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
3596f4a2713aSLionel Sambuc TemplateArgumentListInfo &TemplateArgs) {
3597f4a2713aSLionel Sambuc TemplateParameterList *Params = Template->getTemplateParameters();
3598f4a2713aSLionel Sambuc unsigned NumParams = Params->size();
3599f4a2713aSLionel Sambuc unsigned NumArgs = TemplateArgs.size();
3600f4a2713aSLionel Sambuc
3601f4a2713aSLionel Sambuc SourceRange Range;
3602f4a2713aSLionel Sambuc if (NumArgs > NumParams)
3603f4a2713aSLionel Sambuc Range = SourceRange(TemplateArgs[NumParams].getLocation(),
3604f4a2713aSLionel Sambuc TemplateArgs.getRAngleLoc());
3605f4a2713aSLionel Sambuc S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
3606f4a2713aSLionel Sambuc << (NumArgs > NumParams)
3607f4a2713aSLionel Sambuc << (isa<ClassTemplateDecl>(Template)? 0 :
3608f4a2713aSLionel Sambuc isa<FunctionTemplateDecl>(Template)? 1 :
3609f4a2713aSLionel Sambuc isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
3610f4a2713aSLionel Sambuc << Template << Range;
3611f4a2713aSLionel Sambuc S.Diag(Template->getLocation(), diag::note_template_decl_here)
3612f4a2713aSLionel Sambuc << Params->getSourceRange();
3613f4a2713aSLionel Sambuc return true;
3614f4a2713aSLionel Sambuc }
3615f4a2713aSLionel Sambuc
3616f4a2713aSLionel Sambuc /// \brief Check whether the template parameter is a pack expansion, and if so,
3617f4a2713aSLionel Sambuc /// determine the number of parameters produced by that expansion. For instance:
3618f4a2713aSLionel Sambuc ///
3619f4a2713aSLionel Sambuc /// \code
3620f4a2713aSLionel Sambuc /// template<typename ...Ts> struct A {
3621f4a2713aSLionel Sambuc /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B;
3622f4a2713aSLionel Sambuc /// };
3623f4a2713aSLionel Sambuc /// \endcode
3624f4a2713aSLionel Sambuc ///
3625f4a2713aSLionel Sambuc /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us
3626f4a2713aSLionel Sambuc /// is not a pack expansion, so returns an empty Optional.
getExpandedPackSize(NamedDecl * Param)3627f4a2713aSLionel Sambuc static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) {
3628f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *NTTP
3629f4a2713aSLionel Sambuc = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
3630f4a2713aSLionel Sambuc if (NTTP->isExpandedParameterPack())
3631f4a2713aSLionel Sambuc return NTTP->getNumExpansionTypes();
3632f4a2713aSLionel Sambuc }
3633f4a2713aSLionel Sambuc
3634f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *TTP
3635f4a2713aSLionel Sambuc = dyn_cast<TemplateTemplateParmDecl>(Param)) {
3636f4a2713aSLionel Sambuc if (TTP->isExpandedParameterPack())
3637f4a2713aSLionel Sambuc return TTP->getNumExpansionTemplateParameters();
3638f4a2713aSLionel Sambuc }
3639f4a2713aSLionel Sambuc
3640f4a2713aSLionel Sambuc return None;
3641f4a2713aSLionel Sambuc }
3642f4a2713aSLionel Sambuc
3643f4a2713aSLionel Sambuc /// \brief Check that the given template argument list is well-formed
3644f4a2713aSLionel Sambuc /// for specializing the given template.
CheckTemplateArgumentList(TemplateDecl * Template,SourceLocation TemplateLoc,TemplateArgumentListInfo & TemplateArgs,bool PartialTemplateArgs,SmallVectorImpl<TemplateArgument> & Converted)3645f4a2713aSLionel Sambuc bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
3646f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
3647f4a2713aSLionel Sambuc TemplateArgumentListInfo &TemplateArgs,
3648f4a2713aSLionel Sambuc bool PartialTemplateArgs,
3649*0a6a1f1dSLionel Sambuc SmallVectorImpl<TemplateArgument> &Converted) {
3650*0a6a1f1dSLionel Sambuc // Make a copy of the template arguments for processing. Only make the
3651*0a6a1f1dSLionel Sambuc // changes at the end when successful in matching the arguments to the
3652*0a6a1f1dSLionel Sambuc // template.
3653*0a6a1f1dSLionel Sambuc TemplateArgumentListInfo NewArgs = TemplateArgs;
3654f4a2713aSLionel Sambuc
3655f4a2713aSLionel Sambuc TemplateParameterList *Params = Template->getTemplateParameters();
3656f4a2713aSLionel Sambuc
3657*0a6a1f1dSLionel Sambuc SourceLocation RAngleLoc = NewArgs.getRAngleLoc();
3658f4a2713aSLionel Sambuc
3659f4a2713aSLionel Sambuc // C++ [temp.arg]p1:
3660f4a2713aSLionel Sambuc // [...] The type and form of each template-argument specified in
3661f4a2713aSLionel Sambuc // a template-id shall match the type and form specified for the
3662f4a2713aSLionel Sambuc // corresponding parameter declared by the template in its
3663f4a2713aSLionel Sambuc // template-parameter-list.
3664f4a2713aSLionel Sambuc bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
3665f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 2> ArgumentPack;
3666*0a6a1f1dSLionel Sambuc unsigned ArgIdx = 0, NumArgs = NewArgs.size();
3667f4a2713aSLionel Sambuc LocalInstantiationScope InstScope(*this, true);
3668f4a2713aSLionel Sambuc for (TemplateParameterList::iterator Param = Params->begin(),
3669f4a2713aSLionel Sambuc ParamEnd = Params->end();
3670f4a2713aSLionel Sambuc Param != ParamEnd; /* increment in loop */) {
3671f4a2713aSLionel Sambuc // If we have an expanded parameter pack, make sure we don't have too
3672f4a2713aSLionel Sambuc // many arguments.
3673f4a2713aSLionel Sambuc if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) {
3674f4a2713aSLionel Sambuc if (*Expansions == ArgumentPack.size()) {
3675f4a2713aSLionel Sambuc // We're done with this parameter pack. Pack up its arguments and add
3676f4a2713aSLionel Sambuc // them to the list.
3677f4a2713aSLionel Sambuc Converted.push_back(
3678f4a2713aSLionel Sambuc TemplateArgument::CreatePackCopy(Context,
3679f4a2713aSLionel Sambuc ArgumentPack.data(),
3680f4a2713aSLionel Sambuc ArgumentPack.size()));
3681f4a2713aSLionel Sambuc ArgumentPack.clear();
3682f4a2713aSLionel Sambuc
3683f4a2713aSLionel Sambuc // This argument is assigned to the next parameter.
3684f4a2713aSLionel Sambuc ++Param;
3685f4a2713aSLionel Sambuc continue;
3686f4a2713aSLionel Sambuc } else if (ArgIdx == NumArgs && !PartialTemplateArgs) {
3687f4a2713aSLionel Sambuc // Not enough arguments for this parameter pack.
3688f4a2713aSLionel Sambuc Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
3689f4a2713aSLionel Sambuc << false
3690f4a2713aSLionel Sambuc << (isa<ClassTemplateDecl>(Template)? 0 :
3691f4a2713aSLionel Sambuc isa<FunctionTemplateDecl>(Template)? 1 :
3692f4a2713aSLionel Sambuc isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
3693f4a2713aSLionel Sambuc << Template;
3694f4a2713aSLionel Sambuc Diag(Template->getLocation(), diag::note_template_decl_here)
3695f4a2713aSLionel Sambuc << Params->getSourceRange();
3696f4a2713aSLionel Sambuc return true;
3697f4a2713aSLionel Sambuc }
3698f4a2713aSLionel Sambuc }
3699f4a2713aSLionel Sambuc
3700f4a2713aSLionel Sambuc if (ArgIdx < NumArgs) {
3701f4a2713aSLionel Sambuc // Check the template argument we were given.
3702*0a6a1f1dSLionel Sambuc if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template,
3703f4a2713aSLionel Sambuc TemplateLoc, RAngleLoc,
3704f4a2713aSLionel Sambuc ArgumentPack.size(), Converted))
3705f4a2713aSLionel Sambuc return true;
3706f4a2713aSLionel Sambuc
3707*0a6a1f1dSLionel Sambuc bool PackExpansionIntoNonPack =
3708*0a6a1f1dSLionel Sambuc NewArgs[ArgIdx].getArgument().isPackExpansion() &&
3709*0a6a1f1dSLionel Sambuc (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param));
3710*0a6a1f1dSLionel Sambuc if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) {
3711*0a6a1f1dSLionel Sambuc // Core issue 1430: we have a pack expansion as an argument to an
3712*0a6a1f1dSLionel Sambuc // alias template, and it's not part of a parameter pack. This
3713*0a6a1f1dSLionel Sambuc // can't be canonicalized, so reject it now.
3714*0a6a1f1dSLionel Sambuc Diag(NewArgs[ArgIdx].getLocation(),
3715*0a6a1f1dSLionel Sambuc diag::err_alias_template_expansion_into_fixed_list)
3716*0a6a1f1dSLionel Sambuc << NewArgs[ArgIdx].getSourceRange();
3717*0a6a1f1dSLionel Sambuc Diag((*Param)->getLocation(), diag::note_template_param_here);
3718*0a6a1f1dSLionel Sambuc return true;
3719*0a6a1f1dSLionel Sambuc }
3720*0a6a1f1dSLionel Sambuc
3721f4a2713aSLionel Sambuc // We're now done with this argument.
3722f4a2713aSLionel Sambuc ++ArgIdx;
3723f4a2713aSLionel Sambuc
3724f4a2713aSLionel Sambuc if ((*Param)->isTemplateParameterPack()) {
3725f4a2713aSLionel Sambuc // The template parameter was a template parameter pack, so take the
3726f4a2713aSLionel Sambuc // deduced argument and place it on the argument pack. Note that we
3727f4a2713aSLionel Sambuc // stay on the same template parameter so that we can deduce more
3728f4a2713aSLionel Sambuc // arguments.
3729f4a2713aSLionel Sambuc ArgumentPack.push_back(Converted.pop_back_val());
3730f4a2713aSLionel Sambuc } else {
3731f4a2713aSLionel Sambuc // Move to the next template parameter.
3732f4a2713aSLionel Sambuc ++Param;
3733f4a2713aSLionel Sambuc }
3734f4a2713aSLionel Sambuc
3735*0a6a1f1dSLionel Sambuc // If we just saw a pack expansion into a non-pack, then directly convert
3736*0a6a1f1dSLionel Sambuc // the remaining arguments, because we don't know what parameters they'll
3737*0a6a1f1dSLionel Sambuc // match up with.
3738*0a6a1f1dSLionel Sambuc if (PackExpansionIntoNonPack) {
3739*0a6a1f1dSLionel Sambuc if (!ArgumentPack.empty()) {
3740f4a2713aSLionel Sambuc // If we were part way through filling in an expanded parameter pack,
3741f4a2713aSLionel Sambuc // fall back to just producing individual arguments.
3742f4a2713aSLionel Sambuc Converted.insert(Converted.end(),
3743f4a2713aSLionel Sambuc ArgumentPack.begin(), ArgumentPack.end());
3744f4a2713aSLionel Sambuc ArgumentPack.clear();
3745f4a2713aSLionel Sambuc }
3746f4a2713aSLionel Sambuc
3747f4a2713aSLionel Sambuc while (ArgIdx < NumArgs) {
3748*0a6a1f1dSLionel Sambuc Converted.push_back(NewArgs[ArgIdx].getArgument());
3749f4a2713aSLionel Sambuc ++ArgIdx;
3750f4a2713aSLionel Sambuc }
3751f4a2713aSLionel Sambuc
3752f4a2713aSLionel Sambuc return false;
3753f4a2713aSLionel Sambuc }
3754f4a2713aSLionel Sambuc
3755f4a2713aSLionel Sambuc continue;
3756f4a2713aSLionel Sambuc }
3757f4a2713aSLionel Sambuc
3758f4a2713aSLionel Sambuc // If we're checking a partial template argument list, we're done.
3759f4a2713aSLionel Sambuc if (PartialTemplateArgs) {
3760f4a2713aSLionel Sambuc if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
3761f4a2713aSLionel Sambuc Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3762f4a2713aSLionel Sambuc ArgumentPack.data(),
3763f4a2713aSLionel Sambuc ArgumentPack.size()));
3764f4a2713aSLionel Sambuc
3765f4a2713aSLionel Sambuc return false;
3766f4a2713aSLionel Sambuc }
3767f4a2713aSLionel Sambuc
3768f4a2713aSLionel Sambuc // If we have a template parameter pack with no more corresponding
3769f4a2713aSLionel Sambuc // arguments, just break out now and we'll fill in the argument pack below.
3770f4a2713aSLionel Sambuc if ((*Param)->isTemplateParameterPack()) {
3771f4a2713aSLionel Sambuc assert(!getExpandedPackSize(*Param) &&
3772f4a2713aSLionel Sambuc "Should have dealt with this already");
3773f4a2713aSLionel Sambuc
3774f4a2713aSLionel Sambuc // A non-expanded parameter pack before the end of the parameter list
3775f4a2713aSLionel Sambuc // only occurs for an ill-formed template parameter list, unless we've
3776f4a2713aSLionel Sambuc // got a partial argument list for a function template, so just bail out.
3777f4a2713aSLionel Sambuc if (Param + 1 != ParamEnd)
3778f4a2713aSLionel Sambuc return true;
3779f4a2713aSLionel Sambuc
3780f4a2713aSLionel Sambuc Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3781f4a2713aSLionel Sambuc ArgumentPack.data(),
3782f4a2713aSLionel Sambuc ArgumentPack.size()));
3783f4a2713aSLionel Sambuc ArgumentPack.clear();
3784f4a2713aSLionel Sambuc
3785f4a2713aSLionel Sambuc ++Param;
3786f4a2713aSLionel Sambuc continue;
3787f4a2713aSLionel Sambuc }
3788f4a2713aSLionel Sambuc
3789f4a2713aSLionel Sambuc // Check whether we have a default argument.
3790f4a2713aSLionel Sambuc TemplateArgumentLoc Arg;
3791f4a2713aSLionel Sambuc
3792f4a2713aSLionel Sambuc // Retrieve the default template argument from the template
3793f4a2713aSLionel Sambuc // parameter. For each kind of template parameter, we substitute the
3794f4a2713aSLionel Sambuc // template arguments provided thus far and any "outer" template arguments
3795f4a2713aSLionel Sambuc // (when the template parameter was part of a nested template) into
3796f4a2713aSLionel Sambuc // the default argument.
3797f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
3798f4a2713aSLionel Sambuc if (!TTP->hasDefaultArgument())
3799*0a6a1f1dSLionel Sambuc return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs);
3800f4a2713aSLionel Sambuc
3801f4a2713aSLionel Sambuc TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
3802f4a2713aSLionel Sambuc Template,
3803f4a2713aSLionel Sambuc TemplateLoc,
3804f4a2713aSLionel Sambuc RAngleLoc,
3805f4a2713aSLionel Sambuc TTP,
3806f4a2713aSLionel Sambuc Converted);
3807f4a2713aSLionel Sambuc if (!ArgType)
3808f4a2713aSLionel Sambuc return true;
3809f4a2713aSLionel Sambuc
3810f4a2713aSLionel Sambuc Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
3811f4a2713aSLionel Sambuc ArgType);
3812f4a2713aSLionel Sambuc } else if (NonTypeTemplateParmDecl *NTTP
3813f4a2713aSLionel Sambuc = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
3814f4a2713aSLionel Sambuc if (!NTTP->hasDefaultArgument())
3815*0a6a1f1dSLionel Sambuc return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs);
3816f4a2713aSLionel Sambuc
3817f4a2713aSLionel Sambuc ExprResult E = SubstDefaultTemplateArgument(*this, Template,
3818f4a2713aSLionel Sambuc TemplateLoc,
3819f4a2713aSLionel Sambuc RAngleLoc,
3820f4a2713aSLionel Sambuc NTTP,
3821f4a2713aSLionel Sambuc Converted);
3822f4a2713aSLionel Sambuc if (E.isInvalid())
3823f4a2713aSLionel Sambuc return true;
3824f4a2713aSLionel Sambuc
3825*0a6a1f1dSLionel Sambuc Expr *Ex = E.getAs<Expr>();
3826f4a2713aSLionel Sambuc Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3827f4a2713aSLionel Sambuc } else {
3828f4a2713aSLionel Sambuc TemplateTemplateParmDecl *TempParm
3829f4a2713aSLionel Sambuc = cast<TemplateTemplateParmDecl>(*Param);
3830f4a2713aSLionel Sambuc
3831f4a2713aSLionel Sambuc if (!TempParm->hasDefaultArgument())
3832*0a6a1f1dSLionel Sambuc return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs);
3833f4a2713aSLionel Sambuc
3834f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc;
3835f4a2713aSLionel Sambuc TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
3836f4a2713aSLionel Sambuc TemplateLoc,
3837f4a2713aSLionel Sambuc RAngleLoc,
3838f4a2713aSLionel Sambuc TempParm,
3839f4a2713aSLionel Sambuc Converted,
3840f4a2713aSLionel Sambuc QualifierLoc);
3841f4a2713aSLionel Sambuc if (Name.isNull())
3842f4a2713aSLionel Sambuc return true;
3843f4a2713aSLionel Sambuc
3844f4a2713aSLionel Sambuc Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3845f4a2713aSLionel Sambuc TempParm->getDefaultArgument().getTemplateNameLoc());
3846f4a2713aSLionel Sambuc }
3847f4a2713aSLionel Sambuc
3848f4a2713aSLionel Sambuc // Introduce an instantiation record that describes where we are using
3849f4a2713aSLionel Sambuc // the default template argument.
3850f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted,
3851f4a2713aSLionel Sambuc SourceRange(TemplateLoc, RAngleLoc));
3852f4a2713aSLionel Sambuc if (Inst.isInvalid())
3853f4a2713aSLionel Sambuc return true;
3854f4a2713aSLionel Sambuc
3855f4a2713aSLionel Sambuc // Check the default template argument.
3856f4a2713aSLionel Sambuc if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
3857f4a2713aSLionel Sambuc RAngleLoc, 0, Converted))
3858f4a2713aSLionel Sambuc return true;
3859f4a2713aSLionel Sambuc
3860f4a2713aSLionel Sambuc // Core issue 150 (assumed resolution): if this is a template template
3861f4a2713aSLionel Sambuc // parameter, keep track of the default template arguments from the
3862f4a2713aSLionel Sambuc // template definition.
3863f4a2713aSLionel Sambuc if (isTemplateTemplateParameter)
3864*0a6a1f1dSLionel Sambuc NewArgs.addArgument(Arg);
3865f4a2713aSLionel Sambuc
3866f4a2713aSLionel Sambuc // Move to the next template parameter and argument.
3867f4a2713aSLionel Sambuc ++Param;
3868f4a2713aSLionel Sambuc ++ArgIdx;
3869f4a2713aSLionel Sambuc }
3870f4a2713aSLionel Sambuc
3871*0a6a1f1dSLionel Sambuc // If we're performing a partial argument substitution, allow any trailing
3872*0a6a1f1dSLionel Sambuc // pack expansions; they might be empty. This can happen even if
3873*0a6a1f1dSLionel Sambuc // PartialTemplateArgs is false (the list of arguments is complete but
3874*0a6a1f1dSLionel Sambuc // still dependent).
3875*0a6a1f1dSLionel Sambuc if (ArgIdx < NumArgs && CurrentInstantiationScope &&
3876*0a6a1f1dSLionel Sambuc CurrentInstantiationScope->getPartiallySubstitutedPack()) {
3877*0a6a1f1dSLionel Sambuc while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion())
3878*0a6a1f1dSLionel Sambuc Converted.push_back(NewArgs[ArgIdx++].getArgument());
3879*0a6a1f1dSLionel Sambuc }
3880*0a6a1f1dSLionel Sambuc
3881f4a2713aSLionel Sambuc // If we have any leftover arguments, then there were too many arguments.
3882f4a2713aSLionel Sambuc // Complain and fail.
3883f4a2713aSLionel Sambuc if (ArgIdx < NumArgs)
3884*0a6a1f1dSLionel Sambuc return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs);
3885*0a6a1f1dSLionel Sambuc
3886*0a6a1f1dSLionel Sambuc // No problems found with the new argument list, propagate changes back
3887*0a6a1f1dSLionel Sambuc // to caller.
3888*0a6a1f1dSLionel Sambuc TemplateArgs = NewArgs;
3889f4a2713aSLionel Sambuc
3890f4a2713aSLionel Sambuc return false;
3891f4a2713aSLionel Sambuc }
3892f4a2713aSLionel Sambuc
3893f4a2713aSLionel Sambuc namespace {
3894f4a2713aSLionel Sambuc class UnnamedLocalNoLinkageFinder
3895f4a2713aSLionel Sambuc : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
3896f4a2713aSLionel Sambuc {
3897f4a2713aSLionel Sambuc Sema &S;
3898f4a2713aSLionel Sambuc SourceRange SR;
3899f4a2713aSLionel Sambuc
3900f4a2713aSLionel Sambuc typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
3901f4a2713aSLionel Sambuc
3902f4a2713aSLionel Sambuc public:
UnnamedLocalNoLinkageFinder(Sema & S,SourceRange SR)3903f4a2713aSLionel Sambuc UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3904f4a2713aSLionel Sambuc
Visit(QualType T)3905f4a2713aSLionel Sambuc bool Visit(QualType T) {
3906f4a2713aSLionel Sambuc return inherited::Visit(T.getTypePtr());
3907f4a2713aSLionel Sambuc }
3908f4a2713aSLionel Sambuc
3909f4a2713aSLionel Sambuc #define TYPE(Class, Parent) \
3910f4a2713aSLionel Sambuc bool Visit##Class##Type(const Class##Type *);
3911f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Class, Parent) \
3912f4a2713aSLionel Sambuc bool Visit##Class##Type(const Class##Type *) { return false; }
3913f4a2713aSLionel Sambuc #define NON_CANONICAL_TYPE(Class, Parent) \
3914f4a2713aSLionel Sambuc bool Visit##Class##Type(const Class##Type *) { return false; }
3915f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
3916f4a2713aSLionel Sambuc
3917f4a2713aSLionel Sambuc bool VisitTagDecl(const TagDecl *Tag);
3918f4a2713aSLionel Sambuc bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3919f4a2713aSLionel Sambuc };
3920f4a2713aSLionel Sambuc }
3921f4a2713aSLionel Sambuc
VisitBuiltinType(const BuiltinType *)3922f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
3923f4a2713aSLionel Sambuc return false;
3924f4a2713aSLionel Sambuc }
3925f4a2713aSLionel Sambuc
VisitComplexType(const ComplexType * T)3926f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3927f4a2713aSLionel Sambuc return Visit(T->getElementType());
3928f4a2713aSLionel Sambuc }
3929f4a2713aSLionel Sambuc
VisitPointerType(const PointerType * T)3930f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
3931f4a2713aSLionel Sambuc return Visit(T->getPointeeType());
3932f4a2713aSLionel Sambuc }
3933f4a2713aSLionel Sambuc
VisitBlockPointerType(const BlockPointerType * T)3934f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
3935f4a2713aSLionel Sambuc const BlockPointerType* T) {
3936f4a2713aSLionel Sambuc return Visit(T->getPointeeType());
3937f4a2713aSLionel Sambuc }
3938f4a2713aSLionel Sambuc
VisitLValueReferenceType(const LValueReferenceType * T)3939f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
3940f4a2713aSLionel Sambuc const LValueReferenceType* T) {
3941f4a2713aSLionel Sambuc return Visit(T->getPointeeType());
3942f4a2713aSLionel Sambuc }
3943f4a2713aSLionel Sambuc
VisitRValueReferenceType(const RValueReferenceType * T)3944f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
3945f4a2713aSLionel Sambuc const RValueReferenceType* T) {
3946f4a2713aSLionel Sambuc return Visit(T->getPointeeType());
3947f4a2713aSLionel Sambuc }
3948f4a2713aSLionel Sambuc
VisitMemberPointerType(const MemberPointerType * T)3949f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
3950f4a2713aSLionel Sambuc const MemberPointerType* T) {
3951f4a2713aSLionel Sambuc return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3952f4a2713aSLionel Sambuc }
3953f4a2713aSLionel Sambuc
VisitConstantArrayType(const ConstantArrayType * T)3954f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
3955f4a2713aSLionel Sambuc const ConstantArrayType* T) {
3956f4a2713aSLionel Sambuc return Visit(T->getElementType());
3957f4a2713aSLionel Sambuc }
3958f4a2713aSLionel Sambuc
VisitIncompleteArrayType(const IncompleteArrayType * T)3959f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
3960f4a2713aSLionel Sambuc const IncompleteArrayType* T) {
3961f4a2713aSLionel Sambuc return Visit(T->getElementType());
3962f4a2713aSLionel Sambuc }
3963f4a2713aSLionel Sambuc
VisitVariableArrayType(const VariableArrayType * T)3964f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
3965f4a2713aSLionel Sambuc const VariableArrayType* T) {
3966f4a2713aSLionel Sambuc return Visit(T->getElementType());
3967f4a2713aSLionel Sambuc }
3968f4a2713aSLionel Sambuc
VisitDependentSizedArrayType(const DependentSizedArrayType * T)3969f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
3970f4a2713aSLionel Sambuc const DependentSizedArrayType* T) {
3971f4a2713aSLionel Sambuc return Visit(T->getElementType());
3972f4a2713aSLionel Sambuc }
3973f4a2713aSLionel Sambuc
VisitDependentSizedExtVectorType(const DependentSizedExtVectorType * T)3974f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
3975f4a2713aSLionel Sambuc const DependentSizedExtVectorType* T) {
3976f4a2713aSLionel Sambuc return Visit(T->getElementType());
3977f4a2713aSLionel Sambuc }
3978f4a2713aSLionel Sambuc
VisitVectorType(const VectorType * T)3979f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3980f4a2713aSLionel Sambuc return Visit(T->getElementType());
3981f4a2713aSLionel Sambuc }
3982f4a2713aSLionel Sambuc
VisitExtVectorType(const ExtVectorType * T)3983f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3984f4a2713aSLionel Sambuc return Visit(T->getElementType());
3985f4a2713aSLionel Sambuc }
3986f4a2713aSLionel Sambuc
VisitFunctionProtoType(const FunctionProtoType * T)3987f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3988f4a2713aSLionel Sambuc const FunctionProtoType* T) {
3989*0a6a1f1dSLionel Sambuc for (const auto &A : T->param_types()) {
3990*0a6a1f1dSLionel Sambuc if (Visit(A))
3991f4a2713aSLionel Sambuc return true;
3992f4a2713aSLionel Sambuc }
3993f4a2713aSLionel Sambuc
3994*0a6a1f1dSLionel Sambuc return Visit(T->getReturnType());
3995f4a2713aSLionel Sambuc }
3996f4a2713aSLionel Sambuc
VisitFunctionNoProtoType(const FunctionNoProtoType * T)3997f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3998f4a2713aSLionel Sambuc const FunctionNoProtoType* T) {
3999*0a6a1f1dSLionel Sambuc return Visit(T->getReturnType());
4000f4a2713aSLionel Sambuc }
4001f4a2713aSLionel Sambuc
VisitUnresolvedUsingType(const UnresolvedUsingType *)4002f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
4003f4a2713aSLionel Sambuc const UnresolvedUsingType*) {
4004f4a2713aSLionel Sambuc return false;
4005f4a2713aSLionel Sambuc }
4006f4a2713aSLionel Sambuc
VisitTypeOfExprType(const TypeOfExprType *)4007f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
4008f4a2713aSLionel Sambuc return false;
4009f4a2713aSLionel Sambuc }
4010f4a2713aSLionel Sambuc
VisitTypeOfType(const TypeOfType * T)4011f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
4012f4a2713aSLionel Sambuc return Visit(T->getUnderlyingType());
4013f4a2713aSLionel Sambuc }
4014f4a2713aSLionel Sambuc
VisitDecltypeType(const DecltypeType *)4015f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
4016f4a2713aSLionel Sambuc return false;
4017f4a2713aSLionel Sambuc }
4018f4a2713aSLionel Sambuc
VisitUnaryTransformType(const UnaryTransformType *)4019f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
4020f4a2713aSLionel Sambuc const UnaryTransformType*) {
4021f4a2713aSLionel Sambuc return false;
4022f4a2713aSLionel Sambuc }
4023f4a2713aSLionel Sambuc
VisitAutoType(const AutoType * T)4024f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
4025f4a2713aSLionel Sambuc return Visit(T->getDeducedType());
4026f4a2713aSLionel Sambuc }
4027f4a2713aSLionel Sambuc
VisitRecordType(const RecordType * T)4028f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
4029f4a2713aSLionel Sambuc return VisitTagDecl(T->getDecl());
4030f4a2713aSLionel Sambuc }
4031f4a2713aSLionel Sambuc
VisitEnumType(const EnumType * T)4032f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
4033f4a2713aSLionel Sambuc return VisitTagDecl(T->getDecl());
4034f4a2713aSLionel Sambuc }
4035f4a2713aSLionel Sambuc
VisitTemplateTypeParmType(const TemplateTypeParmType *)4036f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
4037f4a2713aSLionel Sambuc const TemplateTypeParmType*) {
4038f4a2713aSLionel Sambuc return false;
4039f4a2713aSLionel Sambuc }
4040f4a2713aSLionel Sambuc
VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *)4041f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
4042f4a2713aSLionel Sambuc const SubstTemplateTypeParmPackType *) {
4043f4a2713aSLionel Sambuc return false;
4044f4a2713aSLionel Sambuc }
4045f4a2713aSLionel Sambuc
VisitTemplateSpecializationType(const TemplateSpecializationType *)4046f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
4047f4a2713aSLionel Sambuc const TemplateSpecializationType*) {
4048f4a2713aSLionel Sambuc return false;
4049f4a2713aSLionel Sambuc }
4050f4a2713aSLionel Sambuc
VisitInjectedClassNameType(const InjectedClassNameType * T)4051f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
4052f4a2713aSLionel Sambuc const InjectedClassNameType* T) {
4053f4a2713aSLionel Sambuc return VisitTagDecl(T->getDecl());
4054f4a2713aSLionel Sambuc }
4055f4a2713aSLionel Sambuc
VisitDependentNameType(const DependentNameType * T)4056f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
4057f4a2713aSLionel Sambuc const DependentNameType* T) {
4058f4a2713aSLionel Sambuc return VisitNestedNameSpecifier(T->getQualifier());
4059f4a2713aSLionel Sambuc }
4060f4a2713aSLionel Sambuc
VisitDependentTemplateSpecializationType(const DependentTemplateSpecializationType * T)4061f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
4062f4a2713aSLionel Sambuc const DependentTemplateSpecializationType* T) {
4063f4a2713aSLionel Sambuc return VisitNestedNameSpecifier(T->getQualifier());
4064f4a2713aSLionel Sambuc }
4065f4a2713aSLionel Sambuc
VisitPackExpansionType(const PackExpansionType * T)4066f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
4067f4a2713aSLionel Sambuc const PackExpansionType* T) {
4068f4a2713aSLionel Sambuc return Visit(T->getPattern());
4069f4a2713aSLionel Sambuc }
4070f4a2713aSLionel Sambuc
VisitObjCObjectType(const ObjCObjectType *)4071f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
4072f4a2713aSLionel Sambuc return false;
4073f4a2713aSLionel Sambuc }
4074f4a2713aSLionel Sambuc
VisitObjCInterfaceType(const ObjCInterfaceType *)4075f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
4076f4a2713aSLionel Sambuc const ObjCInterfaceType *) {
4077f4a2713aSLionel Sambuc return false;
4078f4a2713aSLionel Sambuc }
4079f4a2713aSLionel Sambuc
VisitObjCObjectPointerType(const ObjCObjectPointerType *)4080f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
4081f4a2713aSLionel Sambuc const ObjCObjectPointerType *) {
4082f4a2713aSLionel Sambuc return false;
4083f4a2713aSLionel Sambuc }
4084f4a2713aSLionel Sambuc
VisitAtomicType(const AtomicType * T)4085f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
4086f4a2713aSLionel Sambuc return Visit(T->getValueType());
4087f4a2713aSLionel Sambuc }
4088f4a2713aSLionel Sambuc
VisitTagDecl(const TagDecl * Tag)4089f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
4090f4a2713aSLionel Sambuc if (Tag->getDeclContext()->isFunctionOrMethod()) {
4091f4a2713aSLionel Sambuc S.Diag(SR.getBegin(),
4092f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus11 ?
4093f4a2713aSLionel Sambuc diag::warn_cxx98_compat_template_arg_local_type :
4094f4a2713aSLionel Sambuc diag::ext_template_arg_local_type)
4095f4a2713aSLionel Sambuc << S.Context.getTypeDeclType(Tag) << SR;
4096f4a2713aSLionel Sambuc return true;
4097f4a2713aSLionel Sambuc }
4098f4a2713aSLionel Sambuc
4099f4a2713aSLionel Sambuc if (!Tag->hasNameForLinkage()) {
4100f4a2713aSLionel Sambuc S.Diag(SR.getBegin(),
4101f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus11 ?
4102f4a2713aSLionel Sambuc diag::warn_cxx98_compat_template_arg_unnamed_type :
4103f4a2713aSLionel Sambuc diag::ext_template_arg_unnamed_type) << SR;
4104f4a2713aSLionel Sambuc S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
4105f4a2713aSLionel Sambuc return true;
4106f4a2713aSLionel Sambuc }
4107f4a2713aSLionel Sambuc
4108f4a2713aSLionel Sambuc return false;
4109f4a2713aSLionel Sambuc }
4110f4a2713aSLionel Sambuc
VisitNestedNameSpecifier(NestedNameSpecifier * NNS)4111f4a2713aSLionel Sambuc bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
4112f4a2713aSLionel Sambuc NestedNameSpecifier *NNS) {
4113f4a2713aSLionel Sambuc if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
4114f4a2713aSLionel Sambuc return true;
4115f4a2713aSLionel Sambuc
4116f4a2713aSLionel Sambuc switch (NNS->getKind()) {
4117f4a2713aSLionel Sambuc case NestedNameSpecifier::Identifier:
4118f4a2713aSLionel Sambuc case NestedNameSpecifier::Namespace:
4119f4a2713aSLionel Sambuc case NestedNameSpecifier::NamespaceAlias:
4120f4a2713aSLionel Sambuc case NestedNameSpecifier::Global:
4121*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Super:
4122f4a2713aSLionel Sambuc return false;
4123f4a2713aSLionel Sambuc
4124f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpec:
4125f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate:
4126f4a2713aSLionel Sambuc return Visit(QualType(NNS->getAsType(), 0));
4127f4a2713aSLionel Sambuc }
4128f4a2713aSLionel Sambuc llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
4129f4a2713aSLionel Sambuc }
4130f4a2713aSLionel Sambuc
4131f4a2713aSLionel Sambuc
4132f4a2713aSLionel Sambuc /// \brief Check a template argument against its corresponding
4133f4a2713aSLionel Sambuc /// template type parameter.
4134f4a2713aSLionel Sambuc ///
4135f4a2713aSLionel Sambuc /// This routine implements the semantics of C++ [temp.arg.type]. It
4136f4a2713aSLionel Sambuc /// returns true if an error occurred, and false otherwise.
CheckTemplateArgument(TemplateTypeParmDecl * Param,TypeSourceInfo * ArgInfo)4137f4a2713aSLionel Sambuc bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
4138f4a2713aSLionel Sambuc TypeSourceInfo *ArgInfo) {
4139f4a2713aSLionel Sambuc assert(ArgInfo && "invalid TypeSourceInfo");
4140f4a2713aSLionel Sambuc QualType Arg = ArgInfo->getType();
4141f4a2713aSLionel Sambuc SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
4142f4a2713aSLionel Sambuc
4143f4a2713aSLionel Sambuc if (Arg->isVariablyModifiedType()) {
4144f4a2713aSLionel Sambuc return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
4145f4a2713aSLionel Sambuc } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
4146f4a2713aSLionel Sambuc return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
4147f4a2713aSLionel Sambuc }
4148f4a2713aSLionel Sambuc
4149f4a2713aSLionel Sambuc // C++03 [temp.arg.type]p2:
4150f4a2713aSLionel Sambuc // A local type, a type with no linkage, an unnamed type or a type
4151f4a2713aSLionel Sambuc // compounded from any of these types shall not be used as a
4152f4a2713aSLionel Sambuc // template-argument for a template type-parameter.
4153f4a2713aSLionel Sambuc //
4154f4a2713aSLionel Sambuc // C++11 allows these, and even in C++03 we allow them as an extension with
4155f4a2713aSLionel Sambuc // a warning.
4156*0a6a1f1dSLionel Sambuc bool NeedsCheck;
4157*0a6a1f1dSLionel Sambuc if (LangOpts.CPlusPlus11)
4158*0a6a1f1dSLionel Sambuc NeedsCheck =
4159*0a6a1f1dSLionel Sambuc !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type,
4160*0a6a1f1dSLionel Sambuc SR.getBegin()) ||
4161*0a6a1f1dSLionel Sambuc !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type,
4162*0a6a1f1dSLionel Sambuc SR.getBegin());
4163*0a6a1f1dSLionel Sambuc else
4164*0a6a1f1dSLionel Sambuc NeedsCheck = Arg->hasUnnamedOrLocalType();
4165*0a6a1f1dSLionel Sambuc
4166*0a6a1f1dSLionel Sambuc if (NeedsCheck) {
4167f4a2713aSLionel Sambuc UnnamedLocalNoLinkageFinder Finder(*this, SR);
4168f4a2713aSLionel Sambuc (void)Finder.Visit(Context.getCanonicalType(Arg));
4169f4a2713aSLionel Sambuc }
4170f4a2713aSLionel Sambuc
4171f4a2713aSLionel Sambuc return false;
4172f4a2713aSLionel Sambuc }
4173f4a2713aSLionel Sambuc
4174f4a2713aSLionel Sambuc enum NullPointerValueKind {
4175f4a2713aSLionel Sambuc NPV_NotNullPointer,
4176f4a2713aSLionel Sambuc NPV_NullPointer,
4177f4a2713aSLionel Sambuc NPV_Error
4178f4a2713aSLionel Sambuc };
4179f4a2713aSLionel Sambuc
4180f4a2713aSLionel Sambuc /// \brief Determine whether the given template argument is a null pointer
4181f4a2713aSLionel Sambuc /// value of the appropriate type.
4182f4a2713aSLionel Sambuc static NullPointerValueKind
isNullPointerValueTemplateArgument(Sema & S,NonTypeTemplateParmDecl * Param,QualType ParamType,Expr * Arg)4183f4a2713aSLionel Sambuc isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param,
4184f4a2713aSLionel Sambuc QualType ParamType, Expr *Arg) {
4185f4a2713aSLionel Sambuc if (Arg->isValueDependent() || Arg->isTypeDependent())
4186f4a2713aSLionel Sambuc return NPV_NotNullPointer;
4187f4a2713aSLionel Sambuc
4188f4a2713aSLionel Sambuc if (!S.getLangOpts().CPlusPlus11)
4189f4a2713aSLionel Sambuc return NPV_NotNullPointer;
4190f4a2713aSLionel Sambuc
4191f4a2713aSLionel Sambuc // Determine whether we have a constant expression.
4192f4a2713aSLionel Sambuc ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg);
4193f4a2713aSLionel Sambuc if (ArgRV.isInvalid())
4194f4a2713aSLionel Sambuc return NPV_Error;
4195*0a6a1f1dSLionel Sambuc Arg = ArgRV.get();
4196f4a2713aSLionel Sambuc
4197f4a2713aSLionel Sambuc Expr::EvalResult EvalResult;
4198f4a2713aSLionel Sambuc SmallVector<PartialDiagnosticAt, 8> Notes;
4199f4a2713aSLionel Sambuc EvalResult.Diag = &Notes;
4200f4a2713aSLionel Sambuc if (!Arg->EvaluateAsRValue(EvalResult, S.Context) ||
4201f4a2713aSLionel Sambuc EvalResult.HasSideEffects) {
4202f4a2713aSLionel Sambuc SourceLocation DiagLoc = Arg->getExprLoc();
4203f4a2713aSLionel Sambuc
4204f4a2713aSLionel Sambuc // If our only note is the usual "invalid subexpression" note, just point
4205f4a2713aSLionel Sambuc // the caret at its location rather than producing an essentially
4206f4a2713aSLionel Sambuc // redundant note.
4207f4a2713aSLionel Sambuc if (Notes.size() == 1 && Notes[0].second.getDiagID() ==
4208f4a2713aSLionel Sambuc diag::note_invalid_subexpr_in_const_expr) {
4209f4a2713aSLionel Sambuc DiagLoc = Notes[0].first;
4210f4a2713aSLionel Sambuc Notes.clear();
4211f4a2713aSLionel Sambuc }
4212f4a2713aSLionel Sambuc
4213f4a2713aSLionel Sambuc S.Diag(DiagLoc, diag::err_template_arg_not_address_constant)
4214f4a2713aSLionel Sambuc << Arg->getType() << Arg->getSourceRange();
4215f4a2713aSLionel Sambuc for (unsigned I = 0, N = Notes.size(); I != N; ++I)
4216f4a2713aSLionel Sambuc S.Diag(Notes[I].first, Notes[I].second);
4217f4a2713aSLionel Sambuc
4218f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4219f4a2713aSLionel Sambuc return NPV_Error;
4220f4a2713aSLionel Sambuc }
4221f4a2713aSLionel Sambuc
4222f4a2713aSLionel Sambuc // C++11 [temp.arg.nontype]p1:
4223f4a2713aSLionel Sambuc // - an address constant expression of type std::nullptr_t
4224f4a2713aSLionel Sambuc if (Arg->getType()->isNullPtrType())
4225f4a2713aSLionel Sambuc return NPV_NullPointer;
4226f4a2713aSLionel Sambuc
4227f4a2713aSLionel Sambuc // - a constant expression that evaluates to a null pointer value (4.10); or
4228f4a2713aSLionel Sambuc // - a constant expression that evaluates to a null member pointer value
4229f4a2713aSLionel Sambuc // (4.11); or
4230f4a2713aSLionel Sambuc if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) ||
4231f4a2713aSLionel Sambuc (EvalResult.Val.isMemberPointer() &&
4232f4a2713aSLionel Sambuc !EvalResult.Val.getMemberPointerDecl())) {
4233f4a2713aSLionel Sambuc // If our expression has an appropriate type, we've succeeded.
4234f4a2713aSLionel Sambuc bool ObjCLifetimeConversion;
4235f4a2713aSLionel Sambuc if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) ||
4236f4a2713aSLionel Sambuc S.IsQualificationConversion(Arg->getType(), ParamType, false,
4237f4a2713aSLionel Sambuc ObjCLifetimeConversion))
4238f4a2713aSLionel Sambuc return NPV_NullPointer;
4239f4a2713aSLionel Sambuc
4240f4a2713aSLionel Sambuc // The types didn't match, but we know we got a null pointer; complain,
4241f4a2713aSLionel Sambuc // then recover as if the types were correct.
4242f4a2713aSLionel Sambuc S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant)
4243f4a2713aSLionel Sambuc << Arg->getType() << ParamType << Arg->getSourceRange();
4244f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4245f4a2713aSLionel Sambuc return NPV_NullPointer;
4246f4a2713aSLionel Sambuc }
4247f4a2713aSLionel Sambuc
4248f4a2713aSLionel Sambuc // If we don't have a null pointer value, but we do have a NULL pointer
4249f4a2713aSLionel Sambuc // constant, suggest a cast to the appropriate type.
4250f4a2713aSLionel Sambuc if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) {
4251f4a2713aSLionel Sambuc std::string Code = "static_cast<" + ParamType.getAsString() + ">(";
4252f4a2713aSLionel Sambuc S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant)
4253*0a6a1f1dSLionel Sambuc << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code)
4254*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()),
4255f4a2713aSLionel Sambuc ")");
4256f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4257f4a2713aSLionel Sambuc return NPV_NullPointer;
4258f4a2713aSLionel Sambuc }
4259f4a2713aSLionel Sambuc
4260f4a2713aSLionel Sambuc // FIXME: If we ever want to support general, address-constant expressions
4261f4a2713aSLionel Sambuc // as non-type template arguments, we should return the ExprResult here to
4262f4a2713aSLionel Sambuc // be interpreted by the caller.
4263f4a2713aSLionel Sambuc return NPV_NotNullPointer;
4264f4a2713aSLionel Sambuc }
4265f4a2713aSLionel Sambuc
4266f4a2713aSLionel Sambuc /// \brief Checks whether the given template argument is compatible with its
4267f4a2713aSLionel Sambuc /// template parameter.
CheckTemplateArgumentIsCompatibleWithParameter(Sema & S,NonTypeTemplateParmDecl * Param,QualType ParamType,Expr * ArgIn,Expr * Arg,QualType ArgType)4268f4a2713aSLionel Sambuc static bool CheckTemplateArgumentIsCompatibleWithParameter(
4269f4a2713aSLionel Sambuc Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn,
4270f4a2713aSLionel Sambuc Expr *Arg, QualType ArgType) {
4271f4a2713aSLionel Sambuc bool ObjCLifetimeConversion;
4272f4a2713aSLionel Sambuc if (ParamType->isPointerType() &&
4273f4a2713aSLionel Sambuc !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
4274f4a2713aSLionel Sambuc S.IsQualificationConversion(ArgType, ParamType, false,
4275f4a2713aSLionel Sambuc ObjCLifetimeConversion)) {
4276f4a2713aSLionel Sambuc // For pointer-to-object types, qualification conversions are
4277f4a2713aSLionel Sambuc // permitted.
4278f4a2713aSLionel Sambuc } else {
4279f4a2713aSLionel Sambuc if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
4280f4a2713aSLionel Sambuc if (!ParamRef->getPointeeType()->isFunctionType()) {
4281f4a2713aSLionel Sambuc // C++ [temp.arg.nontype]p5b3:
4282f4a2713aSLionel Sambuc // For a non-type template-parameter of type reference to
4283f4a2713aSLionel Sambuc // object, no conversions apply. The type referred to by the
4284f4a2713aSLionel Sambuc // reference may be more cv-qualified than the (otherwise
4285f4a2713aSLionel Sambuc // identical) type of the template- argument. The
4286f4a2713aSLionel Sambuc // template-parameter is bound directly to the
4287f4a2713aSLionel Sambuc // template-argument, which shall be an lvalue.
4288f4a2713aSLionel Sambuc
4289f4a2713aSLionel Sambuc // FIXME: Other qualifiers?
4290f4a2713aSLionel Sambuc unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
4291f4a2713aSLionel Sambuc unsigned ArgQuals = ArgType.getCVRQualifiers();
4292f4a2713aSLionel Sambuc
4293f4a2713aSLionel Sambuc if ((ParamQuals | ArgQuals) != ParamQuals) {
4294f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(),
4295f4a2713aSLionel Sambuc diag::err_template_arg_ref_bind_ignores_quals)
4296f4a2713aSLionel Sambuc << ParamType << Arg->getType() << Arg->getSourceRange();
4297f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4298f4a2713aSLionel Sambuc return true;
4299f4a2713aSLionel Sambuc }
4300f4a2713aSLionel Sambuc }
4301f4a2713aSLionel Sambuc }
4302f4a2713aSLionel Sambuc
4303f4a2713aSLionel Sambuc // At this point, the template argument refers to an object or
4304f4a2713aSLionel Sambuc // function with external linkage. We now need to check whether the
4305f4a2713aSLionel Sambuc // argument and parameter types are compatible.
4306f4a2713aSLionel Sambuc if (!S.Context.hasSameUnqualifiedType(ArgType,
4307f4a2713aSLionel Sambuc ParamType.getNonReferenceType())) {
4308f4a2713aSLionel Sambuc // We can't perform this conversion or binding.
4309f4a2713aSLionel Sambuc if (ParamType->isReferenceType())
4310f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
4311f4a2713aSLionel Sambuc << ParamType << ArgIn->getType() << Arg->getSourceRange();
4312f4a2713aSLionel Sambuc else
4313f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
4314f4a2713aSLionel Sambuc << ArgIn->getType() << ParamType << Arg->getSourceRange();
4315f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4316f4a2713aSLionel Sambuc return true;
4317f4a2713aSLionel Sambuc }
4318f4a2713aSLionel Sambuc }
4319f4a2713aSLionel Sambuc
4320f4a2713aSLionel Sambuc return false;
4321f4a2713aSLionel Sambuc }
4322f4a2713aSLionel Sambuc
4323f4a2713aSLionel Sambuc /// \brief Checks whether the given template argument is the address
4324f4a2713aSLionel Sambuc /// of an object or function according to C++ [temp.arg.nontype]p1.
4325f4a2713aSLionel Sambuc static bool
CheckTemplateArgumentAddressOfObjectOrFunction(Sema & S,NonTypeTemplateParmDecl * Param,QualType ParamType,Expr * ArgIn,TemplateArgument & Converted)4326f4a2713aSLionel Sambuc CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
4327f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *Param,
4328f4a2713aSLionel Sambuc QualType ParamType,
4329f4a2713aSLionel Sambuc Expr *ArgIn,
4330f4a2713aSLionel Sambuc TemplateArgument &Converted) {
4331f4a2713aSLionel Sambuc bool Invalid = false;
4332f4a2713aSLionel Sambuc Expr *Arg = ArgIn;
4333f4a2713aSLionel Sambuc QualType ArgType = Arg->getType();
4334f4a2713aSLionel Sambuc
4335f4a2713aSLionel Sambuc bool AddressTaken = false;
4336f4a2713aSLionel Sambuc SourceLocation AddrOpLoc;
4337f4a2713aSLionel Sambuc if (S.getLangOpts().MicrosoftExt) {
4338f4a2713aSLionel Sambuc // Microsoft Visual C++ strips all casts, allows an arbitrary number of
4339f4a2713aSLionel Sambuc // dereference and address-of operators.
4340f4a2713aSLionel Sambuc Arg = Arg->IgnoreParenCasts();
4341f4a2713aSLionel Sambuc
4342f4a2713aSLionel Sambuc bool ExtWarnMSTemplateArg = false;
4343f4a2713aSLionel Sambuc UnaryOperatorKind FirstOpKind;
4344f4a2713aSLionel Sambuc SourceLocation FirstOpLoc;
4345f4a2713aSLionel Sambuc while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
4346f4a2713aSLionel Sambuc UnaryOperatorKind UnOpKind = UnOp->getOpcode();
4347f4a2713aSLionel Sambuc if (UnOpKind == UO_Deref)
4348f4a2713aSLionel Sambuc ExtWarnMSTemplateArg = true;
4349f4a2713aSLionel Sambuc if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) {
4350f4a2713aSLionel Sambuc Arg = UnOp->getSubExpr()->IgnoreParenCasts();
4351f4a2713aSLionel Sambuc if (!AddrOpLoc.isValid()) {
4352f4a2713aSLionel Sambuc FirstOpKind = UnOpKind;
4353f4a2713aSLionel Sambuc FirstOpLoc = UnOp->getOperatorLoc();
4354f4a2713aSLionel Sambuc }
4355f4a2713aSLionel Sambuc } else
4356f4a2713aSLionel Sambuc break;
4357f4a2713aSLionel Sambuc }
4358f4a2713aSLionel Sambuc if (FirstOpLoc.isValid()) {
4359f4a2713aSLionel Sambuc if (ExtWarnMSTemplateArg)
4360f4a2713aSLionel Sambuc S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument)
4361f4a2713aSLionel Sambuc << ArgIn->getSourceRange();
4362f4a2713aSLionel Sambuc
4363f4a2713aSLionel Sambuc if (FirstOpKind == UO_AddrOf)
4364f4a2713aSLionel Sambuc AddressTaken = true;
4365f4a2713aSLionel Sambuc else if (Arg->getType()->isPointerType()) {
4366f4a2713aSLionel Sambuc // We cannot let pointers get dereferenced here, that is obviously not a
4367f4a2713aSLionel Sambuc // constant expression.
4368f4a2713aSLionel Sambuc assert(FirstOpKind == UO_Deref);
4369f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
4370f4a2713aSLionel Sambuc << Arg->getSourceRange();
4371f4a2713aSLionel Sambuc }
4372f4a2713aSLionel Sambuc }
4373f4a2713aSLionel Sambuc } else {
4374f4a2713aSLionel Sambuc // See through any implicit casts we added to fix the type.
4375f4a2713aSLionel Sambuc Arg = Arg->IgnoreImpCasts();
4376f4a2713aSLionel Sambuc
4377f4a2713aSLionel Sambuc // C++ [temp.arg.nontype]p1:
4378f4a2713aSLionel Sambuc //
4379f4a2713aSLionel Sambuc // A template-argument for a non-type, non-template
4380f4a2713aSLionel Sambuc // template-parameter shall be one of: [...]
4381f4a2713aSLionel Sambuc //
4382f4a2713aSLionel Sambuc // -- the address of an object or function with external
4383f4a2713aSLionel Sambuc // linkage, including function templates and function
4384f4a2713aSLionel Sambuc // template-ids but excluding non-static class members,
4385f4a2713aSLionel Sambuc // expressed as & id-expression where the & is optional if
4386f4a2713aSLionel Sambuc // the name refers to a function or array, or if the
4387f4a2713aSLionel Sambuc // corresponding template-parameter is a reference; or
4388f4a2713aSLionel Sambuc
4389f4a2713aSLionel Sambuc // In C++98/03 mode, give an extension warning on any extra parentheses.
4390f4a2713aSLionel Sambuc // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
4391f4a2713aSLionel Sambuc bool ExtraParens = false;
4392f4a2713aSLionel Sambuc while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
4393f4a2713aSLionel Sambuc if (!Invalid && !ExtraParens) {
4394f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(),
4395f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus11
4396f4a2713aSLionel Sambuc ? diag::warn_cxx98_compat_template_arg_extra_parens
4397f4a2713aSLionel Sambuc : diag::ext_template_arg_extra_parens)
4398f4a2713aSLionel Sambuc << Arg->getSourceRange();
4399f4a2713aSLionel Sambuc ExtraParens = true;
4400f4a2713aSLionel Sambuc }
4401f4a2713aSLionel Sambuc
4402f4a2713aSLionel Sambuc Arg = Parens->getSubExpr();
4403f4a2713aSLionel Sambuc }
4404f4a2713aSLionel Sambuc
4405f4a2713aSLionel Sambuc while (SubstNonTypeTemplateParmExpr *subst =
4406f4a2713aSLionel Sambuc dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
4407f4a2713aSLionel Sambuc Arg = subst->getReplacement()->IgnoreImpCasts();
4408f4a2713aSLionel Sambuc
4409f4a2713aSLionel Sambuc if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
4410f4a2713aSLionel Sambuc if (UnOp->getOpcode() == UO_AddrOf) {
4411f4a2713aSLionel Sambuc Arg = UnOp->getSubExpr();
4412f4a2713aSLionel Sambuc AddressTaken = true;
4413f4a2713aSLionel Sambuc AddrOpLoc = UnOp->getOperatorLoc();
4414f4a2713aSLionel Sambuc }
4415f4a2713aSLionel Sambuc }
4416f4a2713aSLionel Sambuc
4417f4a2713aSLionel Sambuc while (SubstNonTypeTemplateParmExpr *subst =
4418f4a2713aSLionel Sambuc dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
4419f4a2713aSLionel Sambuc Arg = subst->getReplacement()->IgnoreImpCasts();
4420f4a2713aSLionel Sambuc }
4421f4a2713aSLionel Sambuc
4422*0a6a1f1dSLionel Sambuc DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
4423*0a6a1f1dSLionel Sambuc ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr;
4424*0a6a1f1dSLionel Sambuc
4425*0a6a1f1dSLionel Sambuc // If our parameter has pointer type, check for a null template value.
4426*0a6a1f1dSLionel Sambuc if (ParamType->isPointerType() || ParamType->isNullPtrType()) {
4427*0a6a1f1dSLionel Sambuc NullPointerValueKind NPV;
4428*0a6a1f1dSLionel Sambuc // dllimport'd entities aren't constant but are available inside of template
4429*0a6a1f1dSLionel Sambuc // arguments.
4430*0a6a1f1dSLionel Sambuc if (Entity && Entity->hasAttr<DLLImportAttr>())
4431*0a6a1f1dSLionel Sambuc NPV = NPV_NotNullPointer;
4432*0a6a1f1dSLionel Sambuc else
4433*0a6a1f1dSLionel Sambuc NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn);
4434*0a6a1f1dSLionel Sambuc switch (NPV) {
4435*0a6a1f1dSLionel Sambuc case NPV_NullPointer:
4436*0a6a1f1dSLionel Sambuc S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null);
4437*0a6a1f1dSLionel Sambuc Converted = TemplateArgument(S.Context.getCanonicalType(ParamType),
4438*0a6a1f1dSLionel Sambuc /*isNullPtr=*/true);
4439*0a6a1f1dSLionel Sambuc return false;
4440*0a6a1f1dSLionel Sambuc
4441*0a6a1f1dSLionel Sambuc case NPV_Error:
4442*0a6a1f1dSLionel Sambuc return true;
4443*0a6a1f1dSLionel Sambuc
4444*0a6a1f1dSLionel Sambuc case NPV_NotNullPointer:
4445*0a6a1f1dSLionel Sambuc break;
4446*0a6a1f1dSLionel Sambuc }
4447*0a6a1f1dSLionel Sambuc }
4448*0a6a1f1dSLionel Sambuc
4449f4a2713aSLionel Sambuc // Stop checking the precise nature of the argument if it is value dependent,
4450f4a2713aSLionel Sambuc // it should be checked when instantiated.
4451f4a2713aSLionel Sambuc if (Arg->isValueDependent()) {
4452f4a2713aSLionel Sambuc Converted = TemplateArgument(ArgIn);
4453f4a2713aSLionel Sambuc return false;
4454f4a2713aSLionel Sambuc }
4455f4a2713aSLionel Sambuc
4456f4a2713aSLionel Sambuc if (isa<CXXUuidofExpr>(Arg)) {
4457f4a2713aSLionel Sambuc if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType,
4458f4a2713aSLionel Sambuc ArgIn, Arg, ArgType))
4459f4a2713aSLionel Sambuc return true;
4460f4a2713aSLionel Sambuc
4461f4a2713aSLionel Sambuc Converted = TemplateArgument(ArgIn);
4462f4a2713aSLionel Sambuc return false;
4463f4a2713aSLionel Sambuc }
4464f4a2713aSLionel Sambuc
4465f4a2713aSLionel Sambuc if (!DRE) {
4466f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
4467f4a2713aSLionel Sambuc << Arg->getSourceRange();
4468f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4469f4a2713aSLionel Sambuc return true;
4470f4a2713aSLionel Sambuc }
4471f4a2713aSLionel Sambuc
4472f4a2713aSLionel Sambuc // Cannot refer to non-static data members
4473f4a2713aSLionel Sambuc if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) {
4474f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_field)
4475f4a2713aSLionel Sambuc << Entity << Arg->getSourceRange();
4476f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4477f4a2713aSLionel Sambuc return true;
4478f4a2713aSLionel Sambuc }
4479f4a2713aSLionel Sambuc
4480f4a2713aSLionel Sambuc // Cannot refer to non-static member functions
4481f4a2713aSLionel Sambuc if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) {
4482f4a2713aSLionel Sambuc if (!Method->isStatic()) {
4483f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_method)
4484f4a2713aSLionel Sambuc << Method << Arg->getSourceRange();
4485f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4486f4a2713aSLionel Sambuc return true;
4487f4a2713aSLionel Sambuc }
4488f4a2713aSLionel Sambuc }
4489f4a2713aSLionel Sambuc
4490f4a2713aSLionel Sambuc FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity);
4491f4a2713aSLionel Sambuc VarDecl *Var = dyn_cast<VarDecl>(Entity);
4492f4a2713aSLionel Sambuc
4493f4a2713aSLionel Sambuc // A non-type template argument must refer to an object or function.
4494f4a2713aSLionel Sambuc if (!Func && !Var) {
4495f4a2713aSLionel Sambuc // We found something, but we don't know specifically what it is.
4496f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func)
4497f4a2713aSLionel Sambuc << Arg->getSourceRange();
4498f4a2713aSLionel Sambuc S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
4499f4a2713aSLionel Sambuc return true;
4500f4a2713aSLionel Sambuc }
4501f4a2713aSLionel Sambuc
4502f4a2713aSLionel Sambuc // Address / reference template args must have external linkage in C++98.
4503f4a2713aSLionel Sambuc if (Entity->getFormalLinkage() == InternalLinkage) {
4504f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ?
4505f4a2713aSLionel Sambuc diag::warn_cxx98_compat_template_arg_object_internal :
4506f4a2713aSLionel Sambuc diag::ext_template_arg_object_internal)
4507f4a2713aSLionel Sambuc << !Func << Entity << Arg->getSourceRange();
4508f4a2713aSLionel Sambuc S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object)
4509f4a2713aSLionel Sambuc << !Func;
4510f4a2713aSLionel Sambuc } else if (!Entity->hasLinkage()) {
4511f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage)
4512f4a2713aSLionel Sambuc << !Func << Entity << Arg->getSourceRange();
4513f4a2713aSLionel Sambuc S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object)
4514f4a2713aSLionel Sambuc << !Func;
4515f4a2713aSLionel Sambuc return true;
4516f4a2713aSLionel Sambuc }
4517f4a2713aSLionel Sambuc
4518f4a2713aSLionel Sambuc if (Func) {
4519f4a2713aSLionel Sambuc // If the template parameter has pointer type, the function decays.
4520f4a2713aSLionel Sambuc if (ParamType->isPointerType() && !AddressTaken)
4521f4a2713aSLionel Sambuc ArgType = S.Context.getPointerType(Func->getType());
4522f4a2713aSLionel Sambuc else if (AddressTaken && ParamType->isReferenceType()) {
4523f4a2713aSLionel Sambuc // If we originally had an address-of operator, but the
4524f4a2713aSLionel Sambuc // parameter has reference type, complain and (if things look
4525f4a2713aSLionel Sambuc // like they will work) drop the address-of operator.
4526f4a2713aSLionel Sambuc if (!S.Context.hasSameUnqualifiedType(Func->getType(),
4527f4a2713aSLionel Sambuc ParamType.getNonReferenceType())) {
4528f4a2713aSLionel Sambuc S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
4529f4a2713aSLionel Sambuc << ParamType;
4530f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4531f4a2713aSLionel Sambuc return true;
4532f4a2713aSLionel Sambuc }
4533f4a2713aSLionel Sambuc
4534f4a2713aSLionel Sambuc S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
4535f4a2713aSLionel Sambuc << ParamType
4536f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(AddrOpLoc);
4537f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4538f4a2713aSLionel Sambuc
4539f4a2713aSLionel Sambuc ArgType = Func->getType();
4540f4a2713aSLionel Sambuc }
4541f4a2713aSLionel Sambuc } else {
4542f4a2713aSLionel Sambuc // A value of reference type is not an object.
4543f4a2713aSLionel Sambuc if (Var->getType()->isReferenceType()) {
4544f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(),
4545f4a2713aSLionel Sambuc diag::err_template_arg_reference_var)
4546f4a2713aSLionel Sambuc << Var->getType() << Arg->getSourceRange();
4547f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4548f4a2713aSLionel Sambuc return true;
4549f4a2713aSLionel Sambuc }
4550f4a2713aSLionel Sambuc
4551f4a2713aSLionel Sambuc // A template argument must have static storage duration.
4552f4a2713aSLionel Sambuc if (Var->getTLSKind()) {
4553f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local)
4554f4a2713aSLionel Sambuc << Arg->getSourceRange();
4555f4a2713aSLionel Sambuc S.Diag(Var->getLocation(), diag::note_template_arg_refers_here);
4556f4a2713aSLionel Sambuc return true;
4557f4a2713aSLionel Sambuc }
4558f4a2713aSLionel Sambuc
4559f4a2713aSLionel Sambuc // If the template parameter has pointer type, we must have taken
4560f4a2713aSLionel Sambuc // the address of this object.
4561f4a2713aSLionel Sambuc if (ParamType->isReferenceType()) {
4562f4a2713aSLionel Sambuc if (AddressTaken) {
4563f4a2713aSLionel Sambuc // If we originally had an address-of operator, but the
4564f4a2713aSLionel Sambuc // parameter has reference type, complain and (if things look
4565f4a2713aSLionel Sambuc // like they will work) drop the address-of operator.
4566f4a2713aSLionel Sambuc if (!S.Context.hasSameUnqualifiedType(Var->getType(),
4567f4a2713aSLionel Sambuc ParamType.getNonReferenceType())) {
4568f4a2713aSLionel Sambuc S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
4569f4a2713aSLionel Sambuc << ParamType;
4570f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4571f4a2713aSLionel Sambuc return true;
4572f4a2713aSLionel Sambuc }
4573f4a2713aSLionel Sambuc
4574f4a2713aSLionel Sambuc S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
4575f4a2713aSLionel Sambuc << ParamType
4576f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(AddrOpLoc);
4577f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4578f4a2713aSLionel Sambuc
4579f4a2713aSLionel Sambuc ArgType = Var->getType();
4580f4a2713aSLionel Sambuc }
4581f4a2713aSLionel Sambuc } else if (!AddressTaken && ParamType->isPointerType()) {
4582f4a2713aSLionel Sambuc if (Var->getType()->isArrayType()) {
4583f4a2713aSLionel Sambuc // Array-to-pointer decay.
4584f4a2713aSLionel Sambuc ArgType = S.Context.getArrayDecayedType(Var->getType());
4585f4a2713aSLionel Sambuc } else {
4586f4a2713aSLionel Sambuc // If the template parameter has pointer type but the address of
4587f4a2713aSLionel Sambuc // this object was not taken, complain and (possibly) recover by
4588f4a2713aSLionel Sambuc // taking the address of the entity.
4589f4a2713aSLionel Sambuc ArgType = S.Context.getPointerType(Var->getType());
4590f4a2713aSLionel Sambuc if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
4591f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
4592f4a2713aSLionel Sambuc << ParamType;
4593f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4594f4a2713aSLionel Sambuc return true;
4595f4a2713aSLionel Sambuc }
4596f4a2713aSLionel Sambuc
4597f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
4598f4a2713aSLionel Sambuc << ParamType
4599f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
4600f4a2713aSLionel Sambuc
4601f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4602f4a2713aSLionel Sambuc }
4603f4a2713aSLionel Sambuc }
4604f4a2713aSLionel Sambuc }
4605f4a2713aSLionel Sambuc
4606f4a2713aSLionel Sambuc if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn,
4607f4a2713aSLionel Sambuc Arg, ArgType))
4608f4a2713aSLionel Sambuc return true;
4609f4a2713aSLionel Sambuc
4610f4a2713aSLionel Sambuc // Create the template argument.
4611*0a6a1f1dSLionel Sambuc Converted =
4612*0a6a1f1dSLionel Sambuc TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType);
4613f4a2713aSLionel Sambuc S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false);
4614f4a2713aSLionel Sambuc return false;
4615f4a2713aSLionel Sambuc }
4616f4a2713aSLionel Sambuc
4617f4a2713aSLionel Sambuc /// \brief Checks whether the given template argument is a pointer to
4618f4a2713aSLionel Sambuc /// member constant according to C++ [temp.arg.nontype]p1.
CheckTemplateArgumentPointerToMember(Sema & S,NonTypeTemplateParmDecl * Param,QualType ParamType,Expr * & ResultArg,TemplateArgument & Converted)4619f4a2713aSLionel Sambuc static bool CheckTemplateArgumentPointerToMember(Sema &S,
4620f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *Param,
4621f4a2713aSLionel Sambuc QualType ParamType,
4622f4a2713aSLionel Sambuc Expr *&ResultArg,
4623f4a2713aSLionel Sambuc TemplateArgument &Converted) {
4624f4a2713aSLionel Sambuc bool Invalid = false;
4625f4a2713aSLionel Sambuc
4626f4a2713aSLionel Sambuc // Check for a null pointer value.
4627f4a2713aSLionel Sambuc Expr *Arg = ResultArg;
4628f4a2713aSLionel Sambuc switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) {
4629f4a2713aSLionel Sambuc case NPV_Error:
4630f4a2713aSLionel Sambuc return true;
4631f4a2713aSLionel Sambuc case NPV_NullPointer:
4632f4a2713aSLionel Sambuc S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null);
4633*0a6a1f1dSLionel Sambuc Converted = TemplateArgument(S.Context.getCanonicalType(ParamType),
4634*0a6a1f1dSLionel Sambuc /*isNullPtr*/true);
4635*0a6a1f1dSLionel Sambuc if (S.Context.getTargetInfo().getCXXABI().isMicrosoft())
4636*0a6a1f1dSLionel Sambuc S.RequireCompleteType(Arg->getExprLoc(), ParamType, 0);
4637f4a2713aSLionel Sambuc return false;
4638f4a2713aSLionel Sambuc case NPV_NotNullPointer:
4639f4a2713aSLionel Sambuc break;
4640f4a2713aSLionel Sambuc }
4641f4a2713aSLionel Sambuc
4642f4a2713aSLionel Sambuc bool ObjCLifetimeConversion;
4643f4a2713aSLionel Sambuc if (S.IsQualificationConversion(Arg->getType(),
4644f4a2713aSLionel Sambuc ParamType.getNonReferenceType(),
4645f4a2713aSLionel Sambuc false, ObjCLifetimeConversion)) {
4646f4a2713aSLionel Sambuc Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp,
4647*0a6a1f1dSLionel Sambuc Arg->getValueKind()).get();
4648f4a2713aSLionel Sambuc ResultArg = Arg;
4649f4a2713aSLionel Sambuc } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(),
4650f4a2713aSLionel Sambuc ParamType.getNonReferenceType())) {
4651f4a2713aSLionel Sambuc // We can't perform this conversion.
4652f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
4653f4a2713aSLionel Sambuc << Arg->getType() << ParamType << Arg->getSourceRange();
4654f4a2713aSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here);
4655f4a2713aSLionel Sambuc return true;
4656f4a2713aSLionel Sambuc }
4657f4a2713aSLionel Sambuc
4658f4a2713aSLionel Sambuc // See through any implicit casts we added to fix the type.
4659f4a2713aSLionel Sambuc while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
4660f4a2713aSLionel Sambuc Arg = Cast->getSubExpr();
4661f4a2713aSLionel Sambuc
4662f4a2713aSLionel Sambuc // C++ [temp.arg.nontype]p1:
4663f4a2713aSLionel Sambuc //
4664f4a2713aSLionel Sambuc // A template-argument for a non-type, non-template
4665f4a2713aSLionel Sambuc // template-parameter shall be one of: [...]
4666f4a2713aSLionel Sambuc //
4667f4a2713aSLionel Sambuc // -- a pointer to member expressed as described in 5.3.1.
4668*0a6a1f1dSLionel Sambuc DeclRefExpr *DRE = nullptr;
4669f4a2713aSLionel Sambuc
4670f4a2713aSLionel Sambuc // In C++98/03 mode, give an extension warning on any extra parentheses.
4671f4a2713aSLionel Sambuc // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
4672f4a2713aSLionel Sambuc bool ExtraParens = false;
4673f4a2713aSLionel Sambuc while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
4674f4a2713aSLionel Sambuc if (!Invalid && !ExtraParens) {
4675f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(),
4676f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus11 ?
4677f4a2713aSLionel Sambuc diag::warn_cxx98_compat_template_arg_extra_parens :
4678f4a2713aSLionel Sambuc diag::ext_template_arg_extra_parens)
4679f4a2713aSLionel Sambuc << Arg->getSourceRange();
4680f4a2713aSLionel Sambuc ExtraParens = true;
4681f4a2713aSLionel Sambuc }
4682f4a2713aSLionel Sambuc
4683f4a2713aSLionel Sambuc Arg = Parens->getSubExpr();
4684f4a2713aSLionel Sambuc }
4685f4a2713aSLionel Sambuc
4686f4a2713aSLionel Sambuc while (SubstNonTypeTemplateParmExpr *subst =
4687f4a2713aSLionel Sambuc dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
4688f4a2713aSLionel Sambuc Arg = subst->getReplacement()->IgnoreImpCasts();
4689f4a2713aSLionel Sambuc
4690f4a2713aSLionel Sambuc // A pointer-to-member constant written &Class::member.
4691f4a2713aSLionel Sambuc if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
4692f4a2713aSLionel Sambuc if (UnOp->getOpcode() == UO_AddrOf) {
4693f4a2713aSLionel Sambuc DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
4694f4a2713aSLionel Sambuc if (DRE && !DRE->getQualifier())
4695*0a6a1f1dSLionel Sambuc DRE = nullptr;
4696f4a2713aSLionel Sambuc }
4697f4a2713aSLionel Sambuc }
4698f4a2713aSLionel Sambuc // A constant of pointer-to-member type.
4699f4a2713aSLionel Sambuc else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
4700f4a2713aSLionel Sambuc if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
4701f4a2713aSLionel Sambuc if (VD->getType()->isMemberPointerType()) {
4702*0a6a1f1dSLionel Sambuc if (isa<NonTypeTemplateParmDecl>(VD)) {
4703f4a2713aSLionel Sambuc if (Arg->isTypeDependent() || Arg->isValueDependent()) {
4704f4a2713aSLionel Sambuc Converted = TemplateArgument(Arg);
4705f4a2713aSLionel Sambuc } else {
4706f4a2713aSLionel Sambuc VD = cast<ValueDecl>(VD->getCanonicalDecl());
4707*0a6a1f1dSLionel Sambuc Converted = TemplateArgument(VD, ParamType);
4708f4a2713aSLionel Sambuc }
4709f4a2713aSLionel Sambuc return Invalid;
4710f4a2713aSLionel Sambuc }
4711f4a2713aSLionel Sambuc }
4712f4a2713aSLionel Sambuc }
4713f4a2713aSLionel Sambuc
4714*0a6a1f1dSLionel Sambuc DRE = nullptr;
4715f4a2713aSLionel Sambuc }
4716f4a2713aSLionel Sambuc
4717f4a2713aSLionel Sambuc if (!DRE)
4718f4a2713aSLionel Sambuc return S.Diag(Arg->getLocStart(),
4719f4a2713aSLionel Sambuc diag::err_template_arg_not_pointer_to_member_form)
4720f4a2713aSLionel Sambuc << Arg->getSourceRange();
4721f4a2713aSLionel Sambuc
4722f4a2713aSLionel Sambuc if (isa<FieldDecl>(DRE->getDecl()) ||
4723f4a2713aSLionel Sambuc isa<IndirectFieldDecl>(DRE->getDecl()) ||
4724f4a2713aSLionel Sambuc isa<CXXMethodDecl>(DRE->getDecl())) {
4725f4a2713aSLionel Sambuc assert((isa<FieldDecl>(DRE->getDecl()) ||
4726f4a2713aSLionel Sambuc isa<IndirectFieldDecl>(DRE->getDecl()) ||
4727f4a2713aSLionel Sambuc !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
4728f4a2713aSLionel Sambuc "Only non-static member pointers can make it here");
4729f4a2713aSLionel Sambuc
4730f4a2713aSLionel Sambuc // Okay: this is the address of a non-static member, and therefore
4731f4a2713aSLionel Sambuc // a member pointer constant.
4732f4a2713aSLionel Sambuc if (Arg->isTypeDependent() || Arg->isValueDependent()) {
4733f4a2713aSLionel Sambuc Converted = TemplateArgument(Arg);
4734f4a2713aSLionel Sambuc } else {
4735f4a2713aSLionel Sambuc ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl());
4736*0a6a1f1dSLionel Sambuc Converted = TemplateArgument(D, ParamType);
4737f4a2713aSLionel Sambuc }
4738f4a2713aSLionel Sambuc return Invalid;
4739f4a2713aSLionel Sambuc }
4740f4a2713aSLionel Sambuc
4741f4a2713aSLionel Sambuc // We found something else, but we don't know specifically what it is.
4742f4a2713aSLionel Sambuc S.Diag(Arg->getLocStart(),
4743f4a2713aSLionel Sambuc diag::err_template_arg_not_pointer_to_member_form)
4744f4a2713aSLionel Sambuc << Arg->getSourceRange();
4745f4a2713aSLionel Sambuc S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
4746f4a2713aSLionel Sambuc return true;
4747f4a2713aSLionel Sambuc }
4748f4a2713aSLionel Sambuc
4749f4a2713aSLionel Sambuc /// \brief Check a template argument against its corresponding
4750f4a2713aSLionel Sambuc /// non-type template parameter.
4751f4a2713aSLionel Sambuc ///
4752f4a2713aSLionel Sambuc /// This routine implements the semantics of C++ [temp.arg.nontype].
4753f4a2713aSLionel Sambuc /// If an error occurred, it returns ExprError(); otherwise, it
4754*0a6a1f1dSLionel Sambuc /// returns the converted template argument. \p ParamType is the
4755*0a6a1f1dSLionel Sambuc /// type of the non-type template parameter after it has been instantiated.
CheckTemplateArgument(NonTypeTemplateParmDecl * Param,QualType ParamType,Expr * Arg,TemplateArgument & Converted,CheckTemplateArgumentKind CTAK)4756f4a2713aSLionel Sambuc ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
4757*0a6a1f1dSLionel Sambuc QualType ParamType, Expr *Arg,
4758f4a2713aSLionel Sambuc TemplateArgument &Converted,
4759f4a2713aSLionel Sambuc CheckTemplateArgumentKind CTAK) {
4760f4a2713aSLionel Sambuc SourceLocation StartLoc = Arg->getLocStart();
4761f4a2713aSLionel Sambuc
4762f4a2713aSLionel Sambuc // If either the parameter has a dependent type or the argument is
4763f4a2713aSLionel Sambuc // type-dependent, there's nothing we can check now.
4764*0a6a1f1dSLionel Sambuc if (ParamType->isDependentType() || Arg->isTypeDependent()) {
4765f4a2713aSLionel Sambuc // FIXME: Produce a cloned, canonical expression?
4766f4a2713aSLionel Sambuc Converted = TemplateArgument(Arg);
4767*0a6a1f1dSLionel Sambuc return Arg;
4768f4a2713aSLionel Sambuc }
4769f4a2713aSLionel Sambuc
4770*0a6a1f1dSLionel Sambuc // We should have already dropped all cv-qualifiers by now.
4771*0a6a1f1dSLionel Sambuc assert(!ParamType.hasQualifiers() &&
4772*0a6a1f1dSLionel Sambuc "non-type template parameter type cannot be qualified");
4773f4a2713aSLionel Sambuc
4774f4a2713aSLionel Sambuc if (CTAK == CTAK_Deduced &&
4775f4a2713aSLionel Sambuc !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) {
4776f4a2713aSLionel Sambuc // C++ [temp.deduct.type]p17:
4777f4a2713aSLionel Sambuc // If, in the declaration of a function template with a non-type
4778f4a2713aSLionel Sambuc // template-parameter, the non-type template-parameter is used
4779f4a2713aSLionel Sambuc // in an expression in the function parameter-list and, if the
4780f4a2713aSLionel Sambuc // corresponding template-argument is deduced, the
4781f4a2713aSLionel Sambuc // template-argument type shall match the type of the
4782f4a2713aSLionel Sambuc // template-parameter exactly, except that a template-argument
4783f4a2713aSLionel Sambuc // deduced from an array bound may be of any integral type.
4784f4a2713aSLionel Sambuc Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
4785f4a2713aSLionel Sambuc << Arg->getType().getUnqualifiedType()
4786f4a2713aSLionel Sambuc << ParamType.getUnqualifiedType();
4787f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
4788f4a2713aSLionel Sambuc return ExprError();
4789f4a2713aSLionel Sambuc }
4790f4a2713aSLionel Sambuc
4791*0a6a1f1dSLionel Sambuc if (getLangOpts().CPlusPlus1z) {
4792*0a6a1f1dSLionel Sambuc // FIXME: We can do some limited checking for a value-dependent but not
4793*0a6a1f1dSLionel Sambuc // type-dependent argument.
4794*0a6a1f1dSLionel Sambuc if (Arg->isValueDependent()) {
4795*0a6a1f1dSLionel Sambuc Converted = TemplateArgument(Arg);
4796*0a6a1f1dSLionel Sambuc return Arg;
4797*0a6a1f1dSLionel Sambuc }
4798*0a6a1f1dSLionel Sambuc
4799*0a6a1f1dSLionel Sambuc // C++1z [temp.arg.nontype]p1:
4800*0a6a1f1dSLionel Sambuc // A template-argument for a non-type template parameter shall be
4801*0a6a1f1dSLionel Sambuc // a converted constant expression of the type of the template-parameter.
4802*0a6a1f1dSLionel Sambuc APValue Value;
4803*0a6a1f1dSLionel Sambuc ExprResult ArgResult = CheckConvertedConstantExpression(
4804*0a6a1f1dSLionel Sambuc Arg, ParamType, Value, CCEK_TemplateArg);
4805*0a6a1f1dSLionel Sambuc if (ArgResult.isInvalid())
4806*0a6a1f1dSLionel Sambuc return ExprError();
4807*0a6a1f1dSLionel Sambuc
4808*0a6a1f1dSLionel Sambuc QualType CanonParamType = Context.getCanonicalType(ParamType);
4809*0a6a1f1dSLionel Sambuc
4810*0a6a1f1dSLionel Sambuc // Convert the APValue to a TemplateArgument.
4811*0a6a1f1dSLionel Sambuc switch (Value.getKind()) {
4812*0a6a1f1dSLionel Sambuc case APValue::Uninitialized:
4813*0a6a1f1dSLionel Sambuc assert(ParamType->isNullPtrType());
4814*0a6a1f1dSLionel Sambuc Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true);
4815*0a6a1f1dSLionel Sambuc break;
4816*0a6a1f1dSLionel Sambuc case APValue::Int:
4817*0a6a1f1dSLionel Sambuc assert(ParamType->isIntegralOrEnumerationType());
4818*0a6a1f1dSLionel Sambuc Converted = TemplateArgument(Context, Value.getInt(), CanonParamType);
4819*0a6a1f1dSLionel Sambuc break;
4820*0a6a1f1dSLionel Sambuc case APValue::MemberPointer: {
4821*0a6a1f1dSLionel Sambuc assert(ParamType->isMemberPointerType());
4822*0a6a1f1dSLionel Sambuc
4823*0a6a1f1dSLionel Sambuc // FIXME: We need TemplateArgument representation and mangling for these.
4824*0a6a1f1dSLionel Sambuc if (!Value.getMemberPointerPath().empty()) {
4825*0a6a1f1dSLionel Sambuc Diag(Arg->getLocStart(),
4826*0a6a1f1dSLionel Sambuc diag::err_template_arg_member_ptr_base_derived_not_supported)
4827*0a6a1f1dSLionel Sambuc << Value.getMemberPointerDecl() << ParamType
4828*0a6a1f1dSLionel Sambuc << Arg->getSourceRange();
4829*0a6a1f1dSLionel Sambuc return ExprError();
4830*0a6a1f1dSLionel Sambuc }
4831*0a6a1f1dSLionel Sambuc
4832*0a6a1f1dSLionel Sambuc auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl());
4833*0a6a1f1dSLionel Sambuc Converted = VD ? TemplateArgument(VD, CanonParamType)
4834*0a6a1f1dSLionel Sambuc : TemplateArgument(CanonParamType, /*isNullPtr*/true);
4835*0a6a1f1dSLionel Sambuc break;
4836*0a6a1f1dSLionel Sambuc }
4837*0a6a1f1dSLionel Sambuc case APValue::LValue: {
4838*0a6a1f1dSLionel Sambuc // For a non-type template-parameter of pointer or reference type,
4839*0a6a1f1dSLionel Sambuc // the value of the constant expression shall not refer to
4840*0a6a1f1dSLionel Sambuc assert(ParamType->isPointerType() || ParamType->isReferenceType() ||
4841*0a6a1f1dSLionel Sambuc ParamType->isNullPtrType());
4842*0a6a1f1dSLionel Sambuc // -- a temporary object
4843*0a6a1f1dSLionel Sambuc // -- a string literal
4844*0a6a1f1dSLionel Sambuc // -- the result of a typeid expression, or
4845*0a6a1f1dSLionel Sambuc // -- a predefind __func__ variable
4846*0a6a1f1dSLionel Sambuc if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) {
4847*0a6a1f1dSLionel Sambuc if (isa<CXXUuidofExpr>(E)) {
4848*0a6a1f1dSLionel Sambuc Converted = TemplateArgument(const_cast<Expr*>(E));
4849*0a6a1f1dSLionel Sambuc break;
4850*0a6a1f1dSLionel Sambuc }
4851*0a6a1f1dSLionel Sambuc Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
4852*0a6a1f1dSLionel Sambuc << Arg->getSourceRange();
4853*0a6a1f1dSLionel Sambuc return ExprError();
4854*0a6a1f1dSLionel Sambuc }
4855*0a6a1f1dSLionel Sambuc auto *VD = const_cast<ValueDecl *>(
4856*0a6a1f1dSLionel Sambuc Value.getLValueBase().dyn_cast<const ValueDecl *>());
4857*0a6a1f1dSLionel Sambuc // -- a subobject
4858*0a6a1f1dSLionel Sambuc if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 &&
4859*0a6a1f1dSLionel Sambuc VD && VD->getType()->isArrayType() &&
4860*0a6a1f1dSLionel Sambuc Value.getLValuePath()[0].ArrayIndex == 0 &&
4861*0a6a1f1dSLionel Sambuc !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) {
4862*0a6a1f1dSLionel Sambuc // Per defect report (no number yet):
4863*0a6a1f1dSLionel Sambuc // ... other than a pointer to the first element of a complete array
4864*0a6a1f1dSLionel Sambuc // object.
4865*0a6a1f1dSLionel Sambuc } else if (!Value.hasLValuePath() || Value.getLValuePath().size() ||
4866*0a6a1f1dSLionel Sambuc Value.isLValueOnePastTheEnd()) {
4867*0a6a1f1dSLionel Sambuc Diag(StartLoc, diag::err_non_type_template_arg_subobject)
4868*0a6a1f1dSLionel Sambuc << Value.getAsString(Context, ParamType);
4869*0a6a1f1dSLionel Sambuc return ExprError();
4870*0a6a1f1dSLionel Sambuc }
4871*0a6a1f1dSLionel Sambuc assert((VD || !ParamType->isReferenceType()) &&
4872*0a6a1f1dSLionel Sambuc "null reference should not be a constant expression");
4873*0a6a1f1dSLionel Sambuc assert((!VD || !ParamType->isNullPtrType()) &&
4874*0a6a1f1dSLionel Sambuc "non-null value of type nullptr_t?");
4875*0a6a1f1dSLionel Sambuc Converted = VD ? TemplateArgument(VD, CanonParamType)
4876*0a6a1f1dSLionel Sambuc : TemplateArgument(CanonParamType, /*isNullPtr*/true);
4877*0a6a1f1dSLionel Sambuc break;
4878*0a6a1f1dSLionel Sambuc }
4879*0a6a1f1dSLionel Sambuc case APValue::AddrLabelDiff:
4880*0a6a1f1dSLionel Sambuc return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff);
4881*0a6a1f1dSLionel Sambuc case APValue::Float:
4882*0a6a1f1dSLionel Sambuc case APValue::ComplexInt:
4883*0a6a1f1dSLionel Sambuc case APValue::ComplexFloat:
4884*0a6a1f1dSLionel Sambuc case APValue::Vector:
4885*0a6a1f1dSLionel Sambuc case APValue::Array:
4886*0a6a1f1dSLionel Sambuc case APValue::Struct:
4887*0a6a1f1dSLionel Sambuc case APValue::Union:
4888*0a6a1f1dSLionel Sambuc llvm_unreachable("invalid kind for template argument");
4889*0a6a1f1dSLionel Sambuc }
4890*0a6a1f1dSLionel Sambuc
4891*0a6a1f1dSLionel Sambuc return ArgResult.get();
4892*0a6a1f1dSLionel Sambuc }
4893*0a6a1f1dSLionel Sambuc
4894*0a6a1f1dSLionel Sambuc // C++ [temp.arg.nontype]p5:
4895*0a6a1f1dSLionel Sambuc // The following conversions are performed on each expression used
4896*0a6a1f1dSLionel Sambuc // as a non-type template-argument. If a non-type
4897*0a6a1f1dSLionel Sambuc // template-argument cannot be converted to the type of the
4898*0a6a1f1dSLionel Sambuc // corresponding template-parameter then the program is
4899*0a6a1f1dSLionel Sambuc // ill-formed.
4900*0a6a1f1dSLionel Sambuc if (ParamType->isIntegralOrEnumerationType()) {
4901*0a6a1f1dSLionel Sambuc // C++11:
4902*0a6a1f1dSLionel Sambuc // -- for a non-type template-parameter of integral or
4903*0a6a1f1dSLionel Sambuc // enumeration type, conversions permitted in a converted
4904*0a6a1f1dSLionel Sambuc // constant expression are applied.
4905*0a6a1f1dSLionel Sambuc //
4906*0a6a1f1dSLionel Sambuc // C++98:
4907*0a6a1f1dSLionel Sambuc // -- for a non-type template-parameter of integral or
4908*0a6a1f1dSLionel Sambuc // enumeration type, integral promotions (4.5) and integral
4909*0a6a1f1dSLionel Sambuc // conversions (4.7) are applied.
4910*0a6a1f1dSLionel Sambuc
4911f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11) {
4912f4a2713aSLionel Sambuc // We can't check arbitrary value-dependent arguments.
4913f4a2713aSLionel Sambuc // FIXME: If there's no viable conversion to the template parameter type,
4914f4a2713aSLionel Sambuc // we should be able to diagnose that prior to instantiation.
4915f4a2713aSLionel Sambuc if (Arg->isValueDependent()) {
4916f4a2713aSLionel Sambuc Converted = TemplateArgument(Arg);
4917*0a6a1f1dSLionel Sambuc return Arg;
4918f4a2713aSLionel Sambuc }
4919f4a2713aSLionel Sambuc
4920f4a2713aSLionel Sambuc // C++ [temp.arg.nontype]p1:
4921f4a2713aSLionel Sambuc // A template-argument for a non-type, non-template template-parameter
4922f4a2713aSLionel Sambuc // shall be one of:
4923f4a2713aSLionel Sambuc //
4924f4a2713aSLionel Sambuc // -- for a non-type template-parameter of integral or enumeration
4925f4a2713aSLionel Sambuc // type, a converted constant expression of the type of the
4926f4a2713aSLionel Sambuc // template-parameter; or
4927f4a2713aSLionel Sambuc llvm::APSInt Value;
4928f4a2713aSLionel Sambuc ExprResult ArgResult =
4929f4a2713aSLionel Sambuc CheckConvertedConstantExpression(Arg, ParamType, Value,
4930f4a2713aSLionel Sambuc CCEK_TemplateArg);
4931f4a2713aSLionel Sambuc if (ArgResult.isInvalid())
4932f4a2713aSLionel Sambuc return ExprError();
4933f4a2713aSLionel Sambuc
4934f4a2713aSLionel Sambuc // Widen the argument value to sizeof(parameter type). This is almost
4935f4a2713aSLionel Sambuc // always a no-op, except when the parameter type is bool. In
4936f4a2713aSLionel Sambuc // that case, this may extend the argument from 1 bit to 8 bits.
4937f4a2713aSLionel Sambuc QualType IntegerType = ParamType;
4938f4a2713aSLionel Sambuc if (const EnumType *Enum = IntegerType->getAs<EnumType>())
4939f4a2713aSLionel Sambuc IntegerType = Enum->getDecl()->getIntegerType();
4940f4a2713aSLionel Sambuc Value = Value.extOrTrunc(Context.getTypeSize(IntegerType));
4941f4a2713aSLionel Sambuc
4942f4a2713aSLionel Sambuc Converted = TemplateArgument(Context, Value,
4943f4a2713aSLionel Sambuc Context.getCanonicalType(ParamType));
4944f4a2713aSLionel Sambuc return ArgResult;
4945f4a2713aSLionel Sambuc }
4946f4a2713aSLionel Sambuc
4947f4a2713aSLionel Sambuc ExprResult ArgResult = DefaultLvalueConversion(Arg);
4948f4a2713aSLionel Sambuc if (ArgResult.isInvalid())
4949f4a2713aSLionel Sambuc return ExprError();
4950*0a6a1f1dSLionel Sambuc Arg = ArgResult.get();
4951f4a2713aSLionel Sambuc
4952f4a2713aSLionel Sambuc QualType ArgType = Arg->getType();
4953f4a2713aSLionel Sambuc
4954f4a2713aSLionel Sambuc // C++ [temp.arg.nontype]p1:
4955f4a2713aSLionel Sambuc // A template-argument for a non-type, non-template
4956f4a2713aSLionel Sambuc // template-parameter shall be one of:
4957f4a2713aSLionel Sambuc //
4958f4a2713aSLionel Sambuc // -- an integral constant-expression of integral or enumeration
4959f4a2713aSLionel Sambuc // type; or
4960f4a2713aSLionel Sambuc // -- the name of a non-type template-parameter; or
4961f4a2713aSLionel Sambuc SourceLocation NonConstantLoc;
4962f4a2713aSLionel Sambuc llvm::APSInt Value;
4963f4a2713aSLionel Sambuc if (!ArgType->isIntegralOrEnumerationType()) {
4964f4a2713aSLionel Sambuc Diag(Arg->getLocStart(),
4965f4a2713aSLionel Sambuc diag::err_template_arg_not_integral_or_enumeral)
4966f4a2713aSLionel Sambuc << ArgType << Arg->getSourceRange();
4967f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
4968f4a2713aSLionel Sambuc return ExprError();
4969f4a2713aSLionel Sambuc } else if (!Arg->isValueDependent()) {
4970f4a2713aSLionel Sambuc class TmplArgICEDiagnoser : public VerifyICEDiagnoser {
4971f4a2713aSLionel Sambuc QualType T;
4972f4a2713aSLionel Sambuc
4973f4a2713aSLionel Sambuc public:
4974f4a2713aSLionel Sambuc TmplArgICEDiagnoser(QualType T) : T(T) { }
4975f4a2713aSLionel Sambuc
4976*0a6a1f1dSLionel Sambuc void diagnoseNotICE(Sema &S, SourceLocation Loc,
4977*0a6a1f1dSLionel Sambuc SourceRange SR) override {
4978f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR;
4979f4a2713aSLionel Sambuc }
4980f4a2713aSLionel Sambuc } Diagnoser(ArgType);
4981f4a2713aSLionel Sambuc
4982f4a2713aSLionel Sambuc Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser,
4983*0a6a1f1dSLionel Sambuc false).get();
4984f4a2713aSLionel Sambuc if (!Arg)
4985f4a2713aSLionel Sambuc return ExprError();
4986f4a2713aSLionel Sambuc }
4987f4a2713aSLionel Sambuc
4988*0a6a1f1dSLionel Sambuc // From here on out, all we care about is the unqualified form
4989*0a6a1f1dSLionel Sambuc // of the argument type.
4990f4a2713aSLionel Sambuc ArgType = ArgType.getUnqualifiedType();
4991f4a2713aSLionel Sambuc
4992f4a2713aSLionel Sambuc // Try to convert the argument to the parameter's type.
4993f4a2713aSLionel Sambuc if (Context.hasSameType(ParamType, ArgType)) {
4994f4a2713aSLionel Sambuc // Okay: no conversion necessary
4995f4a2713aSLionel Sambuc } else if (ParamType->isBooleanType()) {
4996f4a2713aSLionel Sambuc // This is an integral-to-boolean conversion.
4997*0a6a1f1dSLionel Sambuc Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get();
4998f4a2713aSLionel Sambuc } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
4999f4a2713aSLionel Sambuc !ParamType->isEnumeralType()) {
5000f4a2713aSLionel Sambuc // This is an integral promotion or conversion.
5001*0a6a1f1dSLionel Sambuc Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get();
5002f4a2713aSLionel Sambuc } else {
5003f4a2713aSLionel Sambuc // We can't perform this conversion.
5004f4a2713aSLionel Sambuc Diag(Arg->getLocStart(),
5005f4a2713aSLionel Sambuc diag::err_template_arg_not_convertible)
5006*0a6a1f1dSLionel Sambuc << Arg->getType() << ParamType << Arg->getSourceRange();
5007f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
5008f4a2713aSLionel Sambuc return ExprError();
5009f4a2713aSLionel Sambuc }
5010f4a2713aSLionel Sambuc
5011f4a2713aSLionel Sambuc // Add the value of this argument to the list of converted
5012f4a2713aSLionel Sambuc // arguments. We use the bitwidth and signedness of the template
5013f4a2713aSLionel Sambuc // parameter.
5014f4a2713aSLionel Sambuc if (Arg->isValueDependent()) {
5015f4a2713aSLionel Sambuc // The argument is value-dependent. Create a new
5016f4a2713aSLionel Sambuc // TemplateArgument with the converted expression.
5017f4a2713aSLionel Sambuc Converted = TemplateArgument(Arg);
5018*0a6a1f1dSLionel Sambuc return Arg;
5019f4a2713aSLionel Sambuc }
5020f4a2713aSLionel Sambuc
5021f4a2713aSLionel Sambuc QualType IntegerType = Context.getCanonicalType(ParamType);
5022f4a2713aSLionel Sambuc if (const EnumType *Enum = IntegerType->getAs<EnumType>())
5023f4a2713aSLionel Sambuc IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
5024f4a2713aSLionel Sambuc
5025f4a2713aSLionel Sambuc if (ParamType->isBooleanType()) {
5026f4a2713aSLionel Sambuc // Value must be zero or one.
5027f4a2713aSLionel Sambuc Value = Value != 0;
5028f4a2713aSLionel Sambuc unsigned AllowedBits = Context.getTypeSize(IntegerType);
5029f4a2713aSLionel Sambuc if (Value.getBitWidth() != AllowedBits)
5030f4a2713aSLionel Sambuc Value = Value.extOrTrunc(AllowedBits);
5031f4a2713aSLionel Sambuc Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
5032f4a2713aSLionel Sambuc } else {
5033f4a2713aSLionel Sambuc llvm::APSInt OldValue = Value;
5034f4a2713aSLionel Sambuc
5035f4a2713aSLionel Sambuc // Coerce the template argument's value to the value it will have
5036f4a2713aSLionel Sambuc // based on the template parameter's type.
5037f4a2713aSLionel Sambuc unsigned AllowedBits = Context.getTypeSize(IntegerType);
5038f4a2713aSLionel Sambuc if (Value.getBitWidth() != AllowedBits)
5039f4a2713aSLionel Sambuc Value = Value.extOrTrunc(AllowedBits);
5040f4a2713aSLionel Sambuc Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
5041f4a2713aSLionel Sambuc
5042f4a2713aSLionel Sambuc // Complain if an unsigned parameter received a negative value.
5043f4a2713aSLionel Sambuc if (IntegerType->isUnsignedIntegerOrEnumerationType()
5044f4a2713aSLionel Sambuc && (OldValue.isSigned() && OldValue.isNegative())) {
5045f4a2713aSLionel Sambuc Diag(Arg->getLocStart(), diag::warn_template_arg_negative)
5046f4a2713aSLionel Sambuc << OldValue.toString(10) << Value.toString(10) << Param->getType()
5047f4a2713aSLionel Sambuc << Arg->getSourceRange();
5048f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
5049f4a2713aSLionel Sambuc }
5050f4a2713aSLionel Sambuc
5051f4a2713aSLionel Sambuc // Complain if we overflowed the template parameter's type.
5052f4a2713aSLionel Sambuc unsigned RequiredBits;
5053f4a2713aSLionel Sambuc if (IntegerType->isUnsignedIntegerOrEnumerationType())
5054f4a2713aSLionel Sambuc RequiredBits = OldValue.getActiveBits();
5055f4a2713aSLionel Sambuc else if (OldValue.isUnsigned())
5056f4a2713aSLionel Sambuc RequiredBits = OldValue.getActiveBits() + 1;
5057f4a2713aSLionel Sambuc else
5058f4a2713aSLionel Sambuc RequiredBits = OldValue.getMinSignedBits();
5059f4a2713aSLionel Sambuc if (RequiredBits > AllowedBits) {
5060f4a2713aSLionel Sambuc Diag(Arg->getLocStart(),
5061f4a2713aSLionel Sambuc diag::warn_template_arg_too_large)
5062f4a2713aSLionel Sambuc << OldValue.toString(10) << Value.toString(10) << Param->getType()
5063f4a2713aSLionel Sambuc << Arg->getSourceRange();
5064f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
5065f4a2713aSLionel Sambuc }
5066f4a2713aSLionel Sambuc }
5067f4a2713aSLionel Sambuc
5068f4a2713aSLionel Sambuc Converted = TemplateArgument(Context, Value,
5069f4a2713aSLionel Sambuc ParamType->isEnumeralType()
5070f4a2713aSLionel Sambuc ? Context.getCanonicalType(ParamType)
5071f4a2713aSLionel Sambuc : IntegerType);
5072*0a6a1f1dSLionel Sambuc return Arg;
5073f4a2713aSLionel Sambuc }
5074f4a2713aSLionel Sambuc
5075f4a2713aSLionel Sambuc QualType ArgType = Arg->getType();
5076f4a2713aSLionel Sambuc DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
5077f4a2713aSLionel Sambuc
5078f4a2713aSLionel Sambuc // Handle pointer-to-function, reference-to-function, and
5079f4a2713aSLionel Sambuc // pointer-to-member-function all in (roughly) the same way.
5080f4a2713aSLionel Sambuc if (// -- For a non-type template-parameter of type pointer to
5081f4a2713aSLionel Sambuc // function, only the function-to-pointer conversion (4.3) is
5082f4a2713aSLionel Sambuc // applied. If the template-argument represents a set of
5083f4a2713aSLionel Sambuc // overloaded functions (or a pointer to such), the matching
5084f4a2713aSLionel Sambuc // function is selected from the set (13.4).
5085f4a2713aSLionel Sambuc (ParamType->isPointerType() &&
5086f4a2713aSLionel Sambuc ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
5087f4a2713aSLionel Sambuc // -- For a non-type template-parameter of type reference to
5088f4a2713aSLionel Sambuc // function, no conversions apply. If the template-argument
5089f4a2713aSLionel Sambuc // represents a set of overloaded functions, the matching
5090f4a2713aSLionel Sambuc // function is selected from the set (13.4).
5091f4a2713aSLionel Sambuc (ParamType->isReferenceType() &&
5092f4a2713aSLionel Sambuc ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
5093f4a2713aSLionel Sambuc // -- For a non-type template-parameter of type pointer to
5094f4a2713aSLionel Sambuc // member function, no conversions apply. If the
5095f4a2713aSLionel Sambuc // template-argument represents a set of overloaded member
5096f4a2713aSLionel Sambuc // functions, the matching member function is selected from
5097f4a2713aSLionel Sambuc // the set (13.4).
5098f4a2713aSLionel Sambuc (ParamType->isMemberPointerType() &&
5099f4a2713aSLionel Sambuc ParamType->getAs<MemberPointerType>()->getPointeeType()
5100f4a2713aSLionel Sambuc ->isFunctionType())) {
5101f4a2713aSLionel Sambuc
5102f4a2713aSLionel Sambuc if (Arg->getType() == Context.OverloadTy) {
5103f4a2713aSLionel Sambuc if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
5104f4a2713aSLionel Sambuc true,
5105f4a2713aSLionel Sambuc FoundResult)) {
5106f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(Fn, Arg->getLocStart()))
5107f4a2713aSLionel Sambuc return ExprError();
5108f4a2713aSLionel Sambuc
5109f4a2713aSLionel Sambuc Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
5110f4a2713aSLionel Sambuc ArgType = Arg->getType();
5111f4a2713aSLionel Sambuc } else
5112f4a2713aSLionel Sambuc return ExprError();
5113f4a2713aSLionel Sambuc }
5114f4a2713aSLionel Sambuc
5115f4a2713aSLionel Sambuc if (!ParamType->isMemberPointerType()) {
5116f4a2713aSLionel Sambuc if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
5117f4a2713aSLionel Sambuc ParamType,
5118f4a2713aSLionel Sambuc Arg, Converted))
5119f4a2713aSLionel Sambuc return ExprError();
5120*0a6a1f1dSLionel Sambuc return Arg;
5121f4a2713aSLionel Sambuc }
5122f4a2713aSLionel Sambuc
5123f4a2713aSLionel Sambuc if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg,
5124f4a2713aSLionel Sambuc Converted))
5125f4a2713aSLionel Sambuc return ExprError();
5126*0a6a1f1dSLionel Sambuc return Arg;
5127f4a2713aSLionel Sambuc }
5128f4a2713aSLionel Sambuc
5129f4a2713aSLionel Sambuc if (ParamType->isPointerType()) {
5130f4a2713aSLionel Sambuc // -- for a non-type template-parameter of type pointer to
5131f4a2713aSLionel Sambuc // object, qualification conversions (4.4) and the
5132f4a2713aSLionel Sambuc // array-to-pointer conversion (4.2) are applied.
5133f4a2713aSLionel Sambuc // C++0x also allows a value of std::nullptr_t.
5134f4a2713aSLionel Sambuc assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
5135f4a2713aSLionel Sambuc "Only object pointers allowed here");
5136f4a2713aSLionel Sambuc
5137f4a2713aSLionel Sambuc if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
5138f4a2713aSLionel Sambuc ParamType,
5139f4a2713aSLionel Sambuc Arg, Converted))
5140f4a2713aSLionel Sambuc return ExprError();
5141*0a6a1f1dSLionel Sambuc return Arg;
5142f4a2713aSLionel Sambuc }
5143f4a2713aSLionel Sambuc
5144f4a2713aSLionel Sambuc if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
5145f4a2713aSLionel Sambuc // -- For a non-type template-parameter of type reference to
5146f4a2713aSLionel Sambuc // object, no conversions apply. The type referred to by the
5147f4a2713aSLionel Sambuc // reference may be more cv-qualified than the (otherwise
5148f4a2713aSLionel Sambuc // identical) type of the template-argument. The
5149f4a2713aSLionel Sambuc // template-parameter is bound directly to the
5150f4a2713aSLionel Sambuc // template-argument, which must be an lvalue.
5151f4a2713aSLionel Sambuc assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
5152f4a2713aSLionel Sambuc "Only object references allowed here");
5153f4a2713aSLionel Sambuc
5154f4a2713aSLionel Sambuc if (Arg->getType() == Context.OverloadTy) {
5155f4a2713aSLionel Sambuc if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
5156f4a2713aSLionel Sambuc ParamRefType->getPointeeType(),
5157f4a2713aSLionel Sambuc true,
5158f4a2713aSLionel Sambuc FoundResult)) {
5159f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(Fn, Arg->getLocStart()))
5160f4a2713aSLionel Sambuc return ExprError();
5161f4a2713aSLionel Sambuc
5162f4a2713aSLionel Sambuc Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
5163f4a2713aSLionel Sambuc ArgType = Arg->getType();
5164f4a2713aSLionel Sambuc } else
5165f4a2713aSLionel Sambuc return ExprError();
5166f4a2713aSLionel Sambuc }
5167f4a2713aSLionel Sambuc
5168f4a2713aSLionel Sambuc if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
5169f4a2713aSLionel Sambuc ParamType,
5170f4a2713aSLionel Sambuc Arg, Converted))
5171f4a2713aSLionel Sambuc return ExprError();
5172*0a6a1f1dSLionel Sambuc return Arg;
5173f4a2713aSLionel Sambuc }
5174f4a2713aSLionel Sambuc
5175f4a2713aSLionel Sambuc // Deal with parameters of type std::nullptr_t.
5176f4a2713aSLionel Sambuc if (ParamType->isNullPtrType()) {
5177f4a2713aSLionel Sambuc if (Arg->isTypeDependent() || Arg->isValueDependent()) {
5178f4a2713aSLionel Sambuc Converted = TemplateArgument(Arg);
5179*0a6a1f1dSLionel Sambuc return Arg;
5180f4a2713aSLionel Sambuc }
5181f4a2713aSLionel Sambuc
5182f4a2713aSLionel Sambuc switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) {
5183f4a2713aSLionel Sambuc case NPV_NotNullPointer:
5184f4a2713aSLionel Sambuc Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible)
5185f4a2713aSLionel Sambuc << Arg->getType() << ParamType;
5186f4a2713aSLionel Sambuc Diag(Param->getLocation(), diag::note_template_param_here);
5187f4a2713aSLionel Sambuc return ExprError();
5188f4a2713aSLionel Sambuc
5189f4a2713aSLionel Sambuc case NPV_Error:
5190f4a2713aSLionel Sambuc return ExprError();
5191f4a2713aSLionel Sambuc
5192f4a2713aSLionel Sambuc case NPV_NullPointer:
5193f4a2713aSLionel Sambuc Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null);
5194*0a6a1f1dSLionel Sambuc Converted = TemplateArgument(Context.getCanonicalType(ParamType),
5195*0a6a1f1dSLionel Sambuc /*isNullPtr*/true);
5196*0a6a1f1dSLionel Sambuc return Arg;
5197f4a2713aSLionel Sambuc }
5198f4a2713aSLionel Sambuc }
5199f4a2713aSLionel Sambuc
5200f4a2713aSLionel Sambuc // -- For a non-type template-parameter of type pointer to data
5201f4a2713aSLionel Sambuc // member, qualification conversions (4.4) are applied.
5202f4a2713aSLionel Sambuc assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
5203f4a2713aSLionel Sambuc
5204f4a2713aSLionel Sambuc if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg,
5205f4a2713aSLionel Sambuc Converted))
5206f4a2713aSLionel Sambuc return ExprError();
5207*0a6a1f1dSLionel Sambuc return Arg;
5208f4a2713aSLionel Sambuc }
5209f4a2713aSLionel Sambuc
5210f4a2713aSLionel Sambuc /// \brief Check a template argument against its corresponding
5211f4a2713aSLionel Sambuc /// template template parameter.
5212f4a2713aSLionel Sambuc ///
5213f4a2713aSLionel Sambuc /// This routine implements the semantics of C++ [temp.arg.template].
5214f4a2713aSLionel Sambuc /// It returns true if an error occurred, and false otherwise.
CheckTemplateArgument(TemplateTemplateParmDecl * Param,TemplateArgumentLoc & Arg,unsigned ArgumentPackIndex)5215f4a2713aSLionel Sambuc bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
5216*0a6a1f1dSLionel Sambuc TemplateArgumentLoc &Arg,
5217f4a2713aSLionel Sambuc unsigned ArgumentPackIndex) {
5218f4a2713aSLionel Sambuc TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern();
5219f4a2713aSLionel Sambuc TemplateDecl *Template = Name.getAsTemplateDecl();
5220f4a2713aSLionel Sambuc if (!Template) {
5221f4a2713aSLionel Sambuc // Any dependent template name is fine.
5222f4a2713aSLionel Sambuc assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
5223f4a2713aSLionel Sambuc return false;
5224f4a2713aSLionel Sambuc }
5225f4a2713aSLionel Sambuc
5226f4a2713aSLionel Sambuc // C++0x [temp.arg.template]p1:
5227f4a2713aSLionel Sambuc // A template-argument for a template template-parameter shall be
5228f4a2713aSLionel Sambuc // the name of a class template or an alias template, expressed as an
5229f4a2713aSLionel Sambuc // id-expression. When the template-argument names a class template, only
5230f4a2713aSLionel Sambuc // primary class templates are considered when matching the
5231f4a2713aSLionel Sambuc // template template argument with the corresponding parameter;
5232f4a2713aSLionel Sambuc // partial specializations are not considered even if their
5233f4a2713aSLionel Sambuc // parameter lists match that of the template template parameter.
5234f4a2713aSLionel Sambuc //
5235f4a2713aSLionel Sambuc // Note that we also allow template template parameters here, which
5236f4a2713aSLionel Sambuc // will happen when we are dealing with, e.g., class template
5237f4a2713aSLionel Sambuc // partial specializations.
5238f4a2713aSLionel Sambuc if (!isa<ClassTemplateDecl>(Template) &&
5239f4a2713aSLionel Sambuc !isa<TemplateTemplateParmDecl>(Template) &&
5240f4a2713aSLionel Sambuc !isa<TypeAliasTemplateDecl>(Template)) {
5241f4a2713aSLionel Sambuc assert(isa<FunctionTemplateDecl>(Template) &&
5242f4a2713aSLionel Sambuc "Only function templates are possible here");
5243f4a2713aSLionel Sambuc Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
5244f4a2713aSLionel Sambuc Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
5245f4a2713aSLionel Sambuc << Template;
5246f4a2713aSLionel Sambuc }
5247f4a2713aSLionel Sambuc
5248f4a2713aSLionel Sambuc TemplateParameterList *Params = Param->getTemplateParameters();
5249f4a2713aSLionel Sambuc if (Param->isExpandedParameterPack())
5250f4a2713aSLionel Sambuc Params = Param->getExpansionTemplateParameters(ArgumentPackIndex);
5251f4a2713aSLionel Sambuc
5252f4a2713aSLionel Sambuc return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
5253f4a2713aSLionel Sambuc Params,
5254f4a2713aSLionel Sambuc true,
5255f4a2713aSLionel Sambuc TPL_TemplateTemplateArgumentMatch,
5256f4a2713aSLionel Sambuc Arg.getLocation());
5257f4a2713aSLionel Sambuc }
5258f4a2713aSLionel Sambuc
5259f4a2713aSLionel Sambuc /// \brief Given a non-type template argument that refers to a
5260f4a2713aSLionel Sambuc /// declaration and the type of its corresponding non-type template
5261f4a2713aSLionel Sambuc /// parameter, produce an expression that properly refers to that
5262f4a2713aSLionel Sambuc /// declaration.
5263f4a2713aSLionel Sambuc ExprResult
BuildExpressionFromDeclTemplateArgument(const TemplateArgument & Arg,QualType ParamType,SourceLocation Loc)5264f4a2713aSLionel Sambuc Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
5265f4a2713aSLionel Sambuc QualType ParamType,
5266f4a2713aSLionel Sambuc SourceLocation Loc) {
5267f4a2713aSLionel Sambuc // C++ [temp.param]p8:
5268f4a2713aSLionel Sambuc //
5269f4a2713aSLionel Sambuc // A non-type template-parameter of type "array of T" or
5270f4a2713aSLionel Sambuc // "function returning T" is adjusted to be of type "pointer to
5271f4a2713aSLionel Sambuc // T" or "pointer to function returning T", respectively.
5272f4a2713aSLionel Sambuc if (ParamType->isArrayType())
5273f4a2713aSLionel Sambuc ParamType = Context.getArrayDecayedType(ParamType);
5274f4a2713aSLionel Sambuc else if (ParamType->isFunctionType())
5275f4a2713aSLionel Sambuc ParamType = Context.getPointerType(ParamType);
5276f4a2713aSLionel Sambuc
5277f4a2713aSLionel Sambuc // For a NULL non-type template argument, return nullptr casted to the
5278f4a2713aSLionel Sambuc // parameter's type.
5279f4a2713aSLionel Sambuc if (Arg.getKind() == TemplateArgument::NullPtr) {
5280f4a2713aSLionel Sambuc return ImpCastExprToType(
5281f4a2713aSLionel Sambuc new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc),
5282f4a2713aSLionel Sambuc ParamType,
5283f4a2713aSLionel Sambuc ParamType->getAs<MemberPointerType>()
5284f4a2713aSLionel Sambuc ? CK_NullToMemberPointer
5285f4a2713aSLionel Sambuc : CK_NullToPointer);
5286f4a2713aSLionel Sambuc }
5287f4a2713aSLionel Sambuc assert(Arg.getKind() == TemplateArgument::Declaration &&
5288f4a2713aSLionel Sambuc "Only declaration template arguments permitted here");
5289f4a2713aSLionel Sambuc
5290f4a2713aSLionel Sambuc ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
5291f4a2713aSLionel Sambuc
5292f4a2713aSLionel Sambuc if (VD->getDeclContext()->isRecord() &&
5293f4a2713aSLionel Sambuc (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) ||
5294f4a2713aSLionel Sambuc isa<IndirectFieldDecl>(VD))) {
5295f4a2713aSLionel Sambuc // If the value is a class member, we might have a pointer-to-member.
5296f4a2713aSLionel Sambuc // Determine whether the non-type template template parameter is of
5297f4a2713aSLionel Sambuc // pointer-to-member type. If so, we need to build an appropriate
5298f4a2713aSLionel Sambuc // expression for a pointer-to-member, since a "normal" DeclRefExpr
5299f4a2713aSLionel Sambuc // would refer to the member itself.
5300f4a2713aSLionel Sambuc if (ParamType->isMemberPointerType()) {
5301f4a2713aSLionel Sambuc QualType ClassType
5302f4a2713aSLionel Sambuc = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
5303f4a2713aSLionel Sambuc NestedNameSpecifier *Qualifier
5304*0a6a1f1dSLionel Sambuc = NestedNameSpecifier::Create(Context, nullptr, false,
5305f4a2713aSLionel Sambuc ClassType.getTypePtr());
5306f4a2713aSLionel Sambuc CXXScopeSpec SS;
5307f4a2713aSLionel Sambuc SS.MakeTrivial(Context, Qualifier, Loc);
5308f4a2713aSLionel Sambuc
5309f4a2713aSLionel Sambuc // The actual value-ness of this is unimportant, but for
5310f4a2713aSLionel Sambuc // internal consistency's sake, references to instance methods
5311f4a2713aSLionel Sambuc // are r-values.
5312f4a2713aSLionel Sambuc ExprValueKind VK = VK_LValue;
5313f4a2713aSLionel Sambuc if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
5314f4a2713aSLionel Sambuc VK = VK_RValue;
5315f4a2713aSLionel Sambuc
5316f4a2713aSLionel Sambuc ExprResult RefExpr = BuildDeclRefExpr(VD,
5317f4a2713aSLionel Sambuc VD->getType().getNonReferenceType(),
5318f4a2713aSLionel Sambuc VK,
5319f4a2713aSLionel Sambuc Loc,
5320f4a2713aSLionel Sambuc &SS);
5321f4a2713aSLionel Sambuc if (RefExpr.isInvalid())
5322f4a2713aSLionel Sambuc return ExprError();
5323f4a2713aSLionel Sambuc
5324f4a2713aSLionel Sambuc RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
5325f4a2713aSLionel Sambuc
5326f4a2713aSLionel Sambuc // We might need to perform a trailing qualification conversion, since
5327f4a2713aSLionel Sambuc // the element type on the parameter could be more qualified than the
5328f4a2713aSLionel Sambuc // element type in the expression we constructed.
5329f4a2713aSLionel Sambuc bool ObjCLifetimeConversion;
5330f4a2713aSLionel Sambuc if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
5331f4a2713aSLionel Sambuc ParamType.getUnqualifiedType(), false,
5332f4a2713aSLionel Sambuc ObjCLifetimeConversion))
5333*0a6a1f1dSLionel Sambuc RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp);
5334f4a2713aSLionel Sambuc
5335f4a2713aSLionel Sambuc assert(!RefExpr.isInvalid() &&
5336f4a2713aSLionel Sambuc Context.hasSameType(((Expr*) RefExpr.get())->getType(),
5337f4a2713aSLionel Sambuc ParamType.getUnqualifiedType()));
5338f4a2713aSLionel Sambuc return RefExpr;
5339f4a2713aSLionel Sambuc }
5340f4a2713aSLionel Sambuc }
5341f4a2713aSLionel Sambuc
5342f4a2713aSLionel Sambuc QualType T = VD->getType().getNonReferenceType();
5343f4a2713aSLionel Sambuc
5344f4a2713aSLionel Sambuc if (ParamType->isPointerType()) {
5345f4a2713aSLionel Sambuc // When the non-type template parameter is a pointer, take the
5346f4a2713aSLionel Sambuc // address of the declaration.
5347f4a2713aSLionel Sambuc ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
5348f4a2713aSLionel Sambuc if (RefExpr.isInvalid())
5349f4a2713aSLionel Sambuc return ExprError();
5350f4a2713aSLionel Sambuc
5351f4a2713aSLionel Sambuc if (T->isFunctionType() || T->isArrayType()) {
5352f4a2713aSLionel Sambuc // Decay functions and arrays.
5353*0a6a1f1dSLionel Sambuc RefExpr = DefaultFunctionArrayConversion(RefExpr.get());
5354f4a2713aSLionel Sambuc if (RefExpr.isInvalid())
5355f4a2713aSLionel Sambuc return ExprError();
5356f4a2713aSLionel Sambuc
5357f4a2713aSLionel Sambuc return RefExpr;
5358f4a2713aSLionel Sambuc }
5359f4a2713aSLionel Sambuc
5360f4a2713aSLionel Sambuc // Take the address of everything else
5361f4a2713aSLionel Sambuc return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
5362f4a2713aSLionel Sambuc }
5363f4a2713aSLionel Sambuc
5364f4a2713aSLionel Sambuc ExprValueKind VK = VK_RValue;
5365f4a2713aSLionel Sambuc
5366f4a2713aSLionel Sambuc // If the non-type template parameter has reference type, qualify the
5367f4a2713aSLionel Sambuc // resulting declaration reference with the extra qualifiers on the
5368f4a2713aSLionel Sambuc // type that the reference refers to.
5369f4a2713aSLionel Sambuc if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
5370f4a2713aSLionel Sambuc VK = VK_LValue;
5371f4a2713aSLionel Sambuc T = Context.getQualifiedType(T,
5372f4a2713aSLionel Sambuc TargetRef->getPointeeType().getQualifiers());
5373f4a2713aSLionel Sambuc } else if (isa<FunctionDecl>(VD)) {
5374f4a2713aSLionel Sambuc // References to functions are always lvalues.
5375f4a2713aSLionel Sambuc VK = VK_LValue;
5376f4a2713aSLionel Sambuc }
5377f4a2713aSLionel Sambuc
5378f4a2713aSLionel Sambuc return BuildDeclRefExpr(VD, T, VK, Loc);
5379f4a2713aSLionel Sambuc }
5380f4a2713aSLionel Sambuc
5381f4a2713aSLionel Sambuc /// \brief Construct a new expression that refers to the given
5382f4a2713aSLionel Sambuc /// integral template argument with the given source-location
5383f4a2713aSLionel Sambuc /// information.
5384f4a2713aSLionel Sambuc ///
5385f4a2713aSLionel Sambuc /// This routine takes care of the mapping from an integral template
5386f4a2713aSLionel Sambuc /// argument (which may have any integral type) to the appropriate
5387f4a2713aSLionel Sambuc /// literal value.
5388f4a2713aSLionel Sambuc ExprResult
BuildExpressionFromIntegralTemplateArgument(const TemplateArgument & Arg,SourceLocation Loc)5389f4a2713aSLionel Sambuc Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
5390f4a2713aSLionel Sambuc SourceLocation Loc) {
5391f4a2713aSLionel Sambuc assert(Arg.getKind() == TemplateArgument::Integral &&
5392f4a2713aSLionel Sambuc "Operation is only valid for integral template arguments");
5393f4a2713aSLionel Sambuc QualType OrigT = Arg.getIntegralType();
5394f4a2713aSLionel Sambuc
5395f4a2713aSLionel Sambuc // If this is an enum type that we're instantiating, we need to use an integer
5396f4a2713aSLionel Sambuc // type the same size as the enumerator. We don't want to build an
5397f4a2713aSLionel Sambuc // IntegerLiteral with enum type. The integer type of an enum type can be of
5398f4a2713aSLionel Sambuc // any integral type with C++11 enum classes, make sure we create the right
5399f4a2713aSLionel Sambuc // type of literal for it.
5400f4a2713aSLionel Sambuc QualType T = OrigT;
5401f4a2713aSLionel Sambuc if (const EnumType *ET = OrigT->getAs<EnumType>())
5402f4a2713aSLionel Sambuc T = ET->getDecl()->getIntegerType();
5403f4a2713aSLionel Sambuc
5404f4a2713aSLionel Sambuc Expr *E;
5405f4a2713aSLionel Sambuc if (T->isAnyCharacterType()) {
5406f4a2713aSLionel Sambuc CharacterLiteral::CharacterKind Kind;
5407f4a2713aSLionel Sambuc if (T->isWideCharType())
5408f4a2713aSLionel Sambuc Kind = CharacterLiteral::Wide;
5409f4a2713aSLionel Sambuc else if (T->isChar16Type())
5410f4a2713aSLionel Sambuc Kind = CharacterLiteral::UTF16;
5411f4a2713aSLionel Sambuc else if (T->isChar32Type())
5412f4a2713aSLionel Sambuc Kind = CharacterLiteral::UTF32;
5413f4a2713aSLionel Sambuc else
5414f4a2713aSLionel Sambuc Kind = CharacterLiteral::Ascii;
5415f4a2713aSLionel Sambuc
5416f4a2713aSLionel Sambuc E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(),
5417f4a2713aSLionel Sambuc Kind, T, Loc);
5418f4a2713aSLionel Sambuc } else if (T->isBooleanType()) {
5419f4a2713aSLionel Sambuc E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(),
5420f4a2713aSLionel Sambuc T, Loc);
5421f4a2713aSLionel Sambuc } else if (T->isNullPtrType()) {
5422f4a2713aSLionel Sambuc E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc);
5423f4a2713aSLionel Sambuc } else {
5424f4a2713aSLionel Sambuc E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc);
5425f4a2713aSLionel Sambuc }
5426f4a2713aSLionel Sambuc
5427f4a2713aSLionel Sambuc if (OrigT->isEnumeralType()) {
5428f4a2713aSLionel Sambuc // FIXME: This is a hack. We need a better way to handle substituted
5429f4a2713aSLionel Sambuc // non-type template parameters.
5430*0a6a1f1dSLionel Sambuc E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E,
5431*0a6a1f1dSLionel Sambuc nullptr,
5432f4a2713aSLionel Sambuc Context.getTrivialTypeSourceInfo(OrigT, Loc),
5433f4a2713aSLionel Sambuc Loc, Loc);
5434f4a2713aSLionel Sambuc }
5435f4a2713aSLionel Sambuc
5436*0a6a1f1dSLionel Sambuc return E;
5437f4a2713aSLionel Sambuc }
5438f4a2713aSLionel Sambuc
5439f4a2713aSLionel Sambuc /// \brief Match two template parameters within template parameter lists.
MatchTemplateParameterKind(Sema & S,NamedDecl * New,NamedDecl * Old,bool Complain,Sema::TemplateParameterListEqualKind Kind,SourceLocation TemplateArgLoc)5440f4a2713aSLionel Sambuc static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
5441f4a2713aSLionel Sambuc bool Complain,
5442f4a2713aSLionel Sambuc Sema::TemplateParameterListEqualKind Kind,
5443f4a2713aSLionel Sambuc SourceLocation TemplateArgLoc) {
5444f4a2713aSLionel Sambuc // Check the actual kind (type, non-type, template).
5445f4a2713aSLionel Sambuc if (Old->getKind() != New->getKind()) {
5446f4a2713aSLionel Sambuc if (Complain) {
5447f4a2713aSLionel Sambuc unsigned NextDiag = diag::err_template_param_different_kind;
5448f4a2713aSLionel Sambuc if (TemplateArgLoc.isValid()) {
5449f4a2713aSLionel Sambuc S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
5450f4a2713aSLionel Sambuc NextDiag = diag::note_template_param_different_kind;
5451f4a2713aSLionel Sambuc }
5452f4a2713aSLionel Sambuc S.Diag(New->getLocation(), NextDiag)
5453f4a2713aSLionel Sambuc << (Kind != Sema::TPL_TemplateMatch);
5454f4a2713aSLionel Sambuc S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
5455f4a2713aSLionel Sambuc << (Kind != Sema::TPL_TemplateMatch);
5456f4a2713aSLionel Sambuc }
5457f4a2713aSLionel Sambuc
5458f4a2713aSLionel Sambuc return false;
5459f4a2713aSLionel Sambuc }
5460f4a2713aSLionel Sambuc
5461f4a2713aSLionel Sambuc // Check that both are parameter packs are neither are parameter packs.
5462f4a2713aSLionel Sambuc // However, if we are matching a template template argument to a
5463f4a2713aSLionel Sambuc // template template parameter, the template template parameter can have
5464f4a2713aSLionel Sambuc // a parameter pack where the template template argument does not.
5465f4a2713aSLionel Sambuc if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
5466f4a2713aSLionel Sambuc !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
5467f4a2713aSLionel Sambuc Old->isTemplateParameterPack())) {
5468f4a2713aSLionel Sambuc if (Complain) {
5469f4a2713aSLionel Sambuc unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
5470f4a2713aSLionel Sambuc if (TemplateArgLoc.isValid()) {
5471f4a2713aSLionel Sambuc S.Diag(TemplateArgLoc,
5472f4a2713aSLionel Sambuc diag::err_template_arg_template_params_mismatch);
5473f4a2713aSLionel Sambuc NextDiag = diag::note_template_parameter_pack_non_pack;
5474f4a2713aSLionel Sambuc }
5475f4a2713aSLionel Sambuc
5476f4a2713aSLionel Sambuc unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
5477f4a2713aSLionel Sambuc : isa<NonTypeTemplateParmDecl>(New)? 1
5478f4a2713aSLionel Sambuc : 2;
5479f4a2713aSLionel Sambuc S.Diag(New->getLocation(), NextDiag)
5480f4a2713aSLionel Sambuc << ParamKind << New->isParameterPack();
5481f4a2713aSLionel Sambuc S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
5482f4a2713aSLionel Sambuc << ParamKind << Old->isParameterPack();
5483f4a2713aSLionel Sambuc }
5484f4a2713aSLionel Sambuc
5485f4a2713aSLionel Sambuc return false;
5486f4a2713aSLionel Sambuc }
5487f4a2713aSLionel Sambuc
5488f4a2713aSLionel Sambuc // For non-type template parameters, check the type of the parameter.
5489f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *OldNTTP
5490f4a2713aSLionel Sambuc = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
5491f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
5492f4a2713aSLionel Sambuc
5493f4a2713aSLionel Sambuc // If we are matching a template template argument to a template
5494f4a2713aSLionel Sambuc // template parameter and one of the non-type template parameter types
5495f4a2713aSLionel Sambuc // is dependent, then we must wait until template instantiation time
5496f4a2713aSLionel Sambuc // to actually compare the arguments.
5497f4a2713aSLionel Sambuc if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
5498f4a2713aSLionel Sambuc (OldNTTP->getType()->isDependentType() ||
5499f4a2713aSLionel Sambuc NewNTTP->getType()->isDependentType()))
5500f4a2713aSLionel Sambuc return true;
5501f4a2713aSLionel Sambuc
5502f4a2713aSLionel Sambuc if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
5503f4a2713aSLionel Sambuc if (Complain) {
5504f4a2713aSLionel Sambuc unsigned NextDiag = diag::err_template_nontype_parm_different_type;
5505f4a2713aSLionel Sambuc if (TemplateArgLoc.isValid()) {
5506f4a2713aSLionel Sambuc S.Diag(TemplateArgLoc,
5507f4a2713aSLionel Sambuc diag::err_template_arg_template_params_mismatch);
5508f4a2713aSLionel Sambuc NextDiag = diag::note_template_nontype_parm_different_type;
5509f4a2713aSLionel Sambuc }
5510f4a2713aSLionel Sambuc S.Diag(NewNTTP->getLocation(), NextDiag)
5511f4a2713aSLionel Sambuc << NewNTTP->getType()
5512f4a2713aSLionel Sambuc << (Kind != Sema::TPL_TemplateMatch);
5513f4a2713aSLionel Sambuc S.Diag(OldNTTP->getLocation(),
5514f4a2713aSLionel Sambuc diag::note_template_nontype_parm_prev_declaration)
5515f4a2713aSLionel Sambuc << OldNTTP->getType();
5516f4a2713aSLionel Sambuc }
5517f4a2713aSLionel Sambuc
5518f4a2713aSLionel Sambuc return false;
5519f4a2713aSLionel Sambuc }
5520f4a2713aSLionel Sambuc
5521f4a2713aSLionel Sambuc return true;
5522f4a2713aSLionel Sambuc }
5523f4a2713aSLionel Sambuc
5524f4a2713aSLionel Sambuc // For template template parameters, check the template parameter types.
5525f4a2713aSLionel Sambuc // The template parameter lists of template template
5526f4a2713aSLionel Sambuc // parameters must agree.
5527f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *OldTTP
5528f4a2713aSLionel Sambuc = dyn_cast<TemplateTemplateParmDecl>(Old)) {
5529f4a2713aSLionel Sambuc TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
5530f4a2713aSLionel Sambuc return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
5531f4a2713aSLionel Sambuc OldTTP->getTemplateParameters(),
5532f4a2713aSLionel Sambuc Complain,
5533f4a2713aSLionel Sambuc (Kind == Sema::TPL_TemplateMatch
5534f4a2713aSLionel Sambuc ? Sema::TPL_TemplateTemplateParmMatch
5535f4a2713aSLionel Sambuc : Kind),
5536f4a2713aSLionel Sambuc TemplateArgLoc);
5537f4a2713aSLionel Sambuc }
5538f4a2713aSLionel Sambuc
5539f4a2713aSLionel Sambuc return true;
5540f4a2713aSLionel Sambuc }
5541f4a2713aSLionel Sambuc
5542f4a2713aSLionel Sambuc /// \brief Diagnose a known arity mismatch when comparing template argument
5543f4a2713aSLionel Sambuc /// lists.
5544f4a2713aSLionel Sambuc static
DiagnoseTemplateParameterListArityMismatch(Sema & S,TemplateParameterList * New,TemplateParameterList * Old,Sema::TemplateParameterListEqualKind Kind,SourceLocation TemplateArgLoc)5545f4a2713aSLionel Sambuc void DiagnoseTemplateParameterListArityMismatch(Sema &S,
5546f4a2713aSLionel Sambuc TemplateParameterList *New,
5547f4a2713aSLionel Sambuc TemplateParameterList *Old,
5548f4a2713aSLionel Sambuc Sema::TemplateParameterListEqualKind Kind,
5549f4a2713aSLionel Sambuc SourceLocation TemplateArgLoc) {
5550f4a2713aSLionel Sambuc unsigned NextDiag = diag::err_template_param_list_different_arity;
5551f4a2713aSLionel Sambuc if (TemplateArgLoc.isValid()) {
5552f4a2713aSLionel Sambuc S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
5553f4a2713aSLionel Sambuc NextDiag = diag::note_template_param_list_different_arity;
5554f4a2713aSLionel Sambuc }
5555f4a2713aSLionel Sambuc S.Diag(New->getTemplateLoc(), NextDiag)
5556f4a2713aSLionel Sambuc << (New->size() > Old->size())
5557f4a2713aSLionel Sambuc << (Kind != Sema::TPL_TemplateMatch)
5558f4a2713aSLionel Sambuc << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
5559f4a2713aSLionel Sambuc S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
5560f4a2713aSLionel Sambuc << (Kind != Sema::TPL_TemplateMatch)
5561f4a2713aSLionel Sambuc << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
5562f4a2713aSLionel Sambuc }
5563f4a2713aSLionel Sambuc
5564f4a2713aSLionel Sambuc /// \brief Determine whether the given template parameter lists are
5565f4a2713aSLionel Sambuc /// equivalent.
5566f4a2713aSLionel Sambuc ///
5567f4a2713aSLionel Sambuc /// \param New The new template parameter list, typically written in the
5568f4a2713aSLionel Sambuc /// source code as part of a new template declaration.
5569f4a2713aSLionel Sambuc ///
5570f4a2713aSLionel Sambuc /// \param Old The old template parameter list, typically found via
5571f4a2713aSLionel Sambuc /// name lookup of the template declared with this template parameter
5572f4a2713aSLionel Sambuc /// list.
5573f4a2713aSLionel Sambuc ///
5574f4a2713aSLionel Sambuc /// \param Complain If true, this routine will produce a diagnostic if
5575f4a2713aSLionel Sambuc /// the template parameter lists are not equivalent.
5576f4a2713aSLionel Sambuc ///
5577f4a2713aSLionel Sambuc /// \param Kind describes how we are to match the template parameter lists.
5578f4a2713aSLionel Sambuc ///
5579f4a2713aSLionel Sambuc /// \param TemplateArgLoc If this source location is valid, then we
5580f4a2713aSLionel Sambuc /// are actually checking the template parameter list of a template
5581f4a2713aSLionel Sambuc /// argument (New) against the template parameter list of its
5582f4a2713aSLionel Sambuc /// corresponding template template parameter (Old). We produce
5583f4a2713aSLionel Sambuc /// slightly different diagnostics in this scenario.
5584f4a2713aSLionel Sambuc ///
5585f4a2713aSLionel Sambuc /// \returns True if the template parameter lists are equal, false
5586f4a2713aSLionel Sambuc /// otherwise.
5587f4a2713aSLionel Sambuc bool
TemplateParameterListsAreEqual(TemplateParameterList * New,TemplateParameterList * Old,bool Complain,TemplateParameterListEqualKind Kind,SourceLocation TemplateArgLoc)5588f4a2713aSLionel Sambuc Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
5589f4a2713aSLionel Sambuc TemplateParameterList *Old,
5590f4a2713aSLionel Sambuc bool Complain,
5591f4a2713aSLionel Sambuc TemplateParameterListEqualKind Kind,
5592f4a2713aSLionel Sambuc SourceLocation TemplateArgLoc) {
5593f4a2713aSLionel Sambuc if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
5594f4a2713aSLionel Sambuc if (Complain)
5595f4a2713aSLionel Sambuc DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
5596f4a2713aSLionel Sambuc TemplateArgLoc);
5597f4a2713aSLionel Sambuc
5598f4a2713aSLionel Sambuc return false;
5599f4a2713aSLionel Sambuc }
5600f4a2713aSLionel Sambuc
5601f4a2713aSLionel Sambuc // C++0x [temp.arg.template]p3:
5602f4a2713aSLionel Sambuc // A template-argument matches a template template-parameter (call it P)
5603f4a2713aSLionel Sambuc // when each of the template parameters in the template-parameter-list of
5604f4a2713aSLionel Sambuc // the template-argument's corresponding class template or alias template
5605f4a2713aSLionel Sambuc // (call it A) matches the corresponding template parameter in the
5606f4a2713aSLionel Sambuc // template-parameter-list of P. [...]
5607f4a2713aSLionel Sambuc TemplateParameterList::iterator NewParm = New->begin();
5608f4a2713aSLionel Sambuc TemplateParameterList::iterator NewParmEnd = New->end();
5609f4a2713aSLionel Sambuc for (TemplateParameterList::iterator OldParm = Old->begin(),
5610f4a2713aSLionel Sambuc OldParmEnd = Old->end();
5611f4a2713aSLionel Sambuc OldParm != OldParmEnd; ++OldParm) {
5612f4a2713aSLionel Sambuc if (Kind != TPL_TemplateTemplateArgumentMatch ||
5613f4a2713aSLionel Sambuc !(*OldParm)->isTemplateParameterPack()) {
5614f4a2713aSLionel Sambuc if (NewParm == NewParmEnd) {
5615f4a2713aSLionel Sambuc if (Complain)
5616f4a2713aSLionel Sambuc DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
5617f4a2713aSLionel Sambuc TemplateArgLoc);
5618f4a2713aSLionel Sambuc
5619f4a2713aSLionel Sambuc return false;
5620f4a2713aSLionel Sambuc }
5621f4a2713aSLionel Sambuc
5622f4a2713aSLionel Sambuc if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
5623f4a2713aSLionel Sambuc Kind, TemplateArgLoc))
5624f4a2713aSLionel Sambuc return false;
5625f4a2713aSLionel Sambuc
5626f4a2713aSLionel Sambuc ++NewParm;
5627f4a2713aSLionel Sambuc continue;
5628f4a2713aSLionel Sambuc }
5629f4a2713aSLionel Sambuc
5630f4a2713aSLionel Sambuc // C++0x [temp.arg.template]p3:
5631f4a2713aSLionel Sambuc // [...] When P's template- parameter-list contains a template parameter
5632f4a2713aSLionel Sambuc // pack (14.5.3), the template parameter pack will match zero or more
5633f4a2713aSLionel Sambuc // template parameters or template parameter packs in the
5634f4a2713aSLionel Sambuc // template-parameter-list of A with the same type and form as the
5635f4a2713aSLionel Sambuc // template parameter pack in P (ignoring whether those template
5636f4a2713aSLionel Sambuc // parameters are template parameter packs).
5637f4a2713aSLionel Sambuc for (; NewParm != NewParmEnd; ++NewParm) {
5638f4a2713aSLionel Sambuc if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
5639f4a2713aSLionel Sambuc Kind, TemplateArgLoc))
5640f4a2713aSLionel Sambuc return false;
5641f4a2713aSLionel Sambuc }
5642f4a2713aSLionel Sambuc }
5643f4a2713aSLionel Sambuc
5644f4a2713aSLionel Sambuc // Make sure we exhausted all of the arguments.
5645f4a2713aSLionel Sambuc if (NewParm != NewParmEnd) {
5646f4a2713aSLionel Sambuc if (Complain)
5647f4a2713aSLionel Sambuc DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
5648f4a2713aSLionel Sambuc TemplateArgLoc);
5649f4a2713aSLionel Sambuc
5650f4a2713aSLionel Sambuc return false;
5651f4a2713aSLionel Sambuc }
5652f4a2713aSLionel Sambuc
5653f4a2713aSLionel Sambuc return true;
5654f4a2713aSLionel Sambuc }
5655f4a2713aSLionel Sambuc
5656f4a2713aSLionel Sambuc /// \brief Check whether a template can be declared within this scope.
5657f4a2713aSLionel Sambuc ///
5658f4a2713aSLionel Sambuc /// If the template declaration is valid in this scope, returns
5659f4a2713aSLionel Sambuc /// false. Otherwise, issues a diagnostic and returns true.
5660f4a2713aSLionel Sambuc bool
CheckTemplateDeclScope(Scope * S,TemplateParameterList * TemplateParams)5661f4a2713aSLionel Sambuc Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
5662f4a2713aSLionel Sambuc if (!S)
5663f4a2713aSLionel Sambuc return false;
5664f4a2713aSLionel Sambuc
5665f4a2713aSLionel Sambuc // Find the nearest enclosing declaration scope.
5666f4a2713aSLionel Sambuc while ((S->getFlags() & Scope::DeclScope) == 0 ||
5667f4a2713aSLionel Sambuc (S->getFlags() & Scope::TemplateParamScope) != 0)
5668f4a2713aSLionel Sambuc S = S->getParent();
5669f4a2713aSLionel Sambuc
5670*0a6a1f1dSLionel Sambuc // C++ [temp]p4:
5671*0a6a1f1dSLionel Sambuc // A template [...] shall not have C linkage.
5672f4a2713aSLionel Sambuc DeclContext *Ctx = S->getEntity();
5673*0a6a1f1dSLionel Sambuc if (Ctx && Ctx->isExternCContext())
5674f4a2713aSLionel Sambuc return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
5675f4a2713aSLionel Sambuc << TemplateParams->getSourceRange();
5676f4a2713aSLionel Sambuc
5677f4a2713aSLionel Sambuc while (Ctx && isa<LinkageSpecDecl>(Ctx))
5678f4a2713aSLionel Sambuc Ctx = Ctx->getParent();
5679f4a2713aSLionel Sambuc
5680*0a6a1f1dSLionel Sambuc // C++ [temp]p2:
5681*0a6a1f1dSLionel Sambuc // A template-declaration can appear only as a namespace scope or
5682*0a6a1f1dSLionel Sambuc // class scope declaration.
5683f4a2713aSLionel Sambuc if (Ctx) {
5684f4a2713aSLionel Sambuc if (Ctx->isFileContext())
5685f4a2713aSLionel Sambuc return false;
5686f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) {
5687f4a2713aSLionel Sambuc // C++ [temp.mem]p2:
5688f4a2713aSLionel Sambuc // A local class shall not have member templates.
5689f4a2713aSLionel Sambuc if (RD->isLocalClass())
5690f4a2713aSLionel Sambuc return Diag(TemplateParams->getTemplateLoc(),
5691f4a2713aSLionel Sambuc diag::err_template_inside_local_class)
5692f4a2713aSLionel Sambuc << TemplateParams->getSourceRange();
5693f4a2713aSLionel Sambuc else
5694f4a2713aSLionel Sambuc return false;
5695f4a2713aSLionel Sambuc }
5696f4a2713aSLionel Sambuc }
5697f4a2713aSLionel Sambuc
5698f4a2713aSLionel Sambuc return Diag(TemplateParams->getTemplateLoc(),
5699f4a2713aSLionel Sambuc diag::err_template_outside_namespace_or_class_scope)
5700f4a2713aSLionel Sambuc << TemplateParams->getSourceRange();
5701f4a2713aSLionel Sambuc }
5702f4a2713aSLionel Sambuc
5703f4a2713aSLionel Sambuc /// \brief Determine what kind of template specialization the given declaration
5704f4a2713aSLionel Sambuc /// is.
getTemplateSpecializationKind(Decl * D)5705f4a2713aSLionel Sambuc static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
5706f4a2713aSLionel Sambuc if (!D)
5707f4a2713aSLionel Sambuc return TSK_Undeclared;
5708f4a2713aSLionel Sambuc
5709f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
5710f4a2713aSLionel Sambuc return Record->getTemplateSpecializationKind();
5711f4a2713aSLionel Sambuc if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
5712f4a2713aSLionel Sambuc return Function->getTemplateSpecializationKind();
5713f4a2713aSLionel Sambuc if (VarDecl *Var = dyn_cast<VarDecl>(D))
5714f4a2713aSLionel Sambuc return Var->getTemplateSpecializationKind();
5715f4a2713aSLionel Sambuc
5716f4a2713aSLionel Sambuc return TSK_Undeclared;
5717f4a2713aSLionel Sambuc }
5718f4a2713aSLionel Sambuc
5719f4a2713aSLionel Sambuc /// \brief Check whether a specialization is well-formed in the current
5720f4a2713aSLionel Sambuc /// context.
5721f4a2713aSLionel Sambuc ///
5722f4a2713aSLionel Sambuc /// This routine determines whether a template specialization can be declared
5723f4a2713aSLionel Sambuc /// in the current context (C++ [temp.expl.spec]p2).
5724f4a2713aSLionel Sambuc ///
5725f4a2713aSLionel Sambuc /// \param S the semantic analysis object for which this check is being
5726f4a2713aSLionel Sambuc /// performed.
5727f4a2713aSLionel Sambuc ///
5728f4a2713aSLionel Sambuc /// \param Specialized the entity being specialized or instantiated, which
5729f4a2713aSLionel Sambuc /// may be a kind of template (class template, function template, etc.) or
5730f4a2713aSLionel Sambuc /// a member of a class template (member function, static data member,
5731f4a2713aSLionel Sambuc /// member class).
5732f4a2713aSLionel Sambuc ///
5733f4a2713aSLionel Sambuc /// \param PrevDecl the previous declaration of this entity, if any.
5734f4a2713aSLionel Sambuc ///
5735f4a2713aSLionel Sambuc /// \param Loc the location of the explicit specialization or instantiation of
5736f4a2713aSLionel Sambuc /// this entity.
5737f4a2713aSLionel Sambuc ///
5738f4a2713aSLionel Sambuc /// \param IsPartialSpecialization whether this is a partial specialization of
5739f4a2713aSLionel Sambuc /// a class template.
5740f4a2713aSLionel Sambuc ///
5741f4a2713aSLionel Sambuc /// \returns true if there was an error that we cannot recover from, false
5742f4a2713aSLionel Sambuc /// otherwise.
CheckTemplateSpecializationScope(Sema & S,NamedDecl * Specialized,NamedDecl * PrevDecl,SourceLocation Loc,bool IsPartialSpecialization)5743f4a2713aSLionel Sambuc static bool CheckTemplateSpecializationScope(Sema &S,
5744f4a2713aSLionel Sambuc NamedDecl *Specialized,
5745f4a2713aSLionel Sambuc NamedDecl *PrevDecl,
5746f4a2713aSLionel Sambuc SourceLocation Loc,
5747f4a2713aSLionel Sambuc bool IsPartialSpecialization) {
5748f4a2713aSLionel Sambuc // Keep these "kind" numbers in sync with the %select statements in the
5749f4a2713aSLionel Sambuc // various diagnostics emitted by this routine.
5750f4a2713aSLionel Sambuc int EntityKind = 0;
5751f4a2713aSLionel Sambuc if (isa<ClassTemplateDecl>(Specialized))
5752f4a2713aSLionel Sambuc EntityKind = IsPartialSpecialization? 1 : 0;
5753f4a2713aSLionel Sambuc else if (isa<VarTemplateDecl>(Specialized))
5754f4a2713aSLionel Sambuc EntityKind = IsPartialSpecialization ? 3 : 2;
5755f4a2713aSLionel Sambuc else if (isa<FunctionTemplateDecl>(Specialized))
5756f4a2713aSLionel Sambuc EntityKind = 4;
5757f4a2713aSLionel Sambuc else if (isa<CXXMethodDecl>(Specialized))
5758f4a2713aSLionel Sambuc EntityKind = 5;
5759f4a2713aSLionel Sambuc else if (isa<VarDecl>(Specialized))
5760f4a2713aSLionel Sambuc EntityKind = 6;
5761f4a2713aSLionel Sambuc else if (isa<RecordDecl>(Specialized))
5762f4a2713aSLionel Sambuc EntityKind = 7;
5763f4a2713aSLionel Sambuc else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11)
5764f4a2713aSLionel Sambuc EntityKind = 8;
5765f4a2713aSLionel Sambuc else {
5766f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_template_spec_unknown_kind)
5767f4a2713aSLionel Sambuc << S.getLangOpts().CPlusPlus11;
5768f4a2713aSLionel Sambuc S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
5769f4a2713aSLionel Sambuc return true;
5770f4a2713aSLionel Sambuc }
5771f4a2713aSLionel Sambuc
5772f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p2:
5773f4a2713aSLionel Sambuc // An explicit specialization shall be declared in the namespace
5774f4a2713aSLionel Sambuc // of which the template is a member, or, for member templates, in
5775f4a2713aSLionel Sambuc // the namespace of which the enclosing class or enclosing class
5776f4a2713aSLionel Sambuc // template is a member. An explicit specialization of a member
5777f4a2713aSLionel Sambuc // function, member class or static data member of a class
5778f4a2713aSLionel Sambuc // template shall be declared in the namespace of which the class
5779f4a2713aSLionel Sambuc // template is a member. Such a declaration may also be a
5780f4a2713aSLionel Sambuc // definition. If the declaration is not a definition, the
5781f4a2713aSLionel Sambuc // specialization may be defined later in the name- space in which
5782f4a2713aSLionel Sambuc // the explicit specialization was declared, or in a namespace
5783f4a2713aSLionel Sambuc // that encloses the one in which the explicit specialization was
5784f4a2713aSLionel Sambuc // declared.
5785f4a2713aSLionel Sambuc if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
5786f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_template_spec_decl_function_scope)
5787f4a2713aSLionel Sambuc << Specialized;
5788f4a2713aSLionel Sambuc return true;
5789f4a2713aSLionel Sambuc }
5790f4a2713aSLionel Sambuc
5791f4a2713aSLionel Sambuc if (S.CurContext->isRecord() && !IsPartialSpecialization) {
5792f4a2713aSLionel Sambuc if (S.getLangOpts().MicrosoftExt) {
5793f4a2713aSLionel Sambuc // Do not warn for class scope explicit specialization during
5794f4a2713aSLionel Sambuc // instantiation, warning was already emitted during pattern
5795f4a2713aSLionel Sambuc // semantic analysis.
5796f4a2713aSLionel Sambuc if (!S.ActiveTemplateInstantiations.size())
5797f4a2713aSLionel Sambuc S.Diag(Loc, diag::ext_function_specialization_in_class)
5798f4a2713aSLionel Sambuc << Specialized;
5799f4a2713aSLionel Sambuc } else {
5800f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_template_spec_decl_class_scope)
5801f4a2713aSLionel Sambuc << Specialized;
5802f4a2713aSLionel Sambuc return true;
5803f4a2713aSLionel Sambuc }
5804f4a2713aSLionel Sambuc }
5805f4a2713aSLionel Sambuc
5806f4a2713aSLionel Sambuc if (S.CurContext->isRecord() &&
5807f4a2713aSLionel Sambuc !S.CurContext->Equals(Specialized->getDeclContext())) {
5808f4a2713aSLionel Sambuc // Make sure that we're specializing in the right record context.
5809f4a2713aSLionel Sambuc // Otherwise, things can go horribly wrong.
5810f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_template_spec_decl_class_scope)
5811f4a2713aSLionel Sambuc << Specialized;
5812f4a2713aSLionel Sambuc return true;
5813f4a2713aSLionel Sambuc }
5814f4a2713aSLionel Sambuc
5815f4a2713aSLionel Sambuc // C++ [temp.class.spec]p6:
5816f4a2713aSLionel Sambuc // A class template partial specialization may be declared or redeclared
5817f4a2713aSLionel Sambuc // in any namespace scope in which its definition may be defined (14.5.1
5818f4a2713aSLionel Sambuc // and 14.5.2).
5819f4a2713aSLionel Sambuc DeclContext *SpecializedContext
5820f4a2713aSLionel Sambuc = Specialized->getDeclContext()->getEnclosingNamespaceContext();
5821f4a2713aSLionel Sambuc DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
5822*0a6a1f1dSLionel Sambuc
5823*0a6a1f1dSLionel Sambuc // Make sure that this redeclaration (or definition) occurs in an enclosing
5824*0a6a1f1dSLionel Sambuc // namespace.
5825*0a6a1f1dSLionel Sambuc // Note that HandleDeclarator() performs this check for explicit
5826*0a6a1f1dSLionel Sambuc // specializations of function templates, static data members, and member
5827*0a6a1f1dSLionel Sambuc // functions, so we skip the check here for those kinds of entities.
5828*0a6a1f1dSLionel Sambuc // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
5829*0a6a1f1dSLionel Sambuc // Should we refactor that check, so that it occurs later?
5830*0a6a1f1dSLionel Sambuc if (!DC->Encloses(SpecializedContext) &&
5831*0a6a1f1dSLionel Sambuc !(isa<FunctionTemplateDecl>(Specialized) ||
5832*0a6a1f1dSLionel Sambuc isa<FunctionDecl>(Specialized) ||
5833*0a6a1f1dSLionel Sambuc isa<VarTemplateDecl>(Specialized) ||
5834*0a6a1f1dSLionel Sambuc isa<VarDecl>(Specialized))) {
5835*0a6a1f1dSLionel Sambuc if (isa<TranslationUnitDecl>(SpecializedContext))
5836*0a6a1f1dSLionel Sambuc S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
5837*0a6a1f1dSLionel Sambuc << EntityKind << Specialized;
5838*0a6a1f1dSLionel Sambuc else if (isa<NamespaceDecl>(SpecializedContext))
5839*0a6a1f1dSLionel Sambuc S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
5840*0a6a1f1dSLionel Sambuc << EntityKind << Specialized
5841*0a6a1f1dSLionel Sambuc << cast<NamedDecl>(SpecializedContext);
5842*0a6a1f1dSLionel Sambuc else
5843*0a6a1f1dSLionel Sambuc llvm_unreachable("unexpected namespace context for specialization");
5844*0a6a1f1dSLionel Sambuc
5845*0a6a1f1dSLionel Sambuc S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
5846*0a6a1f1dSLionel Sambuc } else if ((!PrevDecl ||
5847f4a2713aSLionel Sambuc getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
5848*0a6a1f1dSLionel Sambuc getTemplateSpecializationKind(PrevDecl) ==
5849*0a6a1f1dSLionel Sambuc TSK_ImplicitInstantiation)) {
5850f4a2713aSLionel Sambuc // C++ [temp.exp.spec]p2:
5851f4a2713aSLionel Sambuc // An explicit specialization shall be declared in the namespace of which
5852f4a2713aSLionel Sambuc // the template is a member, or, for member templates, in the namespace
5853f4a2713aSLionel Sambuc // of which the enclosing class or enclosing class template is a member.
5854f4a2713aSLionel Sambuc // An explicit specialization of a member function, member class or
5855f4a2713aSLionel Sambuc // static data member of a class template shall be declared in the
5856f4a2713aSLionel Sambuc // namespace of which the class template is a member.
5857f4a2713aSLionel Sambuc //
5858*0a6a1f1dSLionel Sambuc // C++11 [temp.expl.spec]p2:
5859f4a2713aSLionel Sambuc // An explicit specialization shall be declared in a namespace enclosing
5860f4a2713aSLionel Sambuc // the specialized template.
5861*0a6a1f1dSLionel Sambuc // C++11 [temp.explicit]p3:
5862*0a6a1f1dSLionel Sambuc // An explicit instantiation shall appear in an enclosing namespace of its
5863*0a6a1f1dSLionel Sambuc // template.
5864f4a2713aSLionel Sambuc if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
5865f4a2713aSLionel Sambuc bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext);
5866f4a2713aSLionel Sambuc if (isa<TranslationUnitDecl>(SpecializedContext)) {
5867f4a2713aSLionel Sambuc assert(!IsCPlusPlus11Extension &&
5868f4a2713aSLionel Sambuc "DC encloses TU but isn't in enclosing namespace set");
5869f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
5870f4a2713aSLionel Sambuc << EntityKind << Specialized;
5871f4a2713aSLionel Sambuc } else if (isa<NamespaceDecl>(SpecializedContext)) {
5872f4a2713aSLionel Sambuc int Diag;
5873f4a2713aSLionel Sambuc if (!IsCPlusPlus11Extension)
5874f4a2713aSLionel Sambuc Diag = diag::err_template_spec_decl_out_of_scope;
5875f4a2713aSLionel Sambuc else if (!S.getLangOpts().CPlusPlus11)
5876f4a2713aSLionel Sambuc Diag = diag::ext_template_spec_decl_out_of_scope;
5877f4a2713aSLionel Sambuc else
5878f4a2713aSLionel Sambuc Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
5879f4a2713aSLionel Sambuc S.Diag(Loc, Diag)
5880f4a2713aSLionel Sambuc << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
5881f4a2713aSLionel Sambuc }
5882f4a2713aSLionel Sambuc
5883f4a2713aSLionel Sambuc S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
5884f4a2713aSLionel Sambuc }
5885f4a2713aSLionel Sambuc }
5886f4a2713aSLionel Sambuc
5887f4a2713aSLionel Sambuc return false;
5888f4a2713aSLionel Sambuc }
5889f4a2713aSLionel Sambuc
findTemplateParameter(unsigned Depth,Expr * E)5890*0a6a1f1dSLionel Sambuc static SourceRange findTemplateParameter(unsigned Depth, Expr *E) {
5891*0a6a1f1dSLionel Sambuc if (!E->isInstantiationDependent())
5892*0a6a1f1dSLionel Sambuc return SourceLocation();
5893*0a6a1f1dSLionel Sambuc DependencyChecker Checker(Depth);
5894*0a6a1f1dSLionel Sambuc Checker.TraverseStmt(E);
5895*0a6a1f1dSLionel Sambuc if (Checker.Match && Checker.MatchLoc.isInvalid())
5896*0a6a1f1dSLionel Sambuc return E->getSourceRange();
5897*0a6a1f1dSLionel Sambuc return Checker.MatchLoc;
5898*0a6a1f1dSLionel Sambuc }
5899*0a6a1f1dSLionel Sambuc
findTemplateParameter(unsigned Depth,TypeLoc TL)5900*0a6a1f1dSLionel Sambuc static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) {
5901*0a6a1f1dSLionel Sambuc if (!TL.getType()->isDependentType())
5902*0a6a1f1dSLionel Sambuc return SourceLocation();
5903*0a6a1f1dSLionel Sambuc DependencyChecker Checker(Depth);
5904*0a6a1f1dSLionel Sambuc Checker.TraverseTypeLoc(TL);
5905*0a6a1f1dSLionel Sambuc if (Checker.Match && Checker.MatchLoc.isInvalid())
5906*0a6a1f1dSLionel Sambuc return TL.getSourceRange();
5907*0a6a1f1dSLionel Sambuc return Checker.MatchLoc;
5908*0a6a1f1dSLionel Sambuc }
5909*0a6a1f1dSLionel Sambuc
5910f4a2713aSLionel Sambuc /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs
5911f4a2713aSLionel Sambuc /// that checks non-type template partial specialization arguments.
CheckNonTypeTemplatePartialSpecializationArgs(Sema & S,SourceLocation TemplateNameLoc,NonTypeTemplateParmDecl * Param,const TemplateArgument * Args,unsigned NumArgs,bool IsDefaultArgument)5912f4a2713aSLionel Sambuc static bool CheckNonTypeTemplatePartialSpecializationArgs(
5913*0a6a1f1dSLionel Sambuc Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param,
5914*0a6a1f1dSLionel Sambuc const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) {
5915f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumArgs; ++I) {
5916f4a2713aSLionel Sambuc if (Args[I].getKind() == TemplateArgument::Pack) {
5917f4a2713aSLionel Sambuc if (CheckNonTypeTemplatePartialSpecializationArgs(
5918*0a6a1f1dSLionel Sambuc S, TemplateNameLoc, Param, Args[I].pack_begin(),
5919*0a6a1f1dSLionel Sambuc Args[I].pack_size(), IsDefaultArgument))
5920f4a2713aSLionel Sambuc return true;
5921f4a2713aSLionel Sambuc
5922f4a2713aSLionel Sambuc continue;
5923f4a2713aSLionel Sambuc }
5924f4a2713aSLionel Sambuc
5925f4a2713aSLionel Sambuc if (Args[I].getKind() != TemplateArgument::Expression)
5926f4a2713aSLionel Sambuc continue;
5927f4a2713aSLionel Sambuc
5928f4a2713aSLionel Sambuc Expr *ArgExpr = Args[I].getAsExpr();
5929f4a2713aSLionel Sambuc
5930f4a2713aSLionel Sambuc // We can have a pack expansion of any of the bullets below.
5931f4a2713aSLionel Sambuc if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
5932f4a2713aSLionel Sambuc ArgExpr = Expansion->getPattern();
5933f4a2713aSLionel Sambuc
5934f4a2713aSLionel Sambuc // Strip off any implicit casts we added as part of type checking.
5935f4a2713aSLionel Sambuc while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
5936f4a2713aSLionel Sambuc ArgExpr = ICE->getSubExpr();
5937f4a2713aSLionel Sambuc
5938f4a2713aSLionel Sambuc // C++ [temp.class.spec]p8:
5939f4a2713aSLionel Sambuc // A non-type argument is non-specialized if it is the name of a
5940f4a2713aSLionel Sambuc // non-type parameter. All other non-type arguments are
5941f4a2713aSLionel Sambuc // specialized.
5942f4a2713aSLionel Sambuc //
5943f4a2713aSLionel Sambuc // Below, we check the two conditions that only apply to
5944f4a2713aSLionel Sambuc // specialized non-type arguments, so skip any non-specialized
5945f4a2713aSLionel Sambuc // arguments.
5946f4a2713aSLionel Sambuc if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
5947f4a2713aSLionel Sambuc if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
5948f4a2713aSLionel Sambuc continue;
5949f4a2713aSLionel Sambuc
5950f4a2713aSLionel Sambuc // C++ [temp.class.spec]p9:
5951f4a2713aSLionel Sambuc // Within the argument list of a class template partial
5952f4a2713aSLionel Sambuc // specialization, the following restrictions apply:
5953f4a2713aSLionel Sambuc // -- A partially specialized non-type argument expression
5954f4a2713aSLionel Sambuc // shall not involve a template parameter of the partial
5955f4a2713aSLionel Sambuc // specialization except when the argument expression is a
5956f4a2713aSLionel Sambuc // simple identifier.
5957*0a6a1f1dSLionel Sambuc SourceRange ParamUseRange =
5958*0a6a1f1dSLionel Sambuc findTemplateParameter(Param->getDepth(), ArgExpr);
5959*0a6a1f1dSLionel Sambuc if (ParamUseRange.isValid()) {
5960*0a6a1f1dSLionel Sambuc if (IsDefaultArgument) {
5961*0a6a1f1dSLionel Sambuc S.Diag(TemplateNameLoc,
5962*0a6a1f1dSLionel Sambuc diag::err_dependent_non_type_arg_in_partial_spec);
5963*0a6a1f1dSLionel Sambuc S.Diag(ParamUseRange.getBegin(),
5964*0a6a1f1dSLionel Sambuc diag::note_dependent_non_type_default_arg_in_partial_spec)
5965*0a6a1f1dSLionel Sambuc << ParamUseRange;
5966*0a6a1f1dSLionel Sambuc } else {
5967*0a6a1f1dSLionel Sambuc S.Diag(ParamUseRange.getBegin(),
5968f4a2713aSLionel Sambuc diag::err_dependent_non_type_arg_in_partial_spec)
5969*0a6a1f1dSLionel Sambuc << ParamUseRange;
5970*0a6a1f1dSLionel Sambuc }
5971f4a2713aSLionel Sambuc return true;
5972f4a2713aSLionel Sambuc }
5973f4a2713aSLionel Sambuc
5974f4a2713aSLionel Sambuc // -- The type of a template parameter corresponding to a
5975f4a2713aSLionel Sambuc // specialized non-type argument shall not be dependent on a
5976f4a2713aSLionel Sambuc // parameter of the specialization.
5977*0a6a1f1dSLionel Sambuc //
5978*0a6a1f1dSLionel Sambuc // FIXME: We need to delay this check until instantiation in some cases:
5979*0a6a1f1dSLionel Sambuc //
5980*0a6a1f1dSLionel Sambuc // template<template<typename> class X> struct A {
5981*0a6a1f1dSLionel Sambuc // template<typename T, X<T> N> struct B;
5982*0a6a1f1dSLionel Sambuc // template<typename T> struct B<T, 0>;
5983*0a6a1f1dSLionel Sambuc // };
5984*0a6a1f1dSLionel Sambuc // template<typename> using X = int;
5985*0a6a1f1dSLionel Sambuc // A<X>::B<int, 0> b;
5986*0a6a1f1dSLionel Sambuc ParamUseRange = findTemplateParameter(
5987*0a6a1f1dSLionel Sambuc Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc());
5988*0a6a1f1dSLionel Sambuc if (ParamUseRange.isValid()) {
5989*0a6a1f1dSLionel Sambuc S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(),
5990f4a2713aSLionel Sambuc diag::err_dependent_typed_non_type_arg_in_partial_spec)
5991*0a6a1f1dSLionel Sambuc << Param->getType() << ParamUseRange;
5992*0a6a1f1dSLionel Sambuc S.Diag(Param->getLocation(), diag::note_template_param_here)
5993*0a6a1f1dSLionel Sambuc << (IsDefaultArgument ? ParamUseRange : SourceRange());
5994f4a2713aSLionel Sambuc return true;
5995f4a2713aSLionel Sambuc }
5996f4a2713aSLionel Sambuc }
5997f4a2713aSLionel Sambuc
5998f4a2713aSLionel Sambuc return false;
5999f4a2713aSLionel Sambuc }
6000f4a2713aSLionel Sambuc
6001f4a2713aSLionel Sambuc /// \brief Check the non-type template arguments of a class template
6002f4a2713aSLionel Sambuc /// partial specialization according to C++ [temp.class.spec]p9.
6003f4a2713aSLionel Sambuc ///
6004*0a6a1f1dSLionel Sambuc /// \param TemplateNameLoc the location of the template name.
6005f4a2713aSLionel Sambuc /// \param TemplateParams the template parameters of the primary class
6006f4a2713aSLionel Sambuc /// template.
6007*0a6a1f1dSLionel Sambuc /// \param NumExplicit the number of explicitly-specified template arguments.
6008f4a2713aSLionel Sambuc /// \param TemplateArgs the template arguments of the class template
6009f4a2713aSLionel Sambuc /// partial specialization.
6010f4a2713aSLionel Sambuc ///
6011*0a6a1f1dSLionel Sambuc /// \returns \c true if there was an error, \c false otherwise.
CheckTemplatePartialSpecializationArgs(Sema & S,SourceLocation TemplateNameLoc,TemplateParameterList * TemplateParams,unsigned NumExplicit,SmallVectorImpl<TemplateArgument> & TemplateArgs)6012f4a2713aSLionel Sambuc static bool CheckTemplatePartialSpecializationArgs(
6013*0a6a1f1dSLionel Sambuc Sema &S, SourceLocation TemplateNameLoc,
6014*0a6a1f1dSLionel Sambuc TemplateParameterList *TemplateParams, unsigned NumExplicit,
6015f4a2713aSLionel Sambuc SmallVectorImpl<TemplateArgument> &TemplateArgs) {
6016f4a2713aSLionel Sambuc const TemplateArgument *ArgList = TemplateArgs.data();
6017f4a2713aSLionel Sambuc
6018f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
6019f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *Param
6020f4a2713aSLionel Sambuc = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
6021f4a2713aSLionel Sambuc if (!Param)
6022f4a2713aSLionel Sambuc continue;
6023f4a2713aSLionel Sambuc
6024*0a6a1f1dSLionel Sambuc if (CheckNonTypeTemplatePartialSpecializationArgs(
6025*0a6a1f1dSLionel Sambuc S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit))
6026f4a2713aSLionel Sambuc return true;
6027f4a2713aSLionel Sambuc }
6028f4a2713aSLionel Sambuc
6029f4a2713aSLionel Sambuc return false;
6030f4a2713aSLionel Sambuc }
6031f4a2713aSLionel Sambuc
6032f4a2713aSLionel Sambuc DeclResult
ActOnClassTemplateSpecialization(Scope * S,unsigned TagSpec,TagUseKind TUK,SourceLocation KWLoc,SourceLocation ModulePrivateLoc,TemplateIdAnnotation & TemplateId,AttributeList * Attr,MultiTemplateParamsArg TemplateParameterLists)6033f4a2713aSLionel Sambuc Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
6034f4a2713aSLionel Sambuc TagUseKind TUK,
6035f4a2713aSLionel Sambuc SourceLocation KWLoc,
6036f4a2713aSLionel Sambuc SourceLocation ModulePrivateLoc,
6037*0a6a1f1dSLionel Sambuc TemplateIdAnnotation &TemplateId,
6038f4a2713aSLionel Sambuc AttributeList *Attr,
6039f4a2713aSLionel Sambuc MultiTemplateParamsArg TemplateParameterLists) {
6040f4a2713aSLionel Sambuc assert(TUK != TUK_Reference && "References are not specializations");
6041f4a2713aSLionel Sambuc
6042*0a6a1f1dSLionel Sambuc CXXScopeSpec &SS = TemplateId.SS;
6043*0a6a1f1dSLionel Sambuc
6044f4a2713aSLionel Sambuc // NOTE: KWLoc is the location of the tag keyword. This will instead
6045f4a2713aSLionel Sambuc // store the location of the outermost template keyword in the declaration.
6046f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
6047*0a6a1f1dSLionel Sambuc ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc;
6048*0a6a1f1dSLionel Sambuc SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc;
6049*0a6a1f1dSLionel Sambuc SourceLocation LAngleLoc = TemplateId.LAngleLoc;
6050*0a6a1f1dSLionel Sambuc SourceLocation RAngleLoc = TemplateId.RAngleLoc;
6051f4a2713aSLionel Sambuc
6052f4a2713aSLionel Sambuc // Find the class template we're specializing
6053*0a6a1f1dSLionel Sambuc TemplateName Name = TemplateId.Template.get();
6054f4a2713aSLionel Sambuc ClassTemplateDecl *ClassTemplate
6055f4a2713aSLionel Sambuc = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
6056f4a2713aSLionel Sambuc
6057f4a2713aSLionel Sambuc if (!ClassTemplate) {
6058f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
6059f4a2713aSLionel Sambuc << (Name.getAsTemplateDecl() &&
6060f4a2713aSLionel Sambuc isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
6061f4a2713aSLionel Sambuc return true;
6062f4a2713aSLionel Sambuc }
6063f4a2713aSLionel Sambuc
6064f4a2713aSLionel Sambuc bool isExplicitSpecialization = false;
6065f4a2713aSLionel Sambuc bool isPartialSpecialization = false;
6066f4a2713aSLionel Sambuc
6067f4a2713aSLionel Sambuc // Check the validity of the template headers that introduce this
6068f4a2713aSLionel Sambuc // template.
6069f4a2713aSLionel Sambuc // FIXME: We probably shouldn't complain about these headers for
6070f4a2713aSLionel Sambuc // friend declarations.
6071f4a2713aSLionel Sambuc bool Invalid = false;
6072f4a2713aSLionel Sambuc TemplateParameterList *TemplateParams =
6073f4a2713aSLionel Sambuc MatchTemplateParametersToScopeSpecifier(
6074*0a6a1f1dSLionel Sambuc KWLoc, TemplateNameLoc, SS, &TemplateId,
6075*0a6a1f1dSLionel Sambuc TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization,
6076*0a6a1f1dSLionel Sambuc Invalid);
6077f4a2713aSLionel Sambuc if (Invalid)
6078f4a2713aSLionel Sambuc return true;
6079f4a2713aSLionel Sambuc
6080f4a2713aSLionel Sambuc if (TemplateParams && TemplateParams->size() > 0) {
6081f4a2713aSLionel Sambuc isPartialSpecialization = true;
6082f4a2713aSLionel Sambuc
6083f4a2713aSLionel Sambuc if (TUK == TUK_Friend) {
6084f4a2713aSLionel Sambuc Diag(KWLoc, diag::err_partial_specialization_friend)
6085f4a2713aSLionel Sambuc << SourceRange(LAngleLoc, RAngleLoc);
6086f4a2713aSLionel Sambuc return true;
6087f4a2713aSLionel Sambuc }
6088f4a2713aSLionel Sambuc
6089f4a2713aSLionel Sambuc // C++ [temp.class.spec]p10:
6090f4a2713aSLionel Sambuc // The template parameter list of a specialization shall not
6091f4a2713aSLionel Sambuc // contain default template argument values.
6092f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
6093f4a2713aSLionel Sambuc Decl *Param = TemplateParams->getParam(I);
6094f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
6095f4a2713aSLionel Sambuc if (TTP->hasDefaultArgument()) {
6096f4a2713aSLionel Sambuc Diag(TTP->getDefaultArgumentLoc(),
6097f4a2713aSLionel Sambuc diag::err_default_arg_in_partial_spec);
6098f4a2713aSLionel Sambuc TTP->removeDefaultArgument();
6099f4a2713aSLionel Sambuc }
6100f4a2713aSLionel Sambuc } else if (NonTypeTemplateParmDecl *NTTP
6101f4a2713aSLionel Sambuc = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
6102f4a2713aSLionel Sambuc if (Expr *DefArg = NTTP->getDefaultArgument()) {
6103f4a2713aSLionel Sambuc Diag(NTTP->getDefaultArgumentLoc(),
6104f4a2713aSLionel Sambuc diag::err_default_arg_in_partial_spec)
6105f4a2713aSLionel Sambuc << DefArg->getSourceRange();
6106f4a2713aSLionel Sambuc NTTP->removeDefaultArgument();
6107f4a2713aSLionel Sambuc }
6108f4a2713aSLionel Sambuc } else {
6109f4a2713aSLionel Sambuc TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
6110f4a2713aSLionel Sambuc if (TTP->hasDefaultArgument()) {
6111f4a2713aSLionel Sambuc Diag(TTP->getDefaultArgument().getLocation(),
6112f4a2713aSLionel Sambuc diag::err_default_arg_in_partial_spec)
6113f4a2713aSLionel Sambuc << TTP->getDefaultArgument().getSourceRange();
6114f4a2713aSLionel Sambuc TTP->removeDefaultArgument();
6115f4a2713aSLionel Sambuc }
6116f4a2713aSLionel Sambuc }
6117f4a2713aSLionel Sambuc }
6118f4a2713aSLionel Sambuc } else if (TemplateParams) {
6119f4a2713aSLionel Sambuc if (TUK == TUK_Friend)
6120f4a2713aSLionel Sambuc Diag(KWLoc, diag::err_template_spec_friend)
6121f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(
6122f4a2713aSLionel Sambuc SourceRange(TemplateParams->getTemplateLoc(),
6123f4a2713aSLionel Sambuc TemplateParams->getRAngleLoc()))
6124f4a2713aSLionel Sambuc << SourceRange(LAngleLoc, RAngleLoc);
6125f4a2713aSLionel Sambuc else
6126f4a2713aSLionel Sambuc isExplicitSpecialization = true;
6127*0a6a1f1dSLionel Sambuc } else {
6128*0a6a1f1dSLionel Sambuc assert(TUK == TUK_Friend && "should have a 'template<>' for this decl");
6129f4a2713aSLionel Sambuc }
6130f4a2713aSLionel Sambuc
6131f4a2713aSLionel Sambuc // Check that the specialization uses the same tag kind as the
6132f4a2713aSLionel Sambuc // original template.
6133f4a2713aSLionel Sambuc TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
6134f4a2713aSLionel Sambuc assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
6135f4a2713aSLionel Sambuc if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
6136f4a2713aSLionel Sambuc Kind, TUK == TUK_Definition, KWLoc,
6137f4a2713aSLionel Sambuc *ClassTemplate->getIdentifier())) {
6138f4a2713aSLionel Sambuc Diag(KWLoc, diag::err_use_with_wrong_tag)
6139f4a2713aSLionel Sambuc << ClassTemplate
6140f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(KWLoc,
6141f4a2713aSLionel Sambuc ClassTemplate->getTemplatedDecl()->getKindName());
6142f4a2713aSLionel Sambuc Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
6143f4a2713aSLionel Sambuc diag::note_previous_use);
6144f4a2713aSLionel Sambuc Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
6145f4a2713aSLionel Sambuc }
6146f4a2713aSLionel Sambuc
6147f4a2713aSLionel Sambuc // Translate the parser's template argument list in our AST format.
6148*0a6a1f1dSLionel Sambuc TemplateArgumentListInfo TemplateArgs =
6149*0a6a1f1dSLionel Sambuc makeTemplateArgumentListInfo(*this, TemplateId);
6150f4a2713aSLionel Sambuc
6151f4a2713aSLionel Sambuc // Check for unexpanded parameter packs in any of the template arguments.
6152f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6153f4a2713aSLionel Sambuc if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
6154f4a2713aSLionel Sambuc UPPC_PartialSpecialization))
6155f4a2713aSLionel Sambuc return true;
6156f4a2713aSLionel Sambuc
6157f4a2713aSLionel Sambuc // Check that the template argument list is well-formed for this
6158f4a2713aSLionel Sambuc // template.
6159f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> Converted;
6160f4a2713aSLionel Sambuc if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
6161f4a2713aSLionel Sambuc TemplateArgs, false, Converted))
6162f4a2713aSLionel Sambuc return true;
6163f4a2713aSLionel Sambuc
6164f4a2713aSLionel Sambuc // Find the class template (partial) specialization declaration that
6165f4a2713aSLionel Sambuc // corresponds to these arguments.
6166f4a2713aSLionel Sambuc if (isPartialSpecialization) {
6167f4a2713aSLionel Sambuc if (CheckTemplatePartialSpecializationArgs(
6168*0a6a1f1dSLionel Sambuc *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(),
6169*0a6a1f1dSLionel Sambuc TemplateArgs.size(), Converted))
6170f4a2713aSLionel Sambuc return true;
6171f4a2713aSLionel Sambuc
6172f4a2713aSLionel Sambuc bool InstantiationDependent;
6173f4a2713aSLionel Sambuc if (!Name.isDependent() &&
6174f4a2713aSLionel Sambuc !TemplateSpecializationType::anyDependentTemplateArguments(
6175f4a2713aSLionel Sambuc TemplateArgs.getArgumentArray(),
6176f4a2713aSLionel Sambuc TemplateArgs.size(),
6177f4a2713aSLionel Sambuc InstantiationDependent)) {
6178f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
6179f4a2713aSLionel Sambuc << ClassTemplate->getDeclName();
6180f4a2713aSLionel Sambuc isPartialSpecialization = false;
6181f4a2713aSLionel Sambuc }
6182f4a2713aSLionel Sambuc }
6183f4a2713aSLionel Sambuc
6184*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
6185*0a6a1f1dSLionel Sambuc ClassTemplateSpecializationDecl *PrevDecl = nullptr;
6186f4a2713aSLionel Sambuc
6187f4a2713aSLionel Sambuc if (isPartialSpecialization)
6188f4a2713aSLionel Sambuc // FIXME: Template parameter list matters, too
6189*0a6a1f1dSLionel Sambuc PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos);
6190f4a2713aSLionel Sambuc else
6191*0a6a1f1dSLionel Sambuc PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos);
6192f4a2713aSLionel Sambuc
6193*0a6a1f1dSLionel Sambuc ClassTemplateSpecializationDecl *Specialization = nullptr;
6194f4a2713aSLionel Sambuc
6195f4a2713aSLionel Sambuc // Check whether we can declare a class template specialization in
6196f4a2713aSLionel Sambuc // the current scope.
6197f4a2713aSLionel Sambuc if (TUK != TUK_Friend &&
6198f4a2713aSLionel Sambuc CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
6199f4a2713aSLionel Sambuc TemplateNameLoc,
6200f4a2713aSLionel Sambuc isPartialSpecialization))
6201f4a2713aSLionel Sambuc return true;
6202f4a2713aSLionel Sambuc
6203f4a2713aSLionel Sambuc // The canonical type
6204f4a2713aSLionel Sambuc QualType CanonType;
6205*0a6a1f1dSLionel Sambuc if (isPartialSpecialization) {
6206f4a2713aSLionel Sambuc // Build the canonical type that describes the converted template
6207f4a2713aSLionel Sambuc // arguments of the class template partial specialization.
6208f4a2713aSLionel Sambuc TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
6209f4a2713aSLionel Sambuc CanonType = Context.getTemplateSpecializationType(CanonTemplate,
6210f4a2713aSLionel Sambuc Converted.data(),
6211f4a2713aSLionel Sambuc Converted.size());
6212f4a2713aSLionel Sambuc
6213f4a2713aSLionel Sambuc if (Context.hasSameType(CanonType,
6214f4a2713aSLionel Sambuc ClassTemplate->getInjectedClassNameSpecialization())) {
6215f4a2713aSLionel Sambuc // C++ [temp.class.spec]p9b3:
6216f4a2713aSLionel Sambuc //
6217f4a2713aSLionel Sambuc // -- The argument list of the specialization shall not be identical
6218f4a2713aSLionel Sambuc // to the implicit argument list of the primary template.
6219f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
6220f4a2713aSLionel Sambuc << /*class template*/0 << (TUK == TUK_Definition)
6221f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
6222f4a2713aSLionel Sambuc return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
6223f4a2713aSLionel Sambuc ClassTemplate->getIdentifier(),
6224f4a2713aSLionel Sambuc TemplateNameLoc,
6225f4a2713aSLionel Sambuc Attr,
6226f4a2713aSLionel Sambuc TemplateParams,
6227f4a2713aSLionel Sambuc AS_none, /*ModulePrivateLoc=*/SourceLocation(),
6228*0a6a1f1dSLionel Sambuc /*FriendLoc*/SourceLocation(),
6229f4a2713aSLionel Sambuc TemplateParameterLists.size() - 1,
6230f4a2713aSLionel Sambuc TemplateParameterLists.data());
6231f4a2713aSLionel Sambuc }
6232f4a2713aSLionel Sambuc
6233f4a2713aSLionel Sambuc // Create a new class template partial specialization declaration node.
6234f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *PrevPartial
6235f4a2713aSLionel Sambuc = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
6236f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *Partial
6237f4a2713aSLionel Sambuc = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
6238f4a2713aSLionel Sambuc ClassTemplate->getDeclContext(),
6239f4a2713aSLionel Sambuc KWLoc, TemplateNameLoc,
6240f4a2713aSLionel Sambuc TemplateParams,
6241f4a2713aSLionel Sambuc ClassTemplate,
6242f4a2713aSLionel Sambuc Converted.data(),
6243f4a2713aSLionel Sambuc Converted.size(),
6244f4a2713aSLionel Sambuc TemplateArgs,
6245f4a2713aSLionel Sambuc CanonType,
6246f4a2713aSLionel Sambuc PrevPartial);
6247f4a2713aSLionel Sambuc SetNestedNameSpecifier(Partial, SS);
6248f4a2713aSLionel Sambuc if (TemplateParameterLists.size() > 1 && SS.isSet()) {
6249f4a2713aSLionel Sambuc Partial->setTemplateParameterListsInfo(Context,
6250f4a2713aSLionel Sambuc TemplateParameterLists.size() - 1,
6251f4a2713aSLionel Sambuc TemplateParameterLists.data());
6252f4a2713aSLionel Sambuc }
6253f4a2713aSLionel Sambuc
6254f4a2713aSLionel Sambuc if (!PrevPartial)
6255f4a2713aSLionel Sambuc ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
6256f4a2713aSLionel Sambuc Specialization = Partial;
6257f4a2713aSLionel Sambuc
6258f4a2713aSLionel Sambuc // If we are providing an explicit specialization of a member class
6259f4a2713aSLionel Sambuc // template specialization, make a note of that.
6260f4a2713aSLionel Sambuc if (PrevPartial && PrevPartial->getInstantiatedFromMember())
6261f4a2713aSLionel Sambuc PrevPartial->setMemberSpecialization();
6262f4a2713aSLionel Sambuc
6263f4a2713aSLionel Sambuc // Check that all of the template parameters of the class template
6264f4a2713aSLionel Sambuc // partial specialization are deducible from the template
6265f4a2713aSLionel Sambuc // arguments. If not, this class template partial specialization
6266f4a2713aSLionel Sambuc // will never be used.
6267f4a2713aSLionel Sambuc llvm::SmallBitVector DeducibleParams(TemplateParams->size());
6268f4a2713aSLionel Sambuc MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
6269f4a2713aSLionel Sambuc TemplateParams->getDepth(),
6270f4a2713aSLionel Sambuc DeducibleParams);
6271f4a2713aSLionel Sambuc
6272f4a2713aSLionel Sambuc if (!DeducibleParams.all()) {
6273f4a2713aSLionel Sambuc unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count();
6274f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
6275f4a2713aSLionel Sambuc << /*class template*/0 << (NumNonDeducible > 1)
6276f4a2713aSLionel Sambuc << SourceRange(TemplateNameLoc, RAngleLoc);
6277f4a2713aSLionel Sambuc for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
6278f4a2713aSLionel Sambuc if (!DeducibleParams[I]) {
6279f4a2713aSLionel Sambuc NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
6280f4a2713aSLionel Sambuc if (Param->getDeclName())
6281f4a2713aSLionel Sambuc Diag(Param->getLocation(),
6282f4a2713aSLionel Sambuc diag::note_partial_spec_unused_parameter)
6283f4a2713aSLionel Sambuc << Param->getDeclName();
6284f4a2713aSLionel Sambuc else
6285f4a2713aSLionel Sambuc Diag(Param->getLocation(),
6286f4a2713aSLionel Sambuc diag::note_partial_spec_unused_parameter)
6287*0a6a1f1dSLionel Sambuc << "(anonymous)";
6288f4a2713aSLionel Sambuc }
6289f4a2713aSLionel Sambuc }
6290f4a2713aSLionel Sambuc }
6291f4a2713aSLionel Sambuc } else {
6292f4a2713aSLionel Sambuc // Create a new class template specialization declaration node for
6293f4a2713aSLionel Sambuc // this explicit specialization or friend declaration.
6294f4a2713aSLionel Sambuc Specialization
6295f4a2713aSLionel Sambuc = ClassTemplateSpecializationDecl::Create(Context, Kind,
6296f4a2713aSLionel Sambuc ClassTemplate->getDeclContext(),
6297f4a2713aSLionel Sambuc KWLoc, TemplateNameLoc,
6298f4a2713aSLionel Sambuc ClassTemplate,
6299f4a2713aSLionel Sambuc Converted.data(),
6300f4a2713aSLionel Sambuc Converted.size(),
6301f4a2713aSLionel Sambuc PrevDecl);
6302f4a2713aSLionel Sambuc SetNestedNameSpecifier(Specialization, SS);
6303f4a2713aSLionel Sambuc if (TemplateParameterLists.size() > 0) {
6304f4a2713aSLionel Sambuc Specialization->setTemplateParameterListsInfo(Context,
6305f4a2713aSLionel Sambuc TemplateParameterLists.size(),
6306f4a2713aSLionel Sambuc TemplateParameterLists.data());
6307f4a2713aSLionel Sambuc }
6308f4a2713aSLionel Sambuc
6309f4a2713aSLionel Sambuc if (!PrevDecl)
6310f4a2713aSLionel Sambuc ClassTemplate->AddSpecialization(Specialization, InsertPos);
6311f4a2713aSLionel Sambuc
6312f4a2713aSLionel Sambuc CanonType = Context.getTypeDeclType(Specialization);
6313f4a2713aSLionel Sambuc }
6314f4a2713aSLionel Sambuc
6315f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p6:
6316f4a2713aSLionel Sambuc // If a template, a member template or the member of a class template is
6317f4a2713aSLionel Sambuc // explicitly specialized then that specialization shall be declared
6318f4a2713aSLionel Sambuc // before the first use of that specialization that would cause an implicit
6319f4a2713aSLionel Sambuc // instantiation to take place, in every translation unit in which such a
6320f4a2713aSLionel Sambuc // use occurs; no diagnostic is required.
6321f4a2713aSLionel Sambuc if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
6322f4a2713aSLionel Sambuc bool Okay = false;
6323f4a2713aSLionel Sambuc for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
6324f4a2713aSLionel Sambuc // Is there any previous explicit specialization declaration?
6325f4a2713aSLionel Sambuc if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
6326f4a2713aSLionel Sambuc Okay = true;
6327f4a2713aSLionel Sambuc break;
6328f4a2713aSLionel Sambuc }
6329f4a2713aSLionel Sambuc }
6330f4a2713aSLionel Sambuc
6331f4a2713aSLionel Sambuc if (!Okay) {
6332f4a2713aSLionel Sambuc SourceRange Range(TemplateNameLoc, RAngleLoc);
6333f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
6334f4a2713aSLionel Sambuc << Context.getTypeDeclType(Specialization) << Range;
6335f4a2713aSLionel Sambuc
6336f4a2713aSLionel Sambuc Diag(PrevDecl->getPointOfInstantiation(),
6337f4a2713aSLionel Sambuc diag::note_instantiation_required_here)
6338f4a2713aSLionel Sambuc << (PrevDecl->getTemplateSpecializationKind()
6339f4a2713aSLionel Sambuc != TSK_ImplicitInstantiation);
6340f4a2713aSLionel Sambuc return true;
6341f4a2713aSLionel Sambuc }
6342f4a2713aSLionel Sambuc }
6343f4a2713aSLionel Sambuc
6344f4a2713aSLionel Sambuc // If this is not a friend, note that this is an explicit specialization.
6345f4a2713aSLionel Sambuc if (TUK != TUK_Friend)
6346f4a2713aSLionel Sambuc Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
6347f4a2713aSLionel Sambuc
6348f4a2713aSLionel Sambuc // Check that this isn't a redefinition of this specialization.
6349f4a2713aSLionel Sambuc if (TUK == TUK_Definition) {
6350f4a2713aSLionel Sambuc if (RecordDecl *Def = Specialization->getDefinition()) {
6351f4a2713aSLionel Sambuc SourceRange Range(TemplateNameLoc, RAngleLoc);
6352f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_redefinition)
6353f4a2713aSLionel Sambuc << Context.getTypeDeclType(Specialization) << Range;
6354f4a2713aSLionel Sambuc Diag(Def->getLocation(), diag::note_previous_definition);
6355f4a2713aSLionel Sambuc Specialization->setInvalidDecl();
6356f4a2713aSLionel Sambuc return true;
6357f4a2713aSLionel Sambuc }
6358f4a2713aSLionel Sambuc }
6359f4a2713aSLionel Sambuc
6360f4a2713aSLionel Sambuc if (Attr)
6361f4a2713aSLionel Sambuc ProcessDeclAttributeList(S, Specialization, Attr);
6362f4a2713aSLionel Sambuc
6363f4a2713aSLionel Sambuc // Add alignment attributes if necessary; these attributes are checked when
6364f4a2713aSLionel Sambuc // the ASTContext lays out the structure.
6365f4a2713aSLionel Sambuc if (TUK == TUK_Definition) {
6366f4a2713aSLionel Sambuc AddAlignmentAttributesForRecord(Specialization);
6367f4a2713aSLionel Sambuc AddMsStructLayoutForRecord(Specialization);
6368f4a2713aSLionel Sambuc }
6369f4a2713aSLionel Sambuc
6370f4a2713aSLionel Sambuc if (ModulePrivateLoc.isValid())
6371f4a2713aSLionel Sambuc Diag(Specialization->getLocation(), diag::err_module_private_specialization)
6372f4a2713aSLionel Sambuc << (isPartialSpecialization? 1 : 0)
6373f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(ModulePrivateLoc);
6374f4a2713aSLionel Sambuc
6375f4a2713aSLionel Sambuc // Build the fully-sugared type for this class template
6376f4a2713aSLionel Sambuc // specialization as the user wrote in the specialization
6377f4a2713aSLionel Sambuc // itself. This means that we'll pretty-print the type retrieved
6378f4a2713aSLionel Sambuc // from the specialization's declaration the way that the user
6379f4a2713aSLionel Sambuc // actually wrote the specialization, rather than formatting the
6380f4a2713aSLionel Sambuc // name based on the "canonical" representation used to store the
6381f4a2713aSLionel Sambuc // template arguments in the specialization.
6382f4a2713aSLionel Sambuc TypeSourceInfo *WrittenTy
6383f4a2713aSLionel Sambuc = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
6384f4a2713aSLionel Sambuc TemplateArgs, CanonType);
6385f4a2713aSLionel Sambuc if (TUK != TUK_Friend) {
6386f4a2713aSLionel Sambuc Specialization->setTypeAsWritten(WrittenTy);
6387f4a2713aSLionel Sambuc Specialization->setTemplateKeywordLoc(TemplateKWLoc);
6388f4a2713aSLionel Sambuc }
6389f4a2713aSLionel Sambuc
6390f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p9:
6391f4a2713aSLionel Sambuc // A template explicit specialization is in the scope of the
6392f4a2713aSLionel Sambuc // namespace in which the template was defined.
6393f4a2713aSLionel Sambuc //
6394f4a2713aSLionel Sambuc // We actually implement this paragraph where we set the semantic
6395f4a2713aSLionel Sambuc // context (in the creation of the ClassTemplateSpecializationDecl),
6396f4a2713aSLionel Sambuc // but we also maintain the lexical context where the actual
6397f4a2713aSLionel Sambuc // definition occurs.
6398f4a2713aSLionel Sambuc Specialization->setLexicalDeclContext(CurContext);
6399f4a2713aSLionel Sambuc
6400f4a2713aSLionel Sambuc // We may be starting the definition of this specialization.
6401f4a2713aSLionel Sambuc if (TUK == TUK_Definition)
6402f4a2713aSLionel Sambuc Specialization->startDefinition();
6403f4a2713aSLionel Sambuc
6404f4a2713aSLionel Sambuc if (TUK == TUK_Friend) {
6405f4a2713aSLionel Sambuc FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
6406f4a2713aSLionel Sambuc TemplateNameLoc,
6407f4a2713aSLionel Sambuc WrittenTy,
6408f4a2713aSLionel Sambuc /*FIXME:*/KWLoc);
6409f4a2713aSLionel Sambuc Friend->setAccess(AS_public);
6410f4a2713aSLionel Sambuc CurContext->addDecl(Friend);
6411f4a2713aSLionel Sambuc } else {
6412f4a2713aSLionel Sambuc // Add the specialization into its lexical context, so that it can
6413f4a2713aSLionel Sambuc // be seen when iterating through the list of declarations in that
6414f4a2713aSLionel Sambuc // context. However, specializations are not found by name lookup.
6415f4a2713aSLionel Sambuc CurContext->addDecl(Specialization);
6416f4a2713aSLionel Sambuc }
6417f4a2713aSLionel Sambuc return Specialization;
6418f4a2713aSLionel Sambuc }
6419f4a2713aSLionel Sambuc
ActOnTemplateDeclarator(Scope * S,MultiTemplateParamsArg TemplateParameterLists,Declarator & D)6420f4a2713aSLionel Sambuc Decl *Sema::ActOnTemplateDeclarator(Scope *S,
6421f4a2713aSLionel Sambuc MultiTemplateParamsArg TemplateParameterLists,
6422f4a2713aSLionel Sambuc Declarator &D) {
6423f4a2713aSLionel Sambuc Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists);
6424f4a2713aSLionel Sambuc ActOnDocumentableDecl(NewDecl);
6425f4a2713aSLionel Sambuc return NewDecl;
6426f4a2713aSLionel Sambuc }
6427f4a2713aSLionel Sambuc
ActOnStartOfFunctionTemplateDef(Scope * FnBodyScope,MultiTemplateParamsArg TemplateParameterLists,Declarator & D)6428f4a2713aSLionel Sambuc Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
6429f4a2713aSLionel Sambuc MultiTemplateParamsArg TemplateParameterLists,
6430f4a2713aSLionel Sambuc Declarator &D) {
6431*0a6a1f1dSLionel Sambuc assert(getCurFunctionDecl() == nullptr && "Function parsing confused");
6432f4a2713aSLionel Sambuc DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
6433f4a2713aSLionel Sambuc
6434f4a2713aSLionel Sambuc if (FTI.hasPrototype) {
6435f4a2713aSLionel Sambuc // FIXME: Diagnose arguments without names in C.
6436f4a2713aSLionel Sambuc }
6437f4a2713aSLionel Sambuc
6438f4a2713aSLionel Sambuc Scope *ParentScope = FnBodyScope->getParent();
6439f4a2713aSLionel Sambuc
6440f4a2713aSLionel Sambuc D.setFunctionDefinitionKind(FDK_Definition);
6441f4a2713aSLionel Sambuc Decl *DP = HandleDeclarator(ParentScope, D,
6442f4a2713aSLionel Sambuc TemplateParameterLists);
6443f4a2713aSLionel Sambuc return ActOnStartOfFunctionDef(FnBodyScope, DP);
6444f4a2713aSLionel Sambuc }
6445f4a2713aSLionel Sambuc
6446f4a2713aSLionel Sambuc /// \brief Strips various properties off an implicit instantiation
6447f4a2713aSLionel Sambuc /// that has just been explicitly specialized.
StripImplicitInstantiation(NamedDecl * D)6448f4a2713aSLionel Sambuc static void StripImplicitInstantiation(NamedDecl *D) {
6449*0a6a1f1dSLionel Sambuc D->dropAttr<DLLImportAttr>();
6450*0a6a1f1dSLionel Sambuc D->dropAttr<DLLExportAttr>();
6451f4a2713aSLionel Sambuc
6452*0a6a1f1dSLionel Sambuc if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
6453f4a2713aSLionel Sambuc FD->setInlineSpecified(false);
6454f4a2713aSLionel Sambuc }
6455f4a2713aSLionel Sambuc
6456f4a2713aSLionel Sambuc /// \brief Compute the diagnostic location for an explicit instantiation
6457f4a2713aSLionel Sambuc // declaration or definition.
DiagLocForExplicitInstantiation(NamedDecl * D,SourceLocation PointOfInstantiation)6458f4a2713aSLionel Sambuc static SourceLocation DiagLocForExplicitInstantiation(
6459f4a2713aSLionel Sambuc NamedDecl* D, SourceLocation PointOfInstantiation) {
6460f4a2713aSLionel Sambuc // Explicit instantiations following a specialization have no effect and
6461f4a2713aSLionel Sambuc // hence no PointOfInstantiation. In that case, walk decl backwards
6462f4a2713aSLionel Sambuc // until a valid name loc is found.
6463f4a2713aSLionel Sambuc SourceLocation PrevDiagLoc = PointOfInstantiation;
6464f4a2713aSLionel Sambuc for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
6465f4a2713aSLionel Sambuc Prev = Prev->getPreviousDecl()) {
6466f4a2713aSLionel Sambuc PrevDiagLoc = Prev->getLocation();
6467f4a2713aSLionel Sambuc }
6468f4a2713aSLionel Sambuc assert(PrevDiagLoc.isValid() &&
6469f4a2713aSLionel Sambuc "Explicit instantiation without point of instantiation?");
6470f4a2713aSLionel Sambuc return PrevDiagLoc;
6471f4a2713aSLionel Sambuc }
6472f4a2713aSLionel Sambuc
6473f4a2713aSLionel Sambuc /// \brief Diagnose cases where we have an explicit template specialization
6474f4a2713aSLionel Sambuc /// before/after an explicit template instantiation, producing diagnostics
6475f4a2713aSLionel Sambuc /// for those cases where they are required and determining whether the
6476f4a2713aSLionel Sambuc /// new specialization/instantiation will have any effect.
6477f4a2713aSLionel Sambuc ///
6478f4a2713aSLionel Sambuc /// \param NewLoc the location of the new explicit specialization or
6479f4a2713aSLionel Sambuc /// instantiation.
6480f4a2713aSLionel Sambuc ///
6481f4a2713aSLionel Sambuc /// \param NewTSK the kind of the new explicit specialization or instantiation.
6482f4a2713aSLionel Sambuc ///
6483f4a2713aSLionel Sambuc /// \param PrevDecl the previous declaration of the entity.
6484f4a2713aSLionel Sambuc ///
6485f4a2713aSLionel Sambuc /// \param PrevTSK the kind of the old explicit specialization or instantiatin.
6486f4a2713aSLionel Sambuc ///
6487f4a2713aSLionel Sambuc /// \param PrevPointOfInstantiation if valid, indicates where the previus
6488f4a2713aSLionel Sambuc /// declaration was instantiated (either implicitly or explicitly).
6489f4a2713aSLionel Sambuc ///
6490f4a2713aSLionel Sambuc /// \param HasNoEffect will be set to true to indicate that the new
6491f4a2713aSLionel Sambuc /// specialization or instantiation has no effect and should be ignored.
6492f4a2713aSLionel Sambuc ///
6493f4a2713aSLionel Sambuc /// \returns true if there was an error that should prevent the introduction of
6494f4a2713aSLionel Sambuc /// the new declaration into the AST, false otherwise.
6495f4a2713aSLionel Sambuc bool
CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,TemplateSpecializationKind NewTSK,NamedDecl * PrevDecl,TemplateSpecializationKind PrevTSK,SourceLocation PrevPointOfInstantiation,bool & HasNoEffect)6496f4a2713aSLionel Sambuc Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
6497f4a2713aSLionel Sambuc TemplateSpecializationKind NewTSK,
6498f4a2713aSLionel Sambuc NamedDecl *PrevDecl,
6499f4a2713aSLionel Sambuc TemplateSpecializationKind PrevTSK,
6500f4a2713aSLionel Sambuc SourceLocation PrevPointOfInstantiation,
6501f4a2713aSLionel Sambuc bool &HasNoEffect) {
6502f4a2713aSLionel Sambuc HasNoEffect = false;
6503f4a2713aSLionel Sambuc
6504f4a2713aSLionel Sambuc switch (NewTSK) {
6505f4a2713aSLionel Sambuc case TSK_Undeclared:
6506f4a2713aSLionel Sambuc case TSK_ImplicitInstantiation:
6507*0a6a1f1dSLionel Sambuc assert(
6508*0a6a1f1dSLionel Sambuc (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) &&
6509*0a6a1f1dSLionel Sambuc "previous declaration must be implicit!");
6510*0a6a1f1dSLionel Sambuc return false;
6511f4a2713aSLionel Sambuc
6512f4a2713aSLionel Sambuc case TSK_ExplicitSpecialization:
6513f4a2713aSLionel Sambuc switch (PrevTSK) {
6514f4a2713aSLionel Sambuc case TSK_Undeclared:
6515f4a2713aSLionel Sambuc case TSK_ExplicitSpecialization:
6516f4a2713aSLionel Sambuc // Okay, we're just specializing something that is either already
6517f4a2713aSLionel Sambuc // explicitly specialized or has merely been mentioned without any
6518f4a2713aSLionel Sambuc // instantiation.
6519f4a2713aSLionel Sambuc return false;
6520f4a2713aSLionel Sambuc
6521f4a2713aSLionel Sambuc case TSK_ImplicitInstantiation:
6522f4a2713aSLionel Sambuc if (PrevPointOfInstantiation.isInvalid()) {
6523f4a2713aSLionel Sambuc // The declaration itself has not actually been instantiated, so it is
6524f4a2713aSLionel Sambuc // still okay to specialize it.
6525f4a2713aSLionel Sambuc StripImplicitInstantiation(PrevDecl);
6526f4a2713aSLionel Sambuc return false;
6527f4a2713aSLionel Sambuc }
6528f4a2713aSLionel Sambuc // Fall through
6529f4a2713aSLionel Sambuc
6530f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDeclaration:
6531f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDefinition:
6532f4a2713aSLionel Sambuc assert((PrevTSK == TSK_ImplicitInstantiation ||
6533f4a2713aSLionel Sambuc PrevPointOfInstantiation.isValid()) &&
6534f4a2713aSLionel Sambuc "Explicit instantiation without point of instantiation?");
6535f4a2713aSLionel Sambuc
6536f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p6:
6537f4a2713aSLionel Sambuc // If a template, a member template or the member of a class template
6538f4a2713aSLionel Sambuc // is explicitly specialized then that specialization shall be declared
6539f4a2713aSLionel Sambuc // before the first use of that specialization that would cause an
6540f4a2713aSLionel Sambuc // implicit instantiation to take place, in every translation unit in
6541f4a2713aSLionel Sambuc // which such a use occurs; no diagnostic is required.
6542f4a2713aSLionel Sambuc for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
6543f4a2713aSLionel Sambuc // Is there any previous explicit specialization declaration?
6544f4a2713aSLionel Sambuc if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
6545f4a2713aSLionel Sambuc return false;
6546f4a2713aSLionel Sambuc }
6547f4a2713aSLionel Sambuc
6548f4a2713aSLionel Sambuc Diag(NewLoc, diag::err_specialization_after_instantiation)
6549f4a2713aSLionel Sambuc << PrevDecl;
6550f4a2713aSLionel Sambuc Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
6551f4a2713aSLionel Sambuc << (PrevTSK != TSK_ImplicitInstantiation);
6552f4a2713aSLionel Sambuc
6553f4a2713aSLionel Sambuc return true;
6554f4a2713aSLionel Sambuc }
6555f4a2713aSLionel Sambuc
6556f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDeclaration:
6557f4a2713aSLionel Sambuc switch (PrevTSK) {
6558f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDeclaration:
6559f4a2713aSLionel Sambuc // This explicit instantiation declaration is redundant (that's okay).
6560f4a2713aSLionel Sambuc HasNoEffect = true;
6561f4a2713aSLionel Sambuc return false;
6562f4a2713aSLionel Sambuc
6563f4a2713aSLionel Sambuc case TSK_Undeclared:
6564f4a2713aSLionel Sambuc case TSK_ImplicitInstantiation:
6565f4a2713aSLionel Sambuc // We're explicitly instantiating something that may have already been
6566f4a2713aSLionel Sambuc // implicitly instantiated; that's fine.
6567f4a2713aSLionel Sambuc return false;
6568f4a2713aSLionel Sambuc
6569f4a2713aSLionel Sambuc case TSK_ExplicitSpecialization:
6570f4a2713aSLionel Sambuc // C++0x [temp.explicit]p4:
6571f4a2713aSLionel Sambuc // For a given set of template parameters, if an explicit instantiation
6572f4a2713aSLionel Sambuc // of a template appears after a declaration of an explicit
6573f4a2713aSLionel Sambuc // specialization for that template, the explicit instantiation has no
6574f4a2713aSLionel Sambuc // effect.
6575f4a2713aSLionel Sambuc HasNoEffect = true;
6576f4a2713aSLionel Sambuc return false;
6577f4a2713aSLionel Sambuc
6578f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDefinition:
6579f4a2713aSLionel Sambuc // C++0x [temp.explicit]p10:
6580f4a2713aSLionel Sambuc // If an entity is the subject of both an explicit instantiation
6581f4a2713aSLionel Sambuc // declaration and an explicit instantiation definition in the same
6582f4a2713aSLionel Sambuc // translation unit, the definition shall follow the declaration.
6583f4a2713aSLionel Sambuc Diag(NewLoc,
6584f4a2713aSLionel Sambuc diag::err_explicit_instantiation_declaration_after_definition);
6585f4a2713aSLionel Sambuc
6586f4a2713aSLionel Sambuc // Explicit instantiations following a specialization have no effect and
6587f4a2713aSLionel Sambuc // hence no PrevPointOfInstantiation. In that case, walk decl backwards
6588f4a2713aSLionel Sambuc // until a valid name loc is found.
6589f4a2713aSLionel Sambuc Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
6590f4a2713aSLionel Sambuc diag::note_explicit_instantiation_definition_here);
6591f4a2713aSLionel Sambuc HasNoEffect = true;
6592f4a2713aSLionel Sambuc return false;
6593f4a2713aSLionel Sambuc }
6594f4a2713aSLionel Sambuc
6595f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDefinition:
6596f4a2713aSLionel Sambuc switch (PrevTSK) {
6597f4a2713aSLionel Sambuc case TSK_Undeclared:
6598f4a2713aSLionel Sambuc case TSK_ImplicitInstantiation:
6599f4a2713aSLionel Sambuc // We're explicitly instantiating something that may have already been
6600f4a2713aSLionel Sambuc // implicitly instantiated; that's fine.
6601f4a2713aSLionel Sambuc return false;
6602f4a2713aSLionel Sambuc
6603f4a2713aSLionel Sambuc case TSK_ExplicitSpecialization:
6604f4a2713aSLionel Sambuc // C++ DR 259, C++0x [temp.explicit]p4:
6605f4a2713aSLionel Sambuc // For a given set of template parameters, if an explicit
6606f4a2713aSLionel Sambuc // instantiation of a template appears after a declaration of
6607f4a2713aSLionel Sambuc // an explicit specialization for that template, the explicit
6608f4a2713aSLionel Sambuc // instantiation has no effect.
6609f4a2713aSLionel Sambuc //
6610f4a2713aSLionel Sambuc // In C++98/03 mode, we only give an extension warning here, because it
6611f4a2713aSLionel Sambuc // is not harmful to try to explicitly instantiate something that
6612f4a2713aSLionel Sambuc // has been explicitly specialized.
6613f4a2713aSLionel Sambuc Diag(NewLoc, getLangOpts().CPlusPlus11 ?
6614f4a2713aSLionel Sambuc diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
6615f4a2713aSLionel Sambuc diag::ext_explicit_instantiation_after_specialization)
6616f4a2713aSLionel Sambuc << PrevDecl;
6617f4a2713aSLionel Sambuc Diag(PrevDecl->getLocation(),
6618f4a2713aSLionel Sambuc diag::note_previous_template_specialization);
6619f4a2713aSLionel Sambuc HasNoEffect = true;
6620f4a2713aSLionel Sambuc return false;
6621f4a2713aSLionel Sambuc
6622f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDeclaration:
6623f4a2713aSLionel Sambuc // We're explicity instantiating a definition for something for which we
6624f4a2713aSLionel Sambuc // were previously asked to suppress instantiations. That's fine.
6625f4a2713aSLionel Sambuc
6626f4a2713aSLionel Sambuc // C++0x [temp.explicit]p4:
6627f4a2713aSLionel Sambuc // For a given set of template parameters, if an explicit instantiation
6628f4a2713aSLionel Sambuc // of a template appears after a declaration of an explicit
6629f4a2713aSLionel Sambuc // specialization for that template, the explicit instantiation has no
6630f4a2713aSLionel Sambuc // effect.
6631f4a2713aSLionel Sambuc for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
6632f4a2713aSLionel Sambuc // Is there any previous explicit specialization declaration?
6633f4a2713aSLionel Sambuc if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
6634f4a2713aSLionel Sambuc HasNoEffect = true;
6635f4a2713aSLionel Sambuc break;
6636f4a2713aSLionel Sambuc }
6637f4a2713aSLionel Sambuc }
6638f4a2713aSLionel Sambuc
6639f4a2713aSLionel Sambuc return false;
6640f4a2713aSLionel Sambuc
6641f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDefinition:
6642f4a2713aSLionel Sambuc // C++0x [temp.spec]p5:
6643f4a2713aSLionel Sambuc // For a given template and a given set of template-arguments,
6644f4a2713aSLionel Sambuc // - an explicit instantiation definition shall appear at most once
6645f4a2713aSLionel Sambuc // in a program,
6646*0a6a1f1dSLionel Sambuc
6647*0a6a1f1dSLionel Sambuc // MSVCCompat: MSVC silently ignores duplicate explicit instantiations.
6648*0a6a1f1dSLionel Sambuc Diag(NewLoc, (getLangOpts().MSVCCompat)
6649*0a6a1f1dSLionel Sambuc ? diag::ext_explicit_instantiation_duplicate
6650*0a6a1f1dSLionel Sambuc : diag::err_explicit_instantiation_duplicate)
6651f4a2713aSLionel Sambuc << PrevDecl;
6652f4a2713aSLionel Sambuc Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
6653f4a2713aSLionel Sambuc diag::note_previous_explicit_instantiation);
6654f4a2713aSLionel Sambuc HasNoEffect = true;
6655f4a2713aSLionel Sambuc return false;
6656f4a2713aSLionel Sambuc }
6657f4a2713aSLionel Sambuc }
6658f4a2713aSLionel Sambuc
6659f4a2713aSLionel Sambuc llvm_unreachable("Missing specialization/instantiation case?");
6660f4a2713aSLionel Sambuc }
6661f4a2713aSLionel Sambuc
6662f4a2713aSLionel Sambuc /// \brief Perform semantic analysis for the given dependent function
6663f4a2713aSLionel Sambuc /// template specialization.
6664f4a2713aSLionel Sambuc ///
6665f4a2713aSLionel Sambuc /// The only possible way to get a dependent function template specialization
6666f4a2713aSLionel Sambuc /// is with a friend declaration, like so:
6667f4a2713aSLionel Sambuc ///
6668f4a2713aSLionel Sambuc /// \code
6669f4a2713aSLionel Sambuc /// template \<class T> void foo(T);
6670f4a2713aSLionel Sambuc /// template \<class T> class A {
6671f4a2713aSLionel Sambuc /// friend void foo<>(T);
6672f4a2713aSLionel Sambuc /// };
6673f4a2713aSLionel Sambuc /// \endcode
6674f4a2713aSLionel Sambuc ///
6675f4a2713aSLionel Sambuc /// There really isn't any useful analysis we can do here, so we
6676f4a2713aSLionel Sambuc /// just store the information.
6677f4a2713aSLionel Sambuc bool
CheckDependentFunctionTemplateSpecialization(FunctionDecl * FD,const TemplateArgumentListInfo & ExplicitTemplateArgs,LookupResult & Previous)6678f4a2713aSLionel Sambuc Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
6679f4a2713aSLionel Sambuc const TemplateArgumentListInfo &ExplicitTemplateArgs,
6680f4a2713aSLionel Sambuc LookupResult &Previous) {
6681f4a2713aSLionel Sambuc // Remove anything from Previous that isn't a function template in
6682f4a2713aSLionel Sambuc // the correct context.
6683f4a2713aSLionel Sambuc DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
6684f4a2713aSLionel Sambuc LookupResult::Filter F = Previous.makeFilter();
6685f4a2713aSLionel Sambuc while (F.hasNext()) {
6686f4a2713aSLionel Sambuc NamedDecl *D = F.next()->getUnderlyingDecl();
6687f4a2713aSLionel Sambuc if (!isa<FunctionTemplateDecl>(D) ||
6688f4a2713aSLionel Sambuc !FDLookupContext->InEnclosingNamespaceSetOf(
6689f4a2713aSLionel Sambuc D->getDeclContext()->getRedeclContext()))
6690f4a2713aSLionel Sambuc F.erase();
6691f4a2713aSLionel Sambuc }
6692f4a2713aSLionel Sambuc F.done();
6693f4a2713aSLionel Sambuc
6694f4a2713aSLionel Sambuc // Should this be diagnosed here?
6695f4a2713aSLionel Sambuc if (Previous.empty()) return true;
6696f4a2713aSLionel Sambuc
6697f4a2713aSLionel Sambuc FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
6698f4a2713aSLionel Sambuc ExplicitTemplateArgs);
6699f4a2713aSLionel Sambuc return false;
6700f4a2713aSLionel Sambuc }
6701f4a2713aSLionel Sambuc
6702f4a2713aSLionel Sambuc /// \brief Perform semantic analysis for the given function template
6703f4a2713aSLionel Sambuc /// specialization.
6704f4a2713aSLionel Sambuc ///
6705f4a2713aSLionel Sambuc /// This routine performs all of the semantic analysis required for an
6706f4a2713aSLionel Sambuc /// explicit function template specialization. On successful completion,
6707f4a2713aSLionel Sambuc /// the function declaration \p FD will become a function template
6708f4a2713aSLionel Sambuc /// specialization.
6709f4a2713aSLionel Sambuc ///
6710f4a2713aSLionel Sambuc /// \param FD the function declaration, which will be updated to become a
6711f4a2713aSLionel Sambuc /// function template specialization.
6712f4a2713aSLionel Sambuc ///
6713f4a2713aSLionel Sambuc /// \param ExplicitTemplateArgs the explicitly-provided template arguments,
6714f4a2713aSLionel Sambuc /// if any. Note that this may be valid info even when 0 arguments are
6715f4a2713aSLionel Sambuc /// explicitly provided as in, e.g., \c void sort<>(char*, char*);
6716f4a2713aSLionel Sambuc /// as it anyway contains info on the angle brackets locations.
6717f4a2713aSLionel Sambuc ///
6718f4a2713aSLionel Sambuc /// \param Previous the set of declarations that may be specialized by
6719f4a2713aSLionel Sambuc /// this function specialization.
CheckFunctionTemplateSpecialization(FunctionDecl * FD,TemplateArgumentListInfo * ExplicitTemplateArgs,LookupResult & Previous)6720f4a2713aSLionel Sambuc bool Sema::CheckFunctionTemplateSpecialization(
6721f4a2713aSLionel Sambuc FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs,
6722f4a2713aSLionel Sambuc LookupResult &Previous) {
6723f4a2713aSLionel Sambuc // The set of function template specializations that could match this
6724f4a2713aSLionel Sambuc // explicit function template specialization.
6725f4a2713aSLionel Sambuc UnresolvedSet<8> Candidates;
6726f4a2713aSLionel Sambuc TemplateSpecCandidateSet FailedCandidates(FD->getLocation());
6727f4a2713aSLionel Sambuc
6728f4a2713aSLionel Sambuc DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
6729f4a2713aSLionel Sambuc for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
6730f4a2713aSLionel Sambuc I != E; ++I) {
6731f4a2713aSLionel Sambuc NamedDecl *Ovl = (*I)->getUnderlyingDecl();
6732f4a2713aSLionel Sambuc if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
6733f4a2713aSLionel Sambuc // Only consider templates found within the same semantic lookup scope as
6734f4a2713aSLionel Sambuc // FD.
6735f4a2713aSLionel Sambuc if (!FDLookupContext->InEnclosingNamespaceSetOf(
6736f4a2713aSLionel Sambuc Ovl->getDeclContext()->getRedeclContext()))
6737f4a2713aSLionel Sambuc continue;
6738f4a2713aSLionel Sambuc
6739f4a2713aSLionel Sambuc // When matching a constexpr member function template specialization
6740f4a2713aSLionel Sambuc // against the primary template, we don't yet know whether the
6741f4a2713aSLionel Sambuc // specialization has an implicit 'const' (because we don't know whether
6742f4a2713aSLionel Sambuc // it will be a static member function until we know which template it
6743f4a2713aSLionel Sambuc // specializes), so adjust it now assuming it specializes this template.
6744f4a2713aSLionel Sambuc QualType FT = FD->getType();
6745f4a2713aSLionel Sambuc if (FD->isConstexpr()) {
6746f4a2713aSLionel Sambuc CXXMethodDecl *OldMD =
6747f4a2713aSLionel Sambuc dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
6748f4a2713aSLionel Sambuc if (OldMD && OldMD->isConst()) {
6749f4a2713aSLionel Sambuc const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>();
6750f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
6751f4a2713aSLionel Sambuc EPI.TypeQuals |= Qualifiers::Const;
6752*0a6a1f1dSLionel Sambuc FT = Context.getFunctionType(FPT->getReturnType(),
6753*0a6a1f1dSLionel Sambuc FPT->getParamTypes(), EPI);
6754f4a2713aSLionel Sambuc }
6755f4a2713aSLionel Sambuc }
6756f4a2713aSLionel Sambuc
6757f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p11:
6758f4a2713aSLionel Sambuc // A trailing template-argument can be left unspecified in the
6759f4a2713aSLionel Sambuc // template-id naming an explicit function template specialization
6760f4a2713aSLionel Sambuc // provided it can be deduced from the function argument type.
6761f4a2713aSLionel Sambuc // Perform template argument deduction to determine whether we may be
6762f4a2713aSLionel Sambuc // specializing this template.
6763f4a2713aSLionel Sambuc // FIXME: It is somewhat wasteful to build
6764f4a2713aSLionel Sambuc TemplateDeductionInfo Info(FailedCandidates.getLocation());
6765*0a6a1f1dSLionel Sambuc FunctionDecl *Specialization = nullptr;
6766*0a6a1f1dSLionel Sambuc if (TemplateDeductionResult TDK = DeduceTemplateArguments(
6767*0a6a1f1dSLionel Sambuc cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()),
6768*0a6a1f1dSLionel Sambuc ExplicitTemplateArgs, FT, Specialization, Info)) {
6769f4a2713aSLionel Sambuc // Template argument deduction failed; record why it failed, so
6770f4a2713aSLionel Sambuc // that we can provide nifty diagnostics.
6771f4a2713aSLionel Sambuc FailedCandidates.addCandidate()
6772f4a2713aSLionel Sambuc .set(FunTmpl->getTemplatedDecl(),
6773f4a2713aSLionel Sambuc MakeDeductionFailureInfo(Context, TDK, Info));
6774f4a2713aSLionel Sambuc (void)TDK;
6775f4a2713aSLionel Sambuc continue;
6776f4a2713aSLionel Sambuc }
6777f4a2713aSLionel Sambuc
6778f4a2713aSLionel Sambuc // Record this candidate.
6779f4a2713aSLionel Sambuc Candidates.addDecl(Specialization, I.getAccess());
6780f4a2713aSLionel Sambuc }
6781f4a2713aSLionel Sambuc }
6782f4a2713aSLionel Sambuc
6783f4a2713aSLionel Sambuc // Find the most specialized function template.
6784f4a2713aSLionel Sambuc UnresolvedSetIterator Result = getMostSpecialized(
6785f4a2713aSLionel Sambuc Candidates.begin(), Candidates.end(), FailedCandidates,
6786f4a2713aSLionel Sambuc FD->getLocation(),
6787f4a2713aSLionel Sambuc PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(),
6788f4a2713aSLionel Sambuc PDiag(diag::err_function_template_spec_ambiguous)
6789*0a6a1f1dSLionel Sambuc << FD->getDeclName() << (ExplicitTemplateArgs != nullptr),
6790f4a2713aSLionel Sambuc PDiag(diag::note_function_template_spec_matched));
6791f4a2713aSLionel Sambuc
6792f4a2713aSLionel Sambuc if (Result == Candidates.end())
6793f4a2713aSLionel Sambuc return true;
6794f4a2713aSLionel Sambuc
6795f4a2713aSLionel Sambuc // Ignore access information; it doesn't figure into redeclaration checking.
6796f4a2713aSLionel Sambuc FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
6797f4a2713aSLionel Sambuc
6798f4a2713aSLionel Sambuc FunctionTemplateSpecializationInfo *SpecInfo
6799f4a2713aSLionel Sambuc = Specialization->getTemplateSpecializationInfo();
6800f4a2713aSLionel Sambuc assert(SpecInfo && "Function template specialization info missing?");
6801f4a2713aSLionel Sambuc
6802f4a2713aSLionel Sambuc // Note: do not overwrite location info if previous template
6803f4a2713aSLionel Sambuc // specialization kind was explicit.
6804f4a2713aSLionel Sambuc TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
6805f4a2713aSLionel Sambuc if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) {
6806f4a2713aSLionel Sambuc Specialization->setLocation(FD->getLocation());
6807f4a2713aSLionel Sambuc // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr
6808f4a2713aSLionel Sambuc // function can differ from the template declaration with respect to
6809f4a2713aSLionel Sambuc // the constexpr specifier.
6810f4a2713aSLionel Sambuc Specialization->setConstexpr(FD->isConstexpr());
6811f4a2713aSLionel Sambuc }
6812f4a2713aSLionel Sambuc
6813f4a2713aSLionel Sambuc // FIXME: Check if the prior specialization has a point of instantiation.
6814f4a2713aSLionel Sambuc // If so, we have run afoul of .
6815f4a2713aSLionel Sambuc
6816f4a2713aSLionel Sambuc // If this is a friend declaration, then we're not really declaring
6817f4a2713aSLionel Sambuc // an explicit specialization.
6818f4a2713aSLionel Sambuc bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
6819f4a2713aSLionel Sambuc
6820f4a2713aSLionel Sambuc // Check the scope of this explicit specialization.
6821f4a2713aSLionel Sambuc if (!isFriend &&
6822f4a2713aSLionel Sambuc CheckTemplateSpecializationScope(*this,
6823f4a2713aSLionel Sambuc Specialization->getPrimaryTemplate(),
6824f4a2713aSLionel Sambuc Specialization, FD->getLocation(),
6825f4a2713aSLionel Sambuc false))
6826f4a2713aSLionel Sambuc return true;
6827f4a2713aSLionel Sambuc
6828f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p6:
6829f4a2713aSLionel Sambuc // If a template, a member template or the member of a class template is
6830f4a2713aSLionel Sambuc // explicitly specialized then that specialization shall be declared
6831f4a2713aSLionel Sambuc // before the first use of that specialization that would cause an implicit
6832f4a2713aSLionel Sambuc // instantiation to take place, in every translation unit in which such a
6833f4a2713aSLionel Sambuc // use occurs; no diagnostic is required.
6834f4a2713aSLionel Sambuc bool HasNoEffect = false;
6835f4a2713aSLionel Sambuc if (!isFriend &&
6836f4a2713aSLionel Sambuc CheckSpecializationInstantiationRedecl(FD->getLocation(),
6837f4a2713aSLionel Sambuc TSK_ExplicitSpecialization,
6838f4a2713aSLionel Sambuc Specialization,
6839f4a2713aSLionel Sambuc SpecInfo->getTemplateSpecializationKind(),
6840f4a2713aSLionel Sambuc SpecInfo->getPointOfInstantiation(),
6841f4a2713aSLionel Sambuc HasNoEffect))
6842f4a2713aSLionel Sambuc return true;
6843f4a2713aSLionel Sambuc
6844f4a2713aSLionel Sambuc // Mark the prior declaration as an explicit specialization, so that later
6845f4a2713aSLionel Sambuc // clients know that this is an explicit specialization.
6846f4a2713aSLionel Sambuc if (!isFriend) {
6847f4a2713aSLionel Sambuc SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
6848f4a2713aSLionel Sambuc MarkUnusedFileScopedDecl(Specialization);
6849f4a2713aSLionel Sambuc }
6850f4a2713aSLionel Sambuc
6851f4a2713aSLionel Sambuc // Turn the given function declaration into a function template
6852f4a2713aSLionel Sambuc // specialization, with the template arguments from the previous
6853f4a2713aSLionel Sambuc // specialization.
6854f4a2713aSLionel Sambuc // Take copies of (semantic and syntactic) template argument lists.
6855f4a2713aSLionel Sambuc const TemplateArgumentList* TemplArgs = new (Context)
6856f4a2713aSLionel Sambuc TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
6857f4a2713aSLionel Sambuc FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
6858*0a6a1f1dSLionel Sambuc TemplArgs, /*InsertPos=*/nullptr,
6859f4a2713aSLionel Sambuc SpecInfo->getTemplateSpecializationKind(),
6860f4a2713aSLionel Sambuc ExplicitTemplateArgs);
6861f4a2713aSLionel Sambuc
6862f4a2713aSLionel Sambuc // The "previous declaration" for this function template specialization is
6863f4a2713aSLionel Sambuc // the prior function template specialization.
6864f4a2713aSLionel Sambuc Previous.clear();
6865f4a2713aSLionel Sambuc Previous.addDecl(Specialization);
6866f4a2713aSLionel Sambuc return false;
6867f4a2713aSLionel Sambuc }
6868f4a2713aSLionel Sambuc
6869f4a2713aSLionel Sambuc /// \brief Perform semantic analysis for the given non-template member
6870f4a2713aSLionel Sambuc /// specialization.
6871f4a2713aSLionel Sambuc ///
6872f4a2713aSLionel Sambuc /// This routine performs all of the semantic analysis required for an
6873f4a2713aSLionel Sambuc /// explicit member function specialization. On successful completion,
6874f4a2713aSLionel Sambuc /// the function declaration \p FD will become a member function
6875f4a2713aSLionel Sambuc /// specialization.
6876f4a2713aSLionel Sambuc ///
6877f4a2713aSLionel Sambuc /// \param Member the member declaration, which will be updated to become a
6878f4a2713aSLionel Sambuc /// specialization.
6879f4a2713aSLionel Sambuc ///
6880f4a2713aSLionel Sambuc /// \param Previous the set of declarations, one of which may be specialized
6881f4a2713aSLionel Sambuc /// by this function specialization; the set will be modified to contain the
6882f4a2713aSLionel Sambuc /// redeclared member.
6883f4a2713aSLionel Sambuc bool
CheckMemberSpecialization(NamedDecl * Member,LookupResult & Previous)6884f4a2713aSLionel Sambuc Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
6885f4a2713aSLionel Sambuc assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
6886f4a2713aSLionel Sambuc
6887f4a2713aSLionel Sambuc // Try to find the member we are instantiating.
6888*0a6a1f1dSLionel Sambuc NamedDecl *Instantiation = nullptr;
6889*0a6a1f1dSLionel Sambuc NamedDecl *InstantiatedFrom = nullptr;
6890*0a6a1f1dSLionel Sambuc MemberSpecializationInfo *MSInfo = nullptr;
6891f4a2713aSLionel Sambuc
6892f4a2713aSLionel Sambuc if (Previous.empty()) {
6893f4a2713aSLionel Sambuc // Nowhere to look anyway.
6894f4a2713aSLionel Sambuc } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
6895f4a2713aSLionel Sambuc for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
6896f4a2713aSLionel Sambuc I != E; ++I) {
6897f4a2713aSLionel Sambuc NamedDecl *D = (*I)->getUnderlyingDecl();
6898f4a2713aSLionel Sambuc if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
6899*0a6a1f1dSLionel Sambuc QualType Adjusted = Function->getType();
6900*0a6a1f1dSLionel Sambuc if (!hasExplicitCallingConv(Adjusted))
6901*0a6a1f1dSLionel Sambuc Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType());
6902*0a6a1f1dSLionel Sambuc if (Context.hasSameType(Adjusted, Method->getType())) {
6903f4a2713aSLionel Sambuc Instantiation = Method;
6904f4a2713aSLionel Sambuc InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
6905f4a2713aSLionel Sambuc MSInfo = Method->getMemberSpecializationInfo();
6906f4a2713aSLionel Sambuc break;
6907f4a2713aSLionel Sambuc }
6908f4a2713aSLionel Sambuc }
6909f4a2713aSLionel Sambuc }
6910f4a2713aSLionel Sambuc } else if (isa<VarDecl>(Member)) {
6911f4a2713aSLionel Sambuc VarDecl *PrevVar;
6912f4a2713aSLionel Sambuc if (Previous.isSingleResult() &&
6913f4a2713aSLionel Sambuc (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
6914f4a2713aSLionel Sambuc if (PrevVar->isStaticDataMember()) {
6915f4a2713aSLionel Sambuc Instantiation = PrevVar;
6916f4a2713aSLionel Sambuc InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
6917f4a2713aSLionel Sambuc MSInfo = PrevVar->getMemberSpecializationInfo();
6918f4a2713aSLionel Sambuc }
6919f4a2713aSLionel Sambuc } else if (isa<RecordDecl>(Member)) {
6920f4a2713aSLionel Sambuc CXXRecordDecl *PrevRecord;
6921f4a2713aSLionel Sambuc if (Previous.isSingleResult() &&
6922f4a2713aSLionel Sambuc (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
6923f4a2713aSLionel Sambuc Instantiation = PrevRecord;
6924f4a2713aSLionel Sambuc InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
6925f4a2713aSLionel Sambuc MSInfo = PrevRecord->getMemberSpecializationInfo();
6926f4a2713aSLionel Sambuc }
6927f4a2713aSLionel Sambuc } else if (isa<EnumDecl>(Member)) {
6928f4a2713aSLionel Sambuc EnumDecl *PrevEnum;
6929f4a2713aSLionel Sambuc if (Previous.isSingleResult() &&
6930f4a2713aSLionel Sambuc (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) {
6931f4a2713aSLionel Sambuc Instantiation = PrevEnum;
6932f4a2713aSLionel Sambuc InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum();
6933f4a2713aSLionel Sambuc MSInfo = PrevEnum->getMemberSpecializationInfo();
6934f4a2713aSLionel Sambuc }
6935f4a2713aSLionel Sambuc }
6936f4a2713aSLionel Sambuc
6937f4a2713aSLionel Sambuc if (!Instantiation) {
6938f4a2713aSLionel Sambuc // There is no previous declaration that matches. Since member
6939f4a2713aSLionel Sambuc // specializations are always out-of-line, the caller will complain about
6940f4a2713aSLionel Sambuc // this mismatch later.
6941f4a2713aSLionel Sambuc return false;
6942f4a2713aSLionel Sambuc }
6943f4a2713aSLionel Sambuc
6944f4a2713aSLionel Sambuc // If this is a friend, just bail out here before we start turning
6945f4a2713aSLionel Sambuc // things into explicit specializations.
6946f4a2713aSLionel Sambuc if (Member->getFriendObjectKind() != Decl::FOK_None) {
6947f4a2713aSLionel Sambuc // Preserve instantiation information.
6948f4a2713aSLionel Sambuc if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
6949f4a2713aSLionel Sambuc cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
6950f4a2713aSLionel Sambuc cast<CXXMethodDecl>(InstantiatedFrom),
6951f4a2713aSLionel Sambuc cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
6952f4a2713aSLionel Sambuc } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
6953f4a2713aSLionel Sambuc cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
6954f4a2713aSLionel Sambuc cast<CXXRecordDecl>(InstantiatedFrom),
6955f4a2713aSLionel Sambuc cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
6956f4a2713aSLionel Sambuc }
6957f4a2713aSLionel Sambuc
6958f4a2713aSLionel Sambuc Previous.clear();
6959f4a2713aSLionel Sambuc Previous.addDecl(Instantiation);
6960f4a2713aSLionel Sambuc return false;
6961f4a2713aSLionel Sambuc }
6962f4a2713aSLionel Sambuc
6963f4a2713aSLionel Sambuc // Make sure that this is a specialization of a member.
6964f4a2713aSLionel Sambuc if (!InstantiatedFrom) {
6965f4a2713aSLionel Sambuc Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
6966f4a2713aSLionel Sambuc << Member;
6967f4a2713aSLionel Sambuc Diag(Instantiation->getLocation(), diag::note_specialized_decl);
6968f4a2713aSLionel Sambuc return true;
6969f4a2713aSLionel Sambuc }
6970f4a2713aSLionel Sambuc
6971f4a2713aSLionel Sambuc // C++ [temp.expl.spec]p6:
6972f4a2713aSLionel Sambuc // If a template, a member template or the member of a class template is
6973f4a2713aSLionel Sambuc // explicitly specialized then that specialization shall be declared
6974f4a2713aSLionel Sambuc // before the first use of that specialization that would cause an implicit
6975f4a2713aSLionel Sambuc // instantiation to take place, in every translation unit in which such a
6976f4a2713aSLionel Sambuc // use occurs; no diagnostic is required.
6977f4a2713aSLionel Sambuc assert(MSInfo && "Member specialization info missing?");
6978f4a2713aSLionel Sambuc
6979f4a2713aSLionel Sambuc bool HasNoEffect = false;
6980f4a2713aSLionel Sambuc if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
6981f4a2713aSLionel Sambuc TSK_ExplicitSpecialization,
6982f4a2713aSLionel Sambuc Instantiation,
6983f4a2713aSLionel Sambuc MSInfo->getTemplateSpecializationKind(),
6984f4a2713aSLionel Sambuc MSInfo->getPointOfInstantiation(),
6985f4a2713aSLionel Sambuc HasNoEffect))
6986f4a2713aSLionel Sambuc return true;
6987f4a2713aSLionel Sambuc
6988f4a2713aSLionel Sambuc // Check the scope of this explicit specialization.
6989f4a2713aSLionel Sambuc if (CheckTemplateSpecializationScope(*this,
6990f4a2713aSLionel Sambuc InstantiatedFrom,
6991f4a2713aSLionel Sambuc Instantiation, Member->getLocation(),
6992f4a2713aSLionel Sambuc false))
6993f4a2713aSLionel Sambuc return true;
6994f4a2713aSLionel Sambuc
6995f4a2713aSLionel Sambuc // Note that this is an explicit instantiation of a member.
6996f4a2713aSLionel Sambuc // the original declaration to note that it is an explicit specialization
6997f4a2713aSLionel Sambuc // (if it was previously an implicit instantiation). This latter step
6998f4a2713aSLionel Sambuc // makes bookkeeping easier.
6999f4a2713aSLionel Sambuc if (isa<FunctionDecl>(Member)) {
7000f4a2713aSLionel Sambuc FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
7001f4a2713aSLionel Sambuc if (InstantiationFunction->getTemplateSpecializationKind() ==
7002f4a2713aSLionel Sambuc TSK_ImplicitInstantiation) {
7003f4a2713aSLionel Sambuc InstantiationFunction->setTemplateSpecializationKind(
7004f4a2713aSLionel Sambuc TSK_ExplicitSpecialization);
7005f4a2713aSLionel Sambuc InstantiationFunction->setLocation(Member->getLocation());
7006f4a2713aSLionel Sambuc }
7007f4a2713aSLionel Sambuc
7008f4a2713aSLionel Sambuc cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
7009f4a2713aSLionel Sambuc cast<CXXMethodDecl>(InstantiatedFrom),
7010f4a2713aSLionel Sambuc TSK_ExplicitSpecialization);
7011f4a2713aSLionel Sambuc MarkUnusedFileScopedDecl(InstantiationFunction);
7012f4a2713aSLionel Sambuc } else if (isa<VarDecl>(Member)) {
7013f4a2713aSLionel Sambuc VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
7014f4a2713aSLionel Sambuc if (InstantiationVar->getTemplateSpecializationKind() ==
7015f4a2713aSLionel Sambuc TSK_ImplicitInstantiation) {
7016f4a2713aSLionel Sambuc InstantiationVar->setTemplateSpecializationKind(
7017f4a2713aSLionel Sambuc TSK_ExplicitSpecialization);
7018f4a2713aSLionel Sambuc InstantiationVar->setLocation(Member->getLocation());
7019f4a2713aSLionel Sambuc }
7020f4a2713aSLionel Sambuc
7021f4a2713aSLionel Sambuc cast<VarDecl>(Member)->setInstantiationOfStaticDataMember(
7022f4a2713aSLionel Sambuc cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization);
7023f4a2713aSLionel Sambuc MarkUnusedFileScopedDecl(InstantiationVar);
7024f4a2713aSLionel Sambuc } else if (isa<CXXRecordDecl>(Member)) {
7025f4a2713aSLionel Sambuc CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
7026f4a2713aSLionel Sambuc if (InstantiationClass->getTemplateSpecializationKind() ==
7027f4a2713aSLionel Sambuc TSK_ImplicitInstantiation) {
7028f4a2713aSLionel Sambuc InstantiationClass->setTemplateSpecializationKind(
7029f4a2713aSLionel Sambuc TSK_ExplicitSpecialization);
7030f4a2713aSLionel Sambuc InstantiationClass->setLocation(Member->getLocation());
7031f4a2713aSLionel Sambuc }
7032f4a2713aSLionel Sambuc
7033f4a2713aSLionel Sambuc cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
7034f4a2713aSLionel Sambuc cast<CXXRecordDecl>(InstantiatedFrom),
7035f4a2713aSLionel Sambuc TSK_ExplicitSpecialization);
7036f4a2713aSLionel Sambuc } else {
7037f4a2713aSLionel Sambuc assert(isa<EnumDecl>(Member) && "Only member enums remain");
7038f4a2713aSLionel Sambuc EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation);
7039f4a2713aSLionel Sambuc if (InstantiationEnum->getTemplateSpecializationKind() ==
7040f4a2713aSLionel Sambuc TSK_ImplicitInstantiation) {
7041f4a2713aSLionel Sambuc InstantiationEnum->setTemplateSpecializationKind(
7042f4a2713aSLionel Sambuc TSK_ExplicitSpecialization);
7043f4a2713aSLionel Sambuc InstantiationEnum->setLocation(Member->getLocation());
7044f4a2713aSLionel Sambuc }
7045f4a2713aSLionel Sambuc
7046f4a2713aSLionel Sambuc cast<EnumDecl>(Member)->setInstantiationOfMemberEnum(
7047f4a2713aSLionel Sambuc cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization);
7048f4a2713aSLionel Sambuc }
7049f4a2713aSLionel Sambuc
7050f4a2713aSLionel Sambuc // Save the caller the trouble of having to figure out which declaration
7051f4a2713aSLionel Sambuc // this specialization matches.
7052f4a2713aSLionel Sambuc Previous.clear();
7053f4a2713aSLionel Sambuc Previous.addDecl(Instantiation);
7054f4a2713aSLionel Sambuc return false;
7055f4a2713aSLionel Sambuc }
7056f4a2713aSLionel Sambuc
7057f4a2713aSLionel Sambuc /// \brief Check the scope of an explicit instantiation.
7058f4a2713aSLionel Sambuc ///
7059f4a2713aSLionel Sambuc /// \returns true if a serious error occurs, false otherwise.
CheckExplicitInstantiationScope(Sema & S,NamedDecl * D,SourceLocation InstLoc,bool WasQualifiedName)7060f4a2713aSLionel Sambuc static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
7061f4a2713aSLionel Sambuc SourceLocation InstLoc,
7062f4a2713aSLionel Sambuc bool WasQualifiedName) {
7063f4a2713aSLionel Sambuc DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
7064f4a2713aSLionel Sambuc DeclContext *CurContext = S.CurContext->getRedeclContext();
7065f4a2713aSLionel Sambuc
7066f4a2713aSLionel Sambuc if (CurContext->isRecord()) {
7067f4a2713aSLionel Sambuc S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
7068f4a2713aSLionel Sambuc << D;
7069f4a2713aSLionel Sambuc return true;
7070f4a2713aSLionel Sambuc }
7071f4a2713aSLionel Sambuc
7072f4a2713aSLionel Sambuc // C++11 [temp.explicit]p3:
7073f4a2713aSLionel Sambuc // An explicit instantiation shall appear in an enclosing namespace of its
7074f4a2713aSLionel Sambuc // template. If the name declared in the explicit instantiation is an
7075f4a2713aSLionel Sambuc // unqualified name, the explicit instantiation shall appear in the
7076f4a2713aSLionel Sambuc // namespace where its template is declared or, if that namespace is inline
7077f4a2713aSLionel Sambuc // (7.3.1), any namespace from its enclosing namespace set.
7078f4a2713aSLionel Sambuc //
7079f4a2713aSLionel Sambuc // This is DR275, which we do not retroactively apply to C++98/03.
7080f4a2713aSLionel Sambuc if (WasQualifiedName) {
7081f4a2713aSLionel Sambuc if (CurContext->Encloses(OrigContext))
7082f4a2713aSLionel Sambuc return false;
7083f4a2713aSLionel Sambuc } else {
7084f4a2713aSLionel Sambuc if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
7085f4a2713aSLionel Sambuc return false;
7086f4a2713aSLionel Sambuc }
7087f4a2713aSLionel Sambuc
7088f4a2713aSLionel Sambuc if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
7089f4a2713aSLionel Sambuc if (WasQualifiedName)
7090f4a2713aSLionel Sambuc S.Diag(InstLoc,
7091f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus11?
7092f4a2713aSLionel Sambuc diag::err_explicit_instantiation_out_of_scope :
7093f4a2713aSLionel Sambuc diag::warn_explicit_instantiation_out_of_scope_0x)
7094f4a2713aSLionel Sambuc << D << NS;
7095f4a2713aSLionel Sambuc else
7096f4a2713aSLionel Sambuc S.Diag(InstLoc,
7097f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus11?
7098f4a2713aSLionel Sambuc diag::err_explicit_instantiation_unqualified_wrong_namespace :
7099f4a2713aSLionel Sambuc diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
7100f4a2713aSLionel Sambuc << D << NS;
7101f4a2713aSLionel Sambuc } else
7102f4a2713aSLionel Sambuc S.Diag(InstLoc,
7103f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus11?
7104f4a2713aSLionel Sambuc diag::err_explicit_instantiation_must_be_global :
7105f4a2713aSLionel Sambuc diag::warn_explicit_instantiation_must_be_global_0x)
7106f4a2713aSLionel Sambuc << D;
7107f4a2713aSLionel Sambuc S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
7108f4a2713aSLionel Sambuc return false;
7109f4a2713aSLionel Sambuc }
7110f4a2713aSLionel Sambuc
7111f4a2713aSLionel Sambuc /// \brief Determine whether the given scope specifier has a template-id in it.
ScopeSpecifierHasTemplateId(const CXXScopeSpec & SS)7112f4a2713aSLionel Sambuc static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
7113f4a2713aSLionel Sambuc if (!SS.isSet())
7114f4a2713aSLionel Sambuc return false;
7115f4a2713aSLionel Sambuc
7116f4a2713aSLionel Sambuc // C++11 [temp.explicit]p3:
7117f4a2713aSLionel Sambuc // If the explicit instantiation is for a member function, a member class
7118f4a2713aSLionel Sambuc // or a static data member of a class template specialization, the name of
7119f4a2713aSLionel Sambuc // the class template specialization in the qualified-id for the member
7120f4a2713aSLionel Sambuc // name shall be a simple-template-id.
7121f4a2713aSLionel Sambuc //
7122f4a2713aSLionel Sambuc // C++98 has the same restriction, just worded differently.
7123*0a6a1f1dSLionel Sambuc for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS;
7124*0a6a1f1dSLionel Sambuc NNS = NNS->getPrefix())
7125f4a2713aSLionel Sambuc if (const Type *T = NNS->getAsType())
7126f4a2713aSLionel Sambuc if (isa<TemplateSpecializationType>(T))
7127f4a2713aSLionel Sambuc return true;
7128f4a2713aSLionel Sambuc
7129f4a2713aSLionel Sambuc return false;
7130f4a2713aSLionel Sambuc }
7131f4a2713aSLionel Sambuc
7132f4a2713aSLionel Sambuc // Explicit instantiation of a class template specialization
7133f4a2713aSLionel Sambuc DeclResult
ActOnExplicitInstantiation(Scope * S,SourceLocation ExternLoc,SourceLocation TemplateLoc,unsigned TagSpec,SourceLocation KWLoc,const CXXScopeSpec & SS,TemplateTy TemplateD,SourceLocation TemplateNameLoc,SourceLocation LAngleLoc,ASTTemplateArgsPtr TemplateArgsIn,SourceLocation RAngleLoc,AttributeList * Attr)7134f4a2713aSLionel Sambuc Sema::ActOnExplicitInstantiation(Scope *S,
7135f4a2713aSLionel Sambuc SourceLocation ExternLoc,
7136f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
7137f4a2713aSLionel Sambuc unsigned TagSpec,
7138f4a2713aSLionel Sambuc SourceLocation KWLoc,
7139f4a2713aSLionel Sambuc const CXXScopeSpec &SS,
7140f4a2713aSLionel Sambuc TemplateTy TemplateD,
7141f4a2713aSLionel Sambuc SourceLocation TemplateNameLoc,
7142f4a2713aSLionel Sambuc SourceLocation LAngleLoc,
7143f4a2713aSLionel Sambuc ASTTemplateArgsPtr TemplateArgsIn,
7144f4a2713aSLionel Sambuc SourceLocation RAngleLoc,
7145f4a2713aSLionel Sambuc AttributeList *Attr) {
7146f4a2713aSLionel Sambuc // Find the class template we're specializing
7147f4a2713aSLionel Sambuc TemplateName Name = TemplateD.get();
7148f4a2713aSLionel Sambuc TemplateDecl *TD = Name.getAsTemplateDecl();
7149f4a2713aSLionel Sambuc // Check that the specialization uses the same tag kind as the
7150f4a2713aSLionel Sambuc // original template.
7151f4a2713aSLionel Sambuc TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
7152f4a2713aSLionel Sambuc assert(Kind != TTK_Enum &&
7153f4a2713aSLionel Sambuc "Invalid enum tag in class template explicit instantiation!");
7154f4a2713aSLionel Sambuc
7155f4a2713aSLionel Sambuc if (isa<TypeAliasTemplateDecl>(TD)) {
7156f4a2713aSLionel Sambuc Diag(KWLoc, diag::err_tag_reference_non_tag) << Kind;
7157f4a2713aSLionel Sambuc Diag(TD->getTemplatedDecl()->getLocation(),
7158f4a2713aSLionel Sambuc diag::note_previous_use);
7159f4a2713aSLionel Sambuc return true;
7160f4a2713aSLionel Sambuc }
7161f4a2713aSLionel Sambuc
7162f4a2713aSLionel Sambuc ClassTemplateDecl *ClassTemplate = cast<ClassTemplateDecl>(TD);
7163f4a2713aSLionel Sambuc
7164f4a2713aSLionel Sambuc if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
7165f4a2713aSLionel Sambuc Kind, /*isDefinition*/false, KWLoc,
7166f4a2713aSLionel Sambuc *ClassTemplate->getIdentifier())) {
7167f4a2713aSLionel Sambuc Diag(KWLoc, diag::err_use_with_wrong_tag)
7168f4a2713aSLionel Sambuc << ClassTemplate
7169f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(KWLoc,
7170f4a2713aSLionel Sambuc ClassTemplate->getTemplatedDecl()->getKindName());
7171f4a2713aSLionel Sambuc Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
7172f4a2713aSLionel Sambuc diag::note_previous_use);
7173f4a2713aSLionel Sambuc Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
7174f4a2713aSLionel Sambuc }
7175f4a2713aSLionel Sambuc
7176f4a2713aSLionel Sambuc // C++0x [temp.explicit]p2:
7177f4a2713aSLionel Sambuc // There are two forms of explicit instantiation: an explicit instantiation
7178f4a2713aSLionel Sambuc // definition and an explicit instantiation declaration. An explicit
7179f4a2713aSLionel Sambuc // instantiation declaration begins with the extern keyword. [...]
7180f4a2713aSLionel Sambuc TemplateSpecializationKind TSK
7181f4a2713aSLionel Sambuc = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
7182f4a2713aSLionel Sambuc : TSK_ExplicitInstantiationDeclaration;
7183f4a2713aSLionel Sambuc
7184f4a2713aSLionel Sambuc // Translate the parser's template argument list in our AST format.
7185f4a2713aSLionel Sambuc TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
7186f4a2713aSLionel Sambuc translateTemplateArguments(TemplateArgsIn, TemplateArgs);
7187f4a2713aSLionel Sambuc
7188f4a2713aSLionel Sambuc // Check that the template argument list is well-formed for this
7189f4a2713aSLionel Sambuc // template.
7190f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> Converted;
7191f4a2713aSLionel Sambuc if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
7192f4a2713aSLionel Sambuc TemplateArgs, false, Converted))
7193f4a2713aSLionel Sambuc return true;
7194f4a2713aSLionel Sambuc
7195f4a2713aSLionel Sambuc // Find the class template specialization declaration that
7196f4a2713aSLionel Sambuc // corresponds to these arguments.
7197*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
7198f4a2713aSLionel Sambuc ClassTemplateSpecializationDecl *PrevDecl
7199*0a6a1f1dSLionel Sambuc = ClassTemplate->findSpecialization(Converted, InsertPos);
7200f4a2713aSLionel Sambuc
7201f4a2713aSLionel Sambuc TemplateSpecializationKind PrevDecl_TSK
7202f4a2713aSLionel Sambuc = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
7203f4a2713aSLionel Sambuc
7204f4a2713aSLionel Sambuc // C++0x [temp.explicit]p2:
7205f4a2713aSLionel Sambuc // [...] An explicit instantiation shall appear in an enclosing
7206f4a2713aSLionel Sambuc // namespace of its template. [...]
7207f4a2713aSLionel Sambuc //
7208f4a2713aSLionel Sambuc // This is C++ DR 275.
7209f4a2713aSLionel Sambuc if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
7210f4a2713aSLionel Sambuc SS.isSet()))
7211f4a2713aSLionel Sambuc return true;
7212f4a2713aSLionel Sambuc
7213*0a6a1f1dSLionel Sambuc ClassTemplateSpecializationDecl *Specialization = nullptr;
7214f4a2713aSLionel Sambuc
7215f4a2713aSLionel Sambuc bool HasNoEffect = false;
7216f4a2713aSLionel Sambuc if (PrevDecl) {
7217f4a2713aSLionel Sambuc if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
7218f4a2713aSLionel Sambuc PrevDecl, PrevDecl_TSK,
7219f4a2713aSLionel Sambuc PrevDecl->getPointOfInstantiation(),
7220f4a2713aSLionel Sambuc HasNoEffect))
7221f4a2713aSLionel Sambuc return PrevDecl;
7222f4a2713aSLionel Sambuc
7223f4a2713aSLionel Sambuc // Even though HasNoEffect == true means that this explicit instantiation
7224f4a2713aSLionel Sambuc // has no effect on semantics, we go on to put its syntax in the AST.
7225f4a2713aSLionel Sambuc
7226f4a2713aSLionel Sambuc if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
7227f4a2713aSLionel Sambuc PrevDecl_TSK == TSK_Undeclared) {
7228f4a2713aSLionel Sambuc // Since the only prior class template specialization with these
7229f4a2713aSLionel Sambuc // arguments was referenced but not declared, reuse that
7230f4a2713aSLionel Sambuc // declaration node as our own, updating the source location
7231f4a2713aSLionel Sambuc // for the template name to reflect our new declaration.
7232f4a2713aSLionel Sambuc // (Other source locations will be updated later.)
7233f4a2713aSLionel Sambuc Specialization = PrevDecl;
7234f4a2713aSLionel Sambuc Specialization->setLocation(TemplateNameLoc);
7235*0a6a1f1dSLionel Sambuc PrevDecl = nullptr;
7236f4a2713aSLionel Sambuc }
7237f4a2713aSLionel Sambuc }
7238f4a2713aSLionel Sambuc
7239f4a2713aSLionel Sambuc if (!Specialization) {
7240f4a2713aSLionel Sambuc // Create a new class template specialization declaration node for
7241f4a2713aSLionel Sambuc // this explicit specialization.
7242f4a2713aSLionel Sambuc Specialization
7243f4a2713aSLionel Sambuc = ClassTemplateSpecializationDecl::Create(Context, Kind,
7244f4a2713aSLionel Sambuc ClassTemplate->getDeclContext(),
7245f4a2713aSLionel Sambuc KWLoc, TemplateNameLoc,
7246f4a2713aSLionel Sambuc ClassTemplate,
7247f4a2713aSLionel Sambuc Converted.data(),
7248f4a2713aSLionel Sambuc Converted.size(),
7249f4a2713aSLionel Sambuc PrevDecl);
7250f4a2713aSLionel Sambuc SetNestedNameSpecifier(Specialization, SS);
7251f4a2713aSLionel Sambuc
7252f4a2713aSLionel Sambuc if (!HasNoEffect && !PrevDecl) {
7253f4a2713aSLionel Sambuc // Insert the new specialization.
7254f4a2713aSLionel Sambuc ClassTemplate->AddSpecialization(Specialization, InsertPos);
7255f4a2713aSLionel Sambuc }
7256f4a2713aSLionel Sambuc }
7257f4a2713aSLionel Sambuc
7258f4a2713aSLionel Sambuc // Build the fully-sugared type for this explicit instantiation as
7259f4a2713aSLionel Sambuc // the user wrote in the explicit instantiation itself. This means
7260f4a2713aSLionel Sambuc // that we'll pretty-print the type retrieved from the
7261f4a2713aSLionel Sambuc // specialization's declaration the way that the user actually wrote
7262f4a2713aSLionel Sambuc // the explicit instantiation, rather than formatting the name based
7263f4a2713aSLionel Sambuc // on the "canonical" representation used to store the template
7264f4a2713aSLionel Sambuc // arguments in the specialization.
7265f4a2713aSLionel Sambuc TypeSourceInfo *WrittenTy
7266f4a2713aSLionel Sambuc = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
7267f4a2713aSLionel Sambuc TemplateArgs,
7268f4a2713aSLionel Sambuc Context.getTypeDeclType(Specialization));
7269f4a2713aSLionel Sambuc Specialization->setTypeAsWritten(WrittenTy);
7270f4a2713aSLionel Sambuc
7271f4a2713aSLionel Sambuc // Set source locations for keywords.
7272f4a2713aSLionel Sambuc Specialization->setExternLoc(ExternLoc);
7273f4a2713aSLionel Sambuc Specialization->setTemplateKeywordLoc(TemplateLoc);
7274f4a2713aSLionel Sambuc Specialization->setRBraceLoc(SourceLocation());
7275f4a2713aSLionel Sambuc
7276f4a2713aSLionel Sambuc if (Attr)
7277f4a2713aSLionel Sambuc ProcessDeclAttributeList(S, Specialization, Attr);
7278f4a2713aSLionel Sambuc
7279f4a2713aSLionel Sambuc // Add the explicit instantiation into its lexical context. However,
7280f4a2713aSLionel Sambuc // since explicit instantiations are never found by name lookup, we
7281f4a2713aSLionel Sambuc // just put it into the declaration context directly.
7282f4a2713aSLionel Sambuc Specialization->setLexicalDeclContext(CurContext);
7283f4a2713aSLionel Sambuc CurContext->addDecl(Specialization);
7284f4a2713aSLionel Sambuc
7285f4a2713aSLionel Sambuc // Syntax is now OK, so return if it has no other effect on semantics.
7286f4a2713aSLionel Sambuc if (HasNoEffect) {
7287f4a2713aSLionel Sambuc // Set the template specialization kind.
7288f4a2713aSLionel Sambuc Specialization->setTemplateSpecializationKind(TSK);
7289f4a2713aSLionel Sambuc return Specialization;
7290f4a2713aSLionel Sambuc }
7291f4a2713aSLionel Sambuc
7292f4a2713aSLionel Sambuc // C++ [temp.explicit]p3:
7293f4a2713aSLionel Sambuc // A definition of a class template or class member template
7294f4a2713aSLionel Sambuc // shall be in scope at the point of the explicit instantiation of
7295f4a2713aSLionel Sambuc // the class template or class member template.
7296f4a2713aSLionel Sambuc //
7297f4a2713aSLionel Sambuc // This check comes when we actually try to perform the
7298f4a2713aSLionel Sambuc // instantiation.
7299f4a2713aSLionel Sambuc ClassTemplateSpecializationDecl *Def
7300f4a2713aSLionel Sambuc = cast_or_null<ClassTemplateSpecializationDecl>(
7301f4a2713aSLionel Sambuc Specialization->getDefinition());
7302f4a2713aSLionel Sambuc if (!Def)
7303f4a2713aSLionel Sambuc InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
7304f4a2713aSLionel Sambuc else if (TSK == TSK_ExplicitInstantiationDefinition) {
7305f4a2713aSLionel Sambuc MarkVTableUsed(TemplateNameLoc, Specialization, true);
7306f4a2713aSLionel Sambuc Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
7307f4a2713aSLionel Sambuc }
7308f4a2713aSLionel Sambuc
7309f4a2713aSLionel Sambuc // Instantiate the members of this class template specialization.
7310f4a2713aSLionel Sambuc Def = cast_or_null<ClassTemplateSpecializationDecl>(
7311f4a2713aSLionel Sambuc Specialization->getDefinition());
7312f4a2713aSLionel Sambuc if (Def) {
7313f4a2713aSLionel Sambuc TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
7314f4a2713aSLionel Sambuc
7315f4a2713aSLionel Sambuc // Fix a TSK_ExplicitInstantiationDeclaration followed by a
7316f4a2713aSLionel Sambuc // TSK_ExplicitInstantiationDefinition
7317f4a2713aSLionel Sambuc if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
7318f4a2713aSLionel Sambuc TSK == TSK_ExplicitInstantiationDefinition)
7319*0a6a1f1dSLionel Sambuc // FIXME: Need to notify the ASTMutationListener that we did this.
7320f4a2713aSLionel Sambuc Def->setTemplateSpecializationKind(TSK);
7321f4a2713aSLionel Sambuc
7322f4a2713aSLionel Sambuc InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
7323f4a2713aSLionel Sambuc }
7324f4a2713aSLionel Sambuc
7325f4a2713aSLionel Sambuc // Set the template specialization kind.
7326f4a2713aSLionel Sambuc Specialization->setTemplateSpecializationKind(TSK);
7327f4a2713aSLionel Sambuc return Specialization;
7328f4a2713aSLionel Sambuc }
7329f4a2713aSLionel Sambuc
7330f4a2713aSLionel Sambuc // Explicit instantiation of a member class of a class template.
7331f4a2713aSLionel Sambuc DeclResult
ActOnExplicitInstantiation(Scope * S,SourceLocation ExternLoc,SourceLocation TemplateLoc,unsigned TagSpec,SourceLocation KWLoc,CXXScopeSpec & SS,IdentifierInfo * Name,SourceLocation NameLoc,AttributeList * Attr)7332f4a2713aSLionel Sambuc Sema::ActOnExplicitInstantiation(Scope *S,
7333f4a2713aSLionel Sambuc SourceLocation ExternLoc,
7334f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
7335f4a2713aSLionel Sambuc unsigned TagSpec,
7336f4a2713aSLionel Sambuc SourceLocation KWLoc,
7337f4a2713aSLionel Sambuc CXXScopeSpec &SS,
7338f4a2713aSLionel Sambuc IdentifierInfo *Name,
7339f4a2713aSLionel Sambuc SourceLocation NameLoc,
7340f4a2713aSLionel Sambuc AttributeList *Attr) {
7341f4a2713aSLionel Sambuc
7342f4a2713aSLionel Sambuc bool Owned = false;
7343f4a2713aSLionel Sambuc bool IsDependent = false;
7344f4a2713aSLionel Sambuc Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
7345f4a2713aSLionel Sambuc KWLoc, SS, Name, NameLoc, Attr, AS_none,
7346f4a2713aSLionel Sambuc /*ModulePrivateLoc=*/SourceLocation(),
7347f4a2713aSLionel Sambuc MultiTemplateParamsArg(), Owned, IsDependent,
7348*0a6a1f1dSLionel Sambuc SourceLocation(), false, TypeResult(),
7349*0a6a1f1dSLionel Sambuc /*IsTypeSpecifier*/false);
7350f4a2713aSLionel Sambuc assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
7351f4a2713aSLionel Sambuc
7352f4a2713aSLionel Sambuc if (!TagD)
7353f4a2713aSLionel Sambuc return true;
7354f4a2713aSLionel Sambuc
7355f4a2713aSLionel Sambuc TagDecl *Tag = cast<TagDecl>(TagD);
7356f4a2713aSLionel Sambuc assert(!Tag->isEnum() && "shouldn't see enumerations here");
7357f4a2713aSLionel Sambuc
7358f4a2713aSLionel Sambuc if (Tag->isInvalidDecl())
7359f4a2713aSLionel Sambuc return true;
7360f4a2713aSLionel Sambuc
7361f4a2713aSLionel Sambuc CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
7362f4a2713aSLionel Sambuc CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
7363f4a2713aSLionel Sambuc if (!Pattern) {
7364f4a2713aSLionel Sambuc Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
7365f4a2713aSLionel Sambuc << Context.getTypeDeclType(Record);
7366f4a2713aSLionel Sambuc Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
7367f4a2713aSLionel Sambuc return true;
7368f4a2713aSLionel Sambuc }
7369f4a2713aSLionel Sambuc
7370f4a2713aSLionel Sambuc // C++0x [temp.explicit]p2:
7371f4a2713aSLionel Sambuc // If the explicit instantiation is for a class or member class, the
7372f4a2713aSLionel Sambuc // elaborated-type-specifier in the declaration shall include a
7373f4a2713aSLionel Sambuc // simple-template-id.
7374f4a2713aSLionel Sambuc //
7375f4a2713aSLionel Sambuc // C++98 has the same restriction, just worded differently.
7376f4a2713aSLionel Sambuc if (!ScopeSpecifierHasTemplateId(SS))
7377f4a2713aSLionel Sambuc Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
7378f4a2713aSLionel Sambuc << Record << SS.getRange();
7379f4a2713aSLionel Sambuc
7380f4a2713aSLionel Sambuc // C++0x [temp.explicit]p2:
7381f4a2713aSLionel Sambuc // There are two forms of explicit instantiation: an explicit instantiation
7382f4a2713aSLionel Sambuc // definition and an explicit instantiation declaration. An explicit
7383f4a2713aSLionel Sambuc // instantiation declaration begins with the extern keyword. [...]
7384f4a2713aSLionel Sambuc TemplateSpecializationKind TSK
7385f4a2713aSLionel Sambuc = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
7386f4a2713aSLionel Sambuc : TSK_ExplicitInstantiationDeclaration;
7387f4a2713aSLionel Sambuc
7388f4a2713aSLionel Sambuc // C++0x [temp.explicit]p2:
7389f4a2713aSLionel Sambuc // [...] An explicit instantiation shall appear in an enclosing
7390f4a2713aSLionel Sambuc // namespace of its template. [...]
7391f4a2713aSLionel Sambuc //
7392f4a2713aSLionel Sambuc // This is C++ DR 275.
7393f4a2713aSLionel Sambuc CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
7394f4a2713aSLionel Sambuc
7395f4a2713aSLionel Sambuc // Verify that it is okay to explicitly instantiate here.
7396f4a2713aSLionel Sambuc CXXRecordDecl *PrevDecl
7397f4a2713aSLionel Sambuc = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
7398f4a2713aSLionel Sambuc if (!PrevDecl && Record->getDefinition())
7399f4a2713aSLionel Sambuc PrevDecl = Record;
7400f4a2713aSLionel Sambuc if (PrevDecl) {
7401f4a2713aSLionel Sambuc MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
7402f4a2713aSLionel Sambuc bool HasNoEffect = false;
7403f4a2713aSLionel Sambuc assert(MSInfo && "No member specialization information?");
7404f4a2713aSLionel Sambuc if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
7405f4a2713aSLionel Sambuc PrevDecl,
7406f4a2713aSLionel Sambuc MSInfo->getTemplateSpecializationKind(),
7407f4a2713aSLionel Sambuc MSInfo->getPointOfInstantiation(),
7408f4a2713aSLionel Sambuc HasNoEffect))
7409f4a2713aSLionel Sambuc return true;
7410f4a2713aSLionel Sambuc if (HasNoEffect)
7411f4a2713aSLionel Sambuc return TagD;
7412f4a2713aSLionel Sambuc }
7413f4a2713aSLionel Sambuc
7414f4a2713aSLionel Sambuc CXXRecordDecl *RecordDef
7415f4a2713aSLionel Sambuc = cast_or_null<CXXRecordDecl>(Record->getDefinition());
7416f4a2713aSLionel Sambuc if (!RecordDef) {
7417f4a2713aSLionel Sambuc // C++ [temp.explicit]p3:
7418f4a2713aSLionel Sambuc // A definition of a member class of a class template shall be in scope
7419f4a2713aSLionel Sambuc // at the point of an explicit instantiation of the member class.
7420f4a2713aSLionel Sambuc CXXRecordDecl *Def
7421f4a2713aSLionel Sambuc = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
7422f4a2713aSLionel Sambuc if (!Def) {
7423f4a2713aSLionel Sambuc Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
7424f4a2713aSLionel Sambuc << 0 << Record->getDeclName() << Record->getDeclContext();
7425f4a2713aSLionel Sambuc Diag(Pattern->getLocation(), diag::note_forward_declaration)
7426f4a2713aSLionel Sambuc << Pattern;
7427f4a2713aSLionel Sambuc return true;
7428f4a2713aSLionel Sambuc } else {
7429f4a2713aSLionel Sambuc if (InstantiateClass(NameLoc, Record, Def,
7430f4a2713aSLionel Sambuc getTemplateInstantiationArgs(Record),
7431f4a2713aSLionel Sambuc TSK))
7432f4a2713aSLionel Sambuc return true;
7433f4a2713aSLionel Sambuc
7434f4a2713aSLionel Sambuc RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
7435f4a2713aSLionel Sambuc if (!RecordDef)
7436f4a2713aSLionel Sambuc return true;
7437f4a2713aSLionel Sambuc }
7438f4a2713aSLionel Sambuc }
7439f4a2713aSLionel Sambuc
7440f4a2713aSLionel Sambuc // Instantiate all of the members of the class.
7441f4a2713aSLionel Sambuc InstantiateClassMembers(NameLoc, RecordDef,
7442f4a2713aSLionel Sambuc getTemplateInstantiationArgs(Record), TSK);
7443f4a2713aSLionel Sambuc
7444f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDefinition)
7445f4a2713aSLionel Sambuc MarkVTableUsed(NameLoc, RecordDef, true);
7446f4a2713aSLionel Sambuc
7447f4a2713aSLionel Sambuc // FIXME: We don't have any representation for explicit instantiations of
7448f4a2713aSLionel Sambuc // member classes. Such a representation is not needed for compilation, but it
7449f4a2713aSLionel Sambuc // should be available for clients that want to see all of the declarations in
7450f4a2713aSLionel Sambuc // the source code.
7451f4a2713aSLionel Sambuc return TagD;
7452f4a2713aSLionel Sambuc }
7453f4a2713aSLionel Sambuc
ActOnExplicitInstantiation(Scope * S,SourceLocation ExternLoc,SourceLocation TemplateLoc,Declarator & D)7454f4a2713aSLionel Sambuc DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
7455f4a2713aSLionel Sambuc SourceLocation ExternLoc,
7456f4a2713aSLionel Sambuc SourceLocation TemplateLoc,
7457f4a2713aSLionel Sambuc Declarator &D) {
7458f4a2713aSLionel Sambuc // Explicit instantiations always require a name.
7459f4a2713aSLionel Sambuc // TODO: check if/when DNInfo should replace Name.
7460f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
7461f4a2713aSLionel Sambuc DeclarationName Name = NameInfo.getName();
7462f4a2713aSLionel Sambuc if (!Name) {
7463f4a2713aSLionel Sambuc if (!D.isInvalidType())
7464f4a2713aSLionel Sambuc Diag(D.getDeclSpec().getLocStart(),
7465f4a2713aSLionel Sambuc diag::err_explicit_instantiation_requires_name)
7466f4a2713aSLionel Sambuc << D.getDeclSpec().getSourceRange()
7467f4a2713aSLionel Sambuc << D.getSourceRange();
7468f4a2713aSLionel Sambuc
7469f4a2713aSLionel Sambuc return true;
7470f4a2713aSLionel Sambuc }
7471f4a2713aSLionel Sambuc
7472f4a2713aSLionel Sambuc // The scope passed in may not be a decl scope. Zip up the scope tree until
7473f4a2713aSLionel Sambuc // we find one that is.
7474f4a2713aSLionel Sambuc while ((S->getFlags() & Scope::DeclScope) == 0 ||
7475f4a2713aSLionel Sambuc (S->getFlags() & Scope::TemplateParamScope) != 0)
7476f4a2713aSLionel Sambuc S = S->getParent();
7477f4a2713aSLionel Sambuc
7478f4a2713aSLionel Sambuc // Determine the type of the declaration.
7479f4a2713aSLionel Sambuc TypeSourceInfo *T = GetTypeForDeclarator(D, S);
7480f4a2713aSLionel Sambuc QualType R = T->getType();
7481f4a2713aSLionel Sambuc if (R.isNull())
7482f4a2713aSLionel Sambuc return true;
7483f4a2713aSLionel Sambuc
7484f4a2713aSLionel Sambuc // C++ [dcl.stc]p1:
7485f4a2713aSLionel Sambuc // A storage-class-specifier shall not be specified in [...] an explicit
7486f4a2713aSLionel Sambuc // instantiation (14.7.2) directive.
7487f4a2713aSLionel Sambuc if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
7488f4a2713aSLionel Sambuc Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
7489f4a2713aSLionel Sambuc << Name;
7490f4a2713aSLionel Sambuc return true;
7491f4a2713aSLionel Sambuc } else if (D.getDeclSpec().getStorageClassSpec()
7492f4a2713aSLionel Sambuc != DeclSpec::SCS_unspecified) {
7493f4a2713aSLionel Sambuc // Complain about then remove the storage class specifier.
7494f4a2713aSLionel Sambuc Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
7495f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
7496f4a2713aSLionel Sambuc
7497f4a2713aSLionel Sambuc D.getMutableDeclSpec().ClearStorageClassSpecs();
7498f4a2713aSLionel Sambuc }
7499f4a2713aSLionel Sambuc
7500f4a2713aSLionel Sambuc // C++0x [temp.explicit]p1:
7501f4a2713aSLionel Sambuc // [...] An explicit instantiation of a function template shall not use the
7502f4a2713aSLionel Sambuc // inline or constexpr specifiers.
7503f4a2713aSLionel Sambuc // Presumably, this also applies to member functions of class templates as
7504f4a2713aSLionel Sambuc // well.
7505f4a2713aSLionel Sambuc if (D.getDeclSpec().isInlineSpecified())
7506f4a2713aSLionel Sambuc Diag(D.getDeclSpec().getInlineSpecLoc(),
7507f4a2713aSLionel Sambuc getLangOpts().CPlusPlus11 ?
7508f4a2713aSLionel Sambuc diag::err_explicit_instantiation_inline :
7509f4a2713aSLionel Sambuc diag::warn_explicit_instantiation_inline_0x)
7510f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
7511f4a2713aSLionel Sambuc if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType())
7512f4a2713aSLionel Sambuc // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
7513f4a2713aSLionel Sambuc // not already specified.
7514f4a2713aSLionel Sambuc Diag(D.getDeclSpec().getConstexprSpecLoc(),
7515f4a2713aSLionel Sambuc diag::err_explicit_instantiation_constexpr);
7516f4a2713aSLionel Sambuc
7517f4a2713aSLionel Sambuc // C++0x [temp.explicit]p2:
7518f4a2713aSLionel Sambuc // There are two forms of explicit instantiation: an explicit instantiation
7519f4a2713aSLionel Sambuc // definition and an explicit instantiation declaration. An explicit
7520f4a2713aSLionel Sambuc // instantiation declaration begins with the extern keyword. [...]
7521f4a2713aSLionel Sambuc TemplateSpecializationKind TSK
7522f4a2713aSLionel Sambuc = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
7523f4a2713aSLionel Sambuc : TSK_ExplicitInstantiationDeclaration;
7524f4a2713aSLionel Sambuc
7525f4a2713aSLionel Sambuc LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
7526f4a2713aSLionel Sambuc LookupParsedName(Previous, S, &D.getCXXScopeSpec());
7527f4a2713aSLionel Sambuc
7528f4a2713aSLionel Sambuc if (!R->isFunctionType()) {
7529f4a2713aSLionel Sambuc // C++ [temp.explicit]p1:
7530f4a2713aSLionel Sambuc // A [...] static data member of a class template can be explicitly
7531f4a2713aSLionel Sambuc // instantiated from the member definition associated with its class
7532f4a2713aSLionel Sambuc // template.
7533f4a2713aSLionel Sambuc // C++1y [temp.explicit]p1:
7534f4a2713aSLionel Sambuc // A [...] variable [...] template specialization can be explicitly
7535f4a2713aSLionel Sambuc // instantiated from its template.
7536f4a2713aSLionel Sambuc if (Previous.isAmbiguous())
7537f4a2713aSLionel Sambuc return true;
7538f4a2713aSLionel Sambuc
7539f4a2713aSLionel Sambuc VarDecl *Prev = Previous.getAsSingle<VarDecl>();
7540f4a2713aSLionel Sambuc VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>();
7541f4a2713aSLionel Sambuc
7542f4a2713aSLionel Sambuc if (!PrevTemplate) {
7543f4a2713aSLionel Sambuc if (!Prev || !Prev->isStaticDataMember()) {
7544f4a2713aSLionel Sambuc // We expect to see a data data member here.
7545f4a2713aSLionel Sambuc Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
7546f4a2713aSLionel Sambuc << Name;
7547f4a2713aSLionel Sambuc for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
7548f4a2713aSLionel Sambuc P != PEnd; ++P)
7549f4a2713aSLionel Sambuc Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
7550f4a2713aSLionel Sambuc return true;
7551f4a2713aSLionel Sambuc }
7552f4a2713aSLionel Sambuc
7553f4a2713aSLionel Sambuc if (!Prev->getInstantiatedFromStaticDataMember()) {
7554f4a2713aSLionel Sambuc // FIXME: Check for explicit specialization?
7555f4a2713aSLionel Sambuc Diag(D.getIdentifierLoc(),
7556f4a2713aSLionel Sambuc diag::err_explicit_instantiation_data_member_not_instantiated)
7557f4a2713aSLionel Sambuc << Prev;
7558f4a2713aSLionel Sambuc Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
7559f4a2713aSLionel Sambuc // FIXME: Can we provide a note showing where this was declared?
7560f4a2713aSLionel Sambuc return true;
7561f4a2713aSLionel Sambuc }
7562f4a2713aSLionel Sambuc } else {
7563f4a2713aSLionel Sambuc // Explicitly instantiate a variable template.
7564f4a2713aSLionel Sambuc
7565f4a2713aSLionel Sambuc // C++1y [dcl.spec.auto]p6:
7566f4a2713aSLionel Sambuc // ... A program that uses auto or decltype(auto) in a context not
7567f4a2713aSLionel Sambuc // explicitly allowed in this section is ill-formed.
7568f4a2713aSLionel Sambuc //
7569f4a2713aSLionel Sambuc // This includes auto-typed variable template instantiations.
7570f4a2713aSLionel Sambuc if (R->isUndeducedType()) {
7571f4a2713aSLionel Sambuc Diag(T->getTypeLoc().getLocStart(),
7572f4a2713aSLionel Sambuc diag::err_auto_not_allowed_var_inst);
7573f4a2713aSLionel Sambuc return true;
7574f4a2713aSLionel Sambuc }
7575f4a2713aSLionel Sambuc
7576f4a2713aSLionel Sambuc if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
7577f4a2713aSLionel Sambuc // C++1y [temp.explicit]p3:
7578f4a2713aSLionel Sambuc // If the explicit instantiation is for a variable, the unqualified-id
7579f4a2713aSLionel Sambuc // in the declaration shall be a template-id.
7580f4a2713aSLionel Sambuc Diag(D.getIdentifierLoc(),
7581f4a2713aSLionel Sambuc diag::err_explicit_instantiation_without_template_id)
7582f4a2713aSLionel Sambuc << PrevTemplate;
7583f4a2713aSLionel Sambuc Diag(PrevTemplate->getLocation(),
7584f4a2713aSLionel Sambuc diag::note_explicit_instantiation_here);
7585f4a2713aSLionel Sambuc return true;
7586f4a2713aSLionel Sambuc }
7587f4a2713aSLionel Sambuc
7588f4a2713aSLionel Sambuc // Translate the parser's template argument list into our AST format.
7589*0a6a1f1dSLionel Sambuc TemplateArgumentListInfo TemplateArgs =
7590*0a6a1f1dSLionel Sambuc makeTemplateArgumentListInfo(*this, *D.getName().TemplateId);
7591f4a2713aSLionel Sambuc
7592f4a2713aSLionel Sambuc DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc,
7593f4a2713aSLionel Sambuc D.getIdentifierLoc(), TemplateArgs);
7594f4a2713aSLionel Sambuc if (Res.isInvalid())
7595f4a2713aSLionel Sambuc return true;
7596f4a2713aSLionel Sambuc
7597f4a2713aSLionel Sambuc // Ignore access control bits, we don't need them for redeclaration
7598f4a2713aSLionel Sambuc // checking.
7599f4a2713aSLionel Sambuc Prev = cast<VarDecl>(Res.get());
7600f4a2713aSLionel Sambuc }
7601f4a2713aSLionel Sambuc
7602f4a2713aSLionel Sambuc // C++0x [temp.explicit]p2:
7603f4a2713aSLionel Sambuc // If the explicit instantiation is for a member function, a member class
7604f4a2713aSLionel Sambuc // or a static data member of a class template specialization, the name of
7605f4a2713aSLionel Sambuc // the class template specialization in the qualified-id for the member
7606f4a2713aSLionel Sambuc // name shall be a simple-template-id.
7607f4a2713aSLionel Sambuc //
7608f4a2713aSLionel Sambuc // C++98 has the same restriction, just worded differently.
7609f4a2713aSLionel Sambuc //
7610f4a2713aSLionel Sambuc // This does not apply to variable template specializations, where the
7611f4a2713aSLionel Sambuc // template-id is in the unqualified-id instead.
7612f4a2713aSLionel Sambuc if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate)
7613f4a2713aSLionel Sambuc Diag(D.getIdentifierLoc(),
7614f4a2713aSLionel Sambuc diag::ext_explicit_instantiation_without_qualified_id)
7615f4a2713aSLionel Sambuc << Prev << D.getCXXScopeSpec().getRange();
7616f4a2713aSLionel Sambuc
7617f4a2713aSLionel Sambuc // Check the scope of this explicit instantiation.
7618f4a2713aSLionel Sambuc CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
7619f4a2713aSLionel Sambuc
7620f4a2713aSLionel Sambuc // Verify that it is okay to explicitly instantiate here.
7621f4a2713aSLionel Sambuc TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind();
7622f4a2713aSLionel Sambuc SourceLocation POI = Prev->getPointOfInstantiation();
7623f4a2713aSLionel Sambuc bool HasNoEffect = false;
7624f4a2713aSLionel Sambuc if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
7625f4a2713aSLionel Sambuc PrevTSK, POI, HasNoEffect))
7626f4a2713aSLionel Sambuc return true;
7627f4a2713aSLionel Sambuc
7628f4a2713aSLionel Sambuc if (!HasNoEffect) {
7629f4a2713aSLionel Sambuc // Instantiate static data member or variable template.
7630f4a2713aSLionel Sambuc
7631f4a2713aSLionel Sambuc Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
7632f4a2713aSLionel Sambuc if (PrevTemplate) {
7633f4a2713aSLionel Sambuc // Merge attributes.
7634f4a2713aSLionel Sambuc if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList())
7635f4a2713aSLionel Sambuc ProcessDeclAttributeList(S, Prev, Attr);
7636f4a2713aSLionel Sambuc }
7637f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDefinition)
7638f4a2713aSLionel Sambuc InstantiateVariableDefinition(D.getIdentifierLoc(), Prev);
7639f4a2713aSLionel Sambuc }
7640f4a2713aSLionel Sambuc
7641f4a2713aSLionel Sambuc // Check the new variable specialization against the parsed input.
7642f4a2713aSLionel Sambuc if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) {
7643f4a2713aSLionel Sambuc Diag(T->getTypeLoc().getLocStart(),
7644f4a2713aSLionel Sambuc diag::err_invalid_var_template_spec_type)
7645f4a2713aSLionel Sambuc << 0 << PrevTemplate << R << Prev->getType();
7646f4a2713aSLionel Sambuc Diag(PrevTemplate->getLocation(), diag::note_template_declared_here)
7647f4a2713aSLionel Sambuc << 2 << PrevTemplate->getDeclName();
7648f4a2713aSLionel Sambuc return true;
7649f4a2713aSLionel Sambuc }
7650f4a2713aSLionel Sambuc
7651f4a2713aSLionel Sambuc // FIXME: Create an ExplicitInstantiation node?
7652*0a6a1f1dSLionel Sambuc return (Decl*) nullptr;
7653f4a2713aSLionel Sambuc }
7654f4a2713aSLionel Sambuc
7655f4a2713aSLionel Sambuc // If the declarator is a template-id, translate the parser's template
7656f4a2713aSLionel Sambuc // argument list into our AST format.
7657f4a2713aSLionel Sambuc bool HasExplicitTemplateArgs = false;
7658f4a2713aSLionel Sambuc TemplateArgumentListInfo TemplateArgs;
7659f4a2713aSLionel Sambuc if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
7660*0a6a1f1dSLionel Sambuc TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId);
7661f4a2713aSLionel Sambuc HasExplicitTemplateArgs = true;
7662f4a2713aSLionel Sambuc }
7663f4a2713aSLionel Sambuc
7664f4a2713aSLionel Sambuc // C++ [temp.explicit]p1:
7665f4a2713aSLionel Sambuc // A [...] function [...] can be explicitly instantiated from its template.
7666f4a2713aSLionel Sambuc // A member function [...] of a class template can be explicitly
7667f4a2713aSLionel Sambuc // instantiated from the member definition associated with its class
7668f4a2713aSLionel Sambuc // template.
7669f4a2713aSLionel Sambuc UnresolvedSet<8> Matches;
7670f4a2713aSLionel Sambuc TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc());
7671f4a2713aSLionel Sambuc for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
7672f4a2713aSLionel Sambuc P != PEnd; ++P) {
7673f4a2713aSLionel Sambuc NamedDecl *Prev = *P;
7674f4a2713aSLionel Sambuc if (!HasExplicitTemplateArgs) {
7675f4a2713aSLionel Sambuc if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
7676*0a6a1f1dSLionel Sambuc QualType Adjusted = adjustCCAndNoReturn(R, Method->getType());
7677*0a6a1f1dSLionel Sambuc if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) {
7678f4a2713aSLionel Sambuc Matches.clear();
7679f4a2713aSLionel Sambuc
7680f4a2713aSLionel Sambuc Matches.addDecl(Method, P.getAccess());
7681f4a2713aSLionel Sambuc if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
7682f4a2713aSLionel Sambuc break;
7683f4a2713aSLionel Sambuc }
7684f4a2713aSLionel Sambuc }
7685f4a2713aSLionel Sambuc }
7686f4a2713aSLionel Sambuc
7687f4a2713aSLionel Sambuc FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
7688f4a2713aSLionel Sambuc if (!FunTmpl)
7689f4a2713aSLionel Sambuc continue;
7690f4a2713aSLionel Sambuc
7691f4a2713aSLionel Sambuc TemplateDeductionInfo Info(FailedCandidates.getLocation());
7692*0a6a1f1dSLionel Sambuc FunctionDecl *Specialization = nullptr;
7693f4a2713aSLionel Sambuc if (TemplateDeductionResult TDK
7694f4a2713aSLionel Sambuc = DeduceTemplateArguments(FunTmpl,
7695*0a6a1f1dSLionel Sambuc (HasExplicitTemplateArgs ? &TemplateArgs
7696*0a6a1f1dSLionel Sambuc : nullptr),
7697f4a2713aSLionel Sambuc R, Specialization, Info)) {
7698f4a2713aSLionel Sambuc // Keep track of almost-matches.
7699f4a2713aSLionel Sambuc FailedCandidates.addCandidate()
7700f4a2713aSLionel Sambuc .set(FunTmpl->getTemplatedDecl(),
7701f4a2713aSLionel Sambuc MakeDeductionFailureInfo(Context, TDK, Info));
7702f4a2713aSLionel Sambuc (void)TDK;
7703f4a2713aSLionel Sambuc continue;
7704f4a2713aSLionel Sambuc }
7705f4a2713aSLionel Sambuc
7706f4a2713aSLionel Sambuc Matches.addDecl(Specialization, P.getAccess());
7707f4a2713aSLionel Sambuc }
7708f4a2713aSLionel Sambuc
7709f4a2713aSLionel Sambuc // Find the most specialized function template specialization.
7710f4a2713aSLionel Sambuc UnresolvedSetIterator Result = getMostSpecialized(
7711f4a2713aSLionel Sambuc Matches.begin(), Matches.end(), FailedCandidates,
7712f4a2713aSLionel Sambuc D.getIdentifierLoc(),
7713f4a2713aSLionel Sambuc PDiag(diag::err_explicit_instantiation_not_known) << Name,
7714f4a2713aSLionel Sambuc PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
7715f4a2713aSLionel Sambuc PDiag(diag::note_explicit_instantiation_candidate));
7716f4a2713aSLionel Sambuc
7717f4a2713aSLionel Sambuc if (Result == Matches.end())
7718f4a2713aSLionel Sambuc return true;
7719f4a2713aSLionel Sambuc
7720f4a2713aSLionel Sambuc // Ignore access control bits, we don't need them for redeclaration checking.
7721f4a2713aSLionel Sambuc FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
7722f4a2713aSLionel Sambuc
7723*0a6a1f1dSLionel Sambuc // C++11 [except.spec]p4
7724*0a6a1f1dSLionel Sambuc // In an explicit instantiation an exception-specification may be specified,
7725*0a6a1f1dSLionel Sambuc // but is not required.
7726*0a6a1f1dSLionel Sambuc // If an exception-specification is specified in an explicit instantiation
7727*0a6a1f1dSLionel Sambuc // directive, it shall be compatible with the exception-specifications of
7728*0a6a1f1dSLionel Sambuc // other declarations of that function.
7729*0a6a1f1dSLionel Sambuc if (auto *FPT = R->getAs<FunctionProtoType>())
7730*0a6a1f1dSLionel Sambuc if (FPT->hasExceptionSpec()) {
7731*0a6a1f1dSLionel Sambuc unsigned DiagID =
7732*0a6a1f1dSLionel Sambuc diag::err_mismatched_exception_spec_explicit_instantiation;
7733*0a6a1f1dSLionel Sambuc if (getLangOpts().MicrosoftExt)
7734*0a6a1f1dSLionel Sambuc DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation;
7735*0a6a1f1dSLionel Sambuc bool Result = CheckEquivalentExceptionSpec(
7736*0a6a1f1dSLionel Sambuc PDiag(DiagID) << Specialization->getType(),
7737*0a6a1f1dSLionel Sambuc PDiag(diag::note_explicit_instantiation_here),
7738*0a6a1f1dSLionel Sambuc Specialization->getType()->getAs<FunctionProtoType>(),
7739*0a6a1f1dSLionel Sambuc Specialization->getLocation(), FPT, D.getLocStart());
7740*0a6a1f1dSLionel Sambuc // In Microsoft mode, mismatching exception specifications just cause a
7741*0a6a1f1dSLionel Sambuc // warning.
7742*0a6a1f1dSLionel Sambuc if (!getLangOpts().MicrosoftExt && Result)
7743*0a6a1f1dSLionel Sambuc return true;
7744*0a6a1f1dSLionel Sambuc }
7745*0a6a1f1dSLionel Sambuc
7746f4a2713aSLionel Sambuc if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
7747f4a2713aSLionel Sambuc Diag(D.getIdentifierLoc(),
7748f4a2713aSLionel Sambuc diag::err_explicit_instantiation_member_function_not_instantiated)
7749f4a2713aSLionel Sambuc << Specialization
7750f4a2713aSLionel Sambuc << (Specialization->getTemplateSpecializationKind() ==
7751f4a2713aSLionel Sambuc TSK_ExplicitSpecialization);
7752f4a2713aSLionel Sambuc Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
7753f4a2713aSLionel Sambuc return true;
7754f4a2713aSLionel Sambuc }
7755f4a2713aSLionel Sambuc
7756f4a2713aSLionel Sambuc FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
7757f4a2713aSLionel Sambuc if (!PrevDecl && Specialization->isThisDeclarationADefinition())
7758f4a2713aSLionel Sambuc PrevDecl = Specialization;
7759f4a2713aSLionel Sambuc
7760f4a2713aSLionel Sambuc if (PrevDecl) {
7761f4a2713aSLionel Sambuc bool HasNoEffect = false;
7762f4a2713aSLionel Sambuc if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
7763f4a2713aSLionel Sambuc PrevDecl,
7764f4a2713aSLionel Sambuc PrevDecl->getTemplateSpecializationKind(),
7765f4a2713aSLionel Sambuc PrevDecl->getPointOfInstantiation(),
7766f4a2713aSLionel Sambuc HasNoEffect))
7767f4a2713aSLionel Sambuc return true;
7768f4a2713aSLionel Sambuc
7769f4a2713aSLionel Sambuc // FIXME: We may still want to build some representation of this
7770f4a2713aSLionel Sambuc // explicit specialization.
7771f4a2713aSLionel Sambuc if (HasNoEffect)
7772*0a6a1f1dSLionel Sambuc return (Decl*) nullptr;
7773f4a2713aSLionel Sambuc }
7774f4a2713aSLionel Sambuc
7775f4a2713aSLionel Sambuc Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
7776f4a2713aSLionel Sambuc AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
7777f4a2713aSLionel Sambuc if (Attr)
7778f4a2713aSLionel Sambuc ProcessDeclAttributeList(S, Specialization, Attr);
7779f4a2713aSLionel Sambuc
7780*0a6a1f1dSLionel Sambuc if (Specialization->isDefined()) {
7781*0a6a1f1dSLionel Sambuc // Let the ASTConsumer know that this function has been explicitly
7782*0a6a1f1dSLionel Sambuc // instantiated now, and its linkage might have changed.
7783*0a6a1f1dSLionel Sambuc Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization));
7784*0a6a1f1dSLionel Sambuc } else if (TSK == TSK_ExplicitInstantiationDefinition)
7785f4a2713aSLionel Sambuc InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
7786f4a2713aSLionel Sambuc
7787f4a2713aSLionel Sambuc // C++0x [temp.explicit]p2:
7788f4a2713aSLionel Sambuc // If the explicit instantiation is for a member function, a member class
7789f4a2713aSLionel Sambuc // or a static data member of a class template specialization, the name of
7790f4a2713aSLionel Sambuc // the class template specialization in the qualified-id for the member
7791f4a2713aSLionel Sambuc // name shall be a simple-template-id.
7792f4a2713aSLionel Sambuc //
7793f4a2713aSLionel Sambuc // C++98 has the same restriction, just worded differently.
7794f4a2713aSLionel Sambuc FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
7795f4a2713aSLionel Sambuc if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
7796f4a2713aSLionel Sambuc D.getCXXScopeSpec().isSet() &&
7797f4a2713aSLionel Sambuc !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
7798f4a2713aSLionel Sambuc Diag(D.getIdentifierLoc(),
7799f4a2713aSLionel Sambuc diag::ext_explicit_instantiation_without_qualified_id)
7800f4a2713aSLionel Sambuc << Specialization << D.getCXXScopeSpec().getRange();
7801f4a2713aSLionel Sambuc
7802f4a2713aSLionel Sambuc CheckExplicitInstantiationScope(*this,
7803f4a2713aSLionel Sambuc FunTmpl? (NamedDecl *)FunTmpl
7804f4a2713aSLionel Sambuc : Specialization->getInstantiatedFromMemberFunction(),
7805f4a2713aSLionel Sambuc D.getIdentifierLoc(),
7806f4a2713aSLionel Sambuc D.getCXXScopeSpec().isSet());
7807f4a2713aSLionel Sambuc
7808f4a2713aSLionel Sambuc // FIXME: Create some kind of ExplicitInstantiationDecl here.
7809*0a6a1f1dSLionel Sambuc return (Decl*) nullptr;
7810f4a2713aSLionel Sambuc }
7811f4a2713aSLionel Sambuc
7812f4a2713aSLionel Sambuc TypeResult
ActOnDependentTag(Scope * S,unsigned TagSpec,TagUseKind TUK,const CXXScopeSpec & SS,IdentifierInfo * Name,SourceLocation TagLoc,SourceLocation NameLoc)7813f4a2713aSLionel Sambuc Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
7814f4a2713aSLionel Sambuc const CXXScopeSpec &SS, IdentifierInfo *Name,
7815f4a2713aSLionel Sambuc SourceLocation TagLoc, SourceLocation NameLoc) {
7816f4a2713aSLionel Sambuc // This has to hold, because SS is expected to be defined.
7817f4a2713aSLionel Sambuc assert(Name && "Expected a name in a dependent tag");
7818f4a2713aSLionel Sambuc
7819*0a6a1f1dSLionel Sambuc NestedNameSpecifier *NNS = SS.getScopeRep();
7820f4a2713aSLionel Sambuc if (!NNS)
7821f4a2713aSLionel Sambuc return true;
7822f4a2713aSLionel Sambuc
7823f4a2713aSLionel Sambuc TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
7824f4a2713aSLionel Sambuc
7825f4a2713aSLionel Sambuc if (TUK == TUK_Declaration || TUK == TUK_Definition) {
7826f4a2713aSLionel Sambuc Diag(NameLoc, diag::err_dependent_tag_decl)
7827f4a2713aSLionel Sambuc << (TUK == TUK_Definition) << Kind << SS.getRange();
7828f4a2713aSLionel Sambuc return true;
7829f4a2713aSLionel Sambuc }
7830f4a2713aSLionel Sambuc
7831f4a2713aSLionel Sambuc // Create the resulting type.
7832f4a2713aSLionel Sambuc ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
7833f4a2713aSLionel Sambuc QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
7834f4a2713aSLionel Sambuc
7835f4a2713aSLionel Sambuc // Create type-source location information for this type.
7836f4a2713aSLionel Sambuc TypeLocBuilder TLB;
7837f4a2713aSLionel Sambuc DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
7838f4a2713aSLionel Sambuc TL.setElaboratedKeywordLoc(TagLoc);
7839f4a2713aSLionel Sambuc TL.setQualifierLoc(SS.getWithLocInContext(Context));
7840f4a2713aSLionel Sambuc TL.setNameLoc(NameLoc);
7841f4a2713aSLionel Sambuc return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
7842f4a2713aSLionel Sambuc }
7843f4a2713aSLionel Sambuc
7844f4a2713aSLionel Sambuc TypeResult
ActOnTypenameType(Scope * S,SourceLocation TypenameLoc,const CXXScopeSpec & SS,const IdentifierInfo & II,SourceLocation IdLoc)7845f4a2713aSLionel Sambuc Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
7846f4a2713aSLionel Sambuc const CXXScopeSpec &SS, const IdentifierInfo &II,
7847f4a2713aSLionel Sambuc SourceLocation IdLoc) {
7848f4a2713aSLionel Sambuc if (SS.isInvalid())
7849f4a2713aSLionel Sambuc return true;
7850f4a2713aSLionel Sambuc
7851f4a2713aSLionel Sambuc if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
7852f4a2713aSLionel Sambuc Diag(TypenameLoc,
7853f4a2713aSLionel Sambuc getLangOpts().CPlusPlus11 ?
7854f4a2713aSLionel Sambuc diag::warn_cxx98_compat_typename_outside_of_template :
7855f4a2713aSLionel Sambuc diag::ext_typename_outside_of_template)
7856f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(TypenameLoc);
7857f4a2713aSLionel Sambuc
7858f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
7859f4a2713aSLionel Sambuc QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
7860f4a2713aSLionel Sambuc TypenameLoc, QualifierLoc, II, IdLoc);
7861f4a2713aSLionel Sambuc if (T.isNull())
7862f4a2713aSLionel Sambuc return true;
7863f4a2713aSLionel Sambuc
7864f4a2713aSLionel Sambuc TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
7865f4a2713aSLionel Sambuc if (isa<DependentNameType>(T)) {
7866f4a2713aSLionel Sambuc DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>();
7867f4a2713aSLionel Sambuc TL.setElaboratedKeywordLoc(TypenameLoc);
7868f4a2713aSLionel Sambuc TL.setQualifierLoc(QualifierLoc);
7869f4a2713aSLionel Sambuc TL.setNameLoc(IdLoc);
7870f4a2713aSLionel Sambuc } else {
7871f4a2713aSLionel Sambuc ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>();
7872f4a2713aSLionel Sambuc TL.setElaboratedKeywordLoc(TypenameLoc);
7873f4a2713aSLionel Sambuc TL.setQualifierLoc(QualifierLoc);
7874f4a2713aSLionel Sambuc TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc);
7875f4a2713aSLionel Sambuc }
7876f4a2713aSLionel Sambuc
7877f4a2713aSLionel Sambuc return CreateParsedType(T, TSI);
7878f4a2713aSLionel Sambuc }
7879f4a2713aSLionel Sambuc
7880f4a2713aSLionel Sambuc TypeResult
ActOnTypenameType(Scope * S,SourceLocation TypenameLoc,const CXXScopeSpec & SS,SourceLocation TemplateKWLoc,TemplateTy TemplateIn,SourceLocation TemplateNameLoc,SourceLocation LAngleLoc,ASTTemplateArgsPtr TemplateArgsIn,SourceLocation RAngleLoc)7881f4a2713aSLionel Sambuc Sema::ActOnTypenameType(Scope *S,
7882f4a2713aSLionel Sambuc SourceLocation TypenameLoc,
7883f4a2713aSLionel Sambuc const CXXScopeSpec &SS,
7884f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
7885f4a2713aSLionel Sambuc TemplateTy TemplateIn,
7886f4a2713aSLionel Sambuc SourceLocation TemplateNameLoc,
7887f4a2713aSLionel Sambuc SourceLocation LAngleLoc,
7888f4a2713aSLionel Sambuc ASTTemplateArgsPtr TemplateArgsIn,
7889f4a2713aSLionel Sambuc SourceLocation RAngleLoc) {
7890f4a2713aSLionel Sambuc if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
7891f4a2713aSLionel Sambuc Diag(TypenameLoc,
7892f4a2713aSLionel Sambuc getLangOpts().CPlusPlus11 ?
7893f4a2713aSLionel Sambuc diag::warn_cxx98_compat_typename_outside_of_template :
7894f4a2713aSLionel Sambuc diag::ext_typename_outside_of_template)
7895f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(TypenameLoc);
7896f4a2713aSLionel Sambuc
7897f4a2713aSLionel Sambuc // Translate the parser's template argument list in our AST format.
7898f4a2713aSLionel Sambuc TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
7899f4a2713aSLionel Sambuc translateTemplateArguments(TemplateArgsIn, TemplateArgs);
7900f4a2713aSLionel Sambuc
7901f4a2713aSLionel Sambuc TemplateName Template = TemplateIn.get();
7902f4a2713aSLionel Sambuc if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
7903f4a2713aSLionel Sambuc // Construct a dependent template specialization type.
7904f4a2713aSLionel Sambuc assert(DTN && "dependent template has non-dependent name?");
7905*0a6a1f1dSLionel Sambuc assert(DTN->getQualifier() == SS.getScopeRep());
7906f4a2713aSLionel Sambuc QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
7907f4a2713aSLionel Sambuc DTN->getQualifier(),
7908f4a2713aSLionel Sambuc DTN->getIdentifier(),
7909f4a2713aSLionel Sambuc TemplateArgs);
7910f4a2713aSLionel Sambuc
7911f4a2713aSLionel Sambuc // Create source-location information for this type.
7912f4a2713aSLionel Sambuc TypeLocBuilder Builder;
7913f4a2713aSLionel Sambuc DependentTemplateSpecializationTypeLoc SpecTL
7914f4a2713aSLionel Sambuc = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
7915f4a2713aSLionel Sambuc SpecTL.setElaboratedKeywordLoc(TypenameLoc);
7916f4a2713aSLionel Sambuc SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
7917f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
7918f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateNameLoc);
7919f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc);
7920f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc);
7921f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
7922f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
7923f4a2713aSLionel Sambuc return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
7924f4a2713aSLionel Sambuc }
7925f4a2713aSLionel Sambuc
7926f4a2713aSLionel Sambuc QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
7927f4a2713aSLionel Sambuc if (T.isNull())
7928f4a2713aSLionel Sambuc return true;
7929f4a2713aSLionel Sambuc
7930f4a2713aSLionel Sambuc // Provide source-location information for the template specialization type.
7931f4a2713aSLionel Sambuc TypeLocBuilder Builder;
7932f4a2713aSLionel Sambuc TemplateSpecializationTypeLoc SpecTL
7933f4a2713aSLionel Sambuc = Builder.push<TemplateSpecializationTypeLoc>(T);
7934f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
7935f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateNameLoc);
7936f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc);
7937f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc);
7938f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
7939f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
7940f4a2713aSLionel Sambuc
7941f4a2713aSLionel Sambuc T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
7942f4a2713aSLionel Sambuc ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
7943f4a2713aSLionel Sambuc TL.setElaboratedKeywordLoc(TypenameLoc);
7944f4a2713aSLionel Sambuc TL.setQualifierLoc(SS.getWithLocInContext(Context));
7945f4a2713aSLionel Sambuc
7946f4a2713aSLionel Sambuc TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
7947f4a2713aSLionel Sambuc return CreateParsedType(T, TSI);
7948f4a2713aSLionel Sambuc }
7949f4a2713aSLionel Sambuc
7950f4a2713aSLionel Sambuc
7951f4a2713aSLionel Sambuc /// Determine whether this failed name lookup should be treated as being
7952f4a2713aSLionel Sambuc /// disabled by a usage of std::enable_if.
isEnableIf(NestedNameSpecifierLoc NNS,const IdentifierInfo & II,SourceRange & CondRange)7953f4a2713aSLionel Sambuc static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II,
7954f4a2713aSLionel Sambuc SourceRange &CondRange) {
7955f4a2713aSLionel Sambuc // We must be looking for a ::type...
7956f4a2713aSLionel Sambuc if (!II.isStr("type"))
7957f4a2713aSLionel Sambuc return false;
7958f4a2713aSLionel Sambuc
7959f4a2713aSLionel Sambuc // ... within an explicitly-written template specialization...
7960f4a2713aSLionel Sambuc if (!NNS || !NNS.getNestedNameSpecifier()->getAsType())
7961f4a2713aSLionel Sambuc return false;
7962f4a2713aSLionel Sambuc TypeLoc EnableIfTy = NNS.getTypeLoc();
7963f4a2713aSLionel Sambuc TemplateSpecializationTypeLoc EnableIfTSTLoc =
7964f4a2713aSLionel Sambuc EnableIfTy.getAs<TemplateSpecializationTypeLoc>();
7965f4a2713aSLionel Sambuc if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0)
7966f4a2713aSLionel Sambuc return false;
7967f4a2713aSLionel Sambuc const TemplateSpecializationType *EnableIfTST =
7968f4a2713aSLionel Sambuc cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr());
7969f4a2713aSLionel Sambuc
7970f4a2713aSLionel Sambuc // ... which names a complete class template declaration...
7971f4a2713aSLionel Sambuc const TemplateDecl *EnableIfDecl =
7972f4a2713aSLionel Sambuc EnableIfTST->getTemplateName().getAsTemplateDecl();
7973f4a2713aSLionel Sambuc if (!EnableIfDecl || EnableIfTST->isIncompleteType())
7974f4a2713aSLionel Sambuc return false;
7975f4a2713aSLionel Sambuc
7976f4a2713aSLionel Sambuc // ... called "enable_if".
7977f4a2713aSLionel Sambuc const IdentifierInfo *EnableIfII =
7978f4a2713aSLionel Sambuc EnableIfDecl->getDeclName().getAsIdentifierInfo();
7979f4a2713aSLionel Sambuc if (!EnableIfII || !EnableIfII->isStr("enable_if"))
7980f4a2713aSLionel Sambuc return false;
7981f4a2713aSLionel Sambuc
7982f4a2713aSLionel Sambuc // Assume the first template argument is the condition.
7983f4a2713aSLionel Sambuc CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange();
7984f4a2713aSLionel Sambuc return true;
7985f4a2713aSLionel Sambuc }
7986f4a2713aSLionel Sambuc
7987f4a2713aSLionel Sambuc /// \brief Build the type that describes a C++ typename specifier,
7988f4a2713aSLionel Sambuc /// e.g., "typename T::type".
7989f4a2713aSLionel Sambuc QualType
CheckTypenameType(ElaboratedTypeKeyword Keyword,SourceLocation KeywordLoc,NestedNameSpecifierLoc QualifierLoc,const IdentifierInfo & II,SourceLocation IILoc)7990f4a2713aSLionel Sambuc Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
7991f4a2713aSLionel Sambuc SourceLocation KeywordLoc,
7992f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc,
7993f4a2713aSLionel Sambuc const IdentifierInfo &II,
7994f4a2713aSLionel Sambuc SourceLocation IILoc) {
7995f4a2713aSLionel Sambuc CXXScopeSpec SS;
7996f4a2713aSLionel Sambuc SS.Adopt(QualifierLoc);
7997f4a2713aSLionel Sambuc
7998f4a2713aSLionel Sambuc DeclContext *Ctx = computeDeclContext(SS);
7999f4a2713aSLionel Sambuc if (!Ctx) {
8000f4a2713aSLionel Sambuc // If the nested-name-specifier is dependent and couldn't be
8001f4a2713aSLionel Sambuc // resolved to a type, build a typename type.
8002f4a2713aSLionel Sambuc assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
8003f4a2713aSLionel Sambuc return Context.getDependentNameType(Keyword,
8004f4a2713aSLionel Sambuc QualifierLoc.getNestedNameSpecifier(),
8005f4a2713aSLionel Sambuc &II);
8006f4a2713aSLionel Sambuc }
8007f4a2713aSLionel Sambuc
8008f4a2713aSLionel Sambuc // If the nested-name-specifier refers to the current instantiation,
8009f4a2713aSLionel Sambuc // the "typename" keyword itself is superfluous. In C++03, the
8010f4a2713aSLionel Sambuc // program is actually ill-formed. However, DR 382 (in C++0x CD1)
8011f4a2713aSLionel Sambuc // allows such extraneous "typename" keywords, and we retroactively
8012f4a2713aSLionel Sambuc // apply this DR to C++03 code with only a warning. In any case we continue.
8013f4a2713aSLionel Sambuc
8014f4a2713aSLionel Sambuc if (RequireCompleteDeclContext(SS, Ctx))
8015f4a2713aSLionel Sambuc return QualType();
8016f4a2713aSLionel Sambuc
8017f4a2713aSLionel Sambuc DeclarationName Name(&II);
8018f4a2713aSLionel Sambuc LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
8019*0a6a1f1dSLionel Sambuc LookupQualifiedName(Result, Ctx, SS);
8020f4a2713aSLionel Sambuc unsigned DiagID = 0;
8021*0a6a1f1dSLionel Sambuc Decl *Referenced = nullptr;
8022f4a2713aSLionel Sambuc switch (Result.getResultKind()) {
8023f4a2713aSLionel Sambuc case LookupResult::NotFound: {
8024f4a2713aSLionel Sambuc // If we're looking up 'type' within a template named 'enable_if', produce
8025f4a2713aSLionel Sambuc // a more specific diagnostic.
8026f4a2713aSLionel Sambuc SourceRange CondRange;
8027f4a2713aSLionel Sambuc if (isEnableIf(QualifierLoc, II, CondRange)) {
8028f4a2713aSLionel Sambuc Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if)
8029f4a2713aSLionel Sambuc << Ctx << CondRange;
8030f4a2713aSLionel Sambuc return QualType();
8031f4a2713aSLionel Sambuc }
8032f4a2713aSLionel Sambuc
8033f4a2713aSLionel Sambuc DiagID = diag::err_typename_nested_not_found;
8034f4a2713aSLionel Sambuc break;
8035f4a2713aSLionel Sambuc }
8036f4a2713aSLionel Sambuc
8037f4a2713aSLionel Sambuc case LookupResult::FoundUnresolvedValue: {
8038f4a2713aSLionel Sambuc // We found a using declaration that is a value. Most likely, the using
8039f4a2713aSLionel Sambuc // declaration itself is meant to have the 'typename' keyword.
8040f4a2713aSLionel Sambuc SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
8041f4a2713aSLionel Sambuc IILoc);
8042f4a2713aSLionel Sambuc Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
8043f4a2713aSLionel Sambuc << Name << Ctx << FullRange;
8044f4a2713aSLionel Sambuc if (UnresolvedUsingValueDecl *Using
8045f4a2713aSLionel Sambuc = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
8046f4a2713aSLionel Sambuc SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
8047f4a2713aSLionel Sambuc Diag(Loc, diag::note_using_value_decl_missing_typename)
8048f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(Loc, "typename ");
8049f4a2713aSLionel Sambuc }
8050f4a2713aSLionel Sambuc }
8051f4a2713aSLionel Sambuc // Fall through to create a dependent typename type, from which we can recover
8052f4a2713aSLionel Sambuc // better.
8053f4a2713aSLionel Sambuc
8054f4a2713aSLionel Sambuc case LookupResult::NotFoundInCurrentInstantiation:
8055f4a2713aSLionel Sambuc // Okay, it's a member of an unknown instantiation.
8056f4a2713aSLionel Sambuc return Context.getDependentNameType(Keyword,
8057f4a2713aSLionel Sambuc QualifierLoc.getNestedNameSpecifier(),
8058f4a2713aSLionel Sambuc &II);
8059f4a2713aSLionel Sambuc
8060f4a2713aSLionel Sambuc case LookupResult::Found:
8061f4a2713aSLionel Sambuc if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
8062f4a2713aSLionel Sambuc // We found a type. Build an ElaboratedType, since the
8063f4a2713aSLionel Sambuc // typename-specifier was just sugar.
8064*0a6a1f1dSLionel Sambuc MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false);
8065f4a2713aSLionel Sambuc return Context.getElaboratedType(ETK_Typename,
8066f4a2713aSLionel Sambuc QualifierLoc.getNestedNameSpecifier(),
8067f4a2713aSLionel Sambuc Context.getTypeDeclType(Type));
8068f4a2713aSLionel Sambuc }
8069f4a2713aSLionel Sambuc
8070f4a2713aSLionel Sambuc DiagID = diag::err_typename_nested_not_type;
8071f4a2713aSLionel Sambuc Referenced = Result.getFoundDecl();
8072f4a2713aSLionel Sambuc break;
8073f4a2713aSLionel Sambuc
8074f4a2713aSLionel Sambuc case LookupResult::FoundOverloaded:
8075f4a2713aSLionel Sambuc DiagID = diag::err_typename_nested_not_type;
8076f4a2713aSLionel Sambuc Referenced = *Result.begin();
8077f4a2713aSLionel Sambuc break;
8078f4a2713aSLionel Sambuc
8079f4a2713aSLionel Sambuc case LookupResult::Ambiguous:
8080f4a2713aSLionel Sambuc return QualType();
8081f4a2713aSLionel Sambuc }
8082f4a2713aSLionel Sambuc
8083f4a2713aSLionel Sambuc // If we get here, it's because name lookup did not find a
8084f4a2713aSLionel Sambuc // type. Emit an appropriate diagnostic and return an error.
8085f4a2713aSLionel Sambuc SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
8086f4a2713aSLionel Sambuc IILoc);
8087f4a2713aSLionel Sambuc Diag(IILoc, DiagID) << FullRange << Name << Ctx;
8088f4a2713aSLionel Sambuc if (Referenced)
8089f4a2713aSLionel Sambuc Diag(Referenced->getLocation(), diag::note_typename_refers_here)
8090f4a2713aSLionel Sambuc << Name;
8091f4a2713aSLionel Sambuc return QualType();
8092f4a2713aSLionel Sambuc }
8093f4a2713aSLionel Sambuc
8094f4a2713aSLionel Sambuc namespace {
8095f4a2713aSLionel Sambuc // See Sema::RebuildTypeInCurrentInstantiation
8096f4a2713aSLionel Sambuc class CurrentInstantiationRebuilder
8097f4a2713aSLionel Sambuc : public TreeTransform<CurrentInstantiationRebuilder> {
8098f4a2713aSLionel Sambuc SourceLocation Loc;
8099f4a2713aSLionel Sambuc DeclarationName Entity;
8100f4a2713aSLionel Sambuc
8101f4a2713aSLionel Sambuc public:
8102f4a2713aSLionel Sambuc typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
8103f4a2713aSLionel Sambuc
CurrentInstantiationRebuilder(Sema & SemaRef,SourceLocation Loc,DeclarationName Entity)8104f4a2713aSLionel Sambuc CurrentInstantiationRebuilder(Sema &SemaRef,
8105f4a2713aSLionel Sambuc SourceLocation Loc,
8106f4a2713aSLionel Sambuc DeclarationName Entity)
8107f4a2713aSLionel Sambuc : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
8108f4a2713aSLionel Sambuc Loc(Loc), Entity(Entity) { }
8109f4a2713aSLionel Sambuc
8110f4a2713aSLionel Sambuc /// \brief Determine whether the given type \p T has already been
8111f4a2713aSLionel Sambuc /// transformed.
8112f4a2713aSLionel Sambuc ///
8113f4a2713aSLionel Sambuc /// For the purposes of type reconstruction, a type has already been
8114f4a2713aSLionel Sambuc /// transformed if it is NULL or if it is not dependent.
AlreadyTransformed(QualType T)8115f4a2713aSLionel Sambuc bool AlreadyTransformed(QualType T) {
8116f4a2713aSLionel Sambuc return T.isNull() || !T->isDependentType();
8117f4a2713aSLionel Sambuc }
8118f4a2713aSLionel Sambuc
8119f4a2713aSLionel Sambuc /// \brief Returns the location of the entity whose type is being
8120f4a2713aSLionel Sambuc /// rebuilt.
getBaseLocation()8121f4a2713aSLionel Sambuc SourceLocation getBaseLocation() { return Loc; }
8122f4a2713aSLionel Sambuc
8123f4a2713aSLionel Sambuc /// \brief Returns the name of the entity whose type is being rebuilt.
getBaseEntity()8124f4a2713aSLionel Sambuc DeclarationName getBaseEntity() { return Entity; }
8125f4a2713aSLionel Sambuc
8126f4a2713aSLionel Sambuc /// \brief Sets the "base" location and entity when that
8127f4a2713aSLionel Sambuc /// information is known based on another transformation.
setBase(SourceLocation Loc,DeclarationName Entity)8128f4a2713aSLionel Sambuc void setBase(SourceLocation Loc, DeclarationName Entity) {
8129f4a2713aSLionel Sambuc this->Loc = Loc;
8130f4a2713aSLionel Sambuc this->Entity = Entity;
8131f4a2713aSLionel Sambuc }
8132f4a2713aSLionel Sambuc
TransformLambdaExpr(LambdaExpr * E)8133f4a2713aSLionel Sambuc ExprResult TransformLambdaExpr(LambdaExpr *E) {
8134f4a2713aSLionel Sambuc // Lambdas never need to be transformed.
8135f4a2713aSLionel Sambuc return E;
8136f4a2713aSLionel Sambuc }
8137f4a2713aSLionel Sambuc };
8138f4a2713aSLionel Sambuc }
8139f4a2713aSLionel Sambuc
8140f4a2713aSLionel Sambuc /// \brief Rebuilds a type within the context of the current instantiation.
8141f4a2713aSLionel Sambuc ///
8142f4a2713aSLionel Sambuc /// The type \p T is part of the type of an out-of-line member definition of
8143f4a2713aSLionel Sambuc /// a class template (or class template partial specialization) that was parsed
8144f4a2713aSLionel Sambuc /// and constructed before we entered the scope of the class template (or
8145f4a2713aSLionel Sambuc /// partial specialization thereof). This routine will rebuild that type now
8146f4a2713aSLionel Sambuc /// that we have entered the declarator's scope, which may produce different
8147f4a2713aSLionel Sambuc /// canonical types, e.g.,
8148f4a2713aSLionel Sambuc ///
8149f4a2713aSLionel Sambuc /// \code
8150f4a2713aSLionel Sambuc /// template<typename T>
8151f4a2713aSLionel Sambuc /// struct X {
8152f4a2713aSLionel Sambuc /// typedef T* pointer;
8153f4a2713aSLionel Sambuc /// pointer data();
8154f4a2713aSLionel Sambuc /// };
8155f4a2713aSLionel Sambuc ///
8156f4a2713aSLionel Sambuc /// template<typename T>
8157f4a2713aSLionel Sambuc /// typename X<T>::pointer X<T>::data() { ... }
8158f4a2713aSLionel Sambuc /// \endcode
8159f4a2713aSLionel Sambuc ///
8160f4a2713aSLionel Sambuc /// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
8161f4a2713aSLionel Sambuc /// since we do not know that we can look into X<T> when we parsed the type.
8162f4a2713aSLionel Sambuc /// This function will rebuild the type, performing the lookup of "pointer"
8163f4a2713aSLionel Sambuc /// in X<T> and returning an ElaboratedType whose canonical type is the same
8164f4a2713aSLionel Sambuc /// as the canonical type of T*, allowing the return types of the out-of-line
8165f4a2713aSLionel Sambuc /// definition and the declaration to match.
RebuildTypeInCurrentInstantiation(TypeSourceInfo * T,SourceLocation Loc,DeclarationName Name)8166f4a2713aSLionel Sambuc TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
8167f4a2713aSLionel Sambuc SourceLocation Loc,
8168f4a2713aSLionel Sambuc DeclarationName Name) {
8169f4a2713aSLionel Sambuc if (!T || !T->getType()->isDependentType())
8170f4a2713aSLionel Sambuc return T;
8171f4a2713aSLionel Sambuc
8172f4a2713aSLionel Sambuc CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
8173f4a2713aSLionel Sambuc return Rebuilder.TransformType(T);
8174f4a2713aSLionel Sambuc }
8175f4a2713aSLionel Sambuc
RebuildExprInCurrentInstantiation(Expr * E)8176f4a2713aSLionel Sambuc ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
8177f4a2713aSLionel Sambuc CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
8178f4a2713aSLionel Sambuc DeclarationName());
8179f4a2713aSLionel Sambuc return Rebuilder.TransformExpr(E);
8180f4a2713aSLionel Sambuc }
8181f4a2713aSLionel Sambuc
RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec & SS)8182f4a2713aSLionel Sambuc bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
8183f4a2713aSLionel Sambuc if (SS.isInvalid())
8184f4a2713aSLionel Sambuc return true;
8185f4a2713aSLionel Sambuc
8186f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
8187f4a2713aSLionel Sambuc CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
8188f4a2713aSLionel Sambuc DeclarationName());
8189f4a2713aSLionel Sambuc NestedNameSpecifierLoc Rebuilt
8190f4a2713aSLionel Sambuc = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
8191f4a2713aSLionel Sambuc if (!Rebuilt)
8192f4a2713aSLionel Sambuc return true;
8193f4a2713aSLionel Sambuc
8194f4a2713aSLionel Sambuc SS.Adopt(Rebuilt);
8195f4a2713aSLionel Sambuc return false;
8196f4a2713aSLionel Sambuc }
8197f4a2713aSLionel Sambuc
8198f4a2713aSLionel Sambuc /// \brief Rebuild the template parameters now that we know we're in a current
8199f4a2713aSLionel Sambuc /// instantiation.
RebuildTemplateParamsInCurrentInstantiation(TemplateParameterList * Params)8200f4a2713aSLionel Sambuc bool Sema::RebuildTemplateParamsInCurrentInstantiation(
8201f4a2713aSLionel Sambuc TemplateParameterList *Params) {
8202f4a2713aSLionel Sambuc for (unsigned I = 0, N = Params->size(); I != N; ++I) {
8203f4a2713aSLionel Sambuc Decl *Param = Params->getParam(I);
8204f4a2713aSLionel Sambuc
8205f4a2713aSLionel Sambuc // There is nothing to rebuild in a type parameter.
8206f4a2713aSLionel Sambuc if (isa<TemplateTypeParmDecl>(Param))
8207f4a2713aSLionel Sambuc continue;
8208f4a2713aSLionel Sambuc
8209f4a2713aSLionel Sambuc // Rebuild the template parameter list of a template template parameter.
8210f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *TTP
8211f4a2713aSLionel Sambuc = dyn_cast<TemplateTemplateParmDecl>(Param)) {
8212f4a2713aSLionel Sambuc if (RebuildTemplateParamsInCurrentInstantiation(
8213f4a2713aSLionel Sambuc TTP->getTemplateParameters()))
8214f4a2713aSLionel Sambuc return true;
8215f4a2713aSLionel Sambuc
8216f4a2713aSLionel Sambuc continue;
8217f4a2713aSLionel Sambuc }
8218f4a2713aSLionel Sambuc
8219f4a2713aSLionel Sambuc // Rebuild the type of a non-type template parameter.
8220f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
8221f4a2713aSLionel Sambuc TypeSourceInfo *NewTSI
8222f4a2713aSLionel Sambuc = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
8223f4a2713aSLionel Sambuc NTTP->getLocation(),
8224f4a2713aSLionel Sambuc NTTP->getDeclName());
8225f4a2713aSLionel Sambuc if (!NewTSI)
8226f4a2713aSLionel Sambuc return true;
8227f4a2713aSLionel Sambuc
8228f4a2713aSLionel Sambuc if (NewTSI != NTTP->getTypeSourceInfo()) {
8229f4a2713aSLionel Sambuc NTTP->setTypeSourceInfo(NewTSI);
8230f4a2713aSLionel Sambuc NTTP->setType(NewTSI->getType());
8231f4a2713aSLionel Sambuc }
8232f4a2713aSLionel Sambuc }
8233f4a2713aSLionel Sambuc
8234f4a2713aSLionel Sambuc return false;
8235f4a2713aSLionel Sambuc }
8236f4a2713aSLionel Sambuc
8237f4a2713aSLionel Sambuc /// \brief Produces a formatted string that describes the binding of
8238f4a2713aSLionel Sambuc /// template parameters to template arguments.
8239f4a2713aSLionel Sambuc std::string
getTemplateArgumentBindingsText(const TemplateParameterList * Params,const TemplateArgumentList & Args)8240f4a2713aSLionel Sambuc Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
8241f4a2713aSLionel Sambuc const TemplateArgumentList &Args) {
8242f4a2713aSLionel Sambuc return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
8243f4a2713aSLionel Sambuc }
8244f4a2713aSLionel Sambuc
8245f4a2713aSLionel Sambuc std::string
getTemplateArgumentBindingsText(const TemplateParameterList * Params,const TemplateArgument * Args,unsigned NumArgs)8246f4a2713aSLionel Sambuc Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
8247f4a2713aSLionel Sambuc const TemplateArgument *Args,
8248f4a2713aSLionel Sambuc unsigned NumArgs) {
8249f4a2713aSLionel Sambuc SmallString<128> Str;
8250f4a2713aSLionel Sambuc llvm::raw_svector_ostream Out(Str);
8251f4a2713aSLionel Sambuc
8252f4a2713aSLionel Sambuc if (!Params || Params->size() == 0 || NumArgs == 0)
8253f4a2713aSLionel Sambuc return std::string();
8254f4a2713aSLionel Sambuc
8255f4a2713aSLionel Sambuc for (unsigned I = 0, N = Params->size(); I != N; ++I) {
8256f4a2713aSLionel Sambuc if (I >= NumArgs)
8257f4a2713aSLionel Sambuc break;
8258f4a2713aSLionel Sambuc
8259f4a2713aSLionel Sambuc if (I == 0)
8260f4a2713aSLionel Sambuc Out << "[with ";
8261f4a2713aSLionel Sambuc else
8262f4a2713aSLionel Sambuc Out << ", ";
8263f4a2713aSLionel Sambuc
8264f4a2713aSLionel Sambuc if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
8265f4a2713aSLionel Sambuc Out << Id->getName();
8266f4a2713aSLionel Sambuc } else {
8267f4a2713aSLionel Sambuc Out << '$' << I;
8268f4a2713aSLionel Sambuc }
8269f4a2713aSLionel Sambuc
8270f4a2713aSLionel Sambuc Out << " = ";
8271f4a2713aSLionel Sambuc Args[I].print(getPrintingPolicy(), Out);
8272f4a2713aSLionel Sambuc }
8273f4a2713aSLionel Sambuc
8274f4a2713aSLionel Sambuc Out << ']';
8275f4a2713aSLionel Sambuc return Out.str();
8276f4a2713aSLionel Sambuc }
8277f4a2713aSLionel Sambuc
MarkAsLateParsedTemplate(FunctionDecl * FD,Decl * FnD,CachedTokens & Toks)8278f4a2713aSLionel Sambuc void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD,
8279f4a2713aSLionel Sambuc CachedTokens &Toks) {
8280f4a2713aSLionel Sambuc if (!FD)
8281f4a2713aSLionel Sambuc return;
8282f4a2713aSLionel Sambuc
8283f4a2713aSLionel Sambuc LateParsedTemplate *LPT = new LateParsedTemplate;
8284f4a2713aSLionel Sambuc
8285f4a2713aSLionel Sambuc // Take tokens to avoid allocations
8286f4a2713aSLionel Sambuc LPT->Toks.swap(Toks);
8287f4a2713aSLionel Sambuc LPT->D = FnD;
8288f4a2713aSLionel Sambuc LateParsedTemplateMap[FD] = LPT;
8289f4a2713aSLionel Sambuc
8290f4a2713aSLionel Sambuc FD->setLateTemplateParsed(true);
8291f4a2713aSLionel Sambuc }
8292f4a2713aSLionel Sambuc
UnmarkAsLateParsedTemplate(FunctionDecl * FD)8293f4a2713aSLionel Sambuc void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) {
8294f4a2713aSLionel Sambuc if (!FD)
8295f4a2713aSLionel Sambuc return;
8296f4a2713aSLionel Sambuc FD->setLateTemplateParsed(false);
8297f4a2713aSLionel Sambuc }
8298f4a2713aSLionel Sambuc
IsInsideALocalClassWithinATemplateFunction()8299f4a2713aSLionel Sambuc bool Sema::IsInsideALocalClassWithinATemplateFunction() {
8300f4a2713aSLionel Sambuc DeclContext *DC = CurContext;
8301f4a2713aSLionel Sambuc
8302f4a2713aSLionel Sambuc while (DC) {
8303f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
8304f4a2713aSLionel Sambuc const FunctionDecl *FD = RD->isLocalClass();
8305f4a2713aSLionel Sambuc return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
8306f4a2713aSLionel Sambuc } else if (DC->isTranslationUnit() || DC->isNamespace())
8307f4a2713aSLionel Sambuc return false;
8308f4a2713aSLionel Sambuc
8309f4a2713aSLionel Sambuc DC = DC->getParent();
8310f4a2713aSLionel Sambuc }
8311f4a2713aSLionel Sambuc return false;
8312f4a2713aSLionel Sambuc }
8313