blob: 34e5d722ca8f3602325c21f1ebeb741b87835c98 [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Benjamin Kramer013b3662012-01-30 16:17:39 +000029#include "llvm/ADT/SmallBitVector.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000030#include "llvm/ADT/SmallString.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000031#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000032using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000033using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000034
John McCall78b81052010-11-10 02:40:36 +000035// Exported for use by Parser.
36SourceRange
37clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
38 unsigned N) {
39 if (!N) return SourceRange();
40 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
41}
42
Douglas Gregor2dd078a2009-09-02 22:59:36 +000043/// \brief Determine whether the declaration found is acceptable as the name
44/// of a template and, if so, return that template declaration. Otherwise,
45/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000046static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000047 NamedDecl *Orig,
48 bool AllowFunctionTemplates) {
John McCallad00b772010-06-16 08:42:20 +000049 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000051 if (isa<TemplateDecl>(D)) {
52 if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D))
53 return 0;
54
John McCallad00b772010-06-16 08:42:20 +000055 return Orig;
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000056 }
Mike Stump1eb44332009-09-09 15:08:12 +000057
Douglas Gregor2dd078a2009-09-02 22:59:36 +000058 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
59 // C++ [temp.local]p1:
60 // Like normal (non-template) classes, class templates have an
61 // injected-class-name (Clause 9). The injected-class-name
62 // can be used with or without a template-argument-list. When
63 // it is used without a template-argument-list, it is
64 // equivalent to the injected-class-name followed by the
65 // template-parameters of the class template enclosed in
66 // <>. When it is used with a template-argument-list, it
67 // refers to the specified class template specialization,
68 // which could be the current specialization or another
69 // specialization.
70 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000071 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000072 if (Record->getDescribedClassTemplate())
73 return Record->getDescribedClassTemplate();
74
75 if (ClassTemplateSpecializationDecl *Spec
76 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
77 return Spec->getSpecializedTemplate();
78 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor2dd078a2009-09-02 22:59:36 +000080 return 0;
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Douglas Gregor2dd078a2009-09-02 22:59:36 +000083 return 0;
84}
85
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000086void Sema::FilterAcceptableTemplateNames(LookupResult &R,
87 bool AllowFunctionTemplates) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 // The set of class templates we've already seen.
89 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000090 LookupResult::Filter filter = R.makeFilter();
91 while (filter.hasNext()) {
92 NamedDecl *Orig = filter.next();
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000093 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig,
94 AllowFunctionTemplates);
John McCallf7a1a742009-11-24 19:00:30 +000095 if (!Repl)
96 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 else if (Repl != Orig) {
98
99 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000100 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000101 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000102 // one base class). If all of the injected-class-names that are found
103 // refer to specializations of the same class template, and if the name
Richard Smith3e4c6c42011-05-05 21:57:07 +0000104 // is used as a template-name, the reference refers to the class
105 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000106 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000107 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
108 if (!ClassTemplates.insert(ClassTmpl)) {
109 filter.erase();
110 continue;
111 }
John McCall8ba66912010-08-13 07:02:08 +0000112
113 // FIXME: we promote access to public here as a workaround to
114 // the fact that LookupResult doesn't let us remember that we
115 // found this template through a particular injected class name,
116 // which means we end up doing nasty things to the invariants.
117 // Pretending that access is public is *much* safer.
118 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000119 }
John McCallf7a1a742009-11-24 19:00:30 +0000120 }
121 filter.done();
122}
123
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000124bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R,
125 bool AllowFunctionTemplates) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000126 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000127 if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates))
Douglas Gregor312eadb2011-04-24 05:37:28 +0000128 return true;
129
Douglas Gregor3b887352011-04-27 04:48:22 +0000130 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000131}
132
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000133TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000134 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000135 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000136 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000137 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000138 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000139 TemplateTy &TemplateResult,
140 bool &MemberOfUnknownSpecialization) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000141 assert(getLangOpts().CPlusPlus && "No template names in C!");
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000142
Douglas Gregor014e88d2009-11-03 23:16:33 +0000143 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000144 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000145
Douglas Gregor014e88d2009-11-03 23:16:33 +0000146 switch (Name.getKind()) {
147 case UnqualifiedId::IK_Identifier:
148 TName = DeclarationName(Name.Identifier);
149 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000150
Douglas Gregor014e88d2009-11-03 23:16:33 +0000151 case UnqualifiedId::IK_OperatorFunctionId:
152 TName = Context.DeclarationNames.getCXXOperatorName(
153 Name.OperatorFunctionId.Operator);
154 break;
155
Sean Hunte6252d12009-11-28 08:58:14 +0000156 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000157 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
158 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000159
Douglas Gregor014e88d2009-11-03 23:16:33 +0000160 default:
161 return TNK_Non_template;
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
John McCallb3d87482010-08-24 05:47:05 +0000164 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Daniel Dunbar96a00142012-03-09 18:35:03 +0000166 LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000167 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
168 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000169 if (R.empty()) return TNK_Non_template;
170 if (R.isAmbiguous()) {
171 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000172 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000173
174 // FIXME: we might have ambiguous templates, in which case we
175 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000177 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000178
John McCall0bd6feb2009-12-02 08:04:21 +0000179 TemplateName Template;
180 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
John McCall0bd6feb2009-12-02 08:04:21 +0000182 unsigned ResultCount = R.end() - R.begin();
183 if (ResultCount > 1) {
184 // We assume that we'll preserve the qualifier from a function
185 // template name in other ways.
186 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
187 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000188
189 // We'll do this lookup again later.
190 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000191 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000192 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
193
194 if (SS.isSet() && !SS.isInvalid()) {
195 NestedNameSpecifier *Qualifier
196 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000197 Template = Context.getQualifiedTemplateName(Qualifier,
198 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000199 } else {
200 Template = TemplateName(TD);
201 }
202
John McCallb8592062010-08-13 02:23:42 +0000203 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000204 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000205
206 // We'll do this lookup again later.
207 R.suppressDiagnostics();
208 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000209 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
210 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000211 TemplateKind = TNK_Type_template;
212 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
John McCall0bd6feb2009-12-02 08:04:21 +0000215 TemplateResult = TemplateTy::make(Template);
216 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000217}
218
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000219bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000220 SourceLocation IILoc,
221 Scope *S,
222 const CXXScopeSpec *SS,
223 TemplateTy &SuggestedTemplate,
224 TemplateNameKind &SuggestedKind) {
225 // We can't recover unless there's a dependent scope specifier preceding the
226 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000227 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000228 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
229 computeDeclContext(*SS))
230 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000231
Douglas Gregor84d0a192010-01-12 21:28:44 +0000232 // The code is missing a 'template' keyword prior to the dependent template
233 // name.
234 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
235 Diag(IILoc, diag::err_template_kw_missing)
236 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000237 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000238 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000239 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
240 SuggestedKind = TNK_Dependent_template_name;
241 return true;
242}
243
John McCallf7a1a742009-11-24 19:00:30 +0000244void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000245 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000246 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000247 bool EnteringContext,
248 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000249 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000250 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000251 DeclContext *LookupCtx = 0;
252 bool isDependent = false;
253 if (!ObjectType.isNull()) {
254 // This nested-name-specifier occurs in a member access expression, e.g.,
255 // x->B::f, and we are looking into the type of the object.
256 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
257 LookupCtx = computeDeclContext(ObjectType);
258 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000259 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000260 "Caller should have completed object type");
Douglas Gregor1d7049a2012-01-12 16:11:24 +0000261
262 // Template names cannot appear inside an Objective-C class or object type.
263 if (ObjectType->isObjCObjectOrInterfaceType()) {
264 Found.clear();
265 return;
266 }
John McCallf7a1a742009-11-24 19:00:30 +0000267 } else if (SS.isSet()) {
268 // This nested-name-specifier occurs after another nested-name-specifier,
269 // so long into the context associated with the prior nested-name-specifier.
270 LookupCtx = computeDeclContext(SS, EnteringContext);
271 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000272
John McCallf7a1a742009-11-24 19:00:30 +0000273 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000274 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000275 return;
276 }
277
278 bool ObjectTypeSearchedInScope = false;
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000279 bool AllowFunctionTemplatesInLookup = true;
John McCallf7a1a742009-11-24 19:00:30 +0000280 if (LookupCtx) {
281 // Perform "qualified" name lookup into the declaration context we
282 // computed, which is either the type of the base of a member access
283 // expression or the declaration context associated with a prior
284 // nested-name-specifier.
285 LookupQualifiedName(Found, LookupCtx);
John McCallf7a1a742009-11-24 19:00:30 +0000286 if (!ObjectType.isNull() && Found.empty()) {
287 // C++ [basic.lookup.classref]p1:
288 // In a class member access expression (5.2.5), if the . or -> token is
289 // immediately followed by an identifier followed by a <, the
290 // identifier must be looked up to determine whether the < is the
291 // beginning of a template argument list (14.2) or a less-than operator.
292 // The identifier is first looked up in the class of the object
293 // expression. If the identifier is not found, it is then looked up in
294 // the context of the entire postfix-expression and shall name a class
295 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000296 if (S) LookupName(Found, S);
297 ObjectTypeSearchedInScope = true;
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000298 AllowFunctionTemplatesInLookup = false;
John McCallf7a1a742009-11-24 19:00:30 +0000299 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000300 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000301 // We cannot look into a dependent object type or nested nme
302 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000303 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000304 return;
305 } else {
306 // Perform unqualified name lookup in the current scope.
307 LookupName(Found, S);
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000308
309 if (!ObjectType.isNull())
310 AllowFunctionTemplatesInLookup = false;
John McCallf7a1a742009-11-24 19:00:30 +0000311 }
312
Douglas Gregor2e933882010-01-12 17:06:20 +0000313 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000314 // If we did not find any names, attempt to correct any typos.
315 DeclarationName Name = Found.getLookupName();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000316 Found.clear();
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000317 // Simple filter callback that, for keywords, only accepts the C++ *_cast
318 CorrectionCandidateCallback FilterCCC;
319 FilterCCC.WantTypeSpecifiers = false;
320 FilterCCC.WantExpressionKeywords = false;
321 FilterCCC.WantRemainingKeywords = false;
322 FilterCCC.WantCXXNamedCasts = true;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000323 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
324 Found.getLookupKind(), S, &SS,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000325 FilterCCC, LookupCtx)) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000326 Found.setLookupName(Corrected.getCorrection());
327 if (Corrected.getCorrectionDecl())
328 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000329 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000330 if (!Found.empty()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000331 std::string CorrectedStr(Corrected.getAsString(getLangOpts()));
332 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000333 if (LookupCtx)
334 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000335 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
336 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000337 else
338 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000339 << Name << CorrectedQuotedStr
340 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000341 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
342 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000343 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000344 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000345 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000346 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000347 }
348 }
349
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000350 FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000351 if (Found.empty()) {
352 if (isDependent)
353 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000354 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000355 }
John McCallf7a1a742009-11-24 19:00:30 +0000356
357 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
358 // C++ [basic.lookup.classref]p1:
359 // [...] If the lookup in the class of the object expression finds a
360 // template, the name is also looked up in the context of the entire
361 // postfix-expression and [...]
362 //
363 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
364 LookupOrdinaryName);
365 LookupName(FoundOuter, S);
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000366 FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000367
John McCallf7a1a742009-11-24 19:00:30 +0000368 if (FoundOuter.empty()) {
369 // - if the name is not found, the name found in the class of the
370 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000371 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
372 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000373 // - if the name is found in the context of the entire
374 // postfix-expression and does not name a class template, the name
375 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000376 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000377 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000378 // - if the name found is a class template, it must refer to the same
379 // entity as the one found in the class of the object expression,
380 // otherwise the program is ill-formed.
381 if (!Found.isSingleResult() ||
382 Found.getFoundDecl()->getCanonicalDecl()
383 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000384 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000385 diag::ext_nested_name_member_ref_lookup_ambiguous)
386 << Found.getLookupName()
387 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000388 Diag(Found.getRepresentativeDecl()->getLocation(),
389 diag::note_ambig_member_ref_object_type)
390 << ObjectType;
391 Diag(FoundOuter.getFoundDecl()->getLocation(),
392 diag::note_ambig_member_ref_scope);
393
394 // Recover by taking the template that we found in the object
395 // expression's type.
396 }
397 }
398 }
399}
400
John McCall2f841ba2009-12-02 03:53:29 +0000401/// ActOnDependentIdExpression - Handle a dependent id-expression that
402/// was just parsed. This is only possible with an explicit scope
403/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000404ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000405Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000406 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000407 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000408 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000409 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000410 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000411
John McCall2f841ba2009-12-02 03:53:29 +0000412 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000413 isa<CXXMethodDecl>(DC) &&
414 cast<CXXMethodDecl>(DC)->isInstance()) {
415 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000416
John McCallf7a1a742009-11-24 19:00:30 +0000417 // Since the 'this' expression is synthesized, we don't need to
418 // perform the double-lookup check.
419 NamedDecl *FirstQualifierInScope = 0;
420
John McCallaa81e162009-12-01 22:10:20 +0000421 return Owned(CXXDependentScopeMemberExpr::Create(Context,
422 /*This*/ 0, ThisType,
423 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000424 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000425 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000426 TemplateKWLoc,
John McCallf7a1a742009-11-24 19:00:30 +0000427 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000428 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000429 TemplateArgs));
430 }
431
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000432 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000433}
434
John McCall60d7b3a2010-08-24 06:29:42 +0000435ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000436Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000437 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000438 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000439 const TemplateArgumentListInfo *TemplateArgs) {
440 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000441 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000442 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000443 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000444 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000445}
446
Douglas Gregor72c3f312008-12-05 18:15:24 +0000447/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
448/// that the template parameter 'PrevDecl' is being shadowed by a new
449/// declaration at location Loc. Returns true to indicate that this is
450/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000451void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000452 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000453
454 // Microsoft Visual C++ permits template parameters to be shadowed.
David Blaikie4e4d0842012-03-11 07:00:24 +0000455 if (getLangOpts().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000456 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000457
458 // C++ [temp.local]p4:
459 // A template-parameter shall not be redeclared within its
460 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000461 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000462 << cast<NamedDecl>(PrevDecl)->getDeclName();
463 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000464 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000465}
466
Douglas Gregor2943aed2009-03-03 04:44:36 +0000467/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000468/// the parameter D to reference the templated declaration and return a pointer
469/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000470TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
471 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
472 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000473 return Temp;
474 }
475 return 0;
476}
477
Douglas Gregorba68eca2011-01-05 17:40:24 +0000478ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
479 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000480 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000481 "Only template template arguments can be pack expansions here");
482 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
483 "Template template argument pack expansion without packs");
484 ParsedTemplateArgument Result(*this);
485 Result.EllipsisLoc = EllipsisLoc;
486 return Result;
487}
488
Douglas Gregor788cd062009-11-11 01:00:40 +0000489static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
490 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000491
Douglas Gregor788cd062009-11-11 01:00:40 +0000492 switch (Arg.getKind()) {
493 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000494 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000495 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000496 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000497 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000498 return TemplateArgumentLoc(TemplateArgument(T), DI);
499 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000500
Douglas Gregor788cd062009-11-11 01:00:40 +0000501 case ParsedTemplateArgument::NonType: {
502 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
503 return TemplateArgumentLoc(TemplateArgument(E), E);
504 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000505
Douglas Gregor788cd062009-11-11 01:00:40 +0000506 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000507 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000508 TemplateArgument TArg;
509 if (Arg.getEllipsisLoc().isValid())
510 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
511 else
512 TArg = Template;
513 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000514 Arg.getScopeSpec().getWithLocInContext(
515 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000516 Arg.getLocation(),
517 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000518 }
519 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000520
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000521 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000522}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000523
Douglas Gregor788cd062009-11-11 01:00:40 +0000524/// \brief Translates template arguments as provided by the parser
525/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000526void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
527 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000528 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000529 TemplateArgs.addArgument(translateTemplateArgument(*this,
530 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000531}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000532
Douglas Gregor72c3f312008-12-05 18:15:24 +0000533/// ActOnTypeParameter - Called when a C++ template type parameter
534/// (e.g., "typename T") has been parsed. Typename specifies whether
535/// the keyword "typename" was used to declare the type parameter
536/// (otherwise, "class" was used), and KeyLoc is the location of the
537/// "class" or "typename" keyword. ParamName is the name of the
538/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000539/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000540/// If the type parameter has a default argument, it will be added
541/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000542Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
543 SourceLocation EllipsisLoc,
544 SourceLocation KeyLoc,
545 IdentifierInfo *ParamName,
546 SourceLocation ParamNameLoc,
547 unsigned Depth, unsigned Position,
548 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000549 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000550 assert(S->isTemplateParamScope() &&
551 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000552 bool Invalid = false;
553
554 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000555 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000556 LookupOrdinaryName,
557 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000558 if (PrevDecl && PrevDecl->isTemplateParameter()) {
559 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
560 PrevDecl = 0;
561 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000562 }
563
Douglas Gregorddc29e12009-02-06 22:42:48 +0000564 SourceLocation Loc = ParamNameLoc;
565 if (!ParamName)
566 Loc = KeyLoc;
567
Douglas Gregor72c3f312008-12-05 18:15:24 +0000568 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000569 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000570 KeyLoc, Loc, Depth, Position, ParamName,
571 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000572 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000573 if (Invalid)
574 Param->setInvalidDecl();
575
576 if (ParamName) {
577 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000578 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000579 IdResolver.AddDecl(Param);
580 }
581
Douglas Gregor61c4d282011-01-05 15:48:55 +0000582 // C++0x [temp.param]p9:
583 // A default template-argument may be specified for any kind of
584 // template-parameter that is not a template parameter pack.
585 if (DefaultArg && Ellipsis) {
586 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
587 DefaultArg = ParsedType();
588 }
589
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000590 // Handle the default argument, if provided.
591 if (DefaultArg) {
592 TypeSourceInfo *DefaultTInfo;
593 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000594
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000595 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000596
Douglas Gregor6f526752010-12-16 08:48:57 +0000597 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000598 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000599 UPPC_DefaultArgument))
600 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000601
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000602 // Check the template argument itself.
603 if (CheckTemplateArgument(Param, DefaultTInfo)) {
604 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000605 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000606 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000607
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000608 Param->setDefaultArgument(DefaultTInfo, false);
609 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000610
John McCalld226f652010-08-21 09:40:31 +0000611 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000612}
613
Douglas Gregor2943aed2009-03-03 04:44:36 +0000614/// \brief Check that the type of a non-type template parameter is
615/// well-formed.
616///
617/// \returns the (possibly-promoted) parameter type if valid;
618/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000619QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000620Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000621 // We don't allow variably-modified types as the type of non-type template
622 // parameters.
623 if (T->isVariablyModifiedType()) {
624 Diag(Loc, diag::err_variably_modified_nontype_template_param)
625 << T;
626 return QualType();
627 }
628
Douglas Gregor2943aed2009-03-03 04:44:36 +0000629 // C++ [temp.param]p4:
630 //
631 // A non-type template-parameter shall have one of the following
632 // (optionally cv-qualified) types:
633 //
634 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000635 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000636 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000637 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000638 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000639 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000640 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000641 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000642 // -- std::nullptr_t.
643 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000644 // If T is a dependent type, we can't do the check now, so we
645 // assume that it is well-formed.
Richard Smithe37f4842012-03-13 07:21:50 +0000646 T->isDependentType()) {
647 // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter
648 // are ignored when determining its type.
649 return T.getUnqualifiedType();
650 }
651
Douglas Gregor2943aed2009-03-03 04:44:36 +0000652 // C++ [temp.param]p8:
653 //
654 // A non-type template-parameter of type "array of T" or
655 // "function returning T" is adjusted to be of type "pointer to
656 // T" or "pointer to function returning T", respectively.
657 else if (T->isArrayType())
658 // FIXME: Keep the type prior to promotion?
659 return Context.getArrayDecayedType(T);
660 else if (T->isFunctionType())
661 // FIXME: Keep the type prior to promotion?
662 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000663
Douglas Gregor2943aed2009-03-03 04:44:36 +0000664 Diag(Loc, diag::err_template_nontype_parm_bad_type)
665 << T;
666
667 return QualType();
668}
669
John McCalld226f652010-08-21 09:40:31 +0000670Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
671 unsigned Depth,
672 unsigned Position,
673 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000674 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000675 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
676 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000677
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000678 assert(S->isTemplateParamScope() &&
679 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000680 bool Invalid = false;
681
682 IdentifierInfo *ParamName = D.getIdentifier();
683 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000684 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000685 LookupOrdinaryName,
686 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000687 if (PrevDecl && PrevDecl->isTemplateParameter()) {
688 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
689 PrevDecl = 0;
690 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000691 }
692
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000693 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
694 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000695 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000696 Invalid = true;
697 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000698
Douglas Gregor10738d32010-12-23 23:51:58 +0000699 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000700 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000701 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Daniel Dunbar96a00142012-03-09 18:35:03 +0000702 D.getLocStart(),
John McCall7a9813c2010-01-22 00:28:27 +0000703 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000704 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000705 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000706 Param->setAccess(AS_public);
707
Douglas Gregor72c3f312008-12-05 18:15:24 +0000708 if (Invalid)
709 Param->setInvalidDecl();
710
711 if (D.getIdentifier()) {
712 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000713 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000714 IdResolver.AddDecl(Param);
715 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000716
Douglas Gregor61c4d282011-01-05 15:48:55 +0000717 // C++0x [temp.param]p9:
718 // A default template-argument may be specified for any kind of
719 // template-parameter that is not a template parameter pack.
720 if (Default && IsParameterPack) {
721 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
722 Default = 0;
723 }
724
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000725 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000726 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000727 // Check for unexpanded parameter packs.
728 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
729 return Param;
730
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000731 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000732 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
733 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000734 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000735 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000736 }
John Wiegley429bb272011-04-08 18:41:53 +0000737 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000738
John McCall9ae2f072010-08-23 23:25:46 +0000739 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000740 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000741
John McCalld226f652010-08-21 09:40:31 +0000742 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000743}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000744
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000745/// ActOnTemplateTemplateParameter - Called when a C++ template template
746/// parameter (e.g. T in template <template <typename> class T> class array)
747/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000748Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
749 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000750 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000751 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000752 IdentifierInfo *Name,
753 SourceLocation NameLoc,
754 unsigned Depth,
755 unsigned Position,
756 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000757 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000758 assert(S->isTemplateParamScope() &&
759 "Template template parameter not in template parameter scope!");
760
761 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000762 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000763 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000764 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000765 NameLoc.isInvalid()? TmpLoc : NameLoc,
766 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000767 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000768 Param->setAccess(AS_public);
769
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000770 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000771 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000772 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000773 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000774 IdResolver.AddDecl(Param);
775 }
776
Douglas Gregor6f526752010-12-16 08:48:57 +0000777 if (Params->size() == 0) {
778 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
779 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
780 Param->setInvalidDecl();
781 }
782
Douglas Gregor61c4d282011-01-05 15:48:55 +0000783 // C++0x [temp.param]p9:
784 // A default template-argument may be specified for any kind of
785 // template-parameter that is not a template parameter pack.
786 if (IsParameterPack && !Default.isInvalid()) {
787 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
788 Default = ParsedTemplateArgument();
789 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000790
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000791 if (!Default.isInvalid()) {
792 // Check only that we have a template template argument. We don't want to
793 // try to check well-formedness now, because our template template parameter
794 // might have dependent types in its template parameters, which we wouldn't
795 // be able to match now.
796 //
797 // If none of the template template parameter's template arguments mention
798 // other template parameters, we could actually perform more checking here.
799 // However, it isn't worth doing.
800 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
801 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
802 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
803 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000804 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000805 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000806
Douglas Gregor6f526752010-12-16 08:48:57 +0000807 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000808 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000809 DefaultArg.getArgument().getAsTemplate(),
810 UPPC_DefaultArgument))
811 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000812
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000813 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000814 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000815
John McCalld226f652010-08-21 09:40:31 +0000816 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000817}
818
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000819/// ActOnTemplateParameterList - Builds a TemplateParameterList that
820/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000821TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000822Sema::ActOnTemplateParameterList(unsigned Depth,
823 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000824 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000825 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000826 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000827 SourceLocation RAngleLoc) {
828 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000829 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000830
Douglas Gregorddc29e12009-02-06 22:42:48 +0000831 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000832 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000833 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000834}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000835
John McCallb6217662010-03-15 10:12:16 +0000836static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
837 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000838 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000839}
840
John McCallf312b1e2010-08-26 23:41:50 +0000841DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000842Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000843 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000844 IdentifierInfo *Name, SourceLocation NameLoc,
845 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000846 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000847 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000848 unsigned NumOuterTemplateParamLists,
849 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000850 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000851 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000852 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000853 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000854
855 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000856 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000857 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000858
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000859 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
860 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000861
862 // There is no such thing as an unnamed class template.
863 if (!Name) {
864 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000865 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000866 }
867
868 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000869 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000870 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000871 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000872 if (SS.isNotEmpty() && !SS.isInvalid()) {
873 SemanticContext = computeDeclContext(SS, true);
874 if (!SemanticContext) {
Douglas Gregor957ff272012-03-18 00:15:42 +0000875 Diag(NameLoc, diag::err_template_qualified_declarator_no_match)
876 << SS.getScopeRep() << SS.getRange();
Douglas Gregor05396e22009-08-25 17:23:04 +0000877 return true;
878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
John McCall77bb1aa2010-05-01 00:40:08 +0000880 if (RequireCompleteDeclContext(SS, SemanticContext))
881 return true;
882
Douglas Gregor20606502011-10-14 15:31:12 +0000883 // If we're adding a template to a dependent context, we may need to
884 // rebuilding some of the types used within the template parameter list,
885 // now that we know what the current instantiation is.
886 if (SemanticContext->isDependentContext()) {
887 ContextRAII SavedContext(*this, SemanticContext);
888 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
889 Invalid = true;
Douglas Gregor69605872012-03-28 16:01:27 +0000890 } else if (TUK != TUK_Friend && TUK != TUK_Reference)
891 diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc);
Douglas Gregor20606502011-10-14 15:31:12 +0000892
John McCalla24dc2e2009-11-17 02:14:36 +0000893 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000894 } else {
895 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000896 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000897 }
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregor57265e32010-04-12 16:00:01 +0000899 if (Previous.isAmbiguous())
900 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000901
Douglas Gregorddc29e12009-02-06 22:42:48 +0000902 NamedDecl *PrevDecl = 0;
903 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000904 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000905
Douglas Gregorddc29e12009-02-06 22:42:48 +0000906 // If there is a previous declaration with the same name, check
907 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000908 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000909 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000910
911 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000912 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000913 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000914 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000915 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
916 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000917 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000918 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
919 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
920 PrevClassTemplate
921 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
922 ->getSpecializedTemplate();
923 }
924 }
925
John McCall65c49462009-12-18 11:25:59 +0000926 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000927 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000928 // [...] When looking for a prior declaration of a class or a function
929 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000930 // function is neither a qualified name nor a template-id, scopes outside
931 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000932 if (!SS.isSet()) {
933 DeclContext *OutermostContext = CurContext;
934 while (!OutermostContext->isFileContext())
935 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000936
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000937 if (PrevDecl &&
938 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
939 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
940 SemanticContext = PrevDecl->getDeclContext();
941 } else {
942 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000943 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000944 // declaration.
945 PrevDecl = PrevClassTemplate = 0;
946 SemanticContext = OutermostContext;
947 }
John McCalle129d442009-12-17 23:21:11 +0000948 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000949
John McCalle129d442009-12-17 23:21:11 +0000950 if (CurContext->isDependentContext()) {
951 // If this is a dependent context, we don't want to link the friend
952 // class template to the template in scope, because that would perform
953 // checking of the template parameter lists that can't be performed
954 // until the outer context is instantiated.
955 PrevDecl = PrevClassTemplate = 0;
956 }
957 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
958 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000959
Douglas Gregorddc29e12009-02-06 22:42:48 +0000960 if (PrevClassTemplate) {
961 // Ensure that the template parameter lists are compatible.
962 if (!TemplateParameterListsAreEqual(TemplateParams,
963 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000964 /*Complain=*/true,
965 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000966 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000967
968 // C++ [temp.class]p4:
969 // In a redeclaration, partial specialization, explicit
970 // specialization or explicit instantiation of a class template,
971 // the class-key shall agree in kind with the original class
972 // template declaration (7.1.5.3).
973 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000974 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
975 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000976 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000977 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000978 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000979 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000980 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000981 }
982
Douglas Gregorddc29e12009-02-06 22:42:48 +0000983 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000984 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000985 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000986 Diag(NameLoc, diag::err_redefinition) << Name;
987 Diag(Def->getLocation(), diag::note_previous_definition);
988 // FIXME: Would it make sense to try to "forget" the previous
989 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000990 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000991 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000992 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000993 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
994 // Maybe we will complain about the shadowed template parameter.
995 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
996 // Just pretend that we didn't see the previous declaration.
997 PrevDecl = 0;
998 } else if (PrevDecl) {
999 // C++ [temp]p5:
1000 // A class template shall not have the same name as any other
1001 // template, class, function, object, enumeration, enumerator,
1002 // namespace, or type in the same scope (3.3), except as specified
1003 // in (14.5.4).
1004 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
1005 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +00001006 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001007 }
1008
Douglas Gregord684b002009-02-10 19:49:53 +00001009 // Check the template parameter list of this declaration, possibly
1010 // merging in the template parameter list from the previous class
1011 // template declaration.
1012 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001013 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +00001014 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +00001015 SemanticContext->isRecord() &&
1016 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +00001017 ? TPC_ClassTemplateMember
1018 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +00001019 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Douglas Gregor57265e32010-04-12 16:00:01 +00001021 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001022 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +00001023 // template out-of-line.
1024 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001025 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +00001026 Diag(NameLoc, diag::err_member_def_does_not_match)
1027 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001028 Invalid = true;
1029 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001030 }
1031
Mike Stump1eb44332009-09-09 15:08:12 +00001032 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001033 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001034 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001035 PrevClassTemplate->getTemplatedDecl() : 0,
1036 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001037 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001038 if (NumOuterTemplateParamLists > 0)
1039 NewClass->setTemplateParameterListsInfo(Context,
1040 NumOuterTemplateParamLists,
1041 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001042
Eli Friedman572ae0a2012-02-10 02:02:21 +00001043 // Add alignment attributes if necessary; these attributes are checked when
1044 // the ASTContext lays out the structure.
1045 AddAlignmentAttributesForRecord(NewClass);
1046 AddMsStructLayoutForRecord(NewClass);
1047
Douglas Gregorddc29e12009-02-06 22:42:48 +00001048 ClassTemplateDecl *NewTemplate
1049 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1050 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001051 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001052 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001053
Douglas Gregor2ccd89c2011-12-20 18:11:52 +00001054 if (ModulePrivateLoc.isValid())
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001055 NewTemplate->setModulePrivate();
Douglas Gregor8d267c52011-09-09 02:06:17 +00001056
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001057 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001058 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001059 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001060 assert(T->isDependentType() && "Class template type is not dependent?");
1061 (void)T;
1062
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001063 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001064 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001065 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001066 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1067 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001068
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001069 // Set the access specifier.
Douglas Gregor42acead2012-03-17 23:06:31 +00001070 if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord())
John McCall05b23ea2009-09-14 21:59:20 +00001071 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Douglas Gregorddc29e12009-02-06 22:42:48 +00001073 // Set the lexical context of these templates
1074 NewClass->setLexicalDeclContext(CurContext);
1075 NewTemplate->setLexicalDeclContext(CurContext);
1076
John McCall0f434ec2009-07-31 02:45:11 +00001077 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001078 NewClass->startDefinition();
1079
1080 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001081 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001082
John McCall05b23ea2009-09-14 21:59:20 +00001083 if (TUK != TUK_Friend)
1084 PushOnScopeChains(NewTemplate, S);
1085 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001086 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001087 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001088 NewClass->setAccess(PrevClassTemplate->getAccess());
1089 }
John McCall05b23ea2009-09-14 21:59:20 +00001090
Douglas Gregord85bea22009-09-26 06:47:28 +00001091 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1092 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001093
John McCall05b23ea2009-09-14 21:59:20 +00001094 // Friend templates are visible in fairly strange ways.
1095 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001096 DeclContext *DC = SemanticContext->getRedeclContext();
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001097 DC->makeDeclVisibleInContext(NewTemplate);
John McCall05b23ea2009-09-14 21:59:20 +00001098 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1099 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001100 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001101 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001102
Douglas Gregord85bea22009-09-26 06:47:28 +00001103 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1104 NewClass->getLocation(),
1105 NewTemplate,
1106 /*FIXME:*/NewClass->getLocation());
1107 Friend->setAccess(AS_public);
1108 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001109 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001110
Douglas Gregord684b002009-02-10 19:49:53 +00001111 if (Invalid) {
1112 NewTemplate->setInvalidDecl();
1113 NewClass->setInvalidDecl();
1114 }
John McCalld226f652010-08-21 09:40:31 +00001115 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001116}
1117
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001118/// \brief Diagnose the presence of a default template argument on a
1119/// template parameter, which is ill-formed in certain contexts.
1120///
1121/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001122static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001123 Sema::TemplateParamListContext TPC,
1124 SourceLocation ParamLoc,
1125 SourceRange DefArgRange) {
1126 switch (TPC) {
1127 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001128 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001129 return false;
1130
1131 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001132 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001133 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001134 // A default template-argument shall not be specified in a
1135 // function template declaration or a function template
1136 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001137 // If a friend function template declaration specifies a default
1138 // template-argument, that declaration shall be a definition and shall be
1139 // the only declaration of the function template in the translation unit.
1140 // (C++98/03 doesn't have this wording; see DR226).
David Blaikie4e4d0842012-03-11 07:00:24 +00001141 S.Diag(ParamLoc, S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00001142 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1143 : diag::ext_template_parameter_default_in_function_template)
1144 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001145 return false;
1146
1147 case Sema::TPC_ClassTemplateMember:
1148 // C++0x [temp.param]p9:
1149 // A default template-argument shall not be specified in the
1150 // template-parameter-lists of the definition of a member of a
1151 // class template that appears outside of the member's class.
1152 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1153 << DefArgRange;
1154 return true;
1155
1156 case Sema::TPC_FriendFunctionTemplate:
1157 // C++ [temp.param]p9:
1158 // A default template-argument shall not be specified in a
1159 // friend template declaration.
1160 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1161 << DefArgRange;
1162 return true;
1163
1164 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1165 // for friend function templates if there is only a single
1166 // declaration (and it is a definition). Strange!
1167 }
1168
David Blaikie7530c032012-01-17 06:56:22 +00001169 llvm_unreachable("Invalid TemplateParamListContext!");
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001170}
1171
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001172/// \brief Check for unexpanded parameter packs within the template parameters
1173/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001174static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1175 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001176 TemplateParameterList *Params = TTP->getTemplateParameters();
1177 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1178 NamedDecl *P = Params->getParam(I);
1179 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001180 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001181 NTTP->getTypeSourceInfo(),
1182 Sema::UPPC_NonTypeTemplateParameterType))
1183 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001184
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001185 continue;
1186 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001187
1188 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001189 = dyn_cast<TemplateTemplateParmDecl>(P))
1190 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1191 return true;
1192 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001193
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001194 return false;
1195}
1196
Douglas Gregord684b002009-02-10 19:49:53 +00001197/// \brief Checks the validity of a template parameter list, possibly
1198/// considering the template parameter list from a previous
1199/// declaration.
1200///
1201/// If an "old" template parameter list is provided, it must be
1202/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1203/// template parameter list.
1204///
1205/// \param NewParams Template parameter list for a new template
1206/// declaration. This template parameter list will be updated with any
1207/// default arguments that are carried through from the previous
1208/// template parameter list.
1209///
1210/// \param OldParams If provided, template parameter list from a
1211/// previous declaration of the same template. Default template
1212/// arguments will be merged from the old template parameter list to
1213/// the new template parameter list.
1214///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001215/// \param TPC Describes the context in which we are checking the given
1216/// template parameter list.
1217///
Douglas Gregord684b002009-02-10 19:49:53 +00001218/// \returns true if an error occurred, false otherwise.
1219bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001220 TemplateParameterList *OldParams,
1221 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001222 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregord684b002009-02-10 19:49:53 +00001224 // C++ [temp.param]p10:
1225 // The set of default template-arguments available for use with a
1226 // template declaration or definition is obtained by merging the
1227 // default arguments from the definition (if in scope) and all
1228 // declarations in scope in the same way default function
1229 // arguments are (8.3.6).
1230 bool SawDefaultArgument = false;
1231 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001232
Mike Stump1a35fde2009-02-11 23:03:27 +00001233 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001234 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001235 if (OldParams)
1236 OldParam = OldParams->begin();
1237
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001238 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001239 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1240 NewParamEnd = NewParams->end();
1241 NewParam != NewParamEnd; ++NewParam) {
1242 // Variables used to diagnose redundant default arguments
1243 bool RedundantDefaultArg = false;
1244 SourceLocation OldDefaultLoc;
1245 SourceLocation NewDefaultLoc;
1246
David Blaikie1368e582011-10-19 05:19:50 +00001247 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001248 bool MissingDefaultArg = false;
1249
David Blaikie1368e582011-10-19 05:19:50 +00001250 // Variable used to diagnose non-final parameter packs
1251 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001252
Douglas Gregord684b002009-02-10 19:49:53 +00001253 if (TemplateTypeParmDecl *NewTypeParm
1254 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001255 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001256 if (NewTypeParm->hasDefaultArgument() &&
1257 DiagnoseDefaultTemplateArgument(*this, TPC,
1258 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001259 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001260 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001261 NewTypeParm->removeDefaultArgument();
1262
1263 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001264 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001265 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Anders Carlsson49d25572009-06-12 23:20:15 +00001267 if (NewTypeParm->isParameterPack()) {
1268 assert(!NewTypeParm->hasDefaultArgument() &&
1269 "Parameter packs can't have a default argument!");
1270 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001271 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001272 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001273 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1274 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1275 SawDefaultArgument = true;
1276 RedundantDefaultArg = true;
1277 PreviousDefaultArgLoc = NewDefaultLoc;
1278 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1279 // Merge the default argument from the old declaration to the
1280 // new declaration.
1281 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001282 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001283 true);
1284 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1285 } else if (NewTypeParm->hasDefaultArgument()) {
1286 SawDefaultArgument = true;
1287 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1288 } else if (SawDefaultArgument)
1289 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001290 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001291 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001292 // Check for unexpanded parameter packs.
1293 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001294 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001295 UPPC_NonTypeTemplateParameterType)) {
1296 Invalid = true;
1297 continue;
1298 }
1299
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001300 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001301 if (NewNonTypeParm->hasDefaultArgument() &&
1302 DiagnoseDefaultTemplateArgument(*this, TPC,
1303 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001304 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001305 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001306 }
1307
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001308 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001309 NonTypeTemplateParmDecl *OldNonTypeParm
1310 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001311 if (NewNonTypeParm->isParameterPack()) {
1312 assert(!NewNonTypeParm->hasDefaultArgument() &&
1313 "Parameter packs can't have a default argument!");
1314 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001315 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001316 NewNonTypeParm->hasDefaultArgument()) {
1317 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1318 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1319 SawDefaultArgument = true;
1320 RedundantDefaultArg = true;
1321 PreviousDefaultArgLoc = NewDefaultLoc;
1322 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1323 // Merge the default argument from the old declaration to the
1324 // new declaration.
1325 SawDefaultArgument = true;
1326 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001327 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001328 // parameter.
1329 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001330 OldNonTypeParm->getDefaultArgument(),
1331 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001332 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1333 } else if (NewNonTypeParm->hasDefaultArgument()) {
1334 SawDefaultArgument = true;
1335 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1336 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001337 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001338 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001339 TemplateTemplateParmDecl *NewTemplateParm
1340 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001341
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001342 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001343 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001344 Invalid = true;
1345 continue;
1346 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001347
David Blaikie1368e582011-10-19 05:19:50 +00001348 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001349 if (NewTemplateParm->hasDefaultArgument() &&
1350 DiagnoseDefaultTemplateArgument(*this, TPC,
1351 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001352 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001353 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001354
1355 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001356 TemplateTemplateParmDecl *OldTemplateParm
1357 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001358 if (NewTemplateParm->isParameterPack()) {
1359 assert(!NewTemplateParm->hasDefaultArgument() &&
1360 "Parameter packs can't have a default argument!");
1361 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001362 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001363 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001364 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1365 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001366 SawDefaultArgument = true;
1367 RedundantDefaultArg = true;
1368 PreviousDefaultArgLoc = NewDefaultLoc;
1369 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1370 // Merge the default argument from the old declaration to the
1371 // new declaration.
1372 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001373 // FIXME: We need to create a new kind of "default argument" expression
1374 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001375 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001376 OldTemplateParm->getDefaultArgument(),
1377 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001378 PreviousDefaultArgLoc
1379 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001380 } else if (NewTemplateParm->hasDefaultArgument()) {
1381 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001382 PreviousDefaultArgLoc
1383 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001384 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001385 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001386 }
1387
David Blaikie1368e582011-10-19 05:19:50 +00001388 // C++0x [temp.param]p11:
1389 // If a template parameter of a primary class template or alias template
1390 // is a template parameter pack, it shall be the last template parameter.
1391 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1392 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1393 Diag((*NewParam)->getLocation(),
1394 diag::err_template_param_pack_must_be_last_template_parameter);
1395 Invalid = true;
1396 }
1397
Douglas Gregord684b002009-02-10 19:49:53 +00001398 if (RedundantDefaultArg) {
1399 // C++ [temp.param]p12:
1400 // A template-parameter shall not be given default arguments
1401 // by two different declarations in the same scope.
1402 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1403 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1404 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001405 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001406 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001407 // If a template-parameter of a class template has a default
1408 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001409 // have a default template-argument supplied or be a template parameter
1410 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001411 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001412 diag::err_template_param_default_arg_missing);
1413 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1414 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001415 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001416 }
1417
1418 // If we have an old template parameter list that we're merging
1419 // in, move on to the next parameter.
1420 if (OldParams)
1421 ++OldParam;
1422 }
1423
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001424 // We were missing some default arguments at the end of the list, so remove
1425 // all of the default arguments.
1426 if (RemoveDefaultArguments) {
1427 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1428 NewParamEnd = NewParams->end();
1429 NewParam != NewParamEnd; ++NewParam) {
1430 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1431 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001432 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001433 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1434 NTTP->removeDefaultArgument();
1435 else
1436 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1437 }
1438 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001439
Douglas Gregord684b002009-02-10 19:49:53 +00001440 return Invalid;
1441}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001442
John McCall4e2cbb22010-10-20 05:44:58 +00001443namespace {
1444
1445/// A class which looks for a use of a certain level of template
1446/// parameter.
1447struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1448 typedef RecursiveASTVisitor<DependencyChecker> super;
1449
1450 unsigned Depth;
1451 bool Match;
1452
1453 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1454 NamedDecl *ND = Params->getParam(0);
1455 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1456 Depth = PD->getDepth();
1457 } else if (NonTypeTemplateParmDecl *PD =
1458 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1459 Depth = PD->getDepth();
1460 } else {
1461 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1462 }
1463 }
1464
1465 bool Matches(unsigned ParmDepth) {
1466 if (ParmDepth >= Depth) {
1467 Match = true;
1468 return true;
1469 }
1470 return false;
1471 }
1472
1473 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1474 return !Matches(T->getDepth());
1475 }
1476
1477 bool TraverseTemplateName(TemplateName N) {
1478 if (TemplateTemplateParmDecl *PD =
1479 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1480 if (Matches(PD->getDepth())) return false;
1481 return super::TraverseTemplateName(N);
1482 }
1483
1484 bool VisitDeclRefExpr(DeclRefExpr *E) {
1485 if (NonTypeTemplateParmDecl *PD =
1486 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1487 if (PD->getDepth() == Depth) {
1488 Match = true;
1489 return false;
1490 }
1491 }
1492 return super::VisitDeclRefExpr(E);
1493 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001494
1495 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1496 return TraverseType(T->getInjectedSpecializationType());
1497 }
John McCall4e2cbb22010-10-20 05:44:58 +00001498};
1499}
1500
Douglas Gregorc8406492011-05-10 18:27:06 +00001501/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001502/// list.
1503static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001504DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001505 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001506 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001507 return Checker.Match;
1508}
1509
Douglas Gregorc8406492011-05-10 18:27:06 +00001510// Find the source range corresponding to the named type in the given
1511// nested-name-specifier, if any.
1512static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1513 QualType T,
1514 const CXXScopeSpec &SS) {
1515 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1516 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1517 if (const Type *CurType = NNS->getAsType()) {
1518 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1519 return NNSLoc.getTypeLoc().getSourceRange();
1520 } else
1521 break;
1522
1523 NNSLoc = NNSLoc.getPrefix();
1524 }
1525
1526 return SourceRange();
1527}
1528
Mike Stump1eb44332009-09-09 15:08:12 +00001529/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001530/// specifier, returning the template parameter list that applies to the
1531/// name.
1532///
1533/// \param DeclStartLoc the start of the declaration that has a scope
1534/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001535///
Douglas Gregorc8406492011-05-10 18:27:06 +00001536/// \param DeclLoc The location of the declaration itself.
1537///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001538/// \param SS the scope specifier that will be matched to the given template
1539/// parameter lists. This scope specifier precedes a qualified name that is
1540/// being declared.
1541///
1542/// \param ParamLists the template parameter lists, from the outermost to the
1543/// innermost template parameter lists.
1544///
1545/// \param NumParamLists the number of template parameter lists in ParamLists.
1546///
John McCall77e8b112010-04-13 20:37:33 +00001547/// \param IsFriend Whether to apply the slightly different rules for
1548/// matching template parameters to scope specifiers in friend
1549/// declarations.
1550///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001551/// \param IsExplicitSpecialization will be set true if the entity being
1552/// declared is an explicit specialization, false otherwise.
1553///
Mike Stump1eb44332009-09-09 15:08:12 +00001554/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001555/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001556/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001557/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001558/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001559/// itself a template).
1560TemplateParameterList *
1561Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001562 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001563 const CXXScopeSpec &SS,
1564 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001565 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001566 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001567 bool &IsExplicitSpecialization,
1568 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001569 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001570 Invalid = false;
1571
1572 // The sequence of nested types to which we will match up the template
1573 // parameter lists. We first build this list by starting with the type named
1574 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001575 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001576 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001577 if (SS.getScopeRep()) {
1578 if (CXXRecordDecl *Record
1579 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1580 T = Context.getTypeDeclType(Record);
1581 else
1582 T = QualType(SS.getScopeRep()->getAsType(), 0);
1583 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001584
1585 // If we found an explicit specialization that prevents us from needing
1586 // 'template<>' headers, this will be set to the location of that
1587 // explicit specialization.
1588 SourceLocation ExplicitSpecLoc;
1589
1590 while (!T.isNull()) {
1591 NestedTypes.push_back(T);
1592
1593 // Retrieve the parent of a record type.
1594 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1595 // If this type is an explicit specialization, we're done.
1596 if (ClassTemplateSpecializationDecl *Spec
1597 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1598 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1599 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1600 ExplicitSpecLoc = Spec->getLocation();
1601 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001602 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001603 } else if (Record->getTemplateSpecializationKind()
1604 == TSK_ExplicitSpecialization) {
1605 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001606 break;
1607 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001608
1609 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1610 T = Context.getTypeDeclType(Parent);
1611 else
1612 T = QualType();
1613 continue;
1614 }
1615
1616 if (const TemplateSpecializationType *TST
1617 = T->getAs<TemplateSpecializationType>()) {
1618 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1619 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1620 T = Context.getTypeDeclType(Parent);
1621 else
1622 T = QualType();
1623 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001624 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001625 }
1626
1627 // Look one step prior in a dependent template specialization type.
1628 if (const DependentTemplateSpecializationType *DependentTST
1629 = T->getAs<DependentTemplateSpecializationType>()) {
1630 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1631 T = QualType(NNS->getAsType(), 0);
1632 else
1633 T = QualType();
1634 continue;
1635 }
1636
1637 // Look one step prior in a dependent name type.
1638 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1639 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1640 T = QualType(NNS->getAsType(), 0);
1641 else
1642 T = QualType();
1643 continue;
1644 }
1645
1646 // Retrieve the parent of an enumeration type.
1647 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1648 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1649 // check here.
1650 EnumDecl *Enum = EnumT->getDecl();
1651
1652 // Get to the parent type.
1653 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1654 T = Context.getTypeDeclType(Parent);
1655 else
1656 T = QualType();
1657 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001658 }
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Douglas Gregorc8406492011-05-10 18:27:06 +00001660 T = QualType();
1661 }
1662 // Reverse the nested types list, since we want to traverse from the outermost
1663 // to the innermost while checking template-parameter-lists.
1664 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001665
Douglas Gregorc8406492011-05-10 18:27:06 +00001666 // C++0x [temp.expl.spec]p17:
1667 // A member or a member template may be nested within many
1668 // enclosing class templates. In an explicit specialization for
1669 // such a member, the member declaration shall be preceded by a
1670 // template<> for each enclosing class template that is
1671 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001672 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001673 unsigned ParamIdx = 0;
1674 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1675 ++TypeIdx) {
1676 T = NestedTypes[TypeIdx];
1677
1678 // Whether we expect a 'template<>' header.
1679 bool NeedEmptyTemplateHeader = false;
1680
1681 // Whether we expect a template header with parameters.
1682 bool NeedNonemptyTemplateHeader = false;
1683
1684 // For a dependent type, the set of template parameters that we
1685 // expect to see.
1686 TemplateParameterList *ExpectedTemplateParams = 0;
1687
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001688 // C++0x [temp.expl.spec]p15:
1689 // A member or a member template may be nested within many enclosing
1690 // class templates. In an explicit specialization for such a member, the
1691 // member declaration shall be preceded by a template<> for each
1692 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001693 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1694 if (ClassTemplatePartialSpecializationDecl *Partial
1695 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1696 ExpectedTemplateParams = Partial->getTemplateParameters();
1697 NeedNonemptyTemplateHeader = true;
1698 } else if (Record->isDependentType()) {
1699 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001700 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001701 ->getTemplateParameters();
1702 NeedNonemptyTemplateHeader = true;
1703 }
1704 } else if (ClassTemplateSpecializationDecl *Spec
1705 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1706 // C++0x [temp.expl.spec]p4:
1707 // Members of an explicitly specialized class template are defined
1708 // in the same manner as members of normal classes, and not using
1709 // the template<> syntax.
1710 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1711 NeedEmptyTemplateHeader = true;
1712 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001713 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001714 } else if (Record->getTemplateSpecializationKind()) {
1715 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001716 != TSK_ExplicitSpecialization &&
1717 TypeIdx == NumTypes - 1)
1718 IsExplicitSpecialization = true;
1719
1720 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001721 }
1722 } else if (const TemplateSpecializationType *TST
1723 = T->getAs<TemplateSpecializationType>()) {
1724 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1725 ExpectedTemplateParams = Template->getTemplateParameters();
1726 NeedNonemptyTemplateHeader = true;
1727 }
1728 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1729 // FIXME: We actually could/should check the template arguments here
1730 // against the corresponding template parameter list.
1731 NeedNonemptyTemplateHeader = false;
1732 }
1733
Douglas Gregor89b9f102011-06-06 15:22:55 +00001734 // C++ [temp.expl.spec]p16:
1735 // In an explicit specialization declaration for a member of a class
1736 // template or a member template that ap- pears in namespace scope, the
1737 // member template and some of its enclosing class templates may remain
1738 // unspecialized, except that the declaration shall not explicitly
1739 // specialize a class member template if its en- closing class templates
1740 // are not explicitly specialized as well.
1741 if (ParamIdx < NumParamLists) {
1742 if (ParamLists[ParamIdx]->size() == 0) {
1743 if (SawNonEmptyTemplateParameterList) {
1744 Diag(DeclLoc, diag::err_specialize_member_of_template)
1745 << ParamLists[ParamIdx]->getSourceRange();
1746 Invalid = true;
1747 IsExplicitSpecialization = false;
1748 return 0;
1749 }
1750 } else
1751 SawNonEmptyTemplateParameterList = true;
1752 }
1753
Douglas Gregorc8406492011-05-10 18:27:06 +00001754 if (NeedEmptyTemplateHeader) {
1755 // If we're on the last of the types, and we need a 'template<>' header
1756 // here, then it's an explicit specialization.
1757 if (TypeIdx == NumTypes - 1)
1758 IsExplicitSpecialization = true;
1759
1760 if (ParamIdx < NumParamLists) {
1761 if (ParamLists[ParamIdx]->size() > 0) {
1762 // The header has template parameters when it shouldn't. Complain.
1763 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1764 diag::err_template_param_list_matches_nontemplate)
1765 << T
1766 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1767 ParamLists[ParamIdx]->getRAngleLoc())
1768 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1769 Invalid = true;
1770 return 0;
1771 }
1772
1773 // Consume this template header.
1774 ++ParamIdx;
1775 continue;
1776 }
1777
1778 if (!IsFriend) {
1779 // We don't have a template header, but we should.
1780 SourceLocation ExpectedTemplateLoc;
1781 if (NumParamLists > 0)
1782 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1783 else
1784 ExpectedTemplateLoc = DeclStartLoc;
1785
1786 Diag(DeclLoc, diag::err_template_spec_needs_header)
1787 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1788 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1789 }
1790
1791 continue;
1792 }
1793
1794 if (NeedNonemptyTemplateHeader) {
1795 // In friend declarations we can have template-ids which don't
1796 // depend on the corresponding template parameter lists. But
1797 // assume that empty parameter lists are supposed to match this
1798 // template-id.
1799 if (IsFriend && T->isDependentType()) {
1800 if (ParamIdx < NumParamLists &&
1801 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1802 ExpectedTemplateParams = 0;
1803 else
1804 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001805 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001806
Douglas Gregorc8406492011-05-10 18:27:06 +00001807 if (ParamIdx < NumParamLists) {
1808 // Check the template parameter list, if we can.
1809 if (ExpectedTemplateParams &&
1810 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1811 ExpectedTemplateParams,
1812 true, TPL_TemplateMatch))
1813 Invalid = true;
1814
1815 if (!Invalid &&
1816 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1817 TPC_ClassTemplateMember))
1818 Invalid = true;
1819
1820 ++ParamIdx;
1821 continue;
1822 }
1823
1824 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1825 << T
1826 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1827 Invalid = true;
1828 continue;
1829 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001830 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001831
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001832 // If there were at least as many template-ids as there were template
1833 // parameter lists, then there are no template parameter lists remaining for
1834 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001835 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001836 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001838 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001839 if (ParamIdx < NumParamLists - 1) {
1840 bool HasAnyExplicitSpecHeader = false;
1841 bool AllExplicitSpecHeaders = true;
1842 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1843 if (ParamLists[I]->size() == 0)
1844 HasAnyExplicitSpecHeader = true;
1845 else
1846 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001847 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001848
1849 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1850 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1851 : diag::err_template_spec_extra_headers)
1852 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1853 ParamLists[NumParamLists - 2]->getRAngleLoc());
1854
1855 // If there was a specialization somewhere, such that 'template<>' is
1856 // not required, and there were any 'template<>' headers, note where the
1857 // specialization occurred.
1858 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1859 Diag(ExplicitSpecLoc,
1860 diag::note_explicit_template_spec_does_not_need_header)
1861 << NestedTypes.back();
1862
1863 // We have a template parameter list with no corresponding scope, which
1864 // means that the resulting template declaration can't be instantiated
1865 // properly (we'll end up with dependent nodes when we shouldn't).
1866 if (!AllExplicitSpecHeaders)
1867 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001868 }
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Douglas Gregor89b9f102011-06-06 15:22:55 +00001870 // C++ [temp.expl.spec]p16:
1871 // In an explicit specialization declaration for a member of a class
1872 // template or a member template that ap- pears in namespace scope, the
1873 // member template and some of its enclosing class templates may remain
1874 // unspecialized, except that the declaration shall not explicitly
1875 // specialize a class member template if its en- closing class templates
1876 // are not explicitly specialized as well.
1877 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1878 SawNonEmptyTemplateParameterList) {
1879 Diag(DeclLoc, diag::err_specialize_member_of_template)
1880 << ParamLists[ParamIdx]->getSourceRange();
1881 Invalid = true;
1882 IsExplicitSpecialization = false;
1883 return 0;
1884 }
1885
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001886 // Return the last template parameter list, which corresponds to the
1887 // entity being declared.
1888 return ParamLists[NumParamLists - 1];
1889}
1890
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001891void Sema::NoteAllFoundTemplates(TemplateName Name) {
1892 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1893 Diag(Template->getLocation(), diag::note_template_declared_here)
1894 << (isa<FunctionTemplateDecl>(Template)? 0
1895 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001896 : isa<TypeAliasTemplateDecl>(Template)? 2
1897 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001898 << Template->getDeclName();
1899 return;
1900 }
1901
1902 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1903 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1904 IEnd = OST->end();
1905 I != IEnd; ++I)
1906 Diag((*I)->getLocation(), diag::note_template_declared_here)
1907 << 0 << (*I)->getDeclName();
1908
1909 return;
1910 }
1911}
1912
Douglas Gregor7532dc62009-03-30 22:58:21 +00001913QualType Sema::CheckTemplateIdType(TemplateName Name,
1914 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001915 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001916 DependentTemplateName *DTN
1917 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001918 if (DTN && DTN->isIdentifier())
1919 // When building a template-id where the template-name is dependent,
1920 // assume the template is a type template. Either our assumption is
1921 // correct, or the code is ill-formed and will be diagnosed when the
1922 // dependent name is substituted.
1923 return Context.getDependentTemplateSpecializationType(ETK_None,
1924 DTN->getQualifier(),
1925 DTN->getIdentifier(),
1926 TemplateArgs);
1927
Douglas Gregor7532dc62009-03-30 22:58:21 +00001928 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001929 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1930 // We might have a substituted template template parameter pack. If so,
1931 // build a template specialization type for it.
1932 if (Name.getAsSubstTemplateTemplateParmPack())
1933 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001934
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001935 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1936 << Name;
1937 NoteAllFoundTemplates(Name);
1938 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001939 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001940
Douglas Gregor40808ce2009-03-09 23:48:35 +00001941 // Check that the template argument list is well-formed for this
1942 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001943 SmallVector<TemplateArgument, 4> Converted;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001944 bool ExpansionIntoFixedList = false;
John McCalld5532b62009-11-23 01:53:49 +00001945 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00001946 false, Converted, &ExpansionIntoFixedList))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001947 return QualType();
1948
Douglas Gregor40808ce2009-03-09 23:48:35 +00001949 QualType CanonType;
1950
Douglas Gregor561f8122011-07-01 01:22:09 +00001951 bool InstantiationDependent = false;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001952 TypeAliasTemplateDecl *AliasTemplate = 0;
1953 if (!ExpansionIntoFixedList &&
1954 (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001955 // Find the canonical type for this type alias template specialization.
1956 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1957 if (Pattern->isInvalidDecl())
1958 return QualType();
1959
1960 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1961 Converted.data(), Converted.size());
1962
1963 // Only substitute for the innermost template argument list.
1964 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001965 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001966 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1967 for (unsigned I = 0; I < Depth; ++I)
1968 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001969
1970 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1971 CanonType = SubstType(Pattern->getUnderlyingType(),
1972 TemplateArgLists, AliasTemplate->getLocation(),
1973 AliasTemplate->getDeclName());
1974 if (CanonType.isNull())
1975 return QualType();
1976 } else if (Name.isDependent() ||
1977 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001978 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001979 // This class template specialization is a dependent
1980 // type. Therefore, its canonical type is another class template
1981 // specialization type that contains all of the converted
1982 // arguments in canonical form. This ensures that, e.g., A<T> and
1983 // A<T, T> have identical types when A is declared as:
1984 //
1985 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001986 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001987 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001988 Converted.data(),
1989 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Douglas Gregor1275ae02009-07-28 23:00:59 +00001991 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001992 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001993 // In the future, we need to teach getTemplateSpecializationType to only
1994 // build the canonical type and return that to us.
1995 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001996
1997 // This might work out to be a current instantiation, in which
1998 // case the canonical type needs to be the InjectedClassNameType.
1999 //
2000 // TODO: in theory this could be a simple hashtable lookup; most
2001 // changes to CurContext don't change the set of current
2002 // instantiations.
2003 if (isa<ClassTemplateDecl>(Template)) {
2004 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
2005 // If we get out to a namespace, we're done.
2006 if (Ctx->isFileContext()) break;
2007
2008 // If this isn't a record, keep looking.
2009 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
2010 if (!Record) continue;
2011
2012 // Look for one of the two cases with InjectedClassNameTypes
2013 // and check whether it's the same template.
2014 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
2015 !Record->getDescribedClassTemplate())
2016 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002017
John McCall31f17ec2010-04-27 00:57:59 +00002018 // Fetch the injected class name type and check whether its
2019 // injected type is equal to the type we just built.
2020 QualType ICNT = Context.getTypeDeclType(Record);
2021 QualType Injected = cast<InjectedClassNameType>(ICNT)
2022 ->getInjectedSpecializationType();
2023
2024 if (CanonType != Injected->getCanonicalTypeInternal())
2025 continue;
2026
2027 // If so, the canonical type of this TST is the injected
2028 // class name type of the record we just found.
2029 assert(ICNT.isCanonical());
2030 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002031 break;
2032 }
2033 }
Mike Stump1eb44332009-09-09 15:08:12 +00002034 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002035 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002036 // Find the class template specialization declaration that
2037 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002038 void *InsertPos = 0;
2039 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002040 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002041 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002042 if (!Decl) {
2043 // This is the first time we have referenced this class template
2044 // specialization. Create the canonical declaration and add it to
2045 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002046 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002047 ClassTemplate->getTemplatedDecl()->getTagKind(),
2048 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002049 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002050 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002051 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002052 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002053 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002054 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002055 Decl->setLexicalDeclContext(CurContext);
2056 }
2057
2058 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002059 assert(isa<RecordType>(CanonType) &&
2060 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002061 }
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Douglas Gregor40808ce2009-03-09 23:48:35 +00002063 // Build the fully-sugared type for this class template
2064 // specialization, which refers back to the class template
2065 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002066 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002067}
2068
John McCallf312b1e2010-08-26 23:41:50 +00002069TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002070Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00002071 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002072 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002073 ASTTemplateArgsPtr TemplateArgsIn,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002074 SourceLocation RAngleLoc,
2075 bool IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002076 if (SS.isInvalid())
2077 return true;
2078
Douglas Gregor7532dc62009-03-30 22:58:21 +00002079 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002080
Douglas Gregor40808ce2009-03-09 23:48:35 +00002081 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002082 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002083 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002084
Douglas Gregora88f09f2011-02-28 17:23:35 +00002085 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002086 QualType T
2087 = Context.getDependentTemplateSpecializationType(ETK_None,
2088 DTN->getQualifier(),
2089 DTN->getIdentifier(),
2090 TemplateArgs);
2091 // Build type-source information.
Douglas Gregora88f09f2011-02-28 17:23:35 +00002092 TypeLocBuilder TLB;
2093 DependentTemplateSpecializationTypeLoc SpecTL
2094 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002095 SpecTL.setElaboratedKeywordLoc(SourceLocation());
2096 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002097 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002098 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002099 SpecTL.setLAngleLoc(LAngleLoc);
2100 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002101 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2102 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2103 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2104 }
2105
John McCalld5532b62009-11-23 01:53:49 +00002106 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002107 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002108
2109 if (Result.isNull())
2110 return true;
2111
Douglas Gregor059101f2011-03-02 00:47:37 +00002112 // Build type-source information.
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002113 TypeLocBuilder TLB;
Douglas Gregor059101f2011-03-02 00:47:37 +00002114 TemplateSpecializationTypeLoc SpecTL
2115 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002116 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002117 SpecTL.setTemplateNameLoc(TemplateLoc);
2118 SpecTL.setLAngleLoc(LAngleLoc);
2119 SpecTL.setRAngleLoc(RAngleLoc);
2120 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2121 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002122
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002123 // NOTE: avoid constructing an ElaboratedTypeLoc if this is a
2124 // constructor or destructor name (in such a case, the scope specifier
2125 // will be attached to the enclosing Decl or Expr node).
2126 if (SS.isNotEmpty() && !IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002127 // Create an elaborated-type-specifier containing the nested-name-specifier.
2128 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2129 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002130 ElabTL.setElaboratedKeywordLoc(SourceLocation());
Douglas Gregor059101f2011-03-02 00:47:37 +00002131 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2132 }
2133
2134 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002135}
John McCallf1bbbb42009-09-04 01:14:41 +00002136
Douglas Gregor059101f2011-03-02 00:47:37 +00002137TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002138 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002139 SourceLocation TagLoc,
2140 CXXScopeSpec &SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002141 SourceLocation TemplateKWLoc,
2142 TemplateTy TemplateD,
Douglas Gregor059101f2011-03-02 00:47:37 +00002143 SourceLocation TemplateLoc,
2144 SourceLocation LAngleLoc,
2145 ASTTemplateArgsPtr TemplateArgsIn,
2146 SourceLocation RAngleLoc) {
2147 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2148
2149 // Translate the parser's template argument list in our AST format.
2150 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2151 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2152
2153 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002154 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002155 ElaboratedTypeKeyword Keyword
2156 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002157
Douglas Gregor059101f2011-03-02 00:47:37 +00002158 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2159 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2160 DTN->getQualifier(),
2161 DTN->getIdentifier(),
2162 TemplateArgs);
2163
2164 // Build type-source information.
2165 TypeLocBuilder TLB;
2166 DependentTemplateSpecializationTypeLoc SpecTL
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002167 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2168 SpecTL.setElaboratedKeywordLoc(TagLoc);
2169 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002170 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002171 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002172 SpecTL.setLAngleLoc(LAngleLoc);
2173 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002174 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2175 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2176 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2177 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002178
2179 if (TypeAliasTemplateDecl *TAT =
2180 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2181 // C++0x [dcl.type.elab]p2:
2182 // If the identifier resolves to a typedef-name or the simple-template-id
2183 // resolves to an alias template specialization, the
2184 // elaborated-type-specifier is ill-formed.
2185 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2186 Diag(TAT->getLocation(), diag::note_declared_at);
2187 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002188
2189 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2190 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002191 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002192
2193 // Check the tag kind
2194 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002195 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002196
John McCall6b2becf2009-09-08 17:47:29 +00002197 IdentifierInfo *Id = D->getIdentifier();
2198 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002199
Richard Trieubbf34c02011-06-10 03:11:26 +00002200 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2201 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002202 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002203 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002204 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002205 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002206 }
2207 }
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002208
Douglas Gregor059101f2011-03-02 00:47:37 +00002209 // Provide source-location information for the template specialization.
2210 TypeLocBuilder TLB;
2211 TemplateSpecializationTypeLoc SpecTL
2212 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002213 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002214 SpecTL.setTemplateNameLoc(TemplateLoc);
2215 SpecTL.setLAngleLoc(LAngleLoc);
2216 SpecTL.setRAngleLoc(RAngleLoc);
2217 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2218 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002219
Douglas Gregor059101f2011-03-02 00:47:37 +00002220 // Construct an elaborated type containing the nested-name-specifier (if any)
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002221 // and tag keyword.
Douglas Gregor059101f2011-03-02 00:47:37 +00002222 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2223 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002224 ElabTL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002225 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2226 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002227}
2228
John McCall60d7b3a2010-08-24 06:29:42 +00002229ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002230 SourceLocation TemplateKWLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002231 LookupResult &R,
2232 bool RequiresADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002233 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002234 // FIXME: Can we do any checking at this point? I guess we could check the
2235 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002236 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002237 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002238 // foo<int> could identify a single function unambiguously
2239 // This approach does NOT work, since f<int>(1);
2240 // gets resolved prior to resorting to overload resolution
2241 // i.e., template<class T> void f(double);
2242 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002243
2244 // These should be filtered out by our callers.
2245 assert(!R.empty() && "empty lookup results when building templateid");
2246 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2247
John McCallc373d482010-01-27 01:50:18 +00002248 // We don't want lookup warnings at this point.
2249 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002250
John McCallf7a1a742009-11-24 19:00:30 +00002251 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002252 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002253 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002254 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002255 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002256 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002257 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002258
2259 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002260}
2261
John McCallf7a1a742009-11-24 19:00:30 +00002262// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002263ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002264Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002265 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002266 const DeclarationNameInfo &NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002267 const TemplateArgumentListInfo *TemplateArgs) {
2268 assert(TemplateArgs || TemplateKWLoc.isValid());
John McCallf7a1a742009-11-24 19:00:30 +00002269 DeclContext *DC;
2270 if (!(DC = computeDeclContext(SS, false)) ||
2271 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002272 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002273 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002274
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002275 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002276 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002277 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2278 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002279
John McCallf7a1a742009-11-24 19:00:30 +00002280 if (R.isAmbiguous())
2281 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002282
John McCallf7a1a742009-11-24 19:00:30 +00002283 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002284 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2285 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002286 return ExprError();
2287 }
2288
2289 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002290 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2291 << (NestedNameSpecifier*) SS.getScopeRep()
2292 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002293 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2294 return ExprError();
2295 }
2296
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002297 return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002298}
2299
Douglas Gregorc45c2322009-03-31 00:43:58 +00002300/// \brief Form a dependent template name.
2301///
2302/// This action forms a dependent template name given the template
2303/// name and its (presumably dependent) scope specifier. For
2304/// example, given "MetaFun::template apply", the scope specifier \p
2305/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2306/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002307TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002308 CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002309 SourceLocation TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002310 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002311 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002312 bool EnteringContext,
2313 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002314 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2315 Diag(TemplateKWLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00002316 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00002317 diag::warn_cxx98_compat_template_outside_of_template :
2318 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002319 << FixItHint::CreateRemoval(TemplateKWLoc);
2320
Douglas Gregor0707bc52010-01-19 16:01:07 +00002321 DeclContext *LookupCtx = 0;
2322 if (SS.isSet())
2323 LookupCtx = computeDeclContext(SS, EnteringContext);
2324 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002325 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002326 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002327 // C++0x [temp.names]p5:
2328 // If a name prefixed by the keyword template is not the name of
2329 // a template, the program is ill-formed. [Note: the keyword
2330 // template may not be applied to non-template members of class
2331 // templates. -end note ] [ Note: as is the case with the
2332 // typename prefix, the template prefix is allowed in cases
2333 // where it is not strictly necessary; i.e., when the
2334 // nested-name-specifier or the expression on the left of the ->
2335 // or . is not dependent on a template-parameter, or the use
2336 // does not appear in the scope of a template. -end note]
2337 //
2338 // Note: C++03 was more strict here, because it banned the use of
2339 // the "template" keyword prior to a template-name that was not a
2340 // dependent name. C++ DR468 relaxed this requirement (the
2341 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002342 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002343 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002344 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2345 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002346 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002347 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2348 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002349 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2350 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002351 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002352 } else if (TNK == TNK_Non_template) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00002353 Diag(Name.getLocStart(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002354 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002355 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002356 << Name.getSourceRange()
2357 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002358 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002359 } else {
2360 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002361 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002362 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002363 }
2364
Mike Stump1eb44332009-09-09 15:08:12 +00002365 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002366 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002367
Douglas Gregor014e88d2009-11-03 23:16:33 +00002368 switch (Name.getKind()) {
2369 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002370 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002371 Name.Identifier));
2372 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002373
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002374 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002375 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002376 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002377 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002378
2379 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002380 llvm_unreachable(
2381 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002382
Douglas Gregor014e88d2009-11-03 23:16:33 +00002383 default:
2384 break;
2385 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002386
Daniel Dunbar96a00142012-03-09 18:35:03 +00002387 Diag(Name.getLocStart(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002388 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002389 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002390 << Name.getSourceRange()
2391 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002392 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002393}
2394
Mike Stump1eb44332009-09-09 15:08:12 +00002395bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002396 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002397 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002398 const TemplateArgument &Arg = AL.getArgument();
2399
Anders Carlsson436b1562009-06-13 00:33:33 +00002400 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002401 switch(Arg.getKind()) {
2402 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002403 // C++ [temp.arg.type]p1:
2404 // A template-argument for a template-parameter which is a
2405 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002406 break;
2407 case TemplateArgument::Template: {
2408 // We have a template type parameter but the template argument
2409 // is a template without any arguments.
2410 SourceRange SR = AL.getSourceRange();
2411 TemplateName Name = Arg.getAsTemplate();
2412 Diag(SR.getBegin(), diag::err_template_missing_args)
2413 << Name << SR;
2414 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2415 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002416
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002417 return true;
2418 }
2419 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002420 // We have a template type parameter but the template argument
2421 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002422 SourceRange SR = AL.getSourceRange();
2423 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002424 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Anders Carlsson436b1562009-06-13 00:33:33 +00002426 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002427 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002428 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002429
John McCalla93c9342009-12-07 02:54:59 +00002430 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002431 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002432
Anders Carlsson436b1562009-06-13 00:33:33 +00002433 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002434 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2435
2436 // Objective-C ARC:
2437 // If an explicitly-specified template argument type is a lifetime type
2438 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
David Blaikie4e4d0842012-03-11 07:00:24 +00002439 if (getLangOpts().ObjCAutoRefCount &&
Douglas Gregore559ca12011-06-17 22:11:49 +00002440 ArgType->isObjCLifetimeType() &&
2441 !ArgType.getObjCLifetime()) {
2442 Qualifiers Qs;
2443 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2444 ArgType = Context.getQualifiedType(ArgType, Qs);
2445 }
2446
2447 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002448 return false;
2449}
2450
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002451/// \brief Substitute template arguments into the default template argument for
2452/// the given template type parameter.
2453///
2454/// \param SemaRef the semantic analysis object for which we are performing
2455/// the substitution.
2456///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002457/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002458/// for.
2459///
2460/// \param TemplateLoc the location of the template name that started the
2461/// template-id we are checking.
2462///
2463/// \param RAngleLoc the location of the right angle bracket ('>') that
2464/// terminates the template-id.
2465///
2466/// \param Param the template template parameter whose default we are
2467/// substituting into.
2468///
2469/// \param Converted the list of template arguments provided for template
2470/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002471/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002472static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002473SubstDefaultTemplateArgument(Sema &SemaRef,
2474 TemplateDecl *Template,
2475 SourceLocation TemplateLoc,
2476 SourceLocation RAngleLoc,
2477 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002478 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002479 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002480
2481 // If the argument type is dependent, instantiate it now based
2482 // on the previously-computed template arguments.
2483 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002484 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002485 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002486
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002487 MultiLevelTemplateArgumentList AllTemplateArgs
2488 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2489
2490 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002491 Template, Converted.data(),
2492 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002493 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002494
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002495 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2496 Param->getDefaultArgumentLoc(),
2497 Param->getDeclName());
2498 }
2499
2500 return ArgType;
2501}
2502
2503/// \brief Substitute template arguments into the default template argument for
2504/// the given non-type template parameter.
2505///
2506/// \param SemaRef the semantic analysis object for which we are performing
2507/// the substitution.
2508///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002509/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002510/// for.
2511///
2512/// \param TemplateLoc the location of the template name that started the
2513/// template-id we are checking.
2514///
2515/// \param RAngleLoc the location of the right angle bracket ('>') that
2516/// terminates the template-id.
2517///
Douglas Gregor788cd062009-11-11 01:00:40 +00002518/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002519/// substituting into.
2520///
2521/// \param Converted the list of template arguments provided for template
2522/// parameters that precede \p Param in the template parameter list.
2523///
2524/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002525static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002526SubstDefaultTemplateArgument(Sema &SemaRef,
2527 TemplateDecl *Template,
2528 SourceLocation TemplateLoc,
2529 SourceLocation RAngleLoc,
2530 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002531 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002532 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002533 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002534
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002535 MultiLevelTemplateArgumentList AllTemplateArgs
2536 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002537
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002538 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002539 Template, Converted.data(),
2540 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002541 SourceRange(TemplateLoc, RAngleLoc));
2542
2543 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2544}
2545
Douglas Gregor788cd062009-11-11 01:00:40 +00002546/// \brief Substitute template arguments into the default template argument for
2547/// the given template template parameter.
2548///
2549/// \param SemaRef the semantic analysis object for which we are performing
2550/// the substitution.
2551///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002552/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002553/// for.
2554///
2555/// \param TemplateLoc the location of the template name that started the
2556/// template-id we are checking.
2557///
2558/// \param RAngleLoc the location of the right angle bracket ('>') that
2559/// terminates the template-id.
2560///
2561/// \param Param the template template parameter whose default we are
2562/// substituting into.
2563///
2564/// \param Converted the list of template arguments provided for template
2565/// parameters that precede \p Param in the template parameter list.
2566///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002567/// \param QualifierLoc Will be set to the nested-name-specifier (with
2568/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002569///
Douglas Gregor788cd062009-11-11 01:00:40 +00002570/// \returns the substituted template argument, or NULL if an error occurred.
2571static TemplateName
2572SubstDefaultTemplateArgument(Sema &SemaRef,
2573 TemplateDecl *Template,
2574 SourceLocation TemplateLoc,
2575 SourceLocation RAngleLoc,
2576 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002577 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002578 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002579 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002580 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002581
Douglas Gregor788cd062009-11-11 01:00:40 +00002582 MultiLevelTemplateArgumentList AllTemplateArgs
2583 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002584
Douglas Gregor788cd062009-11-11 01:00:40 +00002585 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002586 Template, Converted.data(),
2587 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002588 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002589
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002590 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002591 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002592 if (QualifierLoc) {
2593 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2594 AllTemplateArgs);
2595 if (!QualifierLoc)
2596 return TemplateName();
2597 }
2598
Douglas Gregor1d752d72011-03-02 18:46:51 +00002599 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002600 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002601 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002602 AllTemplateArgs);
2603}
2604
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002605/// \brief If the given template parameter has a default template
2606/// argument, substitute into that default template argument and
2607/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002608TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002609Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2610 SourceLocation TemplateLoc,
2611 SourceLocation RAngleLoc,
2612 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002613 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002614 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002615 if (!TypeParm->hasDefaultArgument())
2616 return TemplateArgumentLoc();
2617
John McCalla93c9342009-12-07 02:54:59 +00002618 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002619 TemplateLoc,
2620 RAngleLoc,
2621 TypeParm,
2622 Converted);
2623 if (DI)
2624 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2625
2626 return TemplateArgumentLoc();
2627 }
2628
2629 if (NonTypeTemplateParmDecl *NonTypeParm
2630 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2631 if (!NonTypeParm->hasDefaultArgument())
2632 return TemplateArgumentLoc();
2633
John McCall60d7b3a2010-08-24 06:29:42 +00002634 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002635 TemplateLoc,
2636 RAngleLoc,
2637 NonTypeParm,
2638 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002639 if (Arg.isInvalid())
2640 return TemplateArgumentLoc();
2641
2642 Expr *ArgE = Arg.takeAs<Expr>();
2643 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2644 }
2645
2646 TemplateTemplateParmDecl *TempTempParm
2647 = cast<TemplateTemplateParmDecl>(Param);
2648 if (!TempTempParm->hasDefaultArgument())
2649 return TemplateArgumentLoc();
2650
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002651
Douglas Gregor1d752d72011-03-02 18:46:51 +00002652 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002653 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002654 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002655 RAngleLoc,
2656 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002657 Converted,
2658 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002659 if (TName.isNull())
2660 return TemplateArgumentLoc();
2661
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002662 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002663 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002664 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2665}
2666
Douglas Gregore7526412009-11-11 19:31:23 +00002667/// \brief Check that the given template argument corresponds to the given
2668/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002669///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002670/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002671/// checked.
2672///
2673/// \param Arg The template argument.
2674///
2675/// \param Template The template in which the template argument resides.
2676///
2677/// \param TemplateLoc The location of the template name for the template
2678/// whose argument list we're matching.
2679///
2680/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2681/// the template argument list.
2682///
2683/// \param ArgumentPackIndex The index into the argument pack where this
2684/// argument will be placed. Only valid if the parameter is a parameter pack.
2685///
2686/// \param Converted The checked, converted argument will be added to the
2687/// end of this small vector.
2688///
2689/// \param CTAK Describes how we arrived at this particular template argument:
2690/// explicitly written, deduced, etc.
2691///
2692/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002693bool Sema::CheckTemplateArgument(NamedDecl *Param,
2694 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002695 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002696 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002697 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002698 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002699 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002700 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002701 // Check template type parameters.
2702 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002703 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002704
Douglas Gregord9e15302009-11-11 19:41:09 +00002705 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002706 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002707 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002708 // with the template arguments we've seen thus far. But if the
2709 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002710 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002711 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2712 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002713
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002714 if (NTTPType->isDependentType() &&
2715 !isa<TemplateTemplateParmDecl>(Template) &&
2716 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002717 // Do substitution on the type of the non-type template parameter.
2718 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002719 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002720 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002721
2722 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002723 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002724 NTTPType = SubstType(NTTPType,
2725 MultiLevelTemplateArgumentList(TemplateArgs),
2726 NTTP->getLocation(),
2727 NTTP->getDeclName());
2728 // If that worked, check the non-type template parameter type
2729 // for validity.
2730 if (!NTTPType.isNull())
2731 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2732 NTTP->getLocation());
2733 if (NTTPType.isNull())
2734 return true;
2735 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002736
Douglas Gregore7526412009-11-11 19:31:23 +00002737 switch (Arg.getArgument().getKind()) {
2738 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002739 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002740
Douglas Gregore7526412009-11-11 19:31:23 +00002741 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002742 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002743 ExprResult Res =
2744 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2745 Result, CTAK);
2746 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002747 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002748
Douglas Gregor910f8002010-11-07 23:05:16 +00002749 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002750 break;
2751 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002752
Douglas Gregore7526412009-11-11 19:31:23 +00002753 case TemplateArgument::Declaration:
2754 case TemplateArgument::Integral:
2755 // We've already checked this template argument, so just copy
2756 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002757 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002758 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002759
Douglas Gregore7526412009-11-11 19:31:23 +00002760 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002761 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002762 // We were given a template template argument. It may not be ill-formed;
2763 // see below.
2764 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002765 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2766 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002767 // We have a template argument such as \c T::template X, which we
2768 // parsed as a template template argument. However, since we now
2769 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002770 // template name into an expression.
2771
2772 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2773 Arg.getTemplateNameLoc());
2774
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002775 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002776 SS.Adopt(Arg.getTemplateQualifierLoc());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002777 // FIXME: the template-template arg was a DependentTemplateName,
2778 // so it was provided with a template keyword. However, its source
2779 // location is not stored in the template argument structure.
2780 SourceLocation TemplateKWLoc;
John Wiegley429bb272011-04-08 18:41:53 +00002781 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002782 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002783 TemplateKWLoc,
2784 NameInfo, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002785
Douglas Gregora7fc9012011-01-05 18:58:31 +00002786 // If we parsed the template argument as a pack expansion, create a
2787 // pack expansion expression.
2788 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002789 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2790 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002791 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002792 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002793
Douglas Gregore7526412009-11-11 19:31:23 +00002794 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002795 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2796 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002797 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002798
Douglas Gregor910f8002010-11-07 23:05:16 +00002799 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002800 break;
2801 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002802
Douglas Gregore7526412009-11-11 19:31:23 +00002803 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002804 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002805 // therefore cannot be a non-type template argument.
2806 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2807 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002808
Douglas Gregore7526412009-11-11 19:31:23 +00002809 Diag(Param->getLocation(), diag::note_template_param_here);
2810 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002811
Douglas Gregore7526412009-11-11 19:31:23 +00002812 case TemplateArgument::Type: {
2813 // We have a non-type template parameter but the template
2814 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002815
Douglas Gregore7526412009-11-11 19:31:23 +00002816 // C++ [temp.arg]p2:
2817 // In a template-argument, an ambiguity between a type-id and
2818 // an expression is resolved to a type-id, regardless of the
2819 // form of the corresponding template-parameter.
2820 //
2821 // We warn specifically about this case, since it can be rather
2822 // confusing for users.
2823 QualType T = Arg.getArgument().getAsType();
2824 SourceRange SR = Arg.getSourceRange();
2825 if (T->isFunctionType())
2826 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2827 else
2828 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2829 Diag(Param->getLocation(), diag::note_template_param_here);
2830 return true;
2831 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002832
Douglas Gregore7526412009-11-11 19:31:23 +00002833 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002834 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002835 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002836
Douglas Gregore7526412009-11-11 19:31:23 +00002837 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002838 }
2839
2840
Douglas Gregore7526412009-11-11 19:31:23 +00002841 // Check template template parameters.
2842 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002843
Douglas Gregore7526412009-11-11 19:31:23 +00002844 // Substitute into the template parameter list of the template
2845 // template parameter, since previously-supplied template arguments
2846 // may appear within the template template parameter.
2847 {
2848 // Set up a template instantiation context.
2849 LocalInstantiationScope Scope(*this);
2850 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002851 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002852 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002853
2854 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002855 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002856 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002857 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002858 MultiLevelTemplateArgumentList(TemplateArgs)));
2859 if (!TempParm)
2860 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002861 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002862
Douglas Gregore7526412009-11-11 19:31:23 +00002863 switch (Arg.getArgument().getKind()) {
2864 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002865 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002866
Douglas Gregore7526412009-11-11 19:31:23 +00002867 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002868 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002869 if (CheckTemplateArgument(TempParm, Arg))
2870 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002871
Douglas Gregor910f8002010-11-07 23:05:16 +00002872 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002873 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002874
Douglas Gregore7526412009-11-11 19:31:23 +00002875 case TemplateArgument::Expression:
2876 case TemplateArgument::Type:
2877 // We have a template template parameter but the template
2878 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002879 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
David Blaikie4e4d0842012-03-11 07:00:24 +00002880 << getLangOpts().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002881 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002882
Douglas Gregore7526412009-11-11 19:31:23 +00002883 case TemplateArgument::Declaration:
David Blaikie7530c032012-01-17 06:56:22 +00002884 llvm_unreachable("Declaration argument with template template parameter");
Douglas Gregore7526412009-11-11 19:31:23 +00002885 case TemplateArgument::Integral:
David Blaikie7530c032012-01-17 06:56:22 +00002886 llvm_unreachable("Integral argument with template template parameter");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002887
Douglas Gregore7526412009-11-11 19:31:23 +00002888 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002889 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002890 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002891
Douglas Gregore7526412009-11-11 19:31:23 +00002892 return false;
2893}
2894
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002895/// \brief Diagnose an arity mismatch in the
2896static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template,
2897 SourceLocation TemplateLoc,
2898 TemplateArgumentListInfo &TemplateArgs) {
2899 TemplateParameterList *Params = Template->getTemplateParameters();
2900 unsigned NumParams = Params->size();
2901 unsigned NumArgs = TemplateArgs.size();
2902
2903 SourceRange Range;
2904 if (NumArgs > NumParams)
2905 Range = SourceRange(TemplateArgs[NumParams].getLocation(),
2906 TemplateArgs.getRAngleLoc());
2907 S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2908 << (NumArgs > NumParams)
2909 << (isa<ClassTemplateDecl>(Template)? 0 :
2910 isa<FunctionTemplateDecl>(Template)? 1 :
2911 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2912 << Template << Range;
2913 S.Diag(Template->getLocation(), diag::note_template_decl_here)
2914 << Params->getSourceRange();
2915 return true;
2916}
2917
Douglas Gregorc15cb382009-02-09 23:23:08 +00002918/// \brief Check that the given template argument list is well-formed
2919/// for specializing the given template.
2920bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2921 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002922 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002923 bool PartialTemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00002924 SmallVectorImpl<TemplateArgument> &Converted,
2925 bool *ExpansionIntoFixedList) {
2926 if (ExpansionIntoFixedList)
2927 *ExpansionIntoFixedList = false;
2928
Douglas Gregorc15cb382009-02-09 23:23:08 +00002929 TemplateParameterList *Params = Template->getTemplateParameters();
2930 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002931 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002932 bool Invalid = false;
2933
John McCalld5532b62009-11-23 01:53:49 +00002934 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2935
Mike Stump1eb44332009-09-09 15:08:12 +00002936 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002937 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Douglas Gregorb70126a2012-02-03 17:16:23 +00002938
Mike Stump1eb44332009-09-09 15:08:12 +00002939 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002940 // [...] The type and form of each template-argument specified in
2941 // a template-id shall match the type and form specified for the
2942 // corresponding parameter declared by the template in its
2943 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002944 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002945 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002946 TemplateParameterList::iterator Param = Params->begin(),
2947 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002948 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002949 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002950 bool SawPackExpansion = false;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002951 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002952 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002953 // If we have an expanded parameter pack, make sure we don't have too
2954 // many arguments.
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002955 // FIXME: This really should fall out from the normal arity checking.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002956 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002957 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002958 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002959 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2960 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2961 << true
2962 << (isa<ClassTemplateDecl>(Template)? 0 :
2963 isa<FunctionTemplateDecl>(Template)? 1 :
2964 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2965 << Template;
2966 Diag(Template->getLocation(), diag::note_template_decl_here)
2967 << Params->getSourceRange();
2968 return true;
2969 }
2970 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002971
Douglas Gregorf35f8282009-11-11 21:54:23 +00002972 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002973 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2974 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002975 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002976 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002977
Douglas Gregor14be16b2010-12-20 16:57:52 +00002978 if ((*Param)->isTemplateParameterPack()) {
2979 // The template parameter was a template parameter pack, so take the
2980 // deduced argument and place it on the argument pack. Note that we
2981 // stay on the same template parameter so that we can deduce more
2982 // arguments.
2983 ArgumentPack.push_back(Converted.back());
2984 Converted.pop_back();
2985 } else {
2986 // Move to the next template parameter.
2987 ++Param;
2988 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002989
2990 // If this template argument is a pack expansion, record that fact
2991 // and break out; we can't actually check any more.
2992 if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) {
2993 SawPackExpansion = true;
2994 ++ArgIdx;
2995 break;
2996 }
2997
Douglas Gregor14be16b2010-12-20 16:57:52 +00002998 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002999 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003000 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003001
Douglas Gregor8735b292011-06-03 02:59:40 +00003002 // If we're checking a partial template argument list, we're done.
3003 if (PartialTemplateArgs) {
3004 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
3005 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3006 ArgumentPack.data(),
3007 ArgumentPack.size()));
3008
3009 return Invalid;
3010 }
3011
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003012 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00003013 // arguments, just break out now and we'll fill in the argument pack below.
3014 if ((*Param)->isTemplateParameterPack())
3015 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00003016
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003017 // Check whether we have a default argument.
Douglas Gregorf35f8282009-11-11 21:54:23 +00003018 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003019
Douglas Gregorf35f8282009-11-11 21:54:23 +00003020 // Retrieve the default template argument from the template
3021 // parameter. For each kind of template parameter, we substitute the
3022 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003023 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00003024 // the default argument.
3025 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003026 if (!TTP->hasDefaultArgument())
3027 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3028 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003029
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003030 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003031 Template,
3032 TemplateLoc,
3033 RAngleLoc,
3034 TTP,
3035 Converted);
3036 if (!ArgType)
3037 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003038
Douglas Gregorf35f8282009-11-11 21:54:23 +00003039 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
3040 ArgType);
3041 } else if (NonTypeTemplateParmDecl *NTTP
3042 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003043 if (!NTTP->hasDefaultArgument())
3044 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3045 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003046
John McCall60d7b3a2010-08-24 06:29:42 +00003047 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003048 TemplateLoc,
3049 RAngleLoc,
3050 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003051 Converted);
3052 if (E.isInvalid())
3053 return true;
3054
3055 Expr *Ex = E.takeAs<Expr>();
3056 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3057 } else {
3058 TemplateTemplateParmDecl *TempParm
3059 = cast<TemplateTemplateParmDecl>(*Param);
3060
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003061 if (!TempParm->hasDefaultArgument())
3062 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3063 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003064
Douglas Gregor1d752d72011-03-02 18:46:51 +00003065 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003066 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003067 TemplateLoc,
3068 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003069 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003070 Converted,
3071 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003072 if (Name.isNull())
3073 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003074
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003075 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3076 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003077 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003078
Douglas Gregorf35f8282009-11-11 21:54:23 +00003079 // Introduce an instantiation record that describes where we are using
3080 // the default template argument.
3081 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003082 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003083 SourceRange(TemplateLoc, RAngleLoc));
3084
Douglas Gregorf35f8282009-11-11 21:54:23 +00003085 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003086 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003087 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003088 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003089
Douglas Gregor67714232011-03-03 02:41:12 +00003090 // Core issue 150 (assumed resolution): if this is a template template
3091 // parameter, keep track of the default template arguments from the
3092 // template definition.
3093 if (isTemplateTemplateParameter)
3094 TemplateArgs.addArgument(Arg);
3095
Douglas Gregor14be16b2010-12-20 16:57:52 +00003096 // Move to the next template parameter and argument.
3097 ++Param;
3098 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003099 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003100
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003101 // If we saw a pack expansion, then directly convert the remaining arguments,
3102 // because we don't know what parameters they'll match up with.
3103 if (SawPackExpansion) {
3104 bool AddToArgumentPack
3105 = Param != ParamEnd && (*Param)->isTemplateParameterPack();
3106 while (ArgIdx < NumArgs) {
3107 if (AddToArgumentPack)
3108 ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument());
3109 else
3110 Converted.push_back(TemplateArgs[ArgIdx].getArgument());
3111 ++ArgIdx;
3112 }
3113
3114 // Push the argument pack onto the list of converted arguments.
3115 if (AddToArgumentPack) {
3116 if (ArgumentPack.empty())
3117 Converted.push_back(TemplateArgument(0, 0));
3118 else {
3119 Converted.push_back(
3120 TemplateArgument::CreatePackCopy(Context,
3121 ArgumentPack.data(),
3122 ArgumentPack.size()));
3123 ArgumentPack.clear();
3124 }
Douglas Gregorb70126a2012-02-03 17:16:23 +00003125 } else if (ExpansionIntoFixedList) {
3126 // We have expanded a pack into a fixed list.
3127 *ExpansionIntoFixedList = true;
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003128 }
3129
3130 return Invalid;
3131 }
3132
3133 // If we have any leftover arguments, then there were too many arguments.
3134 // Complain and fail.
3135 if (ArgIdx < NumArgs)
3136 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
3137
3138 // If we have an expanded parameter pack, make sure we don't have too
3139 // many arguments.
3140 // FIXME: This really should fall out from the normal arity checking.
3141 if (Param != ParamEnd) {
3142 if (NonTypeTemplateParmDecl *NTTP
3143 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
3144 if (NTTP->isExpandedParameterPack() &&
3145 ArgumentPack.size() < NTTP->getNumExpansionTypes()) {
3146 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
3147 << false
3148 << (isa<ClassTemplateDecl>(Template)? 0 :
3149 isa<FunctionTemplateDecl>(Template)? 1 :
3150 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
3151 << Template;
3152 Diag(Template->getLocation(), diag::note_template_decl_here)
3153 << Params->getSourceRange();
3154 return true;
3155 }
3156 }
3157 }
3158
Douglas Gregor14be16b2010-12-20 16:57:52 +00003159 // Form argument packs for each of the parameter packs remaining.
3160 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003161 // If we're checking a partial list of template arguments, don't fill
3162 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003163 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003164 if (!HasParameterPack)
3165 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003166 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003167 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003168 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003169 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3170 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003171 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003172 ArgumentPack.clear();
3173 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003174 } else if (!PartialTemplateArgs)
3175 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003176
Douglas Gregor14be16b2010-12-20 16:57:52 +00003177 ++Param;
3178 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003179
Douglas Gregorc15cb382009-02-09 23:23:08 +00003180 return Invalid;
3181}
3182
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003183namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003184 class UnnamedLocalNoLinkageFinder
3185 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003186 {
3187 Sema &S;
3188 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003189
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003190 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003191
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003192 public:
3193 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3194
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003195 bool Visit(QualType T) {
3196 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003197 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003198
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003199#define TYPE(Class, Parent) \
3200 bool Visit##Class##Type(const Class##Type *);
3201#define ABSTRACT_TYPE(Class, Parent) \
3202 bool Visit##Class##Type(const Class##Type *) { return false; }
3203#define NON_CANONICAL_TYPE(Class, Parent) \
3204 bool Visit##Class##Type(const Class##Type *) { return false; }
3205#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003206
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003207 bool VisitTagDecl(const TagDecl *Tag);
3208 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3209 };
3210}
3211
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003212bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003213 return false;
3214}
3215
3216bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3217 return Visit(T->getElementType());
3218}
3219
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003220bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003221 return Visit(T->getPointeeType());
3222}
3223
3224bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003225 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003226 return Visit(T->getPointeeType());
3227}
3228
3229bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003230 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003231 return Visit(T->getPointeeType());
3232}
3233
3234bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003235 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003236 return Visit(T->getPointeeType());
3237}
3238
3239bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003240 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003241 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3242}
3243
3244bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003245 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003246 return Visit(T->getElementType());
3247}
3248
3249bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003250 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003251 return Visit(T->getElementType());
3252}
3253
3254bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003255 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003256 return Visit(T->getElementType());
3257}
3258
3259bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003260 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003261 return Visit(T->getElementType());
3262}
3263
3264bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003265 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003266 return Visit(T->getElementType());
3267}
3268
3269bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3270 return Visit(T->getElementType());
3271}
3272
3273bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3274 return Visit(T->getElementType());
3275}
3276
3277bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3278 const FunctionProtoType* T) {
3279 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003280 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003281 A != AEnd; ++A) {
3282 if (Visit(*A))
3283 return true;
3284 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003285
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003286 return Visit(T->getResultType());
3287}
3288
3289bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3290 const FunctionNoProtoType* T) {
3291 return Visit(T->getResultType());
3292}
3293
3294bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3295 const UnresolvedUsingType*) {
3296 return false;
3297}
3298
3299bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3300 return false;
3301}
3302
3303bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3304 return Visit(T->getUnderlyingType());
3305}
3306
3307bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3308 return false;
3309}
3310
Sean Huntca63c202011-05-24 22:41:36 +00003311bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3312 const UnaryTransformType*) {
3313 return false;
3314}
3315
Richard Smith34b41d92011-02-20 03:19:35 +00003316bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3317 return Visit(T->getDeducedType());
3318}
3319
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003320bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3321 return VisitTagDecl(T->getDecl());
3322}
3323
3324bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3325 return VisitTagDecl(T->getDecl());
3326}
3327
3328bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3329 const TemplateTypeParmType*) {
3330 return false;
3331}
3332
Douglas Gregorc3069d62011-01-14 02:55:32 +00003333bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3334 const SubstTemplateTypeParmPackType *) {
3335 return false;
3336}
3337
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003338bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3339 const TemplateSpecializationType*) {
3340 return false;
3341}
3342
3343bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3344 const InjectedClassNameType* T) {
3345 return VisitTagDecl(T->getDecl());
3346}
3347
3348bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3349 const DependentNameType* T) {
3350 return VisitNestedNameSpecifier(T->getQualifier());
3351}
3352
3353bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3354 const DependentTemplateSpecializationType* T) {
3355 return VisitNestedNameSpecifier(T->getQualifier());
3356}
3357
Douglas Gregor7536dd52010-12-20 02:24:11 +00003358bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3359 const PackExpansionType* T) {
3360 return Visit(T->getPattern());
3361}
3362
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003363bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3364 return false;
3365}
3366
3367bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3368 const ObjCInterfaceType *) {
3369 return false;
3370}
3371
3372bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3373 const ObjCObjectPointerType *) {
3374 return false;
3375}
3376
Eli Friedmanb001de72011-10-06 23:00:33 +00003377bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3378 return Visit(T->getValueType());
3379}
3380
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003381bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3382 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003383 S.Diag(SR.getBegin(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003384 S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003385 diag::warn_cxx98_compat_template_arg_local_type :
3386 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003387 << S.Context.getTypeDeclType(Tag) << SR;
3388 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003389 }
3390
Richard Smith162e1c12011-04-15 14:24:37 +00003391 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003392 S.Diag(SR.getBegin(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003393 S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003394 diag::warn_cxx98_compat_template_arg_unnamed_type :
3395 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003396 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3397 return true;
3398 }
3399
3400 return false;
3401}
3402
3403bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3404 NestedNameSpecifier *NNS) {
3405 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3406 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003407
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003408 switch (NNS->getKind()) {
3409 case NestedNameSpecifier::Identifier:
3410 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003411 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003412 case NestedNameSpecifier::Global:
3413 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003414
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003415 case NestedNameSpecifier::TypeSpec:
3416 case NestedNameSpecifier::TypeSpecWithTemplate:
3417 return Visit(QualType(NNS->getAsType(), 0));
3418 }
David Blaikie7530c032012-01-17 06:56:22 +00003419 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003420}
3421
3422
Douglas Gregorc15cb382009-02-09 23:23:08 +00003423/// \brief Check a template argument against its corresponding
3424/// template type parameter.
3425///
3426/// This routine implements the semantics of C++ [temp.arg.type]. It
3427/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003428bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003429 TypeSourceInfo *ArgInfo) {
3430 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003431 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003432 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003433
3434 if (Arg->isVariablyModifiedType()) {
3435 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003436 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003437 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003438 }
3439
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003440 // C++03 [temp.arg.type]p2:
3441 // A local type, a type with no linkage, an unnamed type or a type
3442 // compounded from any of these types shall not be used as a
3443 // template-argument for a template type-parameter.
3444 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003445 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003446 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003447 if (LangOpts.CPlusPlus0x ?
3448 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3449 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3450 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3451 SR.getBegin()) != DiagnosticsEngine::Ignored :
3452 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003453 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3454 (void)Finder.Visit(Context.getCanonicalType(Arg));
3455 }
3456
Douglas Gregorc15cb382009-02-09 23:23:08 +00003457 return false;
3458}
3459
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003460/// \brief Checks whether the given template argument is the address
3461/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003462static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003463CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3464 NonTypeTemplateParmDecl *Param,
3465 QualType ParamType,
3466 Expr *ArgIn,
3467 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003468 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003469 Expr *Arg = ArgIn;
3470 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003471
3472 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003473 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003474
3475 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003476 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003477 // A template-argument for a non-type, non-template
3478 // template-parameter shall be one of: [...]
3479 //
3480 // -- the address of an object or function with external
3481 // linkage, including function templates and function
3482 // template-ids but excluding non-static class members,
3483 // expressed as & id-expression where the & is optional if
3484 // the name refers to a function or array, or if the
3485 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003486
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003487 // In C++98/03 mode, give an extension warning on any extra parentheses.
3488 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3489 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003490 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003491 if (!Invalid && !ExtraParens) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003492 S.Diag(Arg->getLocStart(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003493 S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003494 diag::warn_cxx98_compat_template_arg_extra_parens :
3495 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003496 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003497 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003498 }
3499
3500 Arg = Parens->getSubExpr();
3501 }
3502
John McCall91a57552011-07-15 05:09:51 +00003503 while (SubstNonTypeTemplateParmExpr *subst =
3504 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3505 Arg = subst->getReplacement()->IgnoreImpCasts();
3506
Douglas Gregorb7a09262010-04-01 18:32:35 +00003507 bool AddressTaken = false;
3508 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003509 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003510 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003511 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003512 AddressTaken = true;
3513 AddrOpLoc = UnOp->getOperatorLoc();
3514 }
Francois Picheta343a412011-04-29 09:08:14 +00003515 }
John McCall91a57552011-07-15 05:09:51 +00003516
David Blaikie4e4d0842012-03-11 07:00:24 +00003517 if (S.getLangOpts().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003518 Converted = TemplateArgument(ArgIn);
3519 return false;
3520 }
3521
3522 while (SubstNonTypeTemplateParmExpr *subst =
3523 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3524 Arg = subst->getReplacement()->IgnoreImpCasts();
3525
3526 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003527 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003528 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3529 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003530 S.Diag(Param->getLocation(), diag::note_template_param_here);
3531 return true;
3532 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003533
3534 // Stop checking the precise nature of the argument if it is value dependent,
3535 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003536 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003537 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003538 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003539 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003540
Douglas Gregorb7a09262010-04-01 18:32:35 +00003541 if (!isa<ValueDecl>(DRE->getDecl())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003542 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003543 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003544 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003545 S.Diag(Param->getLocation(), diag::note_template_param_here);
3546 return true;
3547 }
3548
3549 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003550
3551 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003552 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003553 S.Diag(Arg->getLocStart(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003554 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003555 S.Diag(Param->getLocation(), diag::note_template_param_here);
3556 return true;
3557 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003558
3559 // Cannot refer to non-static member functions
3560 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003561 if (!Method->isStatic()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003562 S.Diag(Arg->getLocStart(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003563 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003564 S.Diag(Param->getLocation(), diag::note_template_param_here);
3565 return true;
3566 }
Mike Stump1eb44332009-09-09 15:08:12 +00003567
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003568 // Functions must have external linkage.
3569 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003570 if (!isExternalLinkage(Func->getLinkage())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003571 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003572 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003573 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003574 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003575 << true;
3576 return true;
3577 }
3578
3579 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003580 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003581
Douglas Gregorb7a09262010-04-01 18:32:35 +00003582 // If the template parameter has pointer type, the function decays.
3583 if (ParamType->isPointerType() && !AddressTaken)
3584 ArgType = S.Context.getPointerType(Func->getType());
3585 else if (AddressTaken && ParamType->isReferenceType()) {
3586 // If we originally had an address-of operator, but the
3587 // parameter has reference type, complain and (if things look
3588 // like they will work) drop the address-of operator.
3589 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3590 ParamType.getNonReferenceType())) {
3591 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3592 << ParamType;
3593 S.Diag(Param->getLocation(), diag::note_template_param_here);
3594 return true;
3595 }
3596
3597 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3598 << ParamType
3599 << FixItHint::CreateRemoval(AddrOpLoc);
3600 S.Diag(Param->getLocation(), diag::note_template_param_here);
3601
3602 ArgType = Func->getType();
3603 }
3604 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003605 if (!isExternalLinkage(Var->getLinkage())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003606 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003607 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003608 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003609 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003610 << true;
3611 return true;
3612 }
3613
Douglas Gregorb7a09262010-04-01 18:32:35 +00003614 // A value of reference type is not an object.
3615 if (Var->getType()->isReferenceType()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003616 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003617 diag::err_template_arg_reference_var)
3618 << Var->getType() << Arg->getSourceRange();
3619 S.Diag(Param->getLocation(), diag::note_template_param_here);
3620 return true;
3621 }
3622
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003623 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003624 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003625
3626 // If the template parameter has pointer type, we must have taken
3627 // the address of this object.
3628 if (ParamType->isReferenceType()) {
3629 if (AddressTaken) {
3630 // If we originally had an address-of operator, but the
3631 // parameter has reference type, complain and (if things look
3632 // like they will work) drop the address-of operator.
3633 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3634 ParamType.getNonReferenceType())) {
3635 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3636 << ParamType;
3637 S.Diag(Param->getLocation(), diag::note_template_param_here);
3638 return true;
3639 }
3640
3641 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3642 << ParamType
3643 << FixItHint::CreateRemoval(AddrOpLoc);
3644 S.Diag(Param->getLocation(), diag::note_template_param_here);
3645
3646 ArgType = Var->getType();
3647 }
3648 } else if (!AddressTaken && ParamType->isPointerType()) {
3649 if (Var->getType()->isArrayType()) {
3650 // Array-to-pointer decay.
3651 ArgType = S.Context.getArrayDecayedType(Var->getType());
3652 } else {
3653 // If the template parameter has pointer type but the address of
3654 // this object was not taken, complain and (possibly) recover by
3655 // taking the address of the entity.
3656 ArgType = S.Context.getPointerType(Var->getType());
3657 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3658 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3659 << ParamType;
3660 S.Diag(Param->getLocation(), diag::note_template_param_here);
3661 return true;
3662 }
3663
3664 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3665 << ParamType
3666 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3667
3668 S.Diag(Param->getLocation(), diag::note_template_param_here);
3669 }
3670 }
3671 } else {
3672 // We found something else, but we don't know specifically what it is.
Daniel Dunbar96a00142012-03-09 18:35:03 +00003673 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003674 diag::err_template_arg_not_object_or_func)
3675 << Arg->getSourceRange();
3676 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3677 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003678 }
Mike Stump1eb44332009-09-09 15:08:12 +00003679
John McCallf85e1932011-06-15 23:02:42 +00003680 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003681 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003682 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003683 S.IsQualificationConversion(ArgType, ParamType, false,
3684 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003685 // For pointer-to-object types, qualification conversions are
3686 // permitted.
3687 } else {
3688 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3689 if (!ParamRef->getPointeeType()->isFunctionType()) {
3690 // C++ [temp.arg.nontype]p5b3:
3691 // For a non-type template-parameter of type reference to
3692 // object, no conversions apply. The type referred to by the
3693 // reference may be more cv-qualified than the (otherwise
3694 // identical) type of the template- argument. The
3695 // template-parameter is bound directly to the
3696 // template-argument, which shall be an lvalue.
3697
3698 // FIXME: Other qualifiers?
3699 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3700 unsigned ArgQuals = ArgType.getCVRQualifiers();
3701
3702 if ((ParamQuals | ArgQuals) != ParamQuals) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003703 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003704 diag::err_template_arg_ref_bind_ignores_quals)
3705 << ParamType << Arg->getType()
3706 << Arg->getSourceRange();
3707 S.Diag(Param->getLocation(), diag::note_template_param_here);
3708 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003709 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003710 }
3711 }
3712
3713 // At this point, the template argument refers to an object or
3714 // function with external linkage. We now need to check whether the
3715 // argument and parameter types are compatible.
3716 if (!S.Context.hasSameUnqualifiedType(ArgType,
3717 ParamType.getNonReferenceType())) {
3718 // We can't perform this conversion or binding.
3719 if (ParamType->isReferenceType())
3720 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003721 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003722 else
3723 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003724 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003725 S.Diag(Param->getLocation(), diag::note_template_param_here);
3726 return true;
3727 }
3728 }
3729
3730 // Create the template argument.
3731 Converted = TemplateArgument(Entity->getCanonicalDecl());
Eli Friedman5f2987c2012-02-02 03:46:19 +00003732 S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003733 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003734}
3735
3736/// \brief Checks whether the given template argument is a pointer to
3737/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003738bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003739 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003740 bool Invalid = false;
3741
3742 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003743 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003744 Arg = Cast->getSubExpr();
3745
3746 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003747 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003748 // A template-argument for a non-type, non-template
3749 // template-parameter shall be one of: [...]
3750 //
3751 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003752 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003753
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003754 // In C++98/03 mode, give an extension warning on any extra parentheses.
3755 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3756 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003757 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003758 if (!Invalid && !ExtraParens) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003759 Diag(Arg->getLocStart(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003760 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003761 diag::warn_cxx98_compat_template_arg_extra_parens :
3762 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003763 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003764 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003765 }
3766
3767 Arg = Parens->getSubExpr();
3768 }
3769
John McCall91a57552011-07-15 05:09:51 +00003770 while (SubstNonTypeTemplateParmExpr *subst =
3771 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3772 Arg = subst->getReplacement()->IgnoreImpCasts();
3773
Douglas Gregorcaddba02009-11-12 18:38:13 +00003774 // A pointer-to-member constant written &Class::member.
3775 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003776 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003777 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3778 if (DRE && !DRE->getQualifier())
3779 DRE = 0;
3780 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003781 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003782 // A constant of pointer-to-member type.
3783 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3784 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3785 if (VD->getType()->isMemberPointerType()) {
3786 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003787 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003788 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3789 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003790 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003791 else
3792 Converted = TemplateArgument(VD->getCanonicalDecl());
3793 return Invalid;
3794 }
3795 }
3796 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003797
Douglas Gregorcaddba02009-11-12 18:38:13 +00003798 DRE = 0;
3799 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003800
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003801 if (!DRE)
Daniel Dunbar96a00142012-03-09 18:35:03 +00003802 return Diag(Arg->getLocStart(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003803 diag::err_template_arg_not_pointer_to_member_form)
3804 << Arg->getSourceRange();
3805
3806 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3807 assert((isa<FieldDecl>(DRE->getDecl()) ||
3808 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3809 "Only non-static member pointers can make it here");
3810
3811 // Okay: this is the address of a non-static member, and therefore
3812 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003813 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003814 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003815 else
3816 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003817 return Invalid;
3818 }
3819
3820 // We found something else, but we don't know specifically what it is.
Daniel Dunbar96a00142012-03-09 18:35:03 +00003821 Diag(Arg->getLocStart(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003822 diag::err_template_arg_not_pointer_to_member_form)
3823 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003824 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003825 diag::note_template_arg_refers_here);
3826 return true;
3827}
3828
Douglas Gregorc15cb382009-02-09 23:23:08 +00003829/// \brief Check a template argument against its corresponding
3830/// non-type template parameter.
3831///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003832/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003833/// If an error occurred, it returns ExprError(); otherwise, it
3834/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003835/// InstantiatedParamType is the type of the non-type template
3836/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003837ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3838 QualType InstantiatedParamType, Expr *Arg,
3839 TemplateArgument &Converted,
3840 CheckTemplateArgumentKind CTAK) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003841 SourceLocation StartLoc = Arg->getLocStart();
Douglas Gregor40808ce2009-03-09 23:48:35 +00003842
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003843 // If either the parameter has a dependent type or the argument is
3844 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003845 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3846 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003847 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003848 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003849 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003850
3851 // C++ [temp.arg.nontype]p5:
3852 // The following conversions are performed on each expression used
3853 // as a non-type template-argument. If a non-type
3854 // template-argument cannot be converted to the type of the
3855 // corresponding template-parameter then the program is
3856 // ill-formed.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003857 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003858 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003859 // C++11:
3860 // -- for a non-type template-parameter of integral or
3861 // enumeration type, conversions permitted in a converted
3862 // constant expression are applied.
3863 //
3864 // C++98:
3865 // -- for a non-type template-parameter of integral or
3866 // enumeration type, integral promotions (4.5) and integral
3867 // conversions (4.7) are applied.
3868
3869 if (CTAK == CTAK_Deduced &&
3870 !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) {
3871 // C++ [temp.deduct.type]p17:
3872 // If, in the declaration of a function template with a non-type
3873 // template-parameter, the non-type template-parameter is used
3874 // in an expression in the function parameter-list and, if the
3875 // corresponding template-argument is deduced, the
3876 // template-argument type shall match the type of the
3877 // template-parameter exactly, except that a template-argument
3878 // deduced from an array bound may be of any integral type.
3879 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3880 << Arg->getType().getUnqualifiedType()
3881 << ParamType.getUnqualifiedType();
3882 Diag(Param->getLocation(), diag::note_template_param_here);
3883 return ExprError();
3884 }
3885
David Blaikie4e4d0842012-03-11 07:00:24 +00003886 if (getLangOpts().CPlusPlus0x) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003887 // We can't check arbitrary value-dependent arguments.
3888 // FIXME: If there's no viable conversion to the template parameter type,
3889 // we should be able to diagnose that prior to instantiation.
3890 if (Arg->isValueDependent()) {
3891 Converted = TemplateArgument(Arg);
3892 return Owned(Arg);
3893 }
3894
3895 // C++ [temp.arg.nontype]p1:
3896 // A template-argument for a non-type, non-template template-parameter
3897 // shall be one of:
3898 //
3899 // -- for a non-type template-parameter of integral or enumeration
3900 // type, a converted constant expression of the type of the
3901 // template-parameter; or
3902 llvm::APSInt Value;
3903 ExprResult ArgResult =
3904 CheckConvertedConstantExpression(Arg, ParamType, Value,
3905 CCEK_TemplateArg);
3906 if (ArgResult.isInvalid())
3907 return ExprError();
3908
3909 // Widen the argument value to sizeof(parameter type). This is almost
3910 // always a no-op, except when the parameter type is bool. In
3911 // that case, this may extend the argument from 1 bit to 8 bits.
3912 QualType IntegerType = ParamType;
3913 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
3914 IntegerType = Enum->getDecl()->getIntegerType();
3915 Value = Value.extOrTrunc(Context.getTypeSize(IntegerType));
3916
3917 Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType));
3918 return ArgResult;
3919 }
3920
Richard Smith4f870622011-10-27 22:11:44 +00003921 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3922 if (ArgResult.isInvalid())
3923 return ExprError();
3924 Arg = ArgResult.take();
3925
3926 QualType ArgType = Arg->getType();
3927
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003928 // C++ [temp.arg.nontype]p1:
3929 // A template-argument for a non-type, non-template
3930 // template-parameter shall be one of:
3931 //
3932 // -- an integral constant-expression of integral or enumeration
3933 // type; or
3934 // -- the name of a non-type template-parameter; or
3935 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003936 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003937 if (!ArgType->isIntegralOrEnumerationType()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003938 Diag(Arg->getLocStart(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003939 diag::err_template_arg_not_integral_or_enumeral)
3940 << ArgType << Arg->getSourceRange();
3941 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003942 return ExprError();
Richard Smith282e7e62012-02-04 09:53:13 +00003943 } else if (!Arg->isValueDependent()) {
3944 Arg = VerifyIntegerConstantExpression(Arg, &Value,
3945 PDiag(diag::err_template_arg_not_ice) << ArgType, false).take();
3946 if (!Arg)
3947 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003948 }
3949
Douglas Gregor02024a92010-03-28 02:42:43 +00003950 // From here on out, all we care about are the unqualified forms
3951 // of the parameter and argument types.
3952 ParamType = ParamType.getUnqualifiedType();
3953 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003954
3955 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003956 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003957 // Okay: no conversion necessary
John McCalldaa8e4e2010-11-15 09:13:47 +00003958 } else if (ParamType->isBooleanType()) {
3959 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003960 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003961 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3962 !ParamType->isEnumeralType()) {
3963 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003964 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003965 } else {
3966 // We can't perform this conversion.
Daniel Dunbar96a00142012-03-09 18:35:03 +00003967 Diag(Arg->getLocStart(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003968 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003969 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003970 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003971 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003972 }
3973
Douglas Gregorc7469372011-05-04 21:55:00 +00003974 // Add the value of this argument to the list of converted
3975 // arguments. We use the bitwidth and signedness of the template
3976 // parameter.
3977 if (Arg->isValueDependent()) {
3978 // The argument is value-dependent. Create a new
3979 // TemplateArgument with the converted expression.
3980 Converted = TemplateArgument(Arg);
3981 return Owned(Arg);
3982 }
3983
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003984 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003985 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003986 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003987
Douglas Gregorc7469372011-05-04 21:55:00 +00003988 if (ParamType->isBooleanType()) {
3989 // Value must be zero or one.
3990 Value = Value != 0;
3991 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3992 if (Value.getBitWidth() != AllowedBits)
3993 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003994 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003995 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003996 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003997
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003998 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003999 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00004000 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00004001 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00004002 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00004003 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00004004
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004005 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00004006 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00004007 && (OldValue.isSigned() && OldValue.isNegative())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004008 Diag(Arg->getLocStart(), diag::warn_template_arg_negative)
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004009 << OldValue.toString(10) << Value.toString(10) << Param->getType()
4010 << Arg->getSourceRange();
4011 Diag(Param->getLocation(), diag::note_template_param_here);
4012 }
Douglas Gregorc7469372011-05-04 21:55:00 +00004013
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004014 // Complain if we overflowed the template parameter's type.
4015 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00004016 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004017 RequiredBits = OldValue.getActiveBits();
4018 else if (OldValue.isUnsigned())
4019 RequiredBits = OldValue.getActiveBits() + 1;
4020 else
4021 RequiredBits = OldValue.getMinSignedBits();
4022 if (RequiredBits > AllowedBits) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004023 Diag(Arg->getLocStart(),
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004024 diag::warn_template_arg_too_large)
4025 << OldValue.toString(10) << Value.toString(10) << Param->getType()
4026 << Arg->getSourceRange();
4027 Diag(Param->getLocation(), diag::note_template_param_here);
4028 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00004029 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00004030
John McCall833ca992009-10-29 08:12:44 +00004031 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00004032 ParamType->isEnumeralType()
4033 ? Context.getCanonicalType(ParamType)
4034 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00004035 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00004036 }
Douglas Gregora35284b2009-02-11 00:19:33 +00004037
Richard Smith4f870622011-10-27 22:11:44 +00004038 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00004039 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
4040
Douglas Gregorb7a09262010-04-01 18:32:35 +00004041 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
4042 // from a template argument of type std::nullptr_t to a non-type
4043 // template parameter of type pointer to object, pointer to
4044 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004045 if (ArgType->isNullPtrType()) {
4046 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
4047 Converted = TemplateArgument((NamedDecl *)0);
4048 return Owned(Arg);
4049 }
4050
4051 if (ParamType->isNullPtrType()) {
4052 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
4053 Converted = TemplateArgument(Zero, Context.NullPtrTy);
4054 return Owned(Arg);
4055 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004056 }
4057
Douglas Gregorb86b0572009-02-11 01:18:59 +00004058 // Handle pointer-to-function, reference-to-function, and
4059 // pointer-to-member-function all in (roughly) the same way.
4060 if (// -- For a non-type template-parameter of type pointer to
4061 // function, only the function-to-pointer conversion (4.3) is
4062 // applied. If the template-argument represents a set of
4063 // overloaded functions (or a pointer to such), the matching
4064 // function is selected from the set (13.4).
4065 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004066 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004067 // -- For a non-type template-parameter of type reference to
4068 // function, no conversions apply. If the template-argument
4069 // represents a set of overloaded functions, the matching
4070 // function is selected from the set (13.4).
4071 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004072 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004073 // -- For a non-type template-parameter of type pointer to
4074 // member function, no conversions apply. If the
4075 // template-argument represents a set of overloaded member
4076 // functions, the matching member function is selected from
4077 // the set (13.4).
4078 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004079 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00004080 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004081
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004082 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004083 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004084 true,
4085 FoundResult)) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004086 if (DiagnoseUseOfDecl(Fn, Arg->getLocStart()))
John Wiegley429bb272011-04-08 18:41:53 +00004087 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004088
4089 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4090 ArgType = Arg->getType();
4091 } else
John Wiegley429bb272011-04-08 18:41:53 +00004092 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004093 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004094
John Wiegley429bb272011-04-08 18:41:53 +00004095 if (!ParamType->isMemberPointerType()) {
4096 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4097 ParamType,
4098 Arg, Converted))
4099 return ExprError();
4100 return Owned(Arg);
4101 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004102
John McCallf85e1932011-06-15 23:02:42 +00004103 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00004104 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00004105 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004106 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4107 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004108 } else if (!Context.hasSameUnqualifiedType(ArgType,
4109 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00004110 // We can't perform this conversion.
Daniel Dunbar96a00142012-03-09 18:35:03 +00004111 Diag(Arg->getLocStart(),
Douglas Gregora35284b2009-02-11 00:19:33 +00004112 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004113 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00004114 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004115 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004116 }
Mike Stump1eb44332009-09-09 15:08:12 +00004117
John Wiegley429bb272011-04-08 18:41:53 +00004118 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4119 return ExprError();
4120 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00004121 }
4122
Chris Lattnerfe90de72009-02-20 21:37:53 +00004123 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004124 // -- for a non-type template-parameter of type pointer to
4125 // object, qualification conversions (4.4) and the
4126 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00004127 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00004128 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004129 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004130
John Wiegley429bb272011-04-08 18:41:53 +00004131 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4132 ParamType,
4133 Arg, Converted))
4134 return ExprError();
4135 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004136 }
Mike Stump1eb44332009-09-09 15:08:12 +00004137
Ted Kremenek6217b802009-07-29 21:53:49 +00004138 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004139 // -- For a non-type template-parameter of type reference to
4140 // object, no conversions apply. The type referred to by the
4141 // reference may be more cv-qualified than the (otherwise
4142 // identical) type of the template-argument. The
4143 // template-parameter is bound directly to the
4144 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00004145 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004146 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004147
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004148 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004149 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
4150 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004151 true,
4152 FoundResult)) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004153 if (DiagnoseUseOfDecl(Fn, Arg->getLocStart()))
John Wiegley429bb272011-04-08 18:41:53 +00004154 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004155
4156 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4157 ArgType = Arg->getType();
4158 } else
John Wiegley429bb272011-04-08 18:41:53 +00004159 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004160 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004161
John Wiegley429bb272011-04-08 18:41:53 +00004162 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4163 ParamType,
4164 Arg, Converted))
4165 return ExprError();
4166 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004167 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004168
4169 // -- For a non-type template-parameter of type pointer to data
4170 // member, qualification conversions (4.4) are applied.
4171 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4172
John McCallf85e1932011-06-15 23:02:42 +00004173 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004174 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004175 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004176 } else if (IsQualificationConversion(ArgType, ParamType, false,
4177 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004178 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4179 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004180 } else {
4181 // We can't perform this conversion.
Daniel Dunbar96a00142012-03-09 18:35:03 +00004182 Diag(Arg->getLocStart(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004183 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004184 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004185 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004186 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004187 }
4188
John Wiegley429bb272011-04-08 18:41:53 +00004189 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4190 return ExprError();
4191 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004192}
4193
4194/// \brief Check a template argument against its corresponding
4195/// template template parameter.
4196///
4197/// This routine implements the semantics of C++ [temp.arg.template].
4198/// It returns true if an error occurred, and false otherwise.
4199bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004200 const TemplateArgumentLoc &Arg) {
4201 TemplateName Name = Arg.getArgument().getAsTemplate();
4202 TemplateDecl *Template = Name.getAsTemplateDecl();
4203 if (!Template) {
4204 // Any dependent template name is fine.
4205 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4206 return false;
4207 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004208
Richard Smith3e4c6c42011-05-05 21:57:07 +00004209 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004210 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004211 // the name of a class template or an alias template, expressed as an
4212 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004213 // primary class templates are considered when matching the
4214 // template template argument with the corresponding parameter;
4215 // partial specializations are not considered even if their
4216 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004217 //
4218 // Note that we also allow template template parameters here, which
4219 // will happen when we are dealing with, e.g., class template
4220 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004221 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004222 !isa<TemplateTemplateParmDecl>(Template) &&
4223 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004224 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004225 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004226 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004227 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004228 << Template;
4229 }
4230
4231 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4232 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004233 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004234 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004235 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004236}
4237
Douglas Gregor02024a92010-03-28 02:42:43 +00004238/// \brief Given a non-type template argument that refers to a
4239/// declaration and the type of its corresponding non-type template
4240/// parameter, produce an expression that properly refers to that
4241/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004242ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004243Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4244 QualType ParamType,
4245 SourceLocation Loc) {
4246 assert(Arg.getKind() == TemplateArgument::Declaration &&
4247 "Only declaration template arguments permitted here");
4248 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4249
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004250 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004251 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4252 // If the value is a class member, we might have a pointer-to-member.
4253 // Determine whether the non-type template template parameter is of
4254 // pointer-to-member type. If so, we need to build an appropriate
4255 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4256 // would refer to the member itself.
4257 if (ParamType->isMemberPointerType()) {
4258 QualType ClassType
4259 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4260 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004261 = NestedNameSpecifier::Create(Context, 0, false,
4262 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004263 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004264 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004265
4266 // The actual value-ness of this is unimportant, but for
4267 // internal consistency's sake, references to instance methods
4268 // are r-values.
4269 ExprValueKind VK = VK_LValue;
4270 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4271 VK = VK_RValue;
4272
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004273 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004274 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004275 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004276 Loc,
4277 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004278 if (RefExpr.isInvalid())
4279 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004280
John McCall2de56d12010-08-25 11:45:40 +00004281 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004282
Douglas Gregorc0c83002010-04-30 21:46:38 +00004283 // We might need to perform a trailing qualification conversion, since
4284 // the element type on the parameter could be more qualified than the
4285 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004286 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004287 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004288 ParamType.getUnqualifiedType(), false,
4289 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004290 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004291
Douglas Gregor02024a92010-03-28 02:42:43 +00004292 assert(!RefExpr.isInvalid() &&
4293 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004294 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004295 return move(RefExpr);
4296 }
4297 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004298
Douglas Gregor02024a92010-03-28 02:42:43 +00004299 QualType T = VD->getType().getNonReferenceType();
4300 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004301 // When the non-type template parameter is a pointer, take the
4302 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004303 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004304 if (RefExpr.isInvalid())
4305 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004306
4307 if (T->isFunctionType() || T->isArrayType()) {
4308 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004309 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4310 if (RefExpr.isInvalid())
4311 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004312
4313 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004314 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004315
Douglas Gregorb7a09262010-04-01 18:32:35 +00004316 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004317 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004318 }
4319
John McCallf89e55a2010-11-18 06:31:45 +00004320 ExprValueKind VK = VK_RValue;
4321
Douglas Gregor02024a92010-03-28 02:42:43 +00004322 // If the non-type template parameter has reference type, qualify the
4323 // resulting declaration reference with the extra qualifiers on the
4324 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004325 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4326 VK = VK_LValue;
4327 T = Context.getQualifiedType(T,
4328 TargetRef->getPointeeType().getQualifiers());
4329 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004330
John McCallf89e55a2010-11-18 06:31:45 +00004331 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004332}
4333
4334/// \brief Construct a new expression that refers to the given
4335/// integral template argument with the given source-location
4336/// information.
4337///
4338/// This routine takes care of the mapping from an integral template
4339/// argument (which may have any integral type) to the appropriate
4340/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004341ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004342Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4343 SourceLocation Loc) {
4344 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004345 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004346 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004347 if (T->isAnyCharacterType()) {
4348 CharacterLiteral::CharacterKind Kind;
4349 if (T->isWideCharType())
4350 Kind = CharacterLiteral::Wide;
4351 else if (T->isChar16Type())
4352 Kind = CharacterLiteral::UTF16;
4353 else if (T->isChar32Type())
4354 Kind = CharacterLiteral::UTF32;
4355 else
4356 Kind = CharacterLiteral::Ascii;
4357
Douglas Gregor02024a92010-03-28 02:42:43 +00004358 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004359 Arg.getAsIntegral()->getZExtValue(),
4360 Kind, T, Loc));
4361 }
4362
Douglas Gregor02024a92010-03-28 02:42:43 +00004363 if (T->isBooleanType())
4364 return Owned(new (Context) CXXBoolLiteralExpr(
4365 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004366 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004367
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004368 if (T->isNullPtrType())
4369 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4370
Chris Lattner223de242011-04-25 20:37:58 +00004371 // If this is an enum type that we're instantiating, we need to use an integer
4372 // type the same size as the enumerator. We don't want to build an
4373 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004374 QualType BT;
4375 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004376 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004377 else
4378 BT = T;
4379
John McCall4e9272d2011-07-15 07:47:58 +00004380 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4381 if (T->isEnumeralType()) {
4382 // FIXME: This is a hack. We need a better way to handle substituted
4383 // non-type template parameters.
4384 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4385 Context.getTrivialTypeSourceInfo(T, Loc),
4386 Loc, Loc);
4387 }
4388
4389 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004390}
4391
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004392/// \brief Match two template parameters within template parameter lists.
4393static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4394 bool Complain,
4395 Sema::TemplateParameterListEqualKind Kind,
4396 SourceLocation TemplateArgLoc) {
4397 // Check the actual kind (type, non-type, template).
4398 if (Old->getKind() != New->getKind()) {
4399 if (Complain) {
4400 unsigned NextDiag = diag::err_template_param_different_kind;
4401 if (TemplateArgLoc.isValid()) {
4402 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4403 NextDiag = diag::note_template_param_different_kind;
4404 }
4405 S.Diag(New->getLocation(), NextDiag)
4406 << (Kind != Sema::TPL_TemplateMatch);
4407 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4408 << (Kind != Sema::TPL_TemplateMatch);
4409 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004410
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004411 return false;
4412 }
4413
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004414 // Check that both are parameter packs are neither are parameter packs.
4415 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004416 // template template parameter, the template template parameter can have
4417 // a parameter pack where the template template argument does not.
4418 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4419 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4420 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004421 if (Complain) {
4422 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4423 if (TemplateArgLoc.isValid()) {
4424 S.Diag(TemplateArgLoc,
4425 diag::err_template_arg_template_params_mismatch);
4426 NextDiag = diag::note_template_parameter_pack_non_pack;
4427 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004428
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004429 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4430 : isa<NonTypeTemplateParmDecl>(New)? 1
4431 : 2;
4432 S.Diag(New->getLocation(), NextDiag)
4433 << ParamKind << New->isParameterPack();
4434 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4435 << ParamKind << Old->isParameterPack();
4436 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004437
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004438 return false;
4439 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004440
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004441 // For non-type template parameters, check the type of the parameter.
4442 if (NonTypeTemplateParmDecl *OldNTTP
4443 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4444 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004445
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004446 // If we are matching a template template argument to a template
4447 // template parameter and one of the non-type template parameter types
4448 // is dependent, then we must wait until template instantiation time
4449 // to actually compare the arguments.
4450 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4451 (OldNTTP->getType()->isDependentType() ||
4452 NewNTTP->getType()->isDependentType()))
4453 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004454
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004455 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4456 if (Complain) {
4457 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4458 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004459 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004460 diag::err_template_arg_template_params_mismatch);
4461 NextDiag = diag::note_template_nontype_parm_different_type;
4462 }
4463 S.Diag(NewNTTP->getLocation(), NextDiag)
4464 << NewNTTP->getType()
4465 << (Kind != Sema::TPL_TemplateMatch);
4466 S.Diag(OldNTTP->getLocation(),
4467 diag::note_template_nontype_parm_prev_declaration)
4468 << OldNTTP->getType();
4469 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004470
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004471 return false;
4472 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004473
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004474 return true;
4475 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004476
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004477 // For template template parameters, check the template parameter types.
4478 // The template parameter lists of template template
4479 // parameters must agree.
4480 if (TemplateTemplateParmDecl *OldTTP
4481 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004482 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004483 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4484 OldTTP->getTemplateParameters(),
4485 Complain,
4486 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004487 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004488 : Kind),
4489 TemplateArgLoc);
4490 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004491
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004492 return true;
4493}
Douglas Gregor02024a92010-03-28 02:42:43 +00004494
Douglas Gregora0347822011-01-13 00:08:50 +00004495/// \brief Diagnose a known arity mismatch when comparing template argument
4496/// lists.
4497static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004498void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004499 TemplateParameterList *New,
4500 TemplateParameterList *Old,
4501 Sema::TemplateParameterListEqualKind Kind,
4502 SourceLocation TemplateArgLoc) {
4503 unsigned NextDiag = diag::err_template_param_list_different_arity;
4504 if (TemplateArgLoc.isValid()) {
4505 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4506 NextDiag = diag::note_template_param_list_different_arity;
4507 }
4508 S.Diag(New->getTemplateLoc(), NextDiag)
4509 << (New->size() > Old->size())
4510 << (Kind != Sema::TPL_TemplateMatch)
4511 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4512 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4513 << (Kind != Sema::TPL_TemplateMatch)
4514 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4515}
4516
Douglas Gregorddc29e12009-02-06 22:42:48 +00004517/// \brief Determine whether the given template parameter lists are
4518/// equivalent.
4519///
Mike Stump1eb44332009-09-09 15:08:12 +00004520/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004521/// source code as part of a new template declaration.
4522///
4523/// \param Old The old template parameter list, typically found via
4524/// name lookup of the template declared with this template parameter
4525/// list.
4526///
4527/// \param Complain If true, this routine will produce a diagnostic if
4528/// the template parameter lists are not equivalent.
4529///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004530/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004531///
4532/// \param TemplateArgLoc If this source location is valid, then we
4533/// are actually checking the template parameter list of a template
4534/// argument (New) against the template parameter list of its
4535/// corresponding template template parameter (Old). We produce
4536/// slightly different diagnostics in this scenario.
4537///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004538/// \returns True if the template parameter lists are equal, false
4539/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004540bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004541Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4542 TemplateParameterList *Old,
4543 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004544 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004545 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004546 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4547 if (Complain)
4548 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4549 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004550
4551 return false;
4552 }
4553
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004554 // C++0x [temp.arg.template]p3:
4555 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004556 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004557 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004558 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004559 // template-parameter-list of P. [...]
4560 TemplateParameterList::iterator NewParm = New->begin();
4561 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004562 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004563 OldParmEnd = Old->end();
4564 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004565 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4566 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004567 if (NewParm == NewParmEnd) {
4568 if (Complain)
4569 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4570 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004571
Douglas Gregora0347822011-01-13 00:08:50 +00004572 return false;
4573 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004574
Douglas Gregora0347822011-01-13 00:08:50 +00004575 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4576 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004577 return false;
4578
Douglas Gregora0347822011-01-13 00:08:50 +00004579 ++NewParm;
4580 continue;
4581 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004582
Douglas Gregora0347822011-01-13 00:08:50 +00004583 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004584 // [...] When P's template- parameter-list contains a template parameter
4585 // pack (14.5.3), the template parameter pack will match zero or more
4586 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004587 // template-parameter-list of A with the same type and form as the
4588 // template parameter pack in P (ignoring whether those template
4589 // parameters are template parameter packs).
4590 for (; NewParm != NewParmEnd; ++NewParm) {
4591 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4592 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004593 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004594 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004595 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004596
Douglas Gregora0347822011-01-13 00:08:50 +00004597 // Make sure we exhausted all of the arguments.
4598 if (NewParm != NewParmEnd) {
4599 if (Complain)
4600 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4601 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004602
Douglas Gregora0347822011-01-13 00:08:50 +00004603 return false;
4604 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004605
Douglas Gregorddc29e12009-02-06 22:42:48 +00004606 return true;
4607}
4608
4609/// \brief Check whether a template can be declared within this scope.
4610///
4611/// If the template declaration is valid in this scope, returns
4612/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004613bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004614Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004615 if (!S)
4616 return false;
4617
Douglas Gregorddc29e12009-02-06 22:42:48 +00004618 // Find the nearest enclosing declaration scope.
4619 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4620 (S->getFlags() & Scope::TemplateParamScope) != 0)
4621 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004622
Douglas Gregorddc29e12009-02-06 22:42:48 +00004623 // C++ [temp]p2:
4624 // A template-declaration can appear only as a namespace scope or
4625 // class scope declaration.
4626 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004627 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4628 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004629 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004630 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004631
Eli Friedman1503f772009-07-31 01:43:05 +00004632 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004633 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004634
4635 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4636 return false;
4637
Mike Stump1eb44332009-09-09 15:08:12 +00004638 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004639 diag::err_template_outside_namespace_or_class_scope)
4640 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004641}
Douglas Gregorcc636682009-02-17 23:15:12 +00004642
Douglas Gregord5cb8762009-10-07 00:13:32 +00004643/// \brief Determine what kind of template specialization the given declaration
4644/// is.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00004645static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004646 if (!D)
4647 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004648
Douglas Gregorf6b11852009-10-08 15:14:33 +00004649 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4650 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004651 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4652 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004653 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4654 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004655
Douglas Gregord5cb8762009-10-07 00:13:32 +00004656 return TSK_Undeclared;
4657}
4658
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004659/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004660/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004661///
Douglas Gregor9302da62009-10-14 23:50:59 +00004662/// This routine determines whether a template specialization can be declared
4663/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004664///
4665/// \param S the semantic analysis object for which this check is being
4666/// performed.
4667///
4668/// \param Specialized the entity being specialized or instantiated, which
4669/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004670/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004671/// member class).
4672///
4673/// \param PrevDecl the previous declaration of this entity, if any.
4674///
4675/// \param Loc the location of the explicit specialization or instantiation of
4676/// this entity.
4677///
4678/// \param IsPartialSpecialization whether this is a partial specialization of
4679/// a class template.
4680///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004681/// \returns true if there was an error that we cannot recover from, false
4682/// otherwise.
4683static bool CheckTemplateSpecializationScope(Sema &S,
4684 NamedDecl *Specialized,
4685 NamedDecl *PrevDecl,
4686 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004687 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004688 // Keep these "kind" numbers in sync with the %select statements in the
4689 // various diagnostics emitted by this routine.
4690 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004691 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004692 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004693 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004694 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004695 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004696 EntityKind = 3;
4697 else if (isa<VarDecl>(Specialized))
4698 EntityKind = 4;
4699 else if (isa<RecordDecl>(Specialized))
4700 EntityKind = 5;
Richard Smith1af83c42012-03-23 03:33:32 +00004701 else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus0x)
4702 EntityKind = 6;
Douglas Gregord5cb8762009-10-07 00:13:32 +00004703 else {
Richard Smith1af83c42012-03-23 03:33:32 +00004704 S.Diag(Loc, diag::err_template_spec_unknown_kind)
4705 << S.getLangOpts().CPlusPlus0x;
Douglas Gregor9302da62009-10-14 23:50:59 +00004706 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004707 return true;
4708 }
4709
Douglas Gregor88b70942009-02-25 22:02:03 +00004710 // C++ [temp.expl.spec]p2:
4711 // An explicit specialization shall be declared in the namespace
4712 // of which the template is a member, or, for member templates, in
4713 // the namespace of which the enclosing class or enclosing class
4714 // template is a member. An explicit specialization of a member
4715 // function, member class or static data member of a class
4716 // template shall be declared in the namespace of which the class
4717 // template is a member. Such a declaration may also be a
4718 // definition. If the declaration is not a definition, the
4719 // specialization may be defined later in the name- space in which
4720 // the explicit specialization was declared, or in a namespace
4721 // that encloses the one in which the explicit specialization was
4722 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004723 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004724 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004725 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004726 return true;
4727 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004728
Douglas Gregor0a407472009-10-07 17:30:37 +00004729 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004730 if (S.getLangOpts().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004731 // Do not warn for class scope explicit specialization during
4732 // instantiation, warning was already emitted during pattern
4733 // semantic analysis.
4734 if (!S.ActiveTemplateInstantiations.size())
4735 S.Diag(Loc, diag::ext_function_specialization_in_class)
4736 << Specialized;
4737 } else {
4738 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4739 << Specialized;
4740 return true;
4741 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004742 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004743
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004744 if (S.CurContext->isRecord() &&
4745 !S.CurContext->Equals(Specialized->getDeclContext())) {
4746 // Make sure that we're specializing in the right record context.
4747 // Otherwise, things can go horribly wrong.
4748 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4749 << Specialized;
4750 return true;
4751 }
4752
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004753 // C++ [temp.class.spec]p6:
4754 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004755 // in any namespace scope in which its definition may be defined (14.5.1
4756 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004757 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004758 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004759 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004760 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004761 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004762 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4763 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004764 // C++ [temp.exp.spec]p2:
4765 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004766 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004767 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004768 // An explicit specialization of a member function, member class or
4769 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004770 // namespace of which the class template is a member.
4771 //
4772 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004773 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004774 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004775 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4776 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4777 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4778 assert(!IsCPlusPlus0xExtension &&
4779 "DC encloses TU but isn't in enclosing namespace set");
4780 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004781 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004782 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4783 int Diag;
4784 if (!IsCPlusPlus0xExtension)
4785 Diag = diag::err_template_spec_decl_out_of_scope;
David Blaikie4e4d0842012-03-11 07:00:24 +00004786 else if (!S.getLangOpts().CPlusPlus0x)
Richard Smithebaf0e62011-10-18 20:49:44 +00004787 Diag = diag::ext_template_spec_decl_out_of_scope;
4788 else
4789 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4790 S.Diag(Loc, Diag)
4791 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4792 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004793
Douglas Gregor9302da62009-10-14 23:50:59 +00004794 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004795 ComplainedAboutScope =
David Blaikie4e4d0842012-03-11 07:00:24 +00004796 !(IsCPlusPlus0xExtension && S.getLangOpts().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004797 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004798 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004799
4800 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004801 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004802 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004803 // specializations of function templates, static data members, and member
4804 // functions, so we skip the check here for those kinds of entities.
4805 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004806 // Should we refactor that check, so that it occurs later?
4807 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004808 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4809 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004810 if (isa<TranslationUnitDecl>(SpecializedContext))
4811 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4812 << EntityKind << Specialized;
4813 else if (isa<NamespaceDecl>(SpecializedContext))
4814 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4815 << EntityKind << Specialized
4816 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004817
Douglas Gregor9302da62009-10-14 23:50:59 +00004818 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004819 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004820
Douglas Gregord5cb8762009-10-07 00:13:32 +00004821 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004822
Douglas Gregor88b70942009-02-25 22:02:03 +00004823 return false;
4824}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004825
Douglas Gregorbacb9492011-01-03 21:13:47 +00004826/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4827/// that checks non-type template partial specialization arguments.
4828static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4829 NonTypeTemplateParmDecl *Param,
4830 const TemplateArgument *Args,
4831 unsigned NumArgs) {
4832 for (unsigned I = 0; I != NumArgs; ++I) {
4833 if (Args[I].getKind() == TemplateArgument::Pack) {
4834 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004835 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004836 Args[I].pack_size()))
4837 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004838
Douglas Gregore94866f2009-06-12 21:21:02 +00004839 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004840 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004841
Douglas Gregorbacb9492011-01-03 21:13:47 +00004842 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004843 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004844 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004845 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004846
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004847 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004848 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4849 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004850
4851 // Strip off any implicit casts we added as part of type checking.
4852 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4853 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004854
Douglas Gregore94866f2009-06-12 21:21:02 +00004855 // C++ [temp.class.spec]p8:
4856 // A non-type argument is non-specialized if it is the name of a
4857 // non-type parameter. All other non-type arguments are
4858 // specialized.
4859 //
4860 // Below, we check the two conditions that only apply to
4861 // specialized non-type arguments, so skip any non-specialized
4862 // arguments.
4863 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004864 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004865 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004866
Douglas Gregore94866f2009-06-12 21:21:02 +00004867 // C++ [temp.class.spec]p9:
4868 // Within the argument list of a class template partial
4869 // specialization, the following restrictions apply:
4870 // -- A partially specialized non-type argument expression
4871 // shall not involve a template parameter of the partial
4872 // specialization except when the argument expression is a
4873 // simple identifier.
4874 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004875 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004876 diag::err_dependent_non_type_arg_in_partial_spec)
4877 << ArgExpr->getSourceRange();
4878 return true;
4879 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004880
Douglas Gregore94866f2009-06-12 21:21:02 +00004881 // -- The type of a template parameter corresponding to a
4882 // specialized non-type argument shall not be dependent on a
4883 // parameter of the specialization.
4884 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004885 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004886 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4887 << Param->getType()
4888 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004889 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004890 return true;
4891 }
4892 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004893
Douglas Gregorbacb9492011-01-03 21:13:47 +00004894 return false;
4895}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004896
Douglas Gregorbacb9492011-01-03 21:13:47 +00004897/// \brief Check the non-type template arguments of a class template
4898/// partial specialization according to C++ [temp.class.spec]p9.
4899///
4900/// \param TemplateParams the template parameters of the primary class
4901/// template.
4902///
4903/// \param TemplateArg the template arguments of the class template
4904/// partial specialization.
4905///
4906/// \returns true if there was an error, false otherwise.
4907static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4908 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004909 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004910 const TemplateArgument *ArgList = TemplateArgs.data();
4911
4912 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4913 NonTypeTemplateParmDecl *Param
4914 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4915 if (!Param)
4916 continue;
4917
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004918 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004919 &ArgList[I], 1))
4920 return true;
4921 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004922
4923 return false;
4924}
4925
John McCalld226f652010-08-21 09:40:31 +00004926DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004927Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4928 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004929 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004930 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004931 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004932 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004933 SourceLocation TemplateNameLoc,
4934 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004935 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004936 SourceLocation RAngleLoc,
4937 AttributeList *Attr,
4938 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004939 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004940
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004941 // NOTE: KWLoc is the location of the tag keyword. This will instead
4942 // store the location of the outermost template keyword in the declaration.
4943 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4944 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4945
Douglas Gregorcc636682009-02-17 23:15:12 +00004946 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004947 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004948 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004949 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4950
4951 if (!ClassTemplate) {
4952 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004953 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004954 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4955 return true;
4956 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004957
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004958 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004959 bool isPartialSpecialization = false;
4960
Douglas Gregor88b70942009-02-25 22:02:03 +00004961 // Check the validity of the template headers that introduce this
4962 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004963 // FIXME: We probably shouldn't complain about these headers for
4964 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004965 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004966 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004967 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4968 TemplateNameLoc,
4969 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004970 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004971 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004972 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004973 isExplicitSpecialization,
4974 Invalid);
4975 if (Invalid)
4976 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004977
Douglas Gregor05396e22009-08-25 17:23:04 +00004978 if (TemplateParams && TemplateParams->size() > 0) {
4979 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004980
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004981 if (TUK == TUK_Friend) {
4982 Diag(KWLoc, diag::err_partial_specialization_friend)
4983 << SourceRange(LAngleLoc, RAngleLoc);
4984 return true;
4985 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004986
Douglas Gregor05396e22009-08-25 17:23:04 +00004987 // C++ [temp.class.spec]p10:
4988 // The template parameter list of a specialization shall not
4989 // contain default template argument values.
4990 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4991 Decl *Param = TemplateParams->getParam(I);
4992 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4993 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004994 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004995 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004996 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004997 }
4998 } else if (NonTypeTemplateParmDecl *NTTP
4999 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
5000 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00005001 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00005002 diag::err_default_arg_in_partial_spec)
5003 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00005004 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00005005 }
5006 } else {
5007 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00005008 if (TTP->hasDefaultArgument()) {
5009 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00005010 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00005011 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00005012 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00005013 }
5014 }
5015 }
Douglas Gregora735b202009-10-13 14:39:41 +00005016 } else if (TemplateParams) {
5017 if (TUK == TUK_Friend)
5018 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00005019 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00005020 SourceRange(TemplateParams->getTemplateLoc(),
5021 TemplateParams->getRAngleLoc()))
5022 << SourceRange(LAngleLoc, RAngleLoc);
5023 else
5024 isExplicitSpecialization = true;
5025 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00005026 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00005027 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005028 isExplicitSpecialization = true;
5029 }
Douglas Gregor88b70942009-02-25 22:02:03 +00005030
Douglas Gregorcc636682009-02-17 23:15:12 +00005031 // Check that the specialization uses the same tag kind as the
5032 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005033 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5034 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005035 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005036 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005037 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005038 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00005039 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005040 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00005041 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005042 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005043 diag::note_previous_use);
5044 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5045 }
5046
Douglas Gregor40808ce2009-03-09 23:48:35 +00005047 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005048 TemplateArgumentListInfo TemplateArgs;
5049 TemplateArgs.setLAngleLoc(LAngleLoc);
5050 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005051 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00005052
Douglas Gregor925910d2011-01-03 20:35:03 +00005053 // Check for unexpanded parameter packs in any of the template arguments.
5054 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005055 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00005056 UPPC_PartialSpecialization))
5057 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005058
Douglas Gregorcc636682009-02-17 23:15:12 +00005059 // Check that the template argument list is well-formed for this
5060 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005061 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005062 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5063 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005064 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005065
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005066 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00005067 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00005068 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00005069 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00005070 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00005071 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00005072 return true;
5073
Douglas Gregor561f8122011-07-01 01:22:09 +00005074 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005075 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00005076 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005077 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00005078 TemplateArgs.size(),
5079 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00005080 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
5081 << ClassTemplate->getDeclName();
5082 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00005083 }
5084 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005085
Douglas Gregorcc636682009-02-17 23:15:12 +00005086 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005087 ClassTemplateSpecializationDecl *PrevDecl = 0;
5088
5089 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005090 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005091 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005092 = ClassTemplate->findPartialSpecialization(Converted.data(),
5093 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005094 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005095 else
5096 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005097 = ClassTemplate->findSpecialization(Converted.data(),
5098 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00005099
5100 ClassTemplateSpecializationDecl *Specialization = 0;
5101
Douglas Gregor88b70942009-02-25 22:02:03 +00005102 // Check whether we can declare a class template specialization in
5103 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005104 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005105 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
5106 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00005107 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005108 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005109
Douglas Gregorb88e8882009-07-30 17:40:51 +00005110 // The canonical type
5111 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005112 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005113 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00005114 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005115 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005116 // arguments was referenced but not declared, or we're only
5117 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005118 // declaration node as our own, updating its source location and
5119 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005120 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005121 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005122 if (TemplateParameterLists.size() > 0) {
5123 Specialization->setTemplateParameterListsInfo(Context,
5124 TemplateParameterLists.size(),
5125 (TemplateParameterList**) TemplateParameterLists.release());
5126 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005127 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00005128 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005129 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00005130 // Build the canonical type that describes the converted template
5131 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00005132 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
5133 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005134 Converted.data(),
5135 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005136
5137 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005138 ClassTemplate->getInjectedClassNameSpecialization())) {
5139 // C++ [temp.class.spec]p9b3:
5140 //
5141 // -- The argument list of the specialization shall not be identical
5142 // to the implicit argument list of the primary template.
5143 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00005144 << (TUK == TUK_Definition)
5145 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00005146 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5147 ClassTemplate->getIdentifier(),
5148 TemplateNameLoc,
5149 Attr,
5150 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005151 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005152 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005153 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005154 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005155
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005156 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005157 ClassTemplatePartialSpecializationDecl *PrevPartial
5158 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005159 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005160 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005161 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005162 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005163 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005164 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005165 TemplateParams,
5166 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005167 Converted.data(),
5168 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005169 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005170 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005171 PrevPartial,
5172 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005173 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005174 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005175 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005176 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005177 (TemplateParameterList**) TemplateParameterLists.release());
5178 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005179
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005180 if (!PrevPartial)
5181 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005182 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005183
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005184 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005185 // template specialization, make a note of that.
5186 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5187 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005188
Douglas Gregor031a5882009-06-13 00:26:55 +00005189 // Check that all of the template parameters of the class template
5190 // partial specialization are deducible from the template
5191 // arguments. If not, this class template partial specialization
5192 // will never be used.
Benjamin Kramer013b3662012-01-30 16:17:39 +00005193 llvm::SmallBitVector DeducibleParams(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005194 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005195 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005196 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005197
Benjamin Kramer013b3662012-01-30 16:17:39 +00005198 if (!DeducibleParams.all()) {
5199 unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count();
Douglas Gregor031a5882009-06-13 00:26:55 +00005200 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5201 << (NumNonDeducible > 1)
5202 << SourceRange(TemplateNameLoc, RAngleLoc);
5203 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5204 if (!DeducibleParams[I]) {
5205 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5206 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005207 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005208 diag::note_partial_spec_unused_parameter)
5209 << Param->getDeclName();
5210 else
Mike Stump1eb44332009-09-09 15:08:12 +00005211 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005212 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005213 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005214 }
5215 }
5216 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005217 } else {
5218 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005219 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005220 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005221 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005222 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005223 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005224 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005225 Converted.data(),
5226 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005227 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005228 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005229 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005230 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005231 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005232 (TemplateParameterList**) TemplateParameterLists.release());
5233 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005234
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005235 if (!PrevDecl)
5236 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005237
5238 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005239 }
5240
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005241 // C++ [temp.expl.spec]p6:
5242 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005243 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005244 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005245 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005246 // use occurs; no diagnostic is required.
5247 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005248 bool Okay = false;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005249 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005250 // Is there any previous explicit specialization declaration?
5251 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5252 Okay = true;
5253 break;
5254 }
5255 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005256
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005257 if (!Okay) {
5258 SourceRange Range(TemplateNameLoc, RAngleLoc);
5259 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5260 << Context.getTypeDeclType(Specialization) << Range;
5261
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005262 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005263 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005264 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005265 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005266 return true;
5267 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005268 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005269
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005270 // If this is not a friend, note that this is an explicit specialization.
5271 if (TUK != TUK_Friend)
5272 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005273
5274 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005275 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005276 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005277 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005278 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005279 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005280 Diag(Def->getLocation(), diag::note_previous_definition);
5281 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005282 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005283 }
5284 }
5285
John McCall7f1b9872010-12-18 03:30:47 +00005286 if (Attr)
5287 ProcessDeclAttributeList(S, Specialization, Attr);
5288
Douglas Gregord023aec2011-09-09 20:53:38 +00005289 if (ModulePrivateLoc.isValid())
5290 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5291 << (isPartialSpecialization? 1 : 0)
5292 << FixItHint::CreateRemoval(ModulePrivateLoc);
5293
Douglas Gregorfc705b82009-02-26 22:19:44 +00005294 // Build the fully-sugared type for this class template
5295 // specialization as the user wrote in the specialization
5296 // itself. This means that we'll pretty-print the type retrieved
5297 // from the specialization's declaration the way that the user
5298 // actually wrote the specialization, rather than formatting the
5299 // name based on the "canonical" representation used to store the
5300 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005301 TypeSourceInfo *WrittenTy
5302 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5303 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005304 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005305 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005306 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005307 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005308 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005309
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005310 // C++ [temp.expl.spec]p9:
5311 // A template explicit specialization is in the scope of the
5312 // namespace in which the template was defined.
5313 //
5314 // We actually implement this paragraph where we set the semantic
5315 // context (in the creation of the ClassTemplateSpecializationDecl),
5316 // but we also maintain the lexical context where the actual
5317 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005318 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005319
Douglas Gregorcc636682009-02-17 23:15:12 +00005320 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005321 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005322 Specialization->startDefinition();
5323
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005324 if (TUK == TUK_Friend) {
5325 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5326 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005327 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005328 /*FIXME:*/KWLoc);
5329 Friend->setAccess(AS_public);
5330 CurContext->addDecl(Friend);
5331 } else {
5332 // Add the specialization into its lexical context, so that it can
5333 // be seen when iterating through the list of declarations in that
5334 // context. However, specializations are not found by name lookup.
5335 CurContext->addDecl(Specialization);
5336 }
John McCalld226f652010-08-21 09:40:31 +00005337 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005338}
Douglas Gregord57959a2009-03-27 23:10:48 +00005339
John McCalld226f652010-08-21 09:40:31 +00005340Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005341 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005342 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005343 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005344}
5345
John McCalld226f652010-08-21 09:40:31 +00005346Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005347 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005348 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005349 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005350 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005351
Douglas Gregor52591bf2009-06-24 00:54:41 +00005352 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005353 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005354 }
Mike Stump1eb44332009-09-09 15:08:12 +00005355
Douglas Gregor52591bf2009-06-24 00:54:41 +00005356 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005357
Douglas Gregor45fa5602011-11-07 20:56:01 +00005358 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005359 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005360 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005361 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005362 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005363 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005364 FunctionTemplate->getTemplatedDecl());
5365 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5366 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5367 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005368}
5369
John McCall75042392010-02-11 01:33:53 +00005370/// \brief Strips various properties off an implicit instantiation
5371/// that has just been explicitly specialized.
5372static void StripImplicitInstantiation(NamedDecl *D) {
Rafael Espindola860097c2012-02-23 04:17:32 +00005373 // FIXME: "make check" is clean if the call to dropAttrs() is commented out.
Sean Huntcf807c42010-08-18 23:23:40 +00005374 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005375
5376 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5377 FD->setInlineSpecified(false);
5378 }
5379}
5380
Nico Weberd1d512a2012-01-09 19:52:25 +00005381/// \brief Compute the diagnostic location for an explicit instantiation
5382// declaration or definition.
5383static SourceLocation DiagLocForExplicitInstantiation(
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005384 NamedDecl* D, SourceLocation PointOfInstantiation) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005385 // Explicit instantiations following a specialization have no effect and
5386 // hence no PointOfInstantiation. In that case, walk decl backwards
5387 // until a valid name loc is found.
5388 SourceLocation PrevDiagLoc = PointOfInstantiation;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005389 for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
5390 Prev = Prev->getPreviousDecl()) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005391 PrevDiagLoc = Prev->getLocation();
5392 }
5393 assert(PrevDiagLoc.isValid() &&
5394 "Explicit instantiation without point of instantiation?");
5395 return PrevDiagLoc;
5396}
5397
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005398/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005399/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005400/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005401/// new specialization/instantiation will have any effect.
5402///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005403/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005404/// instantiation.
5405///
5406/// \param NewTSK the kind of the new explicit specialization or instantiation.
5407///
5408/// \param PrevDecl the previous declaration of the entity.
5409///
5410/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5411///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005412/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005413/// declaration was instantiated (either implicitly or explicitly).
5414///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005415/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005416/// specialization or instantiation has no effect and should be ignored.
5417///
5418/// \returns true if there was an error that should prevent the introduction of
5419/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005420bool
5421Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5422 TemplateSpecializationKind NewTSK,
5423 NamedDecl *PrevDecl,
5424 TemplateSpecializationKind PrevTSK,
5425 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005426 bool &HasNoEffect) {
5427 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005428
Douglas Gregor454885e2009-10-15 15:54:05 +00005429 switch (NewTSK) {
5430 case TSK_Undeclared:
5431 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005432 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005433
Douglas Gregor454885e2009-10-15 15:54:05 +00005434 case TSK_ExplicitSpecialization:
5435 switch (PrevTSK) {
5436 case TSK_Undeclared:
5437 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005438 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005439 // explicitly specialized or has merely been mentioned without any
5440 // instantiation.
5441 return false;
5442
5443 case TSK_ImplicitInstantiation:
5444 if (PrevPointOfInstantiation.isInvalid()) {
5445 // The declaration itself has not actually been instantiated, so it is
5446 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005447 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005448 return false;
5449 }
5450 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005451
Douglas Gregor454885e2009-10-15 15:54:05 +00005452 case TSK_ExplicitInstantiationDeclaration:
5453 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005454 assert((PrevTSK == TSK_ImplicitInstantiation ||
5455 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005456 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005457
Douglas Gregor454885e2009-10-15 15:54:05 +00005458 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005459 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005460 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005461 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005462 // implicit instantiation to take place, in every translation unit in
5463 // which such a use occurs; no diagnostic is required.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005464 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005465 // Is there any previous explicit specialization declaration?
5466 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5467 return false;
5468 }
5469
Douglas Gregor0d035142009-10-27 18:42:08 +00005470 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005471 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005472 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005473 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005474
Douglas Gregor454885e2009-10-15 15:54:05 +00005475 return true;
5476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005477
Douglas Gregor454885e2009-10-15 15:54:05 +00005478 case TSK_ExplicitInstantiationDeclaration:
5479 switch (PrevTSK) {
5480 case TSK_ExplicitInstantiationDeclaration:
5481 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005482 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005483 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005484
Douglas Gregor454885e2009-10-15 15:54:05 +00005485 case TSK_Undeclared:
5486 case TSK_ImplicitInstantiation:
5487 // We're explicitly instantiating something that may have already been
5488 // implicitly instantiated; that's fine.
5489 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005490
Douglas Gregor454885e2009-10-15 15:54:05 +00005491 case TSK_ExplicitSpecialization:
5492 // C++0x [temp.explicit]p4:
5493 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005494 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005495 // specialization for that template, the explicit instantiation has no
5496 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005497 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005498 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005499
Douglas Gregor454885e2009-10-15 15:54:05 +00005500 case TSK_ExplicitInstantiationDefinition:
5501 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005502 // If an entity is the subject of both an explicit instantiation
5503 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005504 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005505 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005506 diag::err_explicit_instantiation_declaration_after_definition);
Nico Weberff91d242011-12-23 20:58:04 +00005507
5508 // Explicit instantiations following a specialization have no effect and
5509 // hence no PrevPointOfInstantiation. In that case, walk decl backwards
5510 // until a valid name loc is found.
Nico Weberd1d512a2012-01-09 19:52:25 +00005511 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
5512 diag::note_explicit_instantiation_definition_here);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005513 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005514 return false;
5515 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005516
Douglas Gregor454885e2009-10-15 15:54:05 +00005517 case TSK_ExplicitInstantiationDefinition:
5518 switch (PrevTSK) {
5519 case TSK_Undeclared:
5520 case TSK_ImplicitInstantiation:
5521 // We're explicitly instantiating something that may have already been
5522 // implicitly instantiated; that's fine.
5523 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005524
Douglas Gregor454885e2009-10-15 15:54:05 +00005525 case TSK_ExplicitSpecialization:
5526 // C++ DR 259, C++0x [temp.explicit]p4:
5527 // For a given set of template parameters, if an explicit
5528 // instantiation of a template appears after a declaration of
5529 // an explicit specialization for that template, the explicit
5530 // instantiation has no effect.
5531 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005532 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005533 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005534 // has been explicitly specialized.
David Blaikie4e4d0842012-03-11 07:00:24 +00005535 Diag(NewLoc, getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00005536 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5537 diag::ext_explicit_instantiation_after_specialization)
5538 << PrevDecl;
5539 Diag(PrevDecl->getLocation(),
5540 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005541 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005542 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005543
Douglas Gregor454885e2009-10-15 15:54:05 +00005544 case TSK_ExplicitInstantiationDeclaration:
5545 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005546 // were previously asked to suppress instantiations. That's fine.
Nico Weberff91d242011-12-23 20:58:04 +00005547
5548 // C++0x [temp.explicit]p4:
5549 // For a given set of template parameters, if an explicit instantiation
5550 // of a template appears after a declaration of an explicit
5551 // specialization for that template, the explicit instantiation has no
5552 // effect.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005553 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Nico Weberff91d242011-12-23 20:58:04 +00005554 // Is there any previous explicit specialization declaration?
5555 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5556 HasNoEffect = true;
5557 break;
5558 }
5559 }
5560
Douglas Gregor454885e2009-10-15 15:54:05 +00005561 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005562
Douglas Gregor454885e2009-10-15 15:54:05 +00005563 case TSK_ExplicitInstantiationDefinition:
5564 // C++0x [temp.spec]p5:
5565 // For a given template and a given set of template-arguments,
5566 // - an explicit instantiation definition shall appear at most once
5567 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005568 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005569 << PrevDecl;
Nico Weberd1d512a2012-01-09 19:52:25 +00005570 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
Douglas Gregor0d035142009-10-27 18:42:08 +00005571 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005572 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005573 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005574 }
Douglas Gregor454885e2009-10-15 15:54:05 +00005575 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005576
David Blaikieb219cfc2011-09-23 05:06:16 +00005577 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005578}
5579
John McCallaf2094e2010-04-08 09:05:18 +00005580/// \brief Perform semantic analysis for the given dependent function
5581/// template specialization. The only possible way to get a dependent
5582/// function template specialization is with a friend declaration,
5583/// like so:
5584///
5585/// template <class T> void foo(T);
5586/// template <class T> class A {
5587/// friend void foo<>(T);
5588/// };
5589///
5590/// There really isn't any useful analysis we can do here, so we
5591/// just store the information.
5592bool
5593Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5594 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5595 LookupResult &Previous) {
5596 // Remove anything from Previous that isn't a function template in
5597 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005598 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005599 LookupResult::Filter F = Previous.makeFilter();
5600 while (F.hasNext()) {
5601 NamedDecl *D = F.next()->getUnderlyingDecl();
5602 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005603 !FDLookupContext->InEnclosingNamespaceSetOf(
5604 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005605 F.erase();
5606 }
5607 F.done();
5608
5609 // Should this be diagnosed here?
5610 if (Previous.empty()) return true;
5611
5612 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5613 ExplicitTemplateArgs);
5614 return false;
5615}
5616
Abramo Bagnarae03db982010-05-20 15:32:11 +00005617/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005618/// specialization.
5619///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005620/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005621/// explicit function template specialization. On successful completion,
5622/// the function declaration \p FD will become a function template
5623/// specialization.
5624///
5625/// \param FD the function declaration, which will be updated to become a
5626/// function template specialization.
5627///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005628/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5629/// if any. Note that this may be valid info even when 0 arguments are
5630/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5631/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005632///
Francois Pichet59e7c562011-07-08 06:21:47 +00005633/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005634/// this function specialization.
5635bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005636Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005637 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005638 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005639 // The set of function template specializations that could match this
5640 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005641 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005642
Sebastian Redl7a126a42010-08-31 00:36:30 +00005643 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005644 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5645 I != E; ++I) {
5646 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5647 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005648 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005649 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005650 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5651 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005652 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005653
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005654 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005655 // A trailing template-argument can be left unspecified in the
5656 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005657 // provided it can be deduced from the function argument type.
5658 // Perform template argument deduction to determine whether we may be
5659 // specializing this template.
5660 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005661 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005662 FunctionDecl *Specialization = 0;
5663 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005664 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005665 FD->getType(),
5666 Specialization,
5667 Info)) {
5668 // FIXME: Template argument deduction failed; record why it failed, so
5669 // that we can provide nifty diagnostics.
5670 (void)TDK;
5671 continue;
5672 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005673
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005674 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005675 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005676 }
5677 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005678
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005679 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005680 UnresolvedSetIterator Result
5681 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005682 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005683 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005684 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005685 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005686 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005687 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005688 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005689 return true;
John McCallc373d482010-01-27 01:50:18 +00005690
5691 // Ignore access information; it doesn't figure into redeclaration checking.
5692 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005693
5694 FunctionTemplateSpecializationInfo *SpecInfo
5695 = Specialization->getTemplateSpecializationInfo();
5696 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005697
5698 // Note: do not overwrite location info if previous template
5699 // specialization kind was explicit.
5700 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
Richard Smithff234882012-02-20 23:28:05 +00005701 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) {
Francois Pichet59e7c562011-07-08 06:21:47 +00005702 Specialization->setLocation(FD->getLocation());
Richard Smithff234882012-02-20 23:28:05 +00005703 // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr
5704 // function can differ from the template declaration with respect to
5705 // the constexpr specifier.
5706 Specialization->setConstexpr(FD->isConstexpr());
5707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005708
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005709 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005710 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005711
5712 // If this is a friend declaration, then we're not really declaring
5713 // an explicit specialization.
5714 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005715
Douglas Gregord5cb8762009-10-07 00:13:32 +00005716 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005717 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005718 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005719 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005720 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005721 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005722 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005723
5724 // C++ [temp.expl.spec]p6:
5725 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005726 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005727 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005728 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005729 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005730 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005731 if (!isFriend &&
5732 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005733 TSK_ExplicitSpecialization,
5734 Specialization,
5735 SpecInfo->getTemplateSpecializationKind(),
5736 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005737 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005738 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005739
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005740 // Mark the prior declaration as an explicit specialization, so that later
5741 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005742 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005743 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005744 MarkUnusedFileScopedDecl(Specialization);
5745 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005746
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005747 // Turn the given function declaration into a function template
5748 // specialization, with the template arguments from the previous
5749 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005750 // Take copies of (semantic and syntactic) template argument lists.
5751 const TemplateArgumentList* TemplArgs = new (Context)
5752 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005753 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005754 TemplArgs, /*InsertPos=*/0,
5755 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005756 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005757 FD->setStorageClass(Specialization->getStorageClass());
5758
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005759 // The "previous declaration" for this function template specialization is
5760 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005761 Previous.clear();
5762 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005763 return false;
5764}
5765
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005766/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005767/// specialization.
5768///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005769/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005770/// explicit member function specialization. On successful completion,
5771/// the function declaration \p FD will become a member function
5772/// specialization.
5773///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005774/// \param Member the member declaration, which will be updated to become a
5775/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005776///
John McCall68263142009-11-18 22:49:29 +00005777/// \param Previous the set of declarations, one of which may be specialized
5778/// by this function specialization; the set will be modified to contain the
5779/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005780bool
John McCall68263142009-11-18 22:49:29 +00005781Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005782 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005783
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005784 // Try to find the member we are instantiating.
5785 NamedDecl *Instantiation = 0;
5786 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005787 MemberSpecializationInfo *MSInfo = 0;
5788
John McCall68263142009-11-18 22:49:29 +00005789 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005790 // Nowhere to look anyway.
5791 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005792 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5793 I != E; ++I) {
5794 NamedDecl *D = (*I)->getUnderlyingDecl();
5795 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005796 if (Context.hasSameType(Function->getType(), Method->getType())) {
5797 Instantiation = Method;
5798 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005799 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005800 break;
5801 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005802 }
5803 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005804 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005805 VarDecl *PrevVar;
5806 if (Previous.isSingleResult() &&
5807 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005808 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005809 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005810 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005811 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005812 }
5813 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005814 CXXRecordDecl *PrevRecord;
5815 if (Previous.isSingleResult() &&
5816 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5817 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005818 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005819 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005820 }
Richard Smith1af83c42012-03-23 03:33:32 +00005821 } else if (isa<EnumDecl>(Member)) {
5822 EnumDecl *PrevEnum;
5823 if (Previous.isSingleResult() &&
5824 (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) {
5825 Instantiation = PrevEnum;
5826 InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum();
5827 MSInfo = PrevEnum->getMemberSpecializationInfo();
5828 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005829 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005830
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005831 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005832 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005833 // specializations are always out-of-line, the caller will complain about
5834 // this mismatch later.
5835 return false;
5836 }
John McCall77e8b112010-04-13 20:37:33 +00005837
5838 // If this is a friend, just bail out here before we start turning
5839 // things into explicit specializations.
5840 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5841 // Preserve instantiation information.
5842 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5843 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5844 cast<CXXMethodDecl>(InstantiatedFrom),
5845 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5846 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5847 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5848 cast<CXXRecordDecl>(InstantiatedFrom),
5849 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5850 }
5851
5852 Previous.clear();
5853 Previous.addDecl(Instantiation);
5854 return false;
5855 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005856
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005857 // Make sure that this is a specialization of a member.
5858 if (!InstantiatedFrom) {
5859 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5860 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005861 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5862 return true;
5863 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005864
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005865 // C++ [temp.expl.spec]p6:
5866 // If a template, a member template or the member of a class template is
Nico Weberff91d242011-12-23 20:58:04 +00005867 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005868 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005869 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005870 // use occurs; no diagnostic is required.
5871 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005872
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005873 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005874 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5875 TSK_ExplicitSpecialization,
5876 Instantiation,
5877 MSInfo->getTemplateSpecializationKind(),
5878 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005879 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005880 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005881
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005882 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005883 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005884 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005885 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005886 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005887 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005888
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005889 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005890 // the original declaration to note that it is an explicit specialization
5891 // (if it was previously an implicit instantiation). This latter step
5892 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005893 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005894 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5895 if (InstantiationFunction->getTemplateSpecializationKind() ==
5896 TSK_ImplicitInstantiation) {
5897 InstantiationFunction->setTemplateSpecializationKind(
5898 TSK_ExplicitSpecialization);
5899 InstantiationFunction->setLocation(Member->getLocation());
5900 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005901
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005902 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5903 cast<CXXMethodDecl>(InstantiatedFrom),
5904 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005905 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005906 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005907 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5908 if (InstantiationVar->getTemplateSpecializationKind() ==
5909 TSK_ImplicitInstantiation) {
5910 InstantiationVar->setTemplateSpecializationKind(
5911 TSK_ExplicitSpecialization);
5912 InstantiationVar->setLocation(Member->getLocation());
5913 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005914
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005915 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5916 cast<VarDecl>(InstantiatedFrom),
5917 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005918 MarkUnusedFileScopedDecl(InstantiationVar);
Richard Smith1af83c42012-03-23 03:33:32 +00005919 } else if (isa<CXXRecordDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005920 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5921 if (InstantiationClass->getTemplateSpecializationKind() ==
5922 TSK_ImplicitInstantiation) {
5923 InstantiationClass->setTemplateSpecializationKind(
5924 TSK_ExplicitSpecialization);
5925 InstantiationClass->setLocation(Member->getLocation());
5926 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005927
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005928 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005929 cast<CXXRecordDecl>(InstantiatedFrom),
5930 TSK_ExplicitSpecialization);
Richard Smith1af83c42012-03-23 03:33:32 +00005931 } else {
5932 assert(isa<EnumDecl>(Member) && "Only member enums remain");
5933 EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation);
5934 if (InstantiationEnum->getTemplateSpecializationKind() ==
5935 TSK_ImplicitInstantiation) {
5936 InstantiationEnum->setTemplateSpecializationKind(
5937 TSK_ExplicitSpecialization);
5938 InstantiationEnum->setLocation(Member->getLocation());
5939 }
5940
5941 cast<EnumDecl>(Member)->setInstantiationOfMemberEnum(
5942 cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005943 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005944
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005945 // Save the caller the trouble of having to figure out which declaration
5946 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005947 Previous.clear();
5948 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005949 return false;
5950}
5951
Douglas Gregor558c0322009-10-14 23:41:34 +00005952/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005953///
5954/// \returns true if a serious error occurs, false otherwise.
5955static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005956 SourceLocation InstLoc,
5957 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005958 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5959 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005960
Douglas Gregor669eed82010-07-13 00:10:04 +00005961 if (CurContext->isRecord()) {
5962 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5963 << D;
5964 return true;
5965 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005966
Richard Smith3e2e91e2011-10-18 02:28:33 +00005967 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005968 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005969 // template. If the name declared in the explicit instantiation is an
5970 // unqualified name, the explicit instantiation shall appear in the
5971 // namespace where its template is declared or, if that namespace is inline
5972 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005973 //
5974 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005975 if (WasQualifiedName) {
5976 if (CurContext->Encloses(OrigContext))
5977 return false;
5978 } else {
5979 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5980 return false;
5981 }
5982
5983 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5984 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005985 S.Diag(InstLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00005986 S.getLangOpts().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005987 diag::err_explicit_instantiation_out_of_scope :
5988 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005989 << D << NS;
5990 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005991 S.Diag(InstLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00005992 S.getLangOpts().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005993 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5994 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5995 << D << NS;
5996 } else
5997 S.Diag(InstLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00005998 S.getLangOpts().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005999 diag::err_explicit_instantiation_must_be_global :
6000 diag::warn_explicit_instantiation_must_be_global_0x)
6001 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00006002 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00006003 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00006004}
6005
6006/// \brief Determine whether the given scope specifier has a template-id in it.
6007static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
6008 if (!SS.isSet())
6009 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006010
Richard Smith3e2e91e2011-10-18 02:28:33 +00006011 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006012 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006013 // or a static data member of a class template specialization, the name of
6014 // the class template specialization in the qualified-id for the member
6015 // name shall be a simple-template-id.
6016 //
6017 // C++98 has the same restriction, just worded differently.
6018 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
6019 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00006020 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00006021 if (isa<TemplateSpecializationType>(T))
6022 return true;
6023
6024 return false;
6025}
6026
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006027// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00006028DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006029Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006030 SourceLocation ExternLoc,
6031 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006032 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006033 SourceLocation KWLoc,
6034 const CXXScopeSpec &SS,
6035 TemplateTy TemplateD,
6036 SourceLocation TemplateNameLoc,
6037 SourceLocation LAngleLoc,
6038 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006039 SourceLocation RAngleLoc,
6040 AttributeList *Attr) {
6041 // Find the class template we're specializing
6042 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00006043 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006044 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
6045
6046 // Check that the specialization uses the same tag kind as the
6047 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006048 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
6049 assert(Kind != TTK_Enum &&
6050 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006051 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00006052 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006053 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00006054 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006055 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00006056 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006057 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00006058 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006059 diag::note_previous_use);
6060 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
6061 }
6062
Douglas Gregor558c0322009-10-14 23:41:34 +00006063 // C++0x [temp.explicit]p2:
6064 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006065 // definition and an explicit instantiation declaration. An explicit
6066 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00006067 TemplateSpecializationKind TSK
6068 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6069 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006070
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006071 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00006072 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00006073 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006074
6075 // Check that the template argument list is well-formed for this
6076 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00006077 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00006078 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
6079 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006080 return true;
6081
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006082 // Find the class template specialization declaration that
6083 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006084 void *InsertPos = 0;
6085 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00006086 = ClassTemplate->findSpecialization(Converted.data(),
6087 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006088
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006089 TemplateSpecializationKind PrevDecl_TSK
6090 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
6091
Douglas Gregord5cb8762009-10-07 00:13:32 +00006092 // C++0x [temp.explicit]p2:
6093 // [...] An explicit instantiation shall appear in an enclosing
6094 // namespace of its template. [...]
6095 //
6096 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00006097 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
6098 SS.isSet()))
6099 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006100
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006101 ClassTemplateSpecializationDecl *Specialization = 0;
6102
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006103 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006104 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00006105 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006106 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006107 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006108 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00006109 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006110
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006111 // Even though HasNoEffect == true means that this explicit instantiation
6112 // has no effect on semantics, we go on to put its syntax in the AST.
6113
6114 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
6115 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00006116 // Since the only prior class template specialization with these
6117 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006118 // declaration node as our own, updating the source location
6119 // for the template name to reflect our new declaration.
6120 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00006121 Specialization = PrevDecl;
6122 Specialization->setLocation(TemplateNameLoc);
6123 PrevDecl = 0;
6124 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006125 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006126
Douglas Gregor52604ab2009-09-11 21:19:12 +00006127 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006128 // Create a new class template specialization declaration node for
6129 // this explicit specialization.
6130 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00006131 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006132 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00006133 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006134 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00006135 Converted.data(),
6136 Converted.size(),
6137 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00006138 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006139
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006140 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006141 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006142 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006143 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006144 }
6145
6146 // Build the fully-sugared type for this explicit instantiation as
6147 // the user wrote in the explicit instantiation itself. This means
6148 // that we'll pretty-print the type retrieved from the
6149 // specialization's declaration the way that the user actually wrote
6150 // the explicit instantiation, rather than formatting the name based
6151 // on the "canonical" representation used to store the template
6152 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00006153 TypeSourceInfo *WrittenTy
6154 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
6155 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006156 Context.getTypeDeclType(Specialization));
6157 Specialization->setTypeAsWritten(WrittenTy);
6158 TemplateArgsIn.release();
6159
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006160 // Set source locations for keywords.
6161 Specialization->setExternLoc(ExternLoc);
6162 Specialization->setTemplateKeywordLoc(TemplateLoc);
6163
Rafael Espindola0257b7f2012-01-03 06:04:21 +00006164 if (Attr)
6165 ProcessDeclAttributeList(S, Specialization, Attr);
6166
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006167 // Add the explicit instantiation into its lexical context. However,
6168 // since explicit instantiations are never found by name lookup, we
6169 // just put it into the declaration context directly.
6170 Specialization->setLexicalDeclContext(CurContext);
6171 CurContext->addDecl(Specialization);
6172
6173 // Syntax is now OK, so return if it has no other effect on semantics.
6174 if (HasNoEffect) {
6175 // Set the template specialization kind.
6176 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006177 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00006178 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006179
6180 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006181 // A definition of a class template or class member template
6182 // shall be in scope at the point of the explicit instantiation of
6183 // the class template or class member template.
6184 //
6185 // This check comes when we actually try to perform the
6186 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006187 ClassTemplateSpecializationDecl *Def
6188 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006189 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006190 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006191 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006192 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006193 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006194 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6195 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006196
Douglas Gregor0d035142009-10-27 18:42:08 +00006197 // Instantiate the members of this class template specialization.
6198 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006199 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006200 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006201 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6202
6203 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6204 // TSK_ExplicitInstantiationDefinition
6205 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6206 TSK == TSK_ExplicitInstantiationDefinition)
6207 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006208
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006209 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006210 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006211
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006212 // Set the template specialization kind.
6213 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006214 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006215}
6216
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006217// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006218DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006219Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006220 SourceLocation ExternLoc,
6221 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006222 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006223 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006224 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006225 IdentifierInfo *Name,
6226 SourceLocation NameLoc,
6227 AttributeList *Attr) {
6228
Douglas Gregor402abb52009-05-28 23:31:59 +00006229 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006230 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006231 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006232 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006233 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006234 MultiTemplateParamsArg(*this, 0, 0),
Richard Smithbdad7a22012-01-10 01:33:14 +00006235 Owned, IsDependent, SourceLocation(), false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006236 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006237 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6238
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006239 if (!TagD)
6240 return true;
6241
John McCalld226f652010-08-21 09:40:31 +00006242 TagDecl *Tag = cast<TagDecl>(TagD);
Richard Smith1af83c42012-03-23 03:33:32 +00006243 assert(!Tag->isEnum() && "shouldn't see enumerations here");
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006244
Douglas Gregord0c87372009-05-27 17:30:49 +00006245 if (Tag->isInvalidDecl())
6246 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006247
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006248 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6249 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6250 if (!Pattern) {
6251 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6252 << Context.getTypeDeclType(Record);
6253 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6254 return true;
6255 }
6256
Douglas Gregor558c0322009-10-14 23:41:34 +00006257 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006258 // If the explicit instantiation is for a class or member class, the
6259 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006260 // simple-template-id.
6261 //
6262 // C++98 has the same restriction, just worded differently.
6263 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006264 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006265 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006266
Douglas Gregor558c0322009-10-14 23:41:34 +00006267 // C++0x [temp.explicit]p2:
6268 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006269 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006270 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006271 TemplateSpecializationKind TSK
6272 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6273 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006274
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006275 // C++0x [temp.explicit]p2:
6276 // [...] An explicit instantiation shall appear in an enclosing
6277 // namespace of its template. [...]
6278 //
6279 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006280 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006281
Douglas Gregor454885e2009-10-15 15:54:05 +00006282 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006283 CXXRecordDecl *PrevDecl
Douglas Gregoref96ee02012-01-14 16:38:05 +00006284 = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
Douglas Gregor952b0172010-02-11 01:04:33 +00006285 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006286 PrevDecl = Record;
6287 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006288 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006289 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006290 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006291 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006292 PrevDecl,
6293 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006294 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006295 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006296 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006297 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006298 return TagD;
6299 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006300
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006301 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006302 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006303 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006304 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006305 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006306 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006307 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006308 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006309 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006310 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6311 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006312 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6313 << Pattern;
6314 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006315 } else {
6316 if (InstantiateClass(NameLoc, Record, Def,
6317 getTemplateInstantiationArgs(Record),
6318 TSK))
6319 return true;
6320
Douglas Gregor952b0172010-02-11 01:04:33 +00006321 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006322 if (!RecordDef)
6323 return true;
6324 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006325 }
6326
Douglas Gregor0d035142009-10-27 18:42:08 +00006327 // Instantiate all of the members of the class.
6328 InstantiateClassMembers(NameLoc, RecordDef,
6329 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006330
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006331 if (TSK == TSK_ExplicitInstantiationDefinition)
6332 MarkVTableUsed(NameLoc, RecordDef, true);
6333
Mike Stump390b4cc2009-05-16 07:39:55 +00006334 // FIXME: We don't have any representation for explicit instantiations of
6335 // member classes. Such a representation is not needed for compilation, but it
6336 // should be available for clients that want to see all of the declarations in
6337 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006338 return TagD;
6339}
6340
John McCallf312b1e2010-08-26 23:41:50 +00006341DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6342 SourceLocation ExternLoc,
6343 SourceLocation TemplateLoc,
6344 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006345 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006346 // TODO: check if/when DNInfo should replace Name.
6347 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6348 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006349 if (!Name) {
6350 if (!D.isInvalidType())
Daniel Dunbar96a00142012-03-09 18:35:03 +00006351 Diag(D.getDeclSpec().getLocStart(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006352 diag::err_explicit_instantiation_requires_name)
6353 << D.getDeclSpec().getSourceRange()
6354 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006355
Douglas Gregord5a423b2009-09-25 18:43:00 +00006356 return true;
6357 }
6358
6359 // The scope passed in may not be a decl scope. Zip up the scope tree until
6360 // we find one that is.
6361 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6362 (S->getFlags() & Scope::TemplateParamScope) != 0)
6363 S = S->getParent();
6364
6365 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006366 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6367 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006368 if (R.isNull())
6369 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006370
Douglas Gregore885e182011-05-21 18:53:30 +00006371 // C++ [dcl.stc]p1:
6372 // A storage-class-specifier shall not be specified in [...] an explicit
6373 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006374 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006375 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6376 << Name;
6377 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006378 } else if (D.getDeclSpec().getStorageClassSpec()
6379 != DeclSpec::SCS_unspecified) {
6380 // Complain about then remove the storage class specifier.
6381 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6382 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6383
6384 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006385 }
6386
Douglas Gregor663b5a02009-10-14 20:14:33 +00006387 // C++0x [temp.explicit]p1:
6388 // [...] An explicit instantiation of a function template shall not use the
6389 // inline or constexpr specifiers.
6390 // Presumably, this also applies to member functions of class templates as
6391 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006392 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006393 Diag(D.getDeclSpec().getInlineSpecLoc(),
David Blaikie4e4d0842012-03-11 07:00:24 +00006394 getLangOpts().CPlusPlus0x ?
Richard Smith2dc7ece2011-10-18 03:44:03 +00006395 diag::err_explicit_instantiation_inline :
6396 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006397 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6398 if (D.getDeclSpec().isConstexprSpecified())
6399 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6400 // not already specified.
6401 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6402 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006403
Douglas Gregor558c0322009-10-14 23:41:34 +00006404 // C++0x [temp.explicit]p2:
6405 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006406 // definition and an explicit instantiation declaration. An explicit
6407 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006408 TemplateSpecializationKind TSK
6409 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6410 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006411
Abramo Bagnara25777432010-08-11 22:01:17 +00006412 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006413 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006414
6415 if (!R->isFunctionType()) {
6416 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006417 // A [...] static data member of a class template can be explicitly
6418 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006419 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006420 if (Previous.isAmbiguous())
6421 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006422
John McCall1bcee0a2009-12-02 08:25:40 +00006423 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006424 if (!Prev || !Prev->isStaticDataMember()) {
6425 // We expect to see a data data member here.
6426 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6427 << Name;
6428 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6429 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006430 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006431 return true;
6432 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006433
Douglas Gregord5a423b2009-09-25 18:43:00 +00006434 if (!Prev->getInstantiatedFromStaticDataMember()) {
6435 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006436 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006437 diag::err_explicit_instantiation_data_member_not_instantiated)
6438 << Prev;
6439 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6440 // FIXME: Can we provide a note showing where this was declared?
6441 return true;
6442 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006443
Douglas Gregor558c0322009-10-14 23:41:34 +00006444 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006445 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006446 // or a static data member of a class template specialization, the name of
6447 // the class template specialization in the qualified-id for the member
6448 // name shall be a simple-template-id.
6449 //
6450 // C++98 has the same restriction, just worded differently.
6451 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006452 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006453 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006454 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006455
Douglas Gregor558c0322009-10-14 23:41:34 +00006456 // Check the scope of this explicit instantiation.
6457 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006458
Douglas Gregor454885e2009-10-15 15:54:05 +00006459 // Verify that it is okay to explicitly instantiate here.
6460 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6461 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006462 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006463 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006464 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006465 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006466 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006467 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006468 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006469 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006470
Douglas Gregord5a423b2009-09-25 18:43:00 +00006471 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006472 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006473 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006474 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006475
Douglas Gregord5a423b2009-09-25 18:43:00 +00006476 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006477 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006478 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006479
6480 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006481 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006482 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006483 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006484 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6485 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006486 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6487 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006488 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6489 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006490 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006491 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006492 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006493 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006494 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006495
Douglas Gregord5a423b2009-09-25 18:43:00 +00006496 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006497 // A [...] function [...] can be explicitly instantiated from its template.
6498 // A member function [...] of a class template can be explicitly
6499 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006500 // template.
John McCallc373d482010-01-27 01:50:18 +00006501 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006502 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6503 P != PEnd; ++P) {
6504 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006505 if (!HasExplicitTemplateArgs) {
6506 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6507 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6508 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006509
John McCallc373d482010-01-27 01:50:18 +00006510 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006511 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6512 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006513 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006514 }
6515 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006516
Douglas Gregord5a423b2009-09-25 18:43:00 +00006517 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6518 if (!FunTmpl)
6519 continue;
6520
John McCall5769d612010-02-08 23:07:23 +00006521 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006522 FunctionDecl *Specialization = 0;
6523 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006524 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006525 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006526 R, Specialization, Info)) {
6527 // FIXME: Keep track of almost-matches?
6528 (void)TDK;
6529 continue;
6530 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006531
John McCallc373d482010-01-27 01:50:18 +00006532 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006533 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006534
Douglas Gregord5a423b2009-09-25 18:43:00 +00006535 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006536 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006537 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006538 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006539 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6540 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6541 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006542
John McCallc373d482010-01-27 01:50:18 +00006543 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006544 return true;
John McCallc373d482010-01-27 01:50:18 +00006545
6546 // Ignore access control bits, we don't need them for redeclaration checking.
6547 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006548
Douglas Gregor0a897e32009-10-15 17:21:20 +00006549 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006550 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006551 diag::err_explicit_instantiation_member_function_not_instantiated)
6552 << Specialization
6553 << (Specialization->getTemplateSpecializationKind() ==
6554 TSK_ExplicitSpecialization);
6555 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6556 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006557 }
6558
Douglas Gregoref96ee02012-01-14 16:38:05 +00006559 FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006560 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6561 PrevDecl = Specialization;
6562
Douglas Gregor0a897e32009-10-15 17:21:20 +00006563 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006564 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006565 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006566 PrevDecl,
6567 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006568 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006569 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006570 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006571
Douglas Gregor0a897e32009-10-15 17:21:20 +00006572 // FIXME: We may still want to build some representation of this
6573 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006574 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006575 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006576 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006577
6578 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Rafael Espindola256fc4d2012-01-04 05:40:59 +00006579 AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
6580 if (Attr)
6581 ProcessDeclAttributeList(S, Specialization, Attr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006582
Douglas Gregor0a897e32009-10-15 17:21:20 +00006583 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006584 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006585
Douglas Gregor558c0322009-10-14 23:41:34 +00006586 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006587 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006588 // or a static data member of a class template specialization, the name of
6589 // the class template specialization in the qualified-id for the member
6590 // name shall be a simple-template-id.
6591 //
6592 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006593 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006594 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006595 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006596 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006597 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006598 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006599 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006600
Douglas Gregor558c0322009-10-14 23:41:34 +00006601 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006602 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006603 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006604 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006605 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006606
Douglas Gregord5a423b2009-09-25 18:43:00 +00006607 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006608 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006609}
6610
John McCallf312b1e2010-08-26 23:41:50 +00006611TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006612Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6613 const CXXScopeSpec &SS, IdentifierInfo *Name,
6614 SourceLocation TagLoc, SourceLocation NameLoc) {
6615 // This has to hold, because SS is expected to be defined.
6616 assert(Name && "Expected a name in a dependent tag");
6617
6618 NestedNameSpecifier *NNS
6619 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6620 if (!NNS)
6621 return true;
6622
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006623 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006624
Douglas Gregor48c89f42010-04-24 16:38:41 +00006625 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6626 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006627 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006628 return true;
6629 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006630
Douglas Gregor059101f2011-03-02 00:47:37 +00006631 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006632 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006633 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6634
6635 // Create type-source location information for this type.
6636 TypeLocBuilder TLB;
6637 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006638 TL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00006639 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6640 TL.setNameLoc(NameLoc);
6641 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006642}
6643
John McCallf312b1e2010-08-26 23:41:50 +00006644TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006645Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6646 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006647 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006648 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006649 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006650
Richard Smithebaf0e62011-10-18 20:49:44 +00006651 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6652 Diag(TypenameLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00006653 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00006654 diag::warn_cxx98_compat_typename_outside_of_template :
6655 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006656 << FixItHint::CreateRemoval(TypenameLoc);
6657
Douglas Gregor2494dd02011-03-01 01:34:45 +00006658 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006659 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6660 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006661 if (T.isNull())
6662 return true;
John McCall63b43852010-04-29 23:50:39 +00006663
6664 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6665 if (isa<DependentNameType>(T)) {
6666 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006667 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006668 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006669 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006670 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006671 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006672 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006673 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006674 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006676
John McCallb3d87482010-08-24 05:47:05 +00006677 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006678}
6679
John McCallf312b1e2010-08-26 23:41:50 +00006680TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006681Sema::ActOnTypenameType(Scope *S,
6682 SourceLocation TypenameLoc,
6683 const CXXScopeSpec &SS,
6684 SourceLocation TemplateKWLoc,
Douglas Gregora02411e2011-02-27 22:46:49 +00006685 TemplateTy TemplateIn,
6686 SourceLocation TemplateNameLoc,
6687 SourceLocation LAngleLoc,
6688 ASTTemplateArgsPtr TemplateArgsIn,
6689 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006690 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6691 Diag(TypenameLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00006692 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00006693 diag::warn_cxx98_compat_typename_outside_of_template :
6694 diag::ext_typename_outside_of_template)
6695 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006696
6697 // Translate the parser's template argument list in our AST format.
6698 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6699 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6700
6701 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006702 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6703 // Construct a dependent template specialization type.
6704 assert(DTN && "dependent template has non-dependent name?");
6705 assert(DTN->getQualifier()
6706 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6707 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6708 DTN->getQualifier(),
6709 DTN->getIdentifier(),
6710 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006711
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006712 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006713 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006714 DependentTemplateSpecializationTypeLoc SpecTL
6715 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006716 SpecTL.setElaboratedKeywordLoc(TypenameLoc);
6717 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00006718 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006719 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006720 SpecTL.setLAngleLoc(LAngleLoc);
6721 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006722 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6723 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006724 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006725 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006726
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006727 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6728 if (T.isNull())
6729 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006730
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006731 // Provide source-location information for the template specialization type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006732 TypeLocBuilder Builder;
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006733 TemplateSpecializationTypeLoc SpecTL
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006734 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006735 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
6736 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006737 SpecTL.setLAngleLoc(LAngleLoc);
6738 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006739 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6740 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6741
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006742 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6743 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006744 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006745 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6746
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006747 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6748 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006749}
6750
Douglas Gregora02411e2011-02-27 22:46:49 +00006751
Douglas Gregord57959a2009-03-27 23:10:48 +00006752/// \brief Build the type that describes a C++ typename specifier,
6753/// e.g., "typename T::type".
6754QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006755Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6756 SourceLocation KeywordLoc,
6757 NestedNameSpecifierLoc QualifierLoc,
6758 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006759 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006760 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006761 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006762
John McCall77bb1aa2010-05-01 00:40:08 +00006763 DeclContext *Ctx = computeDeclContext(SS);
6764 if (!Ctx) {
6765 // If the nested-name-specifier is dependent and couldn't be
6766 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006767 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6768 return Context.getDependentNameType(Keyword,
6769 QualifierLoc.getNestedNameSpecifier(),
6770 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006771 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006772
John McCall77bb1aa2010-05-01 00:40:08 +00006773 // If the nested-name-specifier refers to the current instantiation,
6774 // the "typename" keyword itself is superfluous. In C++03, the
6775 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6776 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006777 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006778
John McCall77bb1aa2010-05-01 00:40:08 +00006779 if (RequireCompleteDeclContext(SS, Ctx))
6780 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006781
6782 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006783 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006784 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006785 unsigned DiagID = 0;
6786 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006787 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006788 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006789 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006790 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006791
6792 case LookupResult::FoundUnresolvedValue: {
6793 // We found a using declaration that is a value. Most likely, the using
6794 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006795 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006796 IILoc);
6797 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6798 << Name << Ctx << FullRange;
6799 if (UnresolvedUsingValueDecl *Using
6800 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006801 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006802 Diag(Loc, diag::note_using_value_decl_missing_typename)
6803 << FixItHint::CreateInsertion(Loc, "typename ");
6804 }
6805 }
6806 // Fall through to create a dependent typename type, from which we can recover
6807 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006808
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006809 case LookupResult::NotFoundInCurrentInstantiation:
6810 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006811 return Context.getDependentNameType(Keyword,
6812 QualifierLoc.getNestedNameSpecifier(),
6813 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006814
6815 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006816 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006817 // We found a type. Build an ElaboratedType, since the
6818 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006819 return Context.getElaboratedType(ETK_Typename,
6820 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006821 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006822 }
6823
6824 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006825 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006826 break;
6827
6828 case LookupResult::FoundOverloaded:
6829 DiagID = diag::err_typename_nested_not_type;
6830 Referenced = *Result.begin();
6831 break;
6832
John McCall6e247262009-10-10 05:48:19 +00006833 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006834 return QualType();
6835 }
6836
6837 // If we get here, it's because name lookup did not find a
6838 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006839 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006840 IILoc);
6841 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006842 if (Referenced)
6843 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6844 << Name;
6845 return QualType();
6846}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006847
6848namespace {
6849 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006850 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006851 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006852 SourceLocation Loc;
6853 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006854
Douglas Gregor4a959d82009-08-06 16:20:37 +00006855 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006856 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006857
Mike Stump1eb44332009-09-09 15:08:12 +00006858 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006859 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006860 DeclarationName Entity)
6861 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006862 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006863
6864 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006865 /// transformed.
6866 ///
6867 /// For the purposes of type reconstruction, a type has already been
6868 /// transformed if it is NULL or if it is not dependent.
6869 bool AlreadyTransformed(QualType T) {
6870 return T.isNull() || !T->isDependentType();
6871 }
Mike Stump1eb44332009-09-09 15:08:12 +00006872
6873 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006874 /// rebuilt.
6875 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006876
Douglas Gregor4a959d82009-08-06 16:20:37 +00006877 /// \brief Returns the name of the entity whose type is being rebuilt.
6878 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006879
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006880 /// \brief Sets the "base" location and entity when that
6881 /// information is known based on another transformation.
6882 void setBase(SourceLocation Loc, DeclarationName Entity) {
6883 this->Loc = Loc;
6884 this->Entity = Entity;
6885 }
Douglas Gregordfca6f52012-02-13 22:00:16 +00006886
6887 ExprResult TransformLambdaExpr(LambdaExpr *E) {
6888 // Lambdas never need to be transformed.
6889 return E;
6890 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006891 };
6892}
6893
Douglas Gregor4a959d82009-08-06 16:20:37 +00006894/// \brief Rebuilds a type within the context of the current instantiation.
6895///
Mike Stump1eb44332009-09-09 15:08:12 +00006896/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006897/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006898/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006899/// partial specialization thereof). This routine will rebuild that type now
6900/// that we have entered the declarator's scope, which may produce different
6901/// canonical types, e.g.,
6902///
6903/// \code
6904/// template<typename T>
6905/// struct X {
6906/// typedef T* pointer;
6907/// pointer data();
6908/// };
6909///
6910/// template<typename T>
6911/// typename X<T>::pointer X<T>::data() { ... }
6912/// \endcode
6913///
Douglas Gregor4714c122010-03-31 17:34:00 +00006914/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006915/// since we do not know that we can look into X<T> when we parsed the type.
6916/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006917/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006918/// as the canonical type of T*, allowing the return types of the out-of-line
6919/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006920TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6921 SourceLocation Loc,
6922 DeclarationName Name) {
6923 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006924 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006925
Douglas Gregor4a959d82009-08-06 16:20:37 +00006926 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6927 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006928}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006929
John McCall60d7b3a2010-08-24 06:29:42 +00006930ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006931 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6932 DeclarationName());
6933 return Rebuilder.TransformExpr(E);
6934}
6935
John McCall63b43852010-04-29 23:50:39 +00006936bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006937 if (SS.isInvalid())
6938 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006939
Douglas Gregor7e384942011-02-25 16:07:42 +00006940 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006941 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6942 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006943 NestedNameSpecifierLoc Rebuilt
6944 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6945 if (!Rebuilt)
6946 return true;
John McCall63b43852010-04-29 23:50:39 +00006947
Douglas Gregor7e384942011-02-25 16:07:42 +00006948 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006949 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006950}
6951
Douglas Gregor20606502011-10-14 15:31:12 +00006952/// \brief Rebuild the template parameters now that we know we're in a current
6953/// instantiation.
6954bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6955 TemplateParameterList *Params) {
6956 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6957 Decl *Param = Params->getParam(I);
6958
6959 // There is nothing to rebuild in a type parameter.
6960 if (isa<TemplateTypeParmDecl>(Param))
6961 continue;
6962
6963 // Rebuild the template parameter list of a template template parameter.
6964 if (TemplateTemplateParmDecl *TTP
6965 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6966 if (RebuildTemplateParamsInCurrentInstantiation(
6967 TTP->getTemplateParameters()))
6968 return true;
6969
6970 continue;
6971 }
6972
6973 // Rebuild the type of a non-type template parameter.
6974 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6975 TypeSourceInfo *NewTSI
6976 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6977 NTTP->getLocation(),
6978 NTTP->getDeclName());
6979 if (!NewTSI)
6980 return true;
6981
6982 if (NewTSI != NTTP->getTypeSourceInfo()) {
6983 NTTP->setTypeSourceInfo(NewTSI);
6984 NTTP->setType(NewTSI->getType());
6985 }
6986 }
6987
6988 return false;
6989}
6990
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006991/// \brief Produces a formatted string that describes the binding of
6992/// template parameters to template arguments.
6993std::string
6994Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6995 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006996 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006997}
6998
6999std::string
7000Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
7001 const TemplateArgument *Args,
7002 unsigned NumArgs) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00007003 SmallString<128> Str;
Douglas Gregor87dd6972010-12-20 16:52:59 +00007004 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007005
Douglas Gregor9148c3f2009-11-11 19:13:48 +00007006 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00007007 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007008
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007009 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00007010 if (I >= NumArgs)
7011 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007012
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007013 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00007014 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007015 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00007016 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007017
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007018 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00007019 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007020 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00007021 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007022 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007023
Douglas Gregor87dd6972010-12-20 16:52:59 +00007024 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00007025 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007026 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00007027
7028 Out << ']';
7029 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007030}
Francois Pichet8387e2a2011-04-22 22:18:13 +00007031
7032void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
7033 if (!FD)
7034 return;
7035 FD->setLateTemplateParsed(Flag);
7036}
7037
7038bool Sema::IsInsideALocalClassWithinATemplateFunction() {
7039 DeclContext *DC = CurContext;
7040
7041 while (DC) {
7042 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
7043 const FunctionDecl *FD = RD->isLocalClass();
7044 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
7045 } else if (DC->isTranslationUnit() || DC->isNamespace())
7046 return false;
7047
7048 DC = DC->getParent();
7049 }
7050 return false;
7051}