blob: ec11f8d224291c71341a0d4793110ebd495ccf94 [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 Gregor8b0fa522012-03-30 16:20:47 +0000875 // FIXME: Horrible, horrible hack! We can't currently represent this
876 // in the AST, and historically we have just ignored such friend
877 // class templates, so don't complain here.
878 if (TUK != TUK_Friend)
879 Diag(NameLoc, diag::err_template_qualified_declarator_no_match)
880 << SS.getScopeRep() << SS.getRange();
Douglas Gregor05396e22009-08-25 17:23:04 +0000881 return true;
882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
John McCall77bb1aa2010-05-01 00:40:08 +0000884 if (RequireCompleteDeclContext(SS, SemanticContext))
885 return true;
886
Douglas Gregor20606502011-10-14 15:31:12 +0000887 // If we're adding a template to a dependent context, we may need to
888 // rebuilding some of the types used within the template parameter list,
889 // now that we know what the current instantiation is.
890 if (SemanticContext->isDependentContext()) {
891 ContextRAII SavedContext(*this, SemanticContext);
892 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
893 Invalid = true;
Douglas Gregor69605872012-03-28 16:01:27 +0000894 } else if (TUK != TUK_Friend && TUK != TUK_Reference)
895 diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc);
Douglas Gregor20606502011-10-14 15:31:12 +0000896
John McCalla24dc2e2009-11-17 02:14:36 +0000897 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000898 } else {
899 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000900 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000901 }
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregor57265e32010-04-12 16:00:01 +0000903 if (Previous.isAmbiguous())
904 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000905
Douglas Gregorddc29e12009-02-06 22:42:48 +0000906 NamedDecl *PrevDecl = 0;
907 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000908 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000909
Douglas Gregorddc29e12009-02-06 22:42:48 +0000910 // If there is a previous declaration with the same name, check
911 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000912 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000913 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000914
915 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000916 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000917 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000918 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000919 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
920 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000921 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000922 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
923 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
924 PrevClassTemplate
925 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
926 ->getSpecializedTemplate();
927 }
928 }
929
John McCall65c49462009-12-18 11:25:59 +0000930 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000931 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000932 // [...] When looking for a prior declaration of a class or a function
933 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000934 // function is neither a qualified name nor a template-id, scopes outside
935 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000936 if (!SS.isSet()) {
937 DeclContext *OutermostContext = CurContext;
938 while (!OutermostContext->isFileContext())
939 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000940
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000941 if (PrevDecl &&
942 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
943 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
944 SemanticContext = PrevDecl->getDeclContext();
945 } else {
946 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000947 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000948 // declaration.
949 PrevDecl = PrevClassTemplate = 0;
950 SemanticContext = OutermostContext;
951 }
John McCalle129d442009-12-17 23:21:11 +0000952 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000953
John McCalle129d442009-12-17 23:21:11 +0000954 if (CurContext->isDependentContext()) {
955 // If this is a dependent context, we don't want to link the friend
956 // class template to the template in scope, because that would perform
957 // checking of the template parameter lists that can't be performed
958 // until the outer context is instantiated.
959 PrevDecl = PrevClassTemplate = 0;
960 }
961 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
962 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000963
Douglas Gregorddc29e12009-02-06 22:42:48 +0000964 if (PrevClassTemplate) {
965 // Ensure that the template parameter lists are compatible.
966 if (!TemplateParameterListsAreEqual(TemplateParams,
967 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000968 /*Complain=*/true,
969 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000970 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000971
972 // C++ [temp.class]p4:
973 // In a redeclaration, partial specialization, explicit
974 // specialization or explicit instantiation of a class template,
975 // the class-key shall agree in kind with the original class
976 // template declaration (7.1.5.3).
977 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000978 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
979 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000980 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000981 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000982 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000983 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000984 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000985 }
986
Douglas Gregorddc29e12009-02-06 22:42:48 +0000987 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000988 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000989 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000990 Diag(NameLoc, diag::err_redefinition) << Name;
991 Diag(Def->getLocation(), diag::note_previous_definition);
992 // FIXME: Would it make sense to try to "forget" the previous
993 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000994 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000995 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000996 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000997 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
998 // Maybe we will complain about the shadowed template parameter.
999 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
1000 // Just pretend that we didn't see the previous declaration.
1001 PrevDecl = 0;
1002 } else if (PrevDecl) {
1003 // C++ [temp]p5:
1004 // A class template shall not have the same name as any other
1005 // template, class, function, object, enumeration, enumerator,
1006 // namespace, or type in the same scope (3.3), except as specified
1007 // in (14.5.4).
1008 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
1009 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +00001010 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001011 }
1012
Douglas Gregord684b002009-02-10 19:49:53 +00001013 // Check the template parameter list of this declaration, possibly
1014 // merging in the template parameter list from the previous class
1015 // template declaration.
1016 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001017 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +00001018 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +00001019 SemanticContext->isRecord() &&
1020 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +00001021 ? TPC_ClassTemplateMember
1022 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +00001023 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Douglas Gregor57265e32010-04-12 16:00:01 +00001025 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001026 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +00001027 // template out-of-line.
1028 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001029 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +00001030 Diag(NameLoc, diag::err_member_def_does_not_match)
1031 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001032 Invalid = true;
1033 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001034 }
1035
Mike Stump1eb44332009-09-09 15:08:12 +00001036 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001037 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001038 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001039 PrevClassTemplate->getTemplatedDecl() : 0,
1040 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001041 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001042 if (NumOuterTemplateParamLists > 0)
1043 NewClass->setTemplateParameterListsInfo(Context,
1044 NumOuterTemplateParamLists,
1045 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001046
Eli Friedman572ae0a2012-02-10 02:02:21 +00001047 // Add alignment attributes if necessary; these attributes are checked when
1048 // the ASTContext lays out the structure.
1049 AddAlignmentAttributesForRecord(NewClass);
1050 AddMsStructLayoutForRecord(NewClass);
1051
Douglas Gregorddc29e12009-02-06 22:42:48 +00001052 ClassTemplateDecl *NewTemplate
1053 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1054 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001055 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001056 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001057
Douglas Gregor2ccd89c2011-12-20 18:11:52 +00001058 if (ModulePrivateLoc.isValid())
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001059 NewTemplate->setModulePrivate();
Douglas Gregor8d267c52011-09-09 02:06:17 +00001060
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001061 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001062 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001063 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001064 assert(T->isDependentType() && "Class template type is not dependent?");
1065 (void)T;
1066
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001067 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001068 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001069 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001070 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1071 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001072
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001073 // Set the access specifier.
Douglas Gregor42acead2012-03-17 23:06:31 +00001074 if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord())
John McCall05b23ea2009-09-14 21:59:20 +00001075 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Douglas Gregorddc29e12009-02-06 22:42:48 +00001077 // Set the lexical context of these templates
1078 NewClass->setLexicalDeclContext(CurContext);
1079 NewTemplate->setLexicalDeclContext(CurContext);
1080
John McCall0f434ec2009-07-31 02:45:11 +00001081 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001082 NewClass->startDefinition();
1083
1084 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001085 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001086
John McCall05b23ea2009-09-14 21:59:20 +00001087 if (TUK != TUK_Friend)
1088 PushOnScopeChains(NewTemplate, S);
1089 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001090 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001091 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001092 NewClass->setAccess(PrevClassTemplate->getAccess());
1093 }
John McCall05b23ea2009-09-14 21:59:20 +00001094
Douglas Gregord85bea22009-09-26 06:47:28 +00001095 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1096 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001097
John McCall05b23ea2009-09-14 21:59:20 +00001098 // Friend templates are visible in fairly strange ways.
1099 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001100 DeclContext *DC = SemanticContext->getRedeclContext();
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001101 DC->makeDeclVisibleInContext(NewTemplate);
John McCall05b23ea2009-09-14 21:59:20 +00001102 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1103 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001104 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001105 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001106
Douglas Gregord85bea22009-09-26 06:47:28 +00001107 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1108 NewClass->getLocation(),
1109 NewTemplate,
1110 /*FIXME:*/NewClass->getLocation());
1111 Friend->setAccess(AS_public);
1112 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001113 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001114
Douglas Gregord684b002009-02-10 19:49:53 +00001115 if (Invalid) {
1116 NewTemplate->setInvalidDecl();
1117 NewClass->setInvalidDecl();
1118 }
John McCalld226f652010-08-21 09:40:31 +00001119 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001120}
1121
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001122/// \brief Diagnose the presence of a default template argument on a
1123/// template parameter, which is ill-formed in certain contexts.
1124///
1125/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001126static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001127 Sema::TemplateParamListContext TPC,
1128 SourceLocation ParamLoc,
1129 SourceRange DefArgRange) {
1130 switch (TPC) {
1131 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001132 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001133 return false;
1134
1135 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001136 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001137 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001138 // A default template-argument shall not be specified in a
1139 // function template declaration or a function template
1140 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001141 // If a friend function template declaration specifies a default
1142 // template-argument, that declaration shall be a definition and shall be
1143 // the only declaration of the function template in the translation unit.
1144 // (C++98/03 doesn't have this wording; see DR226).
David Blaikie4e4d0842012-03-11 07:00:24 +00001145 S.Diag(ParamLoc, S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00001146 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1147 : diag::ext_template_parameter_default_in_function_template)
1148 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001149 return false;
1150
1151 case Sema::TPC_ClassTemplateMember:
1152 // C++0x [temp.param]p9:
1153 // A default template-argument shall not be specified in the
1154 // template-parameter-lists of the definition of a member of a
1155 // class template that appears outside of the member's class.
1156 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1157 << DefArgRange;
1158 return true;
1159
1160 case Sema::TPC_FriendFunctionTemplate:
1161 // C++ [temp.param]p9:
1162 // A default template-argument shall not be specified in a
1163 // friend template declaration.
1164 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1165 << DefArgRange;
1166 return true;
1167
1168 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1169 // for friend function templates if there is only a single
1170 // declaration (and it is a definition). Strange!
1171 }
1172
David Blaikie7530c032012-01-17 06:56:22 +00001173 llvm_unreachable("Invalid TemplateParamListContext!");
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001174}
1175
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001176/// \brief Check for unexpanded parameter packs within the template parameters
1177/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001178static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1179 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001180 TemplateParameterList *Params = TTP->getTemplateParameters();
1181 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1182 NamedDecl *P = Params->getParam(I);
1183 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001184 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001185 NTTP->getTypeSourceInfo(),
1186 Sema::UPPC_NonTypeTemplateParameterType))
1187 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001188
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001189 continue;
1190 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001191
1192 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001193 = dyn_cast<TemplateTemplateParmDecl>(P))
1194 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1195 return true;
1196 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001197
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001198 return false;
1199}
1200
Douglas Gregord684b002009-02-10 19:49:53 +00001201/// \brief Checks the validity of a template parameter list, possibly
1202/// considering the template parameter list from a previous
1203/// declaration.
1204///
1205/// If an "old" template parameter list is provided, it must be
1206/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1207/// template parameter list.
1208///
1209/// \param NewParams Template parameter list for a new template
1210/// declaration. This template parameter list will be updated with any
1211/// default arguments that are carried through from the previous
1212/// template parameter list.
1213///
1214/// \param OldParams If provided, template parameter list from a
1215/// previous declaration of the same template. Default template
1216/// arguments will be merged from the old template parameter list to
1217/// the new template parameter list.
1218///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001219/// \param TPC Describes the context in which we are checking the given
1220/// template parameter list.
1221///
Douglas Gregord684b002009-02-10 19:49:53 +00001222/// \returns true if an error occurred, false otherwise.
1223bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001224 TemplateParameterList *OldParams,
1225 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001226 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregord684b002009-02-10 19:49:53 +00001228 // C++ [temp.param]p10:
1229 // The set of default template-arguments available for use with a
1230 // template declaration or definition is obtained by merging the
1231 // default arguments from the definition (if in scope) and all
1232 // declarations in scope in the same way default function
1233 // arguments are (8.3.6).
1234 bool SawDefaultArgument = false;
1235 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001236
Mike Stump1a35fde2009-02-11 23:03:27 +00001237 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001238 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001239 if (OldParams)
1240 OldParam = OldParams->begin();
1241
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001242 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001243 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1244 NewParamEnd = NewParams->end();
1245 NewParam != NewParamEnd; ++NewParam) {
1246 // Variables used to diagnose redundant default arguments
1247 bool RedundantDefaultArg = false;
1248 SourceLocation OldDefaultLoc;
1249 SourceLocation NewDefaultLoc;
1250
David Blaikie1368e582011-10-19 05:19:50 +00001251 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001252 bool MissingDefaultArg = false;
1253
David Blaikie1368e582011-10-19 05:19:50 +00001254 // Variable used to diagnose non-final parameter packs
1255 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001256
Douglas Gregord684b002009-02-10 19:49:53 +00001257 if (TemplateTypeParmDecl *NewTypeParm
1258 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001259 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001260 if (NewTypeParm->hasDefaultArgument() &&
1261 DiagnoseDefaultTemplateArgument(*this, TPC,
1262 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001263 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001264 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001265 NewTypeParm->removeDefaultArgument();
1266
1267 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001268 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001269 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Anders Carlsson49d25572009-06-12 23:20:15 +00001271 if (NewTypeParm->isParameterPack()) {
1272 assert(!NewTypeParm->hasDefaultArgument() &&
1273 "Parameter packs can't have a default argument!");
1274 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001275 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001276 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001277 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1278 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1279 SawDefaultArgument = true;
1280 RedundantDefaultArg = true;
1281 PreviousDefaultArgLoc = NewDefaultLoc;
1282 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1283 // Merge the default argument from the old declaration to the
1284 // new declaration.
1285 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001286 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001287 true);
1288 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1289 } else if (NewTypeParm->hasDefaultArgument()) {
1290 SawDefaultArgument = true;
1291 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1292 } else if (SawDefaultArgument)
1293 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001294 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001295 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001296 // Check for unexpanded parameter packs.
1297 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001298 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001299 UPPC_NonTypeTemplateParameterType)) {
1300 Invalid = true;
1301 continue;
1302 }
1303
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001304 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001305 if (NewNonTypeParm->hasDefaultArgument() &&
1306 DiagnoseDefaultTemplateArgument(*this, TPC,
1307 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001308 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001309 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001310 }
1311
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001312 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001313 NonTypeTemplateParmDecl *OldNonTypeParm
1314 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001315 if (NewNonTypeParm->isParameterPack()) {
1316 assert(!NewNonTypeParm->hasDefaultArgument() &&
1317 "Parameter packs can't have a default argument!");
1318 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001319 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001320 NewNonTypeParm->hasDefaultArgument()) {
1321 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1322 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1323 SawDefaultArgument = true;
1324 RedundantDefaultArg = true;
1325 PreviousDefaultArgLoc = NewDefaultLoc;
1326 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1327 // Merge the default argument from the old declaration to the
1328 // new declaration.
1329 SawDefaultArgument = true;
1330 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001331 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001332 // parameter.
1333 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001334 OldNonTypeParm->getDefaultArgument(),
1335 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001336 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1337 } else if (NewNonTypeParm->hasDefaultArgument()) {
1338 SawDefaultArgument = true;
1339 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1340 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001341 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001342 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001343 TemplateTemplateParmDecl *NewTemplateParm
1344 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001345
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001346 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001347 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001348 Invalid = true;
1349 continue;
1350 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001351
David Blaikie1368e582011-10-19 05:19:50 +00001352 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001353 if (NewTemplateParm->hasDefaultArgument() &&
1354 DiagnoseDefaultTemplateArgument(*this, TPC,
1355 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001356 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001357 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001358
1359 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001360 TemplateTemplateParmDecl *OldTemplateParm
1361 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001362 if (NewTemplateParm->isParameterPack()) {
1363 assert(!NewTemplateParm->hasDefaultArgument() &&
1364 "Parameter packs can't have a default argument!");
1365 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001366 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001367 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001368 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1369 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001370 SawDefaultArgument = true;
1371 RedundantDefaultArg = true;
1372 PreviousDefaultArgLoc = NewDefaultLoc;
1373 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1374 // Merge the default argument from the old declaration to the
1375 // new declaration.
1376 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001377 // FIXME: We need to create a new kind of "default argument" expression
1378 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001379 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001380 OldTemplateParm->getDefaultArgument(),
1381 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001382 PreviousDefaultArgLoc
1383 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001384 } else if (NewTemplateParm->hasDefaultArgument()) {
1385 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001386 PreviousDefaultArgLoc
1387 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001388 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001389 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001390 }
1391
David Blaikie1368e582011-10-19 05:19:50 +00001392 // C++0x [temp.param]p11:
1393 // If a template parameter of a primary class template or alias template
1394 // is a template parameter pack, it shall be the last template parameter.
1395 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1396 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1397 Diag((*NewParam)->getLocation(),
1398 diag::err_template_param_pack_must_be_last_template_parameter);
1399 Invalid = true;
1400 }
1401
Douglas Gregord684b002009-02-10 19:49:53 +00001402 if (RedundantDefaultArg) {
1403 // C++ [temp.param]p12:
1404 // A template-parameter shall not be given default arguments
1405 // by two different declarations in the same scope.
1406 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1407 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1408 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001409 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001410 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001411 // If a template-parameter of a class template has a default
1412 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001413 // have a default template-argument supplied or be a template parameter
1414 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001415 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001416 diag::err_template_param_default_arg_missing);
1417 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1418 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001419 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001420 }
1421
1422 // If we have an old template parameter list that we're merging
1423 // in, move on to the next parameter.
1424 if (OldParams)
1425 ++OldParam;
1426 }
1427
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001428 // We were missing some default arguments at the end of the list, so remove
1429 // all of the default arguments.
1430 if (RemoveDefaultArguments) {
1431 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1432 NewParamEnd = NewParams->end();
1433 NewParam != NewParamEnd; ++NewParam) {
1434 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1435 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001436 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001437 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1438 NTTP->removeDefaultArgument();
1439 else
1440 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1441 }
1442 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001443
Douglas Gregord684b002009-02-10 19:49:53 +00001444 return Invalid;
1445}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001446
John McCall4e2cbb22010-10-20 05:44:58 +00001447namespace {
1448
1449/// A class which looks for a use of a certain level of template
1450/// parameter.
1451struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1452 typedef RecursiveASTVisitor<DependencyChecker> super;
1453
1454 unsigned Depth;
1455 bool Match;
1456
1457 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1458 NamedDecl *ND = Params->getParam(0);
1459 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1460 Depth = PD->getDepth();
1461 } else if (NonTypeTemplateParmDecl *PD =
1462 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1463 Depth = PD->getDepth();
1464 } else {
1465 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1466 }
1467 }
1468
1469 bool Matches(unsigned ParmDepth) {
1470 if (ParmDepth >= Depth) {
1471 Match = true;
1472 return true;
1473 }
1474 return false;
1475 }
1476
1477 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1478 return !Matches(T->getDepth());
1479 }
1480
1481 bool TraverseTemplateName(TemplateName N) {
1482 if (TemplateTemplateParmDecl *PD =
1483 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1484 if (Matches(PD->getDepth())) return false;
1485 return super::TraverseTemplateName(N);
1486 }
1487
1488 bool VisitDeclRefExpr(DeclRefExpr *E) {
1489 if (NonTypeTemplateParmDecl *PD =
1490 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1491 if (PD->getDepth() == Depth) {
1492 Match = true;
1493 return false;
1494 }
1495 }
1496 return super::VisitDeclRefExpr(E);
1497 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001498
1499 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1500 return TraverseType(T->getInjectedSpecializationType());
1501 }
John McCall4e2cbb22010-10-20 05:44:58 +00001502};
1503}
1504
Douglas Gregorc8406492011-05-10 18:27:06 +00001505/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001506/// list.
1507static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001508DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001509 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001510 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001511 return Checker.Match;
1512}
1513
Douglas Gregorc8406492011-05-10 18:27:06 +00001514// Find the source range corresponding to the named type in the given
1515// nested-name-specifier, if any.
1516static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1517 QualType T,
1518 const CXXScopeSpec &SS) {
1519 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1520 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1521 if (const Type *CurType = NNS->getAsType()) {
1522 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1523 return NNSLoc.getTypeLoc().getSourceRange();
1524 } else
1525 break;
1526
1527 NNSLoc = NNSLoc.getPrefix();
1528 }
1529
1530 return SourceRange();
1531}
1532
Mike Stump1eb44332009-09-09 15:08:12 +00001533/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001534/// specifier, returning the template parameter list that applies to the
1535/// name.
1536///
1537/// \param DeclStartLoc the start of the declaration that has a scope
1538/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001539///
Douglas Gregorc8406492011-05-10 18:27:06 +00001540/// \param DeclLoc The location of the declaration itself.
1541///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001542/// \param SS the scope specifier that will be matched to the given template
1543/// parameter lists. This scope specifier precedes a qualified name that is
1544/// being declared.
1545///
1546/// \param ParamLists the template parameter lists, from the outermost to the
1547/// innermost template parameter lists.
1548///
1549/// \param NumParamLists the number of template parameter lists in ParamLists.
1550///
John McCall77e8b112010-04-13 20:37:33 +00001551/// \param IsFriend Whether to apply the slightly different rules for
1552/// matching template parameters to scope specifiers in friend
1553/// declarations.
1554///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001555/// \param IsExplicitSpecialization will be set true if the entity being
1556/// declared is an explicit specialization, false otherwise.
1557///
Mike Stump1eb44332009-09-09 15:08:12 +00001558/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001559/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001560/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001561/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001562/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001563/// itself a template).
1564TemplateParameterList *
1565Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001566 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001567 const CXXScopeSpec &SS,
1568 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001569 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001570 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001571 bool &IsExplicitSpecialization,
1572 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001573 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001574 Invalid = false;
1575
1576 // The sequence of nested types to which we will match up the template
1577 // parameter lists. We first build this list by starting with the type named
1578 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001579 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001580 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001581 if (SS.getScopeRep()) {
1582 if (CXXRecordDecl *Record
1583 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1584 T = Context.getTypeDeclType(Record);
1585 else
1586 T = QualType(SS.getScopeRep()->getAsType(), 0);
1587 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001588
1589 // If we found an explicit specialization that prevents us from needing
1590 // 'template<>' headers, this will be set to the location of that
1591 // explicit specialization.
1592 SourceLocation ExplicitSpecLoc;
1593
1594 while (!T.isNull()) {
1595 NestedTypes.push_back(T);
1596
1597 // Retrieve the parent of a record type.
1598 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1599 // If this type is an explicit specialization, we're done.
1600 if (ClassTemplateSpecializationDecl *Spec
1601 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1602 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1603 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1604 ExplicitSpecLoc = Spec->getLocation();
1605 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001606 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001607 } else if (Record->getTemplateSpecializationKind()
1608 == TSK_ExplicitSpecialization) {
1609 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001610 break;
1611 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001612
1613 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1614 T = Context.getTypeDeclType(Parent);
1615 else
1616 T = QualType();
1617 continue;
1618 }
1619
1620 if (const TemplateSpecializationType *TST
1621 = T->getAs<TemplateSpecializationType>()) {
1622 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1623 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1624 T = Context.getTypeDeclType(Parent);
1625 else
1626 T = QualType();
1627 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001628 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001629 }
1630
1631 // Look one step prior in a dependent template specialization type.
1632 if (const DependentTemplateSpecializationType *DependentTST
1633 = T->getAs<DependentTemplateSpecializationType>()) {
1634 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1635 T = QualType(NNS->getAsType(), 0);
1636 else
1637 T = QualType();
1638 continue;
1639 }
1640
1641 // Look one step prior in a dependent name type.
1642 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1643 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1644 T = QualType(NNS->getAsType(), 0);
1645 else
1646 T = QualType();
1647 continue;
1648 }
1649
1650 // Retrieve the parent of an enumeration type.
1651 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1652 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1653 // check here.
1654 EnumDecl *Enum = EnumT->getDecl();
1655
1656 // Get to the parent type.
1657 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1658 T = Context.getTypeDeclType(Parent);
1659 else
1660 T = QualType();
1661 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001662 }
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Douglas Gregorc8406492011-05-10 18:27:06 +00001664 T = QualType();
1665 }
1666 // Reverse the nested types list, since we want to traverse from the outermost
1667 // to the innermost while checking template-parameter-lists.
1668 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001669
Douglas Gregorc8406492011-05-10 18:27:06 +00001670 // C++0x [temp.expl.spec]p17:
1671 // A member or a member template may be nested within many
1672 // enclosing class templates. In an explicit specialization for
1673 // such a member, the member declaration shall be preceded by a
1674 // template<> for each enclosing class template that is
1675 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001676 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001677 unsigned ParamIdx = 0;
1678 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1679 ++TypeIdx) {
1680 T = NestedTypes[TypeIdx];
1681
1682 // Whether we expect a 'template<>' header.
1683 bool NeedEmptyTemplateHeader = false;
1684
1685 // Whether we expect a template header with parameters.
1686 bool NeedNonemptyTemplateHeader = false;
1687
1688 // For a dependent type, the set of template parameters that we
1689 // expect to see.
1690 TemplateParameterList *ExpectedTemplateParams = 0;
1691
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001692 // C++0x [temp.expl.spec]p15:
1693 // A member or a member template may be nested within many enclosing
1694 // class templates. In an explicit specialization for such a member, the
1695 // member declaration shall be preceded by a template<> for each
1696 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001697 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1698 if (ClassTemplatePartialSpecializationDecl *Partial
1699 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1700 ExpectedTemplateParams = Partial->getTemplateParameters();
1701 NeedNonemptyTemplateHeader = true;
1702 } else if (Record->isDependentType()) {
1703 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001704 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001705 ->getTemplateParameters();
1706 NeedNonemptyTemplateHeader = true;
1707 }
1708 } else if (ClassTemplateSpecializationDecl *Spec
1709 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1710 // C++0x [temp.expl.spec]p4:
1711 // Members of an explicitly specialized class template are defined
1712 // in the same manner as members of normal classes, and not using
1713 // the template<> syntax.
1714 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1715 NeedEmptyTemplateHeader = true;
1716 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001717 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001718 } else if (Record->getTemplateSpecializationKind()) {
1719 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001720 != TSK_ExplicitSpecialization &&
1721 TypeIdx == NumTypes - 1)
1722 IsExplicitSpecialization = true;
1723
1724 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001725 }
1726 } else if (const TemplateSpecializationType *TST
1727 = T->getAs<TemplateSpecializationType>()) {
1728 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1729 ExpectedTemplateParams = Template->getTemplateParameters();
1730 NeedNonemptyTemplateHeader = true;
1731 }
1732 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1733 // FIXME: We actually could/should check the template arguments here
1734 // against the corresponding template parameter list.
1735 NeedNonemptyTemplateHeader = false;
1736 }
1737
Douglas Gregor89b9f102011-06-06 15:22:55 +00001738 // C++ [temp.expl.spec]p16:
1739 // In an explicit specialization declaration for a member of a class
1740 // template or a member template that ap- pears in namespace scope, the
1741 // member template and some of its enclosing class templates may remain
1742 // unspecialized, except that the declaration shall not explicitly
1743 // specialize a class member template if its en- closing class templates
1744 // are not explicitly specialized as well.
1745 if (ParamIdx < NumParamLists) {
1746 if (ParamLists[ParamIdx]->size() == 0) {
1747 if (SawNonEmptyTemplateParameterList) {
1748 Diag(DeclLoc, diag::err_specialize_member_of_template)
1749 << ParamLists[ParamIdx]->getSourceRange();
1750 Invalid = true;
1751 IsExplicitSpecialization = false;
1752 return 0;
1753 }
1754 } else
1755 SawNonEmptyTemplateParameterList = true;
1756 }
1757
Douglas Gregorc8406492011-05-10 18:27:06 +00001758 if (NeedEmptyTemplateHeader) {
1759 // If we're on the last of the types, and we need a 'template<>' header
1760 // here, then it's an explicit specialization.
1761 if (TypeIdx == NumTypes - 1)
1762 IsExplicitSpecialization = true;
1763
1764 if (ParamIdx < NumParamLists) {
1765 if (ParamLists[ParamIdx]->size() > 0) {
1766 // The header has template parameters when it shouldn't. Complain.
1767 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1768 diag::err_template_param_list_matches_nontemplate)
1769 << T
1770 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1771 ParamLists[ParamIdx]->getRAngleLoc())
1772 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1773 Invalid = true;
1774 return 0;
1775 }
1776
1777 // Consume this template header.
1778 ++ParamIdx;
1779 continue;
1780 }
1781
1782 if (!IsFriend) {
1783 // We don't have a template header, but we should.
1784 SourceLocation ExpectedTemplateLoc;
1785 if (NumParamLists > 0)
1786 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1787 else
1788 ExpectedTemplateLoc = DeclStartLoc;
1789
1790 Diag(DeclLoc, diag::err_template_spec_needs_header)
1791 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1792 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1793 }
1794
1795 continue;
1796 }
1797
1798 if (NeedNonemptyTemplateHeader) {
1799 // In friend declarations we can have template-ids which don't
1800 // depend on the corresponding template parameter lists. But
1801 // assume that empty parameter lists are supposed to match this
1802 // template-id.
1803 if (IsFriend && T->isDependentType()) {
1804 if (ParamIdx < NumParamLists &&
1805 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1806 ExpectedTemplateParams = 0;
1807 else
1808 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001809 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001810
Douglas Gregorc8406492011-05-10 18:27:06 +00001811 if (ParamIdx < NumParamLists) {
1812 // Check the template parameter list, if we can.
1813 if (ExpectedTemplateParams &&
1814 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1815 ExpectedTemplateParams,
1816 true, TPL_TemplateMatch))
1817 Invalid = true;
1818
1819 if (!Invalid &&
1820 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1821 TPC_ClassTemplateMember))
1822 Invalid = true;
1823
1824 ++ParamIdx;
1825 continue;
1826 }
1827
1828 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1829 << T
1830 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1831 Invalid = true;
1832 continue;
1833 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001834 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001835
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001836 // If there were at least as many template-ids as there were template
1837 // parameter lists, then there are no template parameter lists remaining for
1838 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001839 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001840 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001842 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001843 if (ParamIdx < NumParamLists - 1) {
1844 bool HasAnyExplicitSpecHeader = false;
1845 bool AllExplicitSpecHeaders = true;
1846 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1847 if (ParamLists[I]->size() == 0)
1848 HasAnyExplicitSpecHeader = true;
1849 else
1850 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001851 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001852
1853 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1854 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1855 : diag::err_template_spec_extra_headers)
1856 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1857 ParamLists[NumParamLists - 2]->getRAngleLoc());
1858
1859 // If there was a specialization somewhere, such that 'template<>' is
1860 // not required, and there were any 'template<>' headers, note where the
1861 // specialization occurred.
1862 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1863 Diag(ExplicitSpecLoc,
1864 diag::note_explicit_template_spec_does_not_need_header)
1865 << NestedTypes.back();
1866
1867 // We have a template parameter list with no corresponding scope, which
1868 // means that the resulting template declaration can't be instantiated
1869 // properly (we'll end up with dependent nodes when we shouldn't).
1870 if (!AllExplicitSpecHeaders)
1871 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001872 }
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Douglas Gregor89b9f102011-06-06 15:22:55 +00001874 // C++ [temp.expl.spec]p16:
1875 // In an explicit specialization declaration for a member of a class
1876 // template or a member template that ap- pears in namespace scope, the
1877 // member template and some of its enclosing class templates may remain
1878 // unspecialized, except that the declaration shall not explicitly
1879 // specialize a class member template if its en- closing class templates
1880 // are not explicitly specialized as well.
1881 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1882 SawNonEmptyTemplateParameterList) {
1883 Diag(DeclLoc, diag::err_specialize_member_of_template)
1884 << ParamLists[ParamIdx]->getSourceRange();
1885 Invalid = true;
1886 IsExplicitSpecialization = false;
1887 return 0;
1888 }
1889
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001890 // Return the last template parameter list, which corresponds to the
1891 // entity being declared.
1892 return ParamLists[NumParamLists - 1];
1893}
1894
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001895void Sema::NoteAllFoundTemplates(TemplateName Name) {
1896 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1897 Diag(Template->getLocation(), diag::note_template_declared_here)
1898 << (isa<FunctionTemplateDecl>(Template)? 0
1899 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001900 : isa<TypeAliasTemplateDecl>(Template)? 2
1901 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001902 << Template->getDeclName();
1903 return;
1904 }
1905
1906 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1907 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1908 IEnd = OST->end();
1909 I != IEnd; ++I)
1910 Diag((*I)->getLocation(), diag::note_template_declared_here)
1911 << 0 << (*I)->getDeclName();
1912
1913 return;
1914 }
1915}
1916
Douglas Gregor7532dc62009-03-30 22:58:21 +00001917QualType Sema::CheckTemplateIdType(TemplateName Name,
1918 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001919 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001920 DependentTemplateName *DTN
1921 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001922 if (DTN && DTN->isIdentifier())
1923 // When building a template-id where the template-name is dependent,
1924 // assume the template is a type template. Either our assumption is
1925 // correct, or the code is ill-formed and will be diagnosed when the
1926 // dependent name is substituted.
1927 return Context.getDependentTemplateSpecializationType(ETK_None,
1928 DTN->getQualifier(),
1929 DTN->getIdentifier(),
1930 TemplateArgs);
1931
Douglas Gregor7532dc62009-03-30 22:58:21 +00001932 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001933 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1934 // We might have a substituted template template parameter pack. If so,
1935 // build a template specialization type for it.
1936 if (Name.getAsSubstTemplateTemplateParmPack())
1937 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001938
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001939 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1940 << Name;
1941 NoteAllFoundTemplates(Name);
1942 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001943 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001944
Douglas Gregor40808ce2009-03-09 23:48:35 +00001945 // Check that the template argument list is well-formed for this
1946 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001947 SmallVector<TemplateArgument, 4> Converted;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001948 bool ExpansionIntoFixedList = false;
John McCalld5532b62009-11-23 01:53:49 +00001949 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00001950 false, Converted, &ExpansionIntoFixedList))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001951 return QualType();
1952
Douglas Gregor40808ce2009-03-09 23:48:35 +00001953 QualType CanonType;
1954
Douglas Gregor561f8122011-07-01 01:22:09 +00001955 bool InstantiationDependent = false;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001956 TypeAliasTemplateDecl *AliasTemplate = 0;
1957 if (!ExpansionIntoFixedList &&
1958 (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001959 // Find the canonical type for this type alias template specialization.
1960 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1961 if (Pattern->isInvalidDecl())
1962 return QualType();
1963
1964 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1965 Converted.data(), Converted.size());
1966
1967 // Only substitute for the innermost template argument list.
1968 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001969 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001970 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1971 for (unsigned I = 0; I < Depth; ++I)
1972 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001973
1974 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1975 CanonType = SubstType(Pattern->getUnderlyingType(),
1976 TemplateArgLists, AliasTemplate->getLocation(),
1977 AliasTemplate->getDeclName());
1978 if (CanonType.isNull())
1979 return QualType();
1980 } else if (Name.isDependent() ||
1981 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001982 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001983 // This class template specialization is a dependent
1984 // type. Therefore, its canonical type is another class template
1985 // specialization type that contains all of the converted
1986 // arguments in canonical form. This ensures that, e.g., A<T> and
1987 // A<T, T> have identical types when A is declared as:
1988 //
1989 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001990 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001991 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001992 Converted.data(),
1993 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Douglas Gregor1275ae02009-07-28 23:00:59 +00001995 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001996 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001997 // In the future, we need to teach getTemplateSpecializationType to only
1998 // build the canonical type and return that to us.
1999 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00002000
2001 // This might work out to be a current instantiation, in which
2002 // case the canonical type needs to be the InjectedClassNameType.
2003 //
2004 // TODO: in theory this could be a simple hashtable lookup; most
2005 // changes to CurContext don't change the set of current
2006 // instantiations.
2007 if (isa<ClassTemplateDecl>(Template)) {
2008 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
2009 // If we get out to a namespace, we're done.
2010 if (Ctx->isFileContext()) break;
2011
2012 // If this isn't a record, keep looking.
2013 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
2014 if (!Record) continue;
2015
2016 // Look for one of the two cases with InjectedClassNameTypes
2017 // and check whether it's the same template.
2018 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
2019 !Record->getDescribedClassTemplate())
2020 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002021
John McCall31f17ec2010-04-27 00:57:59 +00002022 // Fetch the injected class name type and check whether its
2023 // injected type is equal to the type we just built.
2024 QualType ICNT = Context.getTypeDeclType(Record);
2025 QualType Injected = cast<InjectedClassNameType>(ICNT)
2026 ->getInjectedSpecializationType();
2027
2028 if (CanonType != Injected->getCanonicalTypeInternal())
2029 continue;
2030
2031 // If so, the canonical type of this TST is the injected
2032 // class name type of the record we just found.
2033 assert(ICNT.isCanonical());
2034 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002035 break;
2036 }
2037 }
Mike Stump1eb44332009-09-09 15:08:12 +00002038 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002039 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002040 // Find the class template specialization declaration that
2041 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002042 void *InsertPos = 0;
2043 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002044 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002045 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002046 if (!Decl) {
2047 // This is the first time we have referenced this class template
2048 // specialization. Create the canonical declaration and add it to
2049 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002050 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002051 ClassTemplate->getTemplatedDecl()->getTagKind(),
2052 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002053 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002054 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002055 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002056 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002057 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002058 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002059 Decl->setLexicalDeclContext(CurContext);
2060 }
2061
2062 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002063 assert(isa<RecordType>(CanonType) &&
2064 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002065 }
Mike Stump1eb44332009-09-09 15:08:12 +00002066
Douglas Gregor40808ce2009-03-09 23:48:35 +00002067 // Build the fully-sugared type for this class template
2068 // specialization, which refers back to the class template
2069 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002070 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002071}
2072
John McCallf312b1e2010-08-26 23:41:50 +00002073TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002074Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00002075 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002076 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002077 ASTTemplateArgsPtr TemplateArgsIn,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002078 SourceLocation RAngleLoc,
2079 bool IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002080 if (SS.isInvalid())
2081 return true;
2082
Douglas Gregor7532dc62009-03-30 22:58:21 +00002083 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002084
Douglas Gregor40808ce2009-03-09 23:48:35 +00002085 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002086 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002087 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002088
Douglas Gregora88f09f2011-02-28 17:23:35 +00002089 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002090 QualType T
2091 = Context.getDependentTemplateSpecializationType(ETK_None,
2092 DTN->getQualifier(),
2093 DTN->getIdentifier(),
2094 TemplateArgs);
2095 // Build type-source information.
Douglas Gregora88f09f2011-02-28 17:23:35 +00002096 TypeLocBuilder TLB;
2097 DependentTemplateSpecializationTypeLoc SpecTL
2098 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002099 SpecTL.setElaboratedKeywordLoc(SourceLocation());
2100 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002101 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002102 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002103 SpecTL.setLAngleLoc(LAngleLoc);
2104 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002105 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2106 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2107 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2108 }
2109
John McCalld5532b62009-11-23 01:53:49 +00002110 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002111 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002112
2113 if (Result.isNull())
2114 return true;
2115
Douglas Gregor059101f2011-03-02 00:47:37 +00002116 // Build type-source information.
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002117 TypeLocBuilder TLB;
Douglas Gregor059101f2011-03-02 00:47:37 +00002118 TemplateSpecializationTypeLoc SpecTL
2119 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002120 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002121 SpecTL.setTemplateNameLoc(TemplateLoc);
2122 SpecTL.setLAngleLoc(LAngleLoc);
2123 SpecTL.setRAngleLoc(RAngleLoc);
2124 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2125 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002126
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002127 // NOTE: avoid constructing an ElaboratedTypeLoc if this is a
2128 // constructor or destructor name (in such a case, the scope specifier
2129 // will be attached to the enclosing Decl or Expr node).
2130 if (SS.isNotEmpty() && !IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002131 // Create an elaborated-type-specifier containing the nested-name-specifier.
2132 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2133 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002134 ElabTL.setElaboratedKeywordLoc(SourceLocation());
Douglas Gregor059101f2011-03-02 00:47:37 +00002135 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2136 }
2137
2138 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002139}
John McCallf1bbbb42009-09-04 01:14:41 +00002140
Douglas Gregor059101f2011-03-02 00:47:37 +00002141TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002142 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002143 SourceLocation TagLoc,
2144 CXXScopeSpec &SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002145 SourceLocation TemplateKWLoc,
2146 TemplateTy TemplateD,
Douglas Gregor059101f2011-03-02 00:47:37 +00002147 SourceLocation TemplateLoc,
2148 SourceLocation LAngleLoc,
2149 ASTTemplateArgsPtr TemplateArgsIn,
2150 SourceLocation RAngleLoc) {
2151 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2152
2153 // Translate the parser's template argument list in our AST format.
2154 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2155 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2156
2157 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002158 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002159 ElaboratedTypeKeyword Keyword
2160 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Douglas Gregor059101f2011-03-02 00:47:37 +00002162 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2163 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2164 DTN->getQualifier(),
2165 DTN->getIdentifier(),
2166 TemplateArgs);
2167
2168 // Build type-source information.
2169 TypeLocBuilder TLB;
2170 DependentTemplateSpecializationTypeLoc SpecTL
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002171 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2172 SpecTL.setElaboratedKeywordLoc(TagLoc);
2173 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002174 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002175 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002176 SpecTL.setLAngleLoc(LAngleLoc);
2177 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002178 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2179 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2180 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2181 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002182
2183 if (TypeAliasTemplateDecl *TAT =
2184 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2185 // C++0x [dcl.type.elab]p2:
2186 // If the identifier resolves to a typedef-name or the simple-template-id
2187 // resolves to an alias template specialization, the
2188 // elaborated-type-specifier is ill-formed.
2189 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2190 Diag(TAT->getLocation(), diag::note_declared_at);
2191 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002192
2193 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2194 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002195 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002196
2197 // Check the tag kind
2198 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002199 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002200
John McCall6b2becf2009-09-08 17:47:29 +00002201 IdentifierInfo *Id = D->getIdentifier();
2202 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002203
Richard Trieubbf34c02011-06-10 03:11:26 +00002204 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2205 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002206 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002207 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002208 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002209 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002210 }
2211 }
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002212
Douglas Gregor059101f2011-03-02 00:47:37 +00002213 // Provide source-location information for the template specialization.
2214 TypeLocBuilder TLB;
2215 TemplateSpecializationTypeLoc SpecTL
2216 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002217 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002218 SpecTL.setTemplateNameLoc(TemplateLoc);
2219 SpecTL.setLAngleLoc(LAngleLoc);
2220 SpecTL.setRAngleLoc(RAngleLoc);
2221 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2222 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002223
Douglas Gregor059101f2011-03-02 00:47:37 +00002224 // Construct an elaborated type containing the nested-name-specifier (if any)
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002225 // and tag keyword.
Douglas Gregor059101f2011-03-02 00:47:37 +00002226 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2227 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002228 ElabTL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002229 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2230 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002231}
2232
John McCall60d7b3a2010-08-24 06:29:42 +00002233ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002234 SourceLocation TemplateKWLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002235 LookupResult &R,
2236 bool RequiresADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002237 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002238 // FIXME: Can we do any checking at this point? I guess we could check the
2239 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002240 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002241 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002242 // foo<int> could identify a single function unambiguously
2243 // This approach does NOT work, since f<int>(1);
2244 // gets resolved prior to resorting to overload resolution
2245 // i.e., template<class T> void f(double);
2246 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002247
2248 // These should be filtered out by our callers.
2249 assert(!R.empty() && "empty lookup results when building templateid");
2250 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2251
John McCallc373d482010-01-27 01:50:18 +00002252 // We don't want lookup warnings at this point.
2253 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002254
John McCallf7a1a742009-11-24 19:00:30 +00002255 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002256 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002257 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002258 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002259 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002260 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002261 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002262
2263 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002264}
2265
John McCallf7a1a742009-11-24 19:00:30 +00002266// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002267ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002268Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002269 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002270 const DeclarationNameInfo &NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002271 const TemplateArgumentListInfo *TemplateArgs) {
2272 assert(TemplateArgs || TemplateKWLoc.isValid());
John McCallf7a1a742009-11-24 19:00:30 +00002273 DeclContext *DC;
2274 if (!(DC = computeDeclContext(SS, false)) ||
2275 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002276 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002277 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002279 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002280 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002281 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2282 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002283
John McCallf7a1a742009-11-24 19:00:30 +00002284 if (R.isAmbiguous())
2285 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002286
John McCallf7a1a742009-11-24 19:00:30 +00002287 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002288 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2289 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002290 return ExprError();
2291 }
2292
2293 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002294 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2295 << (NestedNameSpecifier*) SS.getScopeRep()
2296 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002297 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2298 return ExprError();
2299 }
2300
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002301 return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002302}
2303
Douglas Gregorc45c2322009-03-31 00:43:58 +00002304/// \brief Form a dependent template name.
2305///
2306/// This action forms a dependent template name given the template
2307/// name and its (presumably dependent) scope specifier. For
2308/// example, given "MetaFun::template apply", the scope specifier \p
2309/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2310/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002311TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002312 CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002313 SourceLocation TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002314 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002315 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002316 bool EnteringContext,
2317 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002318 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2319 Diag(TemplateKWLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00002320 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00002321 diag::warn_cxx98_compat_template_outside_of_template :
2322 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002323 << FixItHint::CreateRemoval(TemplateKWLoc);
2324
Douglas Gregor0707bc52010-01-19 16:01:07 +00002325 DeclContext *LookupCtx = 0;
2326 if (SS.isSet())
2327 LookupCtx = computeDeclContext(SS, EnteringContext);
2328 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002329 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002330 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002331 // C++0x [temp.names]p5:
2332 // If a name prefixed by the keyword template is not the name of
2333 // a template, the program is ill-formed. [Note: the keyword
2334 // template may not be applied to non-template members of class
2335 // templates. -end note ] [ Note: as is the case with the
2336 // typename prefix, the template prefix is allowed in cases
2337 // where it is not strictly necessary; i.e., when the
2338 // nested-name-specifier or the expression on the left of the ->
2339 // or . is not dependent on a template-parameter, or the use
2340 // does not appear in the scope of a template. -end note]
2341 //
2342 // Note: C++03 was more strict here, because it banned the use of
2343 // the "template" keyword prior to a template-name that was not a
2344 // dependent name. C++ DR468 relaxed this requirement (the
2345 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002346 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002347 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002348 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2349 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002350 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002351 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2352 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002353 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2354 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002355 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002356 } else if (TNK == TNK_Non_template) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00002357 Diag(Name.getLocStart(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002358 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002359 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002360 << Name.getSourceRange()
2361 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002362 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002363 } else {
2364 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002365 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002366 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002367 }
2368
Mike Stump1eb44332009-09-09 15:08:12 +00002369 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002370 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002371
Douglas Gregor014e88d2009-11-03 23:16:33 +00002372 switch (Name.getKind()) {
2373 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002374 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002375 Name.Identifier));
2376 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002377
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002378 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002379 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002380 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002381 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002382
2383 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002384 llvm_unreachable(
2385 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002386
Douglas Gregor014e88d2009-11-03 23:16:33 +00002387 default:
2388 break;
2389 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002390
Daniel Dunbar96a00142012-03-09 18:35:03 +00002391 Diag(Name.getLocStart(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002392 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002393 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002394 << Name.getSourceRange()
2395 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002396 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002397}
2398
Mike Stump1eb44332009-09-09 15:08:12 +00002399bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002400 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002401 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002402 const TemplateArgument &Arg = AL.getArgument();
2403
Anders Carlsson436b1562009-06-13 00:33:33 +00002404 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002405 switch(Arg.getKind()) {
2406 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002407 // C++ [temp.arg.type]p1:
2408 // A template-argument for a template-parameter which is a
2409 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002410 break;
2411 case TemplateArgument::Template: {
2412 // We have a template type parameter but the template argument
2413 // is a template without any arguments.
2414 SourceRange SR = AL.getSourceRange();
2415 TemplateName Name = Arg.getAsTemplate();
2416 Diag(SR.getBegin(), diag::err_template_missing_args)
2417 << Name << SR;
2418 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2419 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002420
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002421 return true;
2422 }
2423 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002424 // We have a template type parameter but the template argument
2425 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002426 SourceRange SR = AL.getSourceRange();
2427 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002428 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002429
Anders Carlsson436b1562009-06-13 00:33:33 +00002430 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002431 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002432 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002433
John McCalla93c9342009-12-07 02:54:59 +00002434 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002435 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002436
Anders Carlsson436b1562009-06-13 00:33:33 +00002437 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002438 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2439
2440 // Objective-C ARC:
2441 // If an explicitly-specified template argument type is a lifetime type
2442 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
David Blaikie4e4d0842012-03-11 07:00:24 +00002443 if (getLangOpts().ObjCAutoRefCount &&
Douglas Gregore559ca12011-06-17 22:11:49 +00002444 ArgType->isObjCLifetimeType() &&
2445 !ArgType.getObjCLifetime()) {
2446 Qualifiers Qs;
2447 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2448 ArgType = Context.getQualifiedType(ArgType, Qs);
2449 }
2450
2451 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002452 return false;
2453}
2454
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002455/// \brief Substitute template arguments into the default template argument for
2456/// the given template type parameter.
2457///
2458/// \param SemaRef the semantic analysis object for which we are performing
2459/// the substitution.
2460///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002461/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002462/// for.
2463///
2464/// \param TemplateLoc the location of the template name that started the
2465/// template-id we are checking.
2466///
2467/// \param RAngleLoc the location of the right angle bracket ('>') that
2468/// terminates the template-id.
2469///
2470/// \param Param the template template parameter whose default we are
2471/// substituting into.
2472///
2473/// \param Converted the list of template arguments provided for template
2474/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002475/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002476static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002477SubstDefaultTemplateArgument(Sema &SemaRef,
2478 TemplateDecl *Template,
2479 SourceLocation TemplateLoc,
2480 SourceLocation RAngleLoc,
2481 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002482 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002483 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002484
2485 // If the argument type is dependent, instantiate it now based
2486 // on the previously-computed template arguments.
2487 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002488 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002489 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002490
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002491 MultiLevelTemplateArgumentList AllTemplateArgs
2492 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2493
2494 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002495 Template, Converted.data(),
2496 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002497 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002498
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002499 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2500 Param->getDefaultArgumentLoc(),
2501 Param->getDeclName());
2502 }
2503
2504 return ArgType;
2505}
2506
2507/// \brief Substitute template arguments into the default template argument for
2508/// the given non-type template parameter.
2509///
2510/// \param SemaRef the semantic analysis object for which we are performing
2511/// the substitution.
2512///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002513/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002514/// for.
2515///
2516/// \param TemplateLoc the location of the template name that started the
2517/// template-id we are checking.
2518///
2519/// \param RAngleLoc the location of the right angle bracket ('>') that
2520/// terminates the template-id.
2521///
Douglas Gregor788cd062009-11-11 01:00:40 +00002522/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002523/// substituting into.
2524///
2525/// \param Converted the list of template arguments provided for template
2526/// parameters that precede \p Param in the template parameter list.
2527///
2528/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002529static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002530SubstDefaultTemplateArgument(Sema &SemaRef,
2531 TemplateDecl *Template,
2532 SourceLocation TemplateLoc,
2533 SourceLocation RAngleLoc,
2534 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002535 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002536 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002537 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002538
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002539 MultiLevelTemplateArgumentList AllTemplateArgs
2540 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002541
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002542 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002543 Template, Converted.data(),
2544 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002545 SourceRange(TemplateLoc, RAngleLoc));
2546
2547 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2548}
2549
Douglas Gregor788cd062009-11-11 01:00:40 +00002550/// \brief Substitute template arguments into the default template argument for
2551/// the given template template parameter.
2552///
2553/// \param SemaRef the semantic analysis object for which we are performing
2554/// the substitution.
2555///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002556/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002557/// for.
2558///
2559/// \param TemplateLoc the location of the template name that started the
2560/// template-id we are checking.
2561///
2562/// \param RAngleLoc the location of the right angle bracket ('>') that
2563/// terminates the template-id.
2564///
2565/// \param Param the template template parameter whose default we are
2566/// substituting into.
2567///
2568/// \param Converted the list of template arguments provided for template
2569/// parameters that precede \p Param in the template parameter list.
2570///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002571/// \param QualifierLoc Will be set to the nested-name-specifier (with
2572/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002573///
Douglas Gregor788cd062009-11-11 01:00:40 +00002574/// \returns the substituted template argument, or NULL if an error occurred.
2575static TemplateName
2576SubstDefaultTemplateArgument(Sema &SemaRef,
2577 TemplateDecl *Template,
2578 SourceLocation TemplateLoc,
2579 SourceLocation RAngleLoc,
2580 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002581 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002582 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002583 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002584 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002585
Douglas Gregor788cd062009-11-11 01:00:40 +00002586 MultiLevelTemplateArgumentList AllTemplateArgs
2587 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002588
Douglas Gregor788cd062009-11-11 01:00:40 +00002589 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002590 Template, Converted.data(),
2591 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002592 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002593
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002594 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002595 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002596 if (QualifierLoc) {
2597 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2598 AllTemplateArgs);
2599 if (!QualifierLoc)
2600 return TemplateName();
2601 }
2602
Douglas Gregor1d752d72011-03-02 18:46:51 +00002603 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002604 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002605 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002606 AllTemplateArgs);
2607}
2608
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002609/// \brief If the given template parameter has a default template
2610/// argument, substitute into that default template argument and
2611/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002612TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002613Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2614 SourceLocation TemplateLoc,
2615 SourceLocation RAngleLoc,
2616 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002617 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002618 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002619 if (!TypeParm->hasDefaultArgument())
2620 return TemplateArgumentLoc();
2621
John McCalla93c9342009-12-07 02:54:59 +00002622 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002623 TemplateLoc,
2624 RAngleLoc,
2625 TypeParm,
2626 Converted);
2627 if (DI)
2628 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2629
2630 return TemplateArgumentLoc();
2631 }
2632
2633 if (NonTypeTemplateParmDecl *NonTypeParm
2634 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2635 if (!NonTypeParm->hasDefaultArgument())
2636 return TemplateArgumentLoc();
2637
John McCall60d7b3a2010-08-24 06:29:42 +00002638 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002639 TemplateLoc,
2640 RAngleLoc,
2641 NonTypeParm,
2642 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002643 if (Arg.isInvalid())
2644 return TemplateArgumentLoc();
2645
2646 Expr *ArgE = Arg.takeAs<Expr>();
2647 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2648 }
2649
2650 TemplateTemplateParmDecl *TempTempParm
2651 = cast<TemplateTemplateParmDecl>(Param);
2652 if (!TempTempParm->hasDefaultArgument())
2653 return TemplateArgumentLoc();
2654
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002655
Douglas Gregor1d752d72011-03-02 18:46:51 +00002656 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002657 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002658 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002659 RAngleLoc,
2660 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002661 Converted,
2662 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002663 if (TName.isNull())
2664 return TemplateArgumentLoc();
2665
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002666 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002667 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002668 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2669}
2670
Douglas Gregore7526412009-11-11 19:31:23 +00002671/// \brief Check that the given template argument corresponds to the given
2672/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002673///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002674/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002675/// checked.
2676///
2677/// \param Arg The template argument.
2678///
2679/// \param Template The template in which the template argument resides.
2680///
2681/// \param TemplateLoc The location of the template name for the template
2682/// whose argument list we're matching.
2683///
2684/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2685/// the template argument list.
2686///
2687/// \param ArgumentPackIndex The index into the argument pack where this
2688/// argument will be placed. Only valid if the parameter is a parameter pack.
2689///
2690/// \param Converted The checked, converted argument will be added to the
2691/// end of this small vector.
2692///
2693/// \param CTAK Describes how we arrived at this particular template argument:
2694/// explicitly written, deduced, etc.
2695///
2696/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002697bool Sema::CheckTemplateArgument(NamedDecl *Param,
2698 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002699 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002700 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002701 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002702 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002703 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002704 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002705 // Check template type parameters.
2706 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002707 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002708
Douglas Gregord9e15302009-11-11 19:41:09 +00002709 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002710 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002711 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002712 // with the template arguments we've seen thus far. But if the
2713 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002714 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002715 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2716 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002717
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002718 if (NTTPType->isDependentType() &&
2719 !isa<TemplateTemplateParmDecl>(Template) &&
2720 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002721 // Do substitution on the type of the non-type template parameter.
2722 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002723 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002724 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002725
2726 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002727 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002728 NTTPType = SubstType(NTTPType,
2729 MultiLevelTemplateArgumentList(TemplateArgs),
2730 NTTP->getLocation(),
2731 NTTP->getDeclName());
2732 // If that worked, check the non-type template parameter type
2733 // for validity.
2734 if (!NTTPType.isNull())
2735 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2736 NTTP->getLocation());
2737 if (NTTPType.isNull())
2738 return true;
2739 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002740
Douglas Gregore7526412009-11-11 19:31:23 +00002741 switch (Arg.getArgument().getKind()) {
2742 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002743 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002744
Douglas Gregore7526412009-11-11 19:31:23 +00002745 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002746 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002747 ExprResult Res =
2748 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2749 Result, CTAK);
2750 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002751 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002752
Douglas Gregor910f8002010-11-07 23:05:16 +00002753 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002754 break;
2755 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002756
Douglas Gregore7526412009-11-11 19:31:23 +00002757 case TemplateArgument::Declaration:
2758 case TemplateArgument::Integral:
2759 // We've already checked this template argument, so just copy
2760 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002761 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002762 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002763
Douglas Gregore7526412009-11-11 19:31:23 +00002764 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002765 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002766 // We were given a template template argument. It may not be ill-formed;
2767 // see below.
2768 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002769 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2770 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002771 // We have a template argument such as \c T::template X, which we
2772 // parsed as a template template argument. However, since we now
2773 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002774 // template name into an expression.
2775
2776 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2777 Arg.getTemplateNameLoc());
2778
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002779 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002780 SS.Adopt(Arg.getTemplateQualifierLoc());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002781 // FIXME: the template-template arg was a DependentTemplateName,
2782 // so it was provided with a template keyword. However, its source
2783 // location is not stored in the template argument structure.
2784 SourceLocation TemplateKWLoc;
John Wiegley429bb272011-04-08 18:41:53 +00002785 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002786 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002787 TemplateKWLoc,
2788 NameInfo, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789
Douglas Gregora7fc9012011-01-05 18:58:31 +00002790 // If we parsed the template argument as a pack expansion, create a
2791 // pack expansion expression.
2792 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002793 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2794 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002795 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002796 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002797
Douglas Gregore7526412009-11-11 19:31:23 +00002798 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002799 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2800 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002801 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002802
Douglas Gregor910f8002010-11-07 23:05:16 +00002803 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002804 break;
2805 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002806
Douglas Gregore7526412009-11-11 19:31:23 +00002807 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002808 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002809 // therefore cannot be a non-type template argument.
2810 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2811 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002812
Douglas Gregore7526412009-11-11 19:31:23 +00002813 Diag(Param->getLocation(), diag::note_template_param_here);
2814 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002815
Douglas Gregore7526412009-11-11 19:31:23 +00002816 case TemplateArgument::Type: {
2817 // We have a non-type template parameter but the template
2818 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002819
Douglas Gregore7526412009-11-11 19:31:23 +00002820 // C++ [temp.arg]p2:
2821 // In a template-argument, an ambiguity between a type-id and
2822 // an expression is resolved to a type-id, regardless of the
2823 // form of the corresponding template-parameter.
2824 //
2825 // We warn specifically about this case, since it can be rather
2826 // confusing for users.
2827 QualType T = Arg.getArgument().getAsType();
2828 SourceRange SR = Arg.getSourceRange();
2829 if (T->isFunctionType())
2830 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2831 else
2832 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2833 Diag(Param->getLocation(), diag::note_template_param_here);
2834 return true;
2835 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002836
Douglas Gregore7526412009-11-11 19:31:23 +00002837 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002838 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002839 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002840
Douglas Gregore7526412009-11-11 19:31:23 +00002841 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002842 }
2843
2844
Douglas Gregore7526412009-11-11 19:31:23 +00002845 // Check template template parameters.
2846 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002847
Douglas Gregore7526412009-11-11 19:31:23 +00002848 // Substitute into the template parameter list of the template
2849 // template parameter, since previously-supplied template arguments
2850 // may appear within the template template parameter.
2851 {
2852 // Set up a template instantiation context.
2853 LocalInstantiationScope Scope(*this);
2854 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002855 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002856 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002857
2858 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002859 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002860 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002861 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002862 MultiLevelTemplateArgumentList(TemplateArgs)));
2863 if (!TempParm)
2864 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002865 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002866
Douglas Gregore7526412009-11-11 19:31:23 +00002867 switch (Arg.getArgument().getKind()) {
2868 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002869 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002870
Douglas Gregore7526412009-11-11 19:31:23 +00002871 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002872 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002873 if (CheckTemplateArgument(TempParm, Arg))
2874 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002875
Douglas Gregor910f8002010-11-07 23:05:16 +00002876 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002877 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002878
Douglas Gregore7526412009-11-11 19:31:23 +00002879 case TemplateArgument::Expression:
2880 case TemplateArgument::Type:
2881 // We have a template template parameter but the template
2882 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002883 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
David Blaikie4e4d0842012-03-11 07:00:24 +00002884 << getLangOpts().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002885 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002886
Douglas Gregore7526412009-11-11 19:31:23 +00002887 case TemplateArgument::Declaration:
David Blaikie7530c032012-01-17 06:56:22 +00002888 llvm_unreachable("Declaration argument with template template parameter");
Douglas Gregore7526412009-11-11 19:31:23 +00002889 case TemplateArgument::Integral:
David Blaikie7530c032012-01-17 06:56:22 +00002890 llvm_unreachable("Integral argument with template template parameter");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002891
Douglas Gregore7526412009-11-11 19:31:23 +00002892 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002893 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002894 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002895
Douglas Gregore7526412009-11-11 19:31:23 +00002896 return false;
2897}
2898
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002899/// \brief Diagnose an arity mismatch in the
2900static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template,
2901 SourceLocation TemplateLoc,
2902 TemplateArgumentListInfo &TemplateArgs) {
2903 TemplateParameterList *Params = Template->getTemplateParameters();
2904 unsigned NumParams = Params->size();
2905 unsigned NumArgs = TemplateArgs.size();
2906
2907 SourceRange Range;
2908 if (NumArgs > NumParams)
2909 Range = SourceRange(TemplateArgs[NumParams].getLocation(),
2910 TemplateArgs.getRAngleLoc());
2911 S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2912 << (NumArgs > NumParams)
2913 << (isa<ClassTemplateDecl>(Template)? 0 :
2914 isa<FunctionTemplateDecl>(Template)? 1 :
2915 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2916 << Template << Range;
2917 S.Diag(Template->getLocation(), diag::note_template_decl_here)
2918 << Params->getSourceRange();
2919 return true;
2920}
2921
Douglas Gregorc15cb382009-02-09 23:23:08 +00002922/// \brief Check that the given template argument list is well-formed
2923/// for specializing the given template.
2924bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2925 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002926 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002927 bool PartialTemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00002928 SmallVectorImpl<TemplateArgument> &Converted,
2929 bool *ExpansionIntoFixedList) {
2930 if (ExpansionIntoFixedList)
2931 *ExpansionIntoFixedList = false;
2932
Douglas Gregorc15cb382009-02-09 23:23:08 +00002933 TemplateParameterList *Params = Template->getTemplateParameters();
2934 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002935 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002936 bool Invalid = false;
2937
John McCalld5532b62009-11-23 01:53:49 +00002938 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2939
Mike Stump1eb44332009-09-09 15:08:12 +00002940 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002941 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Douglas Gregorb70126a2012-02-03 17:16:23 +00002942
Mike Stump1eb44332009-09-09 15:08:12 +00002943 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002944 // [...] The type and form of each template-argument specified in
2945 // a template-id shall match the type and form specified for the
2946 // corresponding parameter declared by the template in its
2947 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002948 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002949 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002950 TemplateParameterList::iterator Param = Params->begin(),
2951 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002952 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002953 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002954 bool SawPackExpansion = false;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002955 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002956 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002957 // If we have an expanded parameter pack, make sure we don't have too
2958 // many arguments.
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002959 // FIXME: This really should fall out from the normal arity checking.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002960 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002961 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002962 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002963 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2964 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2965 << true
2966 << (isa<ClassTemplateDecl>(Template)? 0 :
2967 isa<FunctionTemplateDecl>(Template)? 1 :
2968 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2969 << Template;
2970 Diag(Template->getLocation(), diag::note_template_decl_here)
2971 << Params->getSourceRange();
2972 return true;
2973 }
2974 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002975
Douglas Gregorf35f8282009-11-11 21:54:23 +00002976 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002977 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2978 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002979 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002980 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002981
Douglas Gregor14be16b2010-12-20 16:57:52 +00002982 if ((*Param)->isTemplateParameterPack()) {
2983 // The template parameter was a template parameter pack, so take the
2984 // deduced argument and place it on the argument pack. Note that we
2985 // stay on the same template parameter so that we can deduce more
2986 // arguments.
2987 ArgumentPack.push_back(Converted.back());
2988 Converted.pop_back();
2989 } else {
2990 // Move to the next template parameter.
2991 ++Param;
2992 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002993
2994 // If this template argument is a pack expansion, record that fact
2995 // and break out; we can't actually check any more.
2996 if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) {
2997 SawPackExpansion = true;
2998 ++ArgIdx;
2999 break;
3000 }
3001
Douglas Gregor14be16b2010-12-20 16:57:52 +00003002 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003003 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003004 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003005
Douglas Gregor8735b292011-06-03 02:59:40 +00003006 // If we're checking a partial template argument list, we're done.
3007 if (PartialTemplateArgs) {
3008 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
3009 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3010 ArgumentPack.data(),
3011 ArgumentPack.size()));
3012
3013 return Invalid;
3014 }
3015
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003016 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00003017 // arguments, just break out now and we'll fill in the argument pack below.
3018 if ((*Param)->isTemplateParameterPack())
3019 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00003020
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003021 // Check whether we have a default argument.
Douglas Gregorf35f8282009-11-11 21:54:23 +00003022 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003023
Douglas Gregorf35f8282009-11-11 21:54:23 +00003024 // Retrieve the default template argument from the template
3025 // parameter. For each kind of template parameter, we substitute the
3026 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003027 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00003028 // the default argument.
3029 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003030 if (!TTP->hasDefaultArgument())
3031 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3032 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003033
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003034 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003035 Template,
3036 TemplateLoc,
3037 RAngleLoc,
3038 TTP,
3039 Converted);
3040 if (!ArgType)
3041 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003042
Douglas Gregorf35f8282009-11-11 21:54:23 +00003043 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
3044 ArgType);
3045 } else if (NonTypeTemplateParmDecl *NTTP
3046 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003047 if (!NTTP->hasDefaultArgument())
3048 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3049 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003050
John McCall60d7b3a2010-08-24 06:29:42 +00003051 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003052 TemplateLoc,
3053 RAngleLoc,
3054 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003055 Converted);
3056 if (E.isInvalid())
3057 return true;
3058
3059 Expr *Ex = E.takeAs<Expr>();
3060 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3061 } else {
3062 TemplateTemplateParmDecl *TempParm
3063 = cast<TemplateTemplateParmDecl>(*Param);
3064
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003065 if (!TempParm->hasDefaultArgument())
3066 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3067 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003068
Douglas Gregor1d752d72011-03-02 18:46:51 +00003069 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003070 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003071 TemplateLoc,
3072 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003073 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003074 Converted,
3075 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003076 if (Name.isNull())
3077 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003078
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003079 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3080 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003081 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003082
Douglas Gregorf35f8282009-11-11 21:54:23 +00003083 // Introduce an instantiation record that describes where we are using
3084 // the default template argument.
3085 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003086 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003087 SourceRange(TemplateLoc, RAngleLoc));
3088
Douglas Gregorf35f8282009-11-11 21:54:23 +00003089 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003090 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003091 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003092 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003093
Douglas Gregor67714232011-03-03 02:41:12 +00003094 // Core issue 150 (assumed resolution): if this is a template template
3095 // parameter, keep track of the default template arguments from the
3096 // template definition.
3097 if (isTemplateTemplateParameter)
3098 TemplateArgs.addArgument(Arg);
3099
Douglas Gregor14be16b2010-12-20 16:57:52 +00003100 // Move to the next template parameter and argument.
3101 ++Param;
3102 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003103 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003104
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003105 // If we saw a pack expansion, then directly convert the remaining arguments,
3106 // because we don't know what parameters they'll match up with.
3107 if (SawPackExpansion) {
3108 bool AddToArgumentPack
3109 = Param != ParamEnd && (*Param)->isTemplateParameterPack();
3110 while (ArgIdx < NumArgs) {
3111 if (AddToArgumentPack)
3112 ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument());
3113 else
3114 Converted.push_back(TemplateArgs[ArgIdx].getArgument());
3115 ++ArgIdx;
3116 }
3117
3118 // Push the argument pack onto the list of converted arguments.
3119 if (AddToArgumentPack) {
3120 if (ArgumentPack.empty())
3121 Converted.push_back(TemplateArgument(0, 0));
3122 else {
3123 Converted.push_back(
3124 TemplateArgument::CreatePackCopy(Context,
3125 ArgumentPack.data(),
3126 ArgumentPack.size()));
3127 ArgumentPack.clear();
3128 }
Douglas Gregorb70126a2012-02-03 17:16:23 +00003129 } else if (ExpansionIntoFixedList) {
3130 // We have expanded a pack into a fixed list.
3131 *ExpansionIntoFixedList = true;
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003132 }
3133
3134 return Invalid;
3135 }
3136
3137 // If we have any leftover arguments, then there were too many arguments.
3138 // Complain and fail.
3139 if (ArgIdx < NumArgs)
3140 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
3141
3142 // If we have an expanded parameter pack, make sure we don't have too
3143 // many arguments.
3144 // FIXME: This really should fall out from the normal arity checking.
3145 if (Param != ParamEnd) {
3146 if (NonTypeTemplateParmDecl *NTTP
3147 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
3148 if (NTTP->isExpandedParameterPack() &&
3149 ArgumentPack.size() < NTTP->getNumExpansionTypes()) {
3150 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
3151 << false
3152 << (isa<ClassTemplateDecl>(Template)? 0 :
3153 isa<FunctionTemplateDecl>(Template)? 1 :
3154 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
3155 << Template;
3156 Diag(Template->getLocation(), diag::note_template_decl_here)
3157 << Params->getSourceRange();
3158 return true;
3159 }
3160 }
3161 }
3162
Douglas Gregor14be16b2010-12-20 16:57:52 +00003163 // Form argument packs for each of the parameter packs remaining.
3164 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003165 // If we're checking a partial list of template arguments, don't fill
3166 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003167 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003168 if (!HasParameterPack)
3169 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003170 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003171 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003172 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003173 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3174 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003175 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003176 ArgumentPack.clear();
3177 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003178 } else if (!PartialTemplateArgs)
3179 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003180
Douglas Gregor14be16b2010-12-20 16:57:52 +00003181 ++Param;
3182 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003183
Douglas Gregorc15cb382009-02-09 23:23:08 +00003184 return Invalid;
3185}
3186
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003187namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003188 class UnnamedLocalNoLinkageFinder
3189 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003190 {
3191 Sema &S;
3192 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003193
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003194 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003195
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003196 public:
3197 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3198
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003199 bool Visit(QualType T) {
3200 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003201 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003202
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003203#define TYPE(Class, Parent) \
3204 bool Visit##Class##Type(const Class##Type *);
3205#define ABSTRACT_TYPE(Class, Parent) \
3206 bool Visit##Class##Type(const Class##Type *) { return false; }
3207#define NON_CANONICAL_TYPE(Class, Parent) \
3208 bool Visit##Class##Type(const Class##Type *) { return false; }
3209#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003210
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003211 bool VisitTagDecl(const TagDecl *Tag);
3212 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3213 };
3214}
3215
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003216bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003217 return false;
3218}
3219
3220bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3221 return Visit(T->getElementType());
3222}
3223
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003224bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003225 return Visit(T->getPointeeType());
3226}
3227
3228bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003229 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003230 return Visit(T->getPointeeType());
3231}
3232
3233bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003234 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003235 return Visit(T->getPointeeType());
3236}
3237
3238bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003239 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003240 return Visit(T->getPointeeType());
3241}
3242
3243bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003244 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003245 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3246}
3247
3248bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003249 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003250 return Visit(T->getElementType());
3251}
3252
3253bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003254 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003255 return Visit(T->getElementType());
3256}
3257
3258bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003259 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003260 return Visit(T->getElementType());
3261}
3262
3263bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003264 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003265 return Visit(T->getElementType());
3266}
3267
3268bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003269 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003270 return Visit(T->getElementType());
3271}
3272
3273bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3274 return Visit(T->getElementType());
3275}
3276
3277bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3278 return Visit(T->getElementType());
3279}
3280
3281bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3282 const FunctionProtoType* T) {
3283 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003284 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003285 A != AEnd; ++A) {
3286 if (Visit(*A))
3287 return true;
3288 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003289
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003290 return Visit(T->getResultType());
3291}
3292
3293bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3294 const FunctionNoProtoType* T) {
3295 return Visit(T->getResultType());
3296}
3297
3298bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3299 const UnresolvedUsingType*) {
3300 return false;
3301}
3302
3303bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3304 return false;
3305}
3306
3307bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3308 return Visit(T->getUnderlyingType());
3309}
3310
3311bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3312 return false;
3313}
3314
Sean Huntca63c202011-05-24 22:41:36 +00003315bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3316 const UnaryTransformType*) {
3317 return false;
3318}
3319
Richard Smith34b41d92011-02-20 03:19:35 +00003320bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3321 return Visit(T->getDeducedType());
3322}
3323
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003324bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3325 return VisitTagDecl(T->getDecl());
3326}
3327
3328bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3329 return VisitTagDecl(T->getDecl());
3330}
3331
3332bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3333 const TemplateTypeParmType*) {
3334 return false;
3335}
3336
Douglas Gregorc3069d62011-01-14 02:55:32 +00003337bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3338 const SubstTemplateTypeParmPackType *) {
3339 return false;
3340}
3341
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003342bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3343 const TemplateSpecializationType*) {
3344 return false;
3345}
3346
3347bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3348 const InjectedClassNameType* T) {
3349 return VisitTagDecl(T->getDecl());
3350}
3351
3352bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3353 const DependentNameType* T) {
3354 return VisitNestedNameSpecifier(T->getQualifier());
3355}
3356
3357bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3358 const DependentTemplateSpecializationType* T) {
3359 return VisitNestedNameSpecifier(T->getQualifier());
3360}
3361
Douglas Gregor7536dd52010-12-20 02:24:11 +00003362bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3363 const PackExpansionType* T) {
3364 return Visit(T->getPattern());
3365}
3366
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003367bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3368 return false;
3369}
3370
3371bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3372 const ObjCInterfaceType *) {
3373 return false;
3374}
3375
3376bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3377 const ObjCObjectPointerType *) {
3378 return false;
3379}
3380
Eli Friedmanb001de72011-10-06 23:00:33 +00003381bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3382 return Visit(T->getValueType());
3383}
3384
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003385bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3386 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003387 S.Diag(SR.getBegin(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003388 S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003389 diag::warn_cxx98_compat_template_arg_local_type :
3390 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003391 << S.Context.getTypeDeclType(Tag) << SR;
3392 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003393 }
3394
Richard Smith162e1c12011-04-15 14:24:37 +00003395 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003396 S.Diag(SR.getBegin(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003397 S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003398 diag::warn_cxx98_compat_template_arg_unnamed_type :
3399 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003400 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3401 return true;
3402 }
3403
3404 return false;
3405}
3406
3407bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3408 NestedNameSpecifier *NNS) {
3409 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3410 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003411
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003412 switch (NNS->getKind()) {
3413 case NestedNameSpecifier::Identifier:
3414 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003415 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003416 case NestedNameSpecifier::Global:
3417 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003418
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003419 case NestedNameSpecifier::TypeSpec:
3420 case NestedNameSpecifier::TypeSpecWithTemplate:
3421 return Visit(QualType(NNS->getAsType(), 0));
3422 }
David Blaikie7530c032012-01-17 06:56:22 +00003423 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003424}
3425
3426
Douglas Gregorc15cb382009-02-09 23:23:08 +00003427/// \brief Check a template argument against its corresponding
3428/// template type parameter.
3429///
3430/// This routine implements the semantics of C++ [temp.arg.type]. It
3431/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003432bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003433 TypeSourceInfo *ArgInfo) {
3434 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003435 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003436 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003437
3438 if (Arg->isVariablyModifiedType()) {
3439 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003440 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003441 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003442 }
3443
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003444 // C++03 [temp.arg.type]p2:
3445 // A local type, a type with no linkage, an unnamed type or a type
3446 // compounded from any of these types shall not be used as a
3447 // template-argument for a template type-parameter.
3448 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003449 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003450 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003451 if (LangOpts.CPlusPlus0x ?
3452 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3453 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3454 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3455 SR.getBegin()) != DiagnosticsEngine::Ignored :
3456 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003457 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3458 (void)Finder.Visit(Context.getCanonicalType(Arg));
3459 }
3460
Douglas Gregorc15cb382009-02-09 23:23:08 +00003461 return false;
3462}
3463
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003464/// \brief Checks whether the given template argument is the address
3465/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003466static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003467CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3468 NonTypeTemplateParmDecl *Param,
3469 QualType ParamType,
3470 Expr *ArgIn,
3471 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003472 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003473 Expr *Arg = ArgIn;
3474 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003475
3476 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003477 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003478
3479 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003480 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003481 // A template-argument for a non-type, non-template
3482 // template-parameter shall be one of: [...]
3483 //
3484 // -- the address of an object or function with external
3485 // linkage, including function templates and function
3486 // template-ids but excluding non-static class members,
3487 // expressed as & id-expression where the & is optional if
3488 // the name refers to a function or array, or if the
3489 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003490
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003491 // In C++98/03 mode, give an extension warning on any extra parentheses.
3492 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3493 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003494 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003495 if (!Invalid && !ExtraParens) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003496 S.Diag(Arg->getLocStart(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003497 S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003498 diag::warn_cxx98_compat_template_arg_extra_parens :
3499 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003500 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003501 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003502 }
3503
3504 Arg = Parens->getSubExpr();
3505 }
3506
John McCall91a57552011-07-15 05:09:51 +00003507 while (SubstNonTypeTemplateParmExpr *subst =
3508 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3509 Arg = subst->getReplacement()->IgnoreImpCasts();
3510
Douglas Gregorb7a09262010-04-01 18:32:35 +00003511 bool AddressTaken = false;
3512 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003513 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003514 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003515 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003516 AddressTaken = true;
3517 AddrOpLoc = UnOp->getOperatorLoc();
3518 }
Francois Picheta343a412011-04-29 09:08:14 +00003519 }
John McCall91a57552011-07-15 05:09:51 +00003520
David Blaikie4e4d0842012-03-11 07:00:24 +00003521 if (S.getLangOpts().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003522 Converted = TemplateArgument(ArgIn);
3523 return false;
3524 }
3525
3526 while (SubstNonTypeTemplateParmExpr *subst =
3527 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3528 Arg = subst->getReplacement()->IgnoreImpCasts();
3529
3530 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003531 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003532 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3533 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003534 S.Diag(Param->getLocation(), diag::note_template_param_here);
3535 return true;
3536 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003537
3538 // Stop checking the precise nature of the argument if it is value dependent,
3539 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003540 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003541 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003542 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003543 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003544
Douglas Gregorb7a09262010-04-01 18:32:35 +00003545 if (!isa<ValueDecl>(DRE->getDecl())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003546 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003547 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003548 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003549 S.Diag(Param->getLocation(), diag::note_template_param_here);
3550 return true;
3551 }
3552
3553 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003554
3555 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003556 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003557 S.Diag(Arg->getLocStart(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003558 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003559 S.Diag(Param->getLocation(), diag::note_template_param_here);
3560 return true;
3561 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003562
3563 // Cannot refer to non-static member functions
3564 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003565 if (!Method->isStatic()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003566 S.Diag(Arg->getLocStart(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003567 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003568 S.Diag(Param->getLocation(), diag::note_template_param_here);
3569 return true;
3570 }
Mike Stump1eb44332009-09-09 15:08:12 +00003571
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003572 // Functions must have external linkage.
3573 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003574 if (!isExternalLinkage(Func->getLinkage())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003575 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003576 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003577 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003578 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003579 << true;
3580 return true;
3581 }
3582
3583 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003584 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003585
Douglas Gregorb7a09262010-04-01 18:32:35 +00003586 // If the template parameter has pointer type, the function decays.
3587 if (ParamType->isPointerType() && !AddressTaken)
3588 ArgType = S.Context.getPointerType(Func->getType());
3589 else if (AddressTaken && ParamType->isReferenceType()) {
3590 // If we originally had an address-of operator, but the
3591 // parameter has reference type, complain and (if things look
3592 // like they will work) drop the address-of operator.
3593 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3594 ParamType.getNonReferenceType())) {
3595 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3596 << ParamType;
3597 S.Diag(Param->getLocation(), diag::note_template_param_here);
3598 return true;
3599 }
3600
3601 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3602 << ParamType
3603 << FixItHint::CreateRemoval(AddrOpLoc);
3604 S.Diag(Param->getLocation(), diag::note_template_param_here);
3605
3606 ArgType = Func->getType();
3607 }
3608 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003609 if (!isExternalLinkage(Var->getLinkage())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003610 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003611 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003612 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003613 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003614 << true;
3615 return true;
3616 }
3617
Douglas Gregorb7a09262010-04-01 18:32:35 +00003618 // A value of reference type is not an object.
3619 if (Var->getType()->isReferenceType()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003620 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003621 diag::err_template_arg_reference_var)
3622 << Var->getType() << Arg->getSourceRange();
3623 S.Diag(Param->getLocation(), diag::note_template_param_here);
3624 return true;
3625 }
3626
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003627 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003628 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003629
3630 // If the template parameter has pointer type, we must have taken
3631 // the address of this object.
3632 if (ParamType->isReferenceType()) {
3633 if (AddressTaken) {
3634 // If we originally had an address-of operator, but the
3635 // parameter has reference type, complain and (if things look
3636 // like they will work) drop the address-of operator.
3637 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3638 ParamType.getNonReferenceType())) {
3639 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3640 << ParamType;
3641 S.Diag(Param->getLocation(), diag::note_template_param_here);
3642 return true;
3643 }
3644
3645 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3646 << ParamType
3647 << FixItHint::CreateRemoval(AddrOpLoc);
3648 S.Diag(Param->getLocation(), diag::note_template_param_here);
3649
3650 ArgType = Var->getType();
3651 }
3652 } else if (!AddressTaken && ParamType->isPointerType()) {
3653 if (Var->getType()->isArrayType()) {
3654 // Array-to-pointer decay.
3655 ArgType = S.Context.getArrayDecayedType(Var->getType());
3656 } else {
3657 // If the template parameter has pointer type but the address of
3658 // this object was not taken, complain and (possibly) recover by
3659 // taking the address of the entity.
3660 ArgType = S.Context.getPointerType(Var->getType());
3661 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3662 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3663 << ParamType;
3664 S.Diag(Param->getLocation(), diag::note_template_param_here);
3665 return true;
3666 }
3667
3668 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3669 << ParamType
3670 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3671
3672 S.Diag(Param->getLocation(), diag::note_template_param_here);
3673 }
3674 }
3675 } else {
3676 // We found something else, but we don't know specifically what it is.
Daniel Dunbar96a00142012-03-09 18:35:03 +00003677 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003678 diag::err_template_arg_not_object_or_func)
3679 << Arg->getSourceRange();
3680 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3681 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003682 }
Mike Stump1eb44332009-09-09 15:08:12 +00003683
John McCallf85e1932011-06-15 23:02:42 +00003684 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003685 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003686 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003687 S.IsQualificationConversion(ArgType, ParamType, false,
3688 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003689 // For pointer-to-object types, qualification conversions are
3690 // permitted.
3691 } else {
3692 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3693 if (!ParamRef->getPointeeType()->isFunctionType()) {
3694 // C++ [temp.arg.nontype]p5b3:
3695 // For a non-type template-parameter of type reference to
3696 // object, no conversions apply. The type referred to by the
3697 // reference may be more cv-qualified than the (otherwise
3698 // identical) type of the template- argument. The
3699 // template-parameter is bound directly to the
3700 // template-argument, which shall be an lvalue.
3701
3702 // FIXME: Other qualifiers?
3703 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3704 unsigned ArgQuals = ArgType.getCVRQualifiers();
3705
3706 if ((ParamQuals | ArgQuals) != ParamQuals) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003707 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003708 diag::err_template_arg_ref_bind_ignores_quals)
3709 << ParamType << Arg->getType()
3710 << Arg->getSourceRange();
3711 S.Diag(Param->getLocation(), diag::note_template_param_here);
3712 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003713 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003714 }
3715 }
3716
3717 // At this point, the template argument refers to an object or
3718 // function with external linkage. We now need to check whether the
3719 // argument and parameter types are compatible.
3720 if (!S.Context.hasSameUnqualifiedType(ArgType,
3721 ParamType.getNonReferenceType())) {
3722 // We can't perform this conversion or binding.
3723 if (ParamType->isReferenceType())
3724 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003725 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003726 else
3727 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003728 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003729 S.Diag(Param->getLocation(), diag::note_template_param_here);
3730 return true;
3731 }
3732 }
3733
3734 // Create the template argument.
3735 Converted = TemplateArgument(Entity->getCanonicalDecl());
Eli Friedman5f2987c2012-02-02 03:46:19 +00003736 S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003737 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003738}
3739
3740/// \brief Checks whether the given template argument is a pointer to
3741/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003742bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003743 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003744 bool Invalid = false;
3745
3746 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003747 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003748 Arg = Cast->getSubExpr();
3749
3750 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003751 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003752 // A template-argument for a non-type, non-template
3753 // template-parameter shall be one of: [...]
3754 //
3755 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003756 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003757
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003758 // In C++98/03 mode, give an extension warning on any extra parentheses.
3759 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3760 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003761 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003762 if (!Invalid && !ExtraParens) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003763 Diag(Arg->getLocStart(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003764 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003765 diag::warn_cxx98_compat_template_arg_extra_parens :
3766 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003767 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003768 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003769 }
3770
3771 Arg = Parens->getSubExpr();
3772 }
3773
John McCall91a57552011-07-15 05:09:51 +00003774 while (SubstNonTypeTemplateParmExpr *subst =
3775 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3776 Arg = subst->getReplacement()->IgnoreImpCasts();
3777
Douglas Gregorcaddba02009-11-12 18:38:13 +00003778 // A pointer-to-member constant written &Class::member.
3779 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003780 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003781 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3782 if (DRE && !DRE->getQualifier())
3783 DRE = 0;
3784 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003785 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003786 // A constant of pointer-to-member type.
3787 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3788 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3789 if (VD->getType()->isMemberPointerType()) {
3790 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003791 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003792 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3793 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003794 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003795 else
3796 Converted = TemplateArgument(VD->getCanonicalDecl());
3797 return Invalid;
3798 }
3799 }
3800 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003801
Douglas Gregorcaddba02009-11-12 18:38:13 +00003802 DRE = 0;
3803 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003804
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003805 if (!DRE)
Daniel Dunbar96a00142012-03-09 18:35:03 +00003806 return Diag(Arg->getLocStart(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003807 diag::err_template_arg_not_pointer_to_member_form)
3808 << Arg->getSourceRange();
3809
3810 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3811 assert((isa<FieldDecl>(DRE->getDecl()) ||
3812 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3813 "Only non-static member pointers can make it here");
3814
3815 // Okay: this is the address of a non-static member, and therefore
3816 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003817 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003818 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003819 else
3820 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003821 return Invalid;
3822 }
3823
3824 // We found something else, but we don't know specifically what it is.
Daniel Dunbar96a00142012-03-09 18:35:03 +00003825 Diag(Arg->getLocStart(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003826 diag::err_template_arg_not_pointer_to_member_form)
3827 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003828 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003829 diag::note_template_arg_refers_here);
3830 return true;
3831}
3832
Douglas Gregorc15cb382009-02-09 23:23:08 +00003833/// \brief Check a template argument against its corresponding
3834/// non-type template parameter.
3835///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003836/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003837/// If an error occurred, it returns ExprError(); otherwise, it
3838/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003839/// InstantiatedParamType is the type of the non-type template
3840/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003841ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3842 QualType InstantiatedParamType, Expr *Arg,
3843 TemplateArgument &Converted,
3844 CheckTemplateArgumentKind CTAK) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003845 SourceLocation StartLoc = Arg->getLocStart();
Douglas Gregor40808ce2009-03-09 23:48:35 +00003846
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003847 // If either the parameter has a dependent type or the argument is
3848 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003849 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3850 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003851 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003852 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003853 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003854
3855 // C++ [temp.arg.nontype]p5:
3856 // The following conversions are performed on each expression used
3857 // as a non-type template-argument. If a non-type
3858 // template-argument cannot be converted to the type of the
3859 // corresponding template-parameter then the program is
3860 // ill-formed.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003861 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003862 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003863 // C++11:
3864 // -- for a non-type template-parameter of integral or
3865 // enumeration type, conversions permitted in a converted
3866 // constant expression are applied.
3867 //
3868 // C++98:
3869 // -- for a non-type template-parameter of integral or
3870 // enumeration type, integral promotions (4.5) and integral
3871 // conversions (4.7) are applied.
3872
3873 if (CTAK == CTAK_Deduced &&
3874 !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) {
3875 // C++ [temp.deduct.type]p17:
3876 // If, in the declaration of a function template with a non-type
3877 // template-parameter, the non-type template-parameter is used
3878 // in an expression in the function parameter-list and, if the
3879 // corresponding template-argument is deduced, the
3880 // template-argument type shall match the type of the
3881 // template-parameter exactly, except that a template-argument
3882 // deduced from an array bound may be of any integral type.
3883 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3884 << Arg->getType().getUnqualifiedType()
3885 << ParamType.getUnqualifiedType();
3886 Diag(Param->getLocation(), diag::note_template_param_here);
3887 return ExprError();
3888 }
3889
David Blaikie4e4d0842012-03-11 07:00:24 +00003890 if (getLangOpts().CPlusPlus0x) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003891 // We can't check arbitrary value-dependent arguments.
3892 // FIXME: If there's no viable conversion to the template parameter type,
3893 // we should be able to diagnose that prior to instantiation.
3894 if (Arg->isValueDependent()) {
3895 Converted = TemplateArgument(Arg);
3896 return Owned(Arg);
3897 }
3898
3899 // C++ [temp.arg.nontype]p1:
3900 // A template-argument for a non-type, non-template template-parameter
3901 // shall be one of:
3902 //
3903 // -- for a non-type template-parameter of integral or enumeration
3904 // type, a converted constant expression of the type of the
3905 // template-parameter; or
3906 llvm::APSInt Value;
3907 ExprResult ArgResult =
3908 CheckConvertedConstantExpression(Arg, ParamType, Value,
3909 CCEK_TemplateArg);
3910 if (ArgResult.isInvalid())
3911 return ExprError();
3912
3913 // Widen the argument value to sizeof(parameter type). This is almost
3914 // always a no-op, except when the parameter type is bool. In
3915 // that case, this may extend the argument from 1 bit to 8 bits.
3916 QualType IntegerType = ParamType;
3917 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
3918 IntegerType = Enum->getDecl()->getIntegerType();
3919 Value = Value.extOrTrunc(Context.getTypeSize(IntegerType));
3920
3921 Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType));
3922 return ArgResult;
3923 }
3924
Richard Smith4f870622011-10-27 22:11:44 +00003925 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3926 if (ArgResult.isInvalid())
3927 return ExprError();
3928 Arg = ArgResult.take();
3929
3930 QualType ArgType = Arg->getType();
3931
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003932 // C++ [temp.arg.nontype]p1:
3933 // A template-argument for a non-type, non-template
3934 // template-parameter shall be one of:
3935 //
3936 // -- an integral constant-expression of integral or enumeration
3937 // type; or
3938 // -- the name of a non-type template-parameter; or
3939 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003940 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003941 if (!ArgType->isIntegralOrEnumerationType()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003942 Diag(Arg->getLocStart(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003943 diag::err_template_arg_not_integral_or_enumeral)
3944 << ArgType << Arg->getSourceRange();
3945 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003946 return ExprError();
Richard Smith282e7e62012-02-04 09:53:13 +00003947 } else if (!Arg->isValueDependent()) {
3948 Arg = VerifyIntegerConstantExpression(Arg, &Value,
3949 PDiag(diag::err_template_arg_not_ice) << ArgType, false).take();
3950 if (!Arg)
3951 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003952 }
3953
Douglas Gregor02024a92010-03-28 02:42:43 +00003954 // From here on out, all we care about are the unqualified forms
3955 // of the parameter and argument types.
3956 ParamType = ParamType.getUnqualifiedType();
3957 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003958
3959 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003960 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003961 // Okay: no conversion necessary
John McCalldaa8e4e2010-11-15 09:13:47 +00003962 } else if (ParamType->isBooleanType()) {
3963 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003964 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003965 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3966 !ParamType->isEnumeralType()) {
3967 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003968 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003969 } else {
3970 // We can't perform this conversion.
Daniel Dunbar96a00142012-03-09 18:35:03 +00003971 Diag(Arg->getLocStart(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003972 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003973 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003974 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003975 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003976 }
3977
Douglas Gregorc7469372011-05-04 21:55:00 +00003978 // Add the value of this argument to the list of converted
3979 // arguments. We use the bitwidth and signedness of the template
3980 // parameter.
3981 if (Arg->isValueDependent()) {
3982 // The argument is value-dependent. Create a new
3983 // TemplateArgument with the converted expression.
3984 Converted = TemplateArgument(Arg);
3985 return Owned(Arg);
3986 }
3987
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003988 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003989 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003990 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003991
Douglas Gregorc7469372011-05-04 21:55:00 +00003992 if (ParamType->isBooleanType()) {
3993 // Value must be zero or one.
3994 Value = Value != 0;
3995 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3996 if (Value.getBitWidth() != AllowedBits)
3997 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003998 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003999 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004000 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00004001
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004002 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004003 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00004004 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00004005 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00004006 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00004007 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00004008
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004009 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00004010 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00004011 && (OldValue.isSigned() && OldValue.isNegative())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004012 Diag(Arg->getLocStart(), diag::warn_template_arg_negative)
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004013 << OldValue.toString(10) << Value.toString(10) << Param->getType()
4014 << Arg->getSourceRange();
4015 Diag(Param->getLocation(), diag::note_template_param_here);
4016 }
Douglas Gregorc7469372011-05-04 21:55:00 +00004017
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004018 // Complain if we overflowed the template parameter's type.
4019 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00004020 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004021 RequiredBits = OldValue.getActiveBits();
4022 else if (OldValue.isUnsigned())
4023 RequiredBits = OldValue.getActiveBits() + 1;
4024 else
4025 RequiredBits = OldValue.getMinSignedBits();
4026 if (RequiredBits > AllowedBits) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004027 Diag(Arg->getLocStart(),
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004028 diag::warn_template_arg_too_large)
4029 << OldValue.toString(10) << Value.toString(10) << Param->getType()
4030 << Arg->getSourceRange();
4031 Diag(Param->getLocation(), diag::note_template_param_here);
4032 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00004033 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00004034
John McCall833ca992009-10-29 08:12:44 +00004035 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00004036 ParamType->isEnumeralType()
4037 ? Context.getCanonicalType(ParamType)
4038 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00004039 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00004040 }
Douglas Gregora35284b2009-02-11 00:19:33 +00004041
Richard Smith4f870622011-10-27 22:11:44 +00004042 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00004043 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
4044
Douglas Gregorb7a09262010-04-01 18:32:35 +00004045 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
4046 // from a template argument of type std::nullptr_t to a non-type
4047 // template parameter of type pointer to object, pointer to
4048 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004049 if (ArgType->isNullPtrType()) {
4050 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
4051 Converted = TemplateArgument((NamedDecl *)0);
4052 return Owned(Arg);
4053 }
4054
4055 if (ParamType->isNullPtrType()) {
4056 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
4057 Converted = TemplateArgument(Zero, Context.NullPtrTy);
4058 return Owned(Arg);
4059 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004060 }
4061
Douglas Gregorb86b0572009-02-11 01:18:59 +00004062 // Handle pointer-to-function, reference-to-function, and
4063 // pointer-to-member-function all in (roughly) the same way.
4064 if (// -- For a non-type template-parameter of type pointer to
4065 // function, only the function-to-pointer conversion (4.3) is
4066 // applied. If the template-argument represents a set of
4067 // overloaded functions (or a pointer to such), the matching
4068 // function is selected from the set (13.4).
4069 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004070 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004071 // -- For a non-type template-parameter of type reference to
4072 // function, no conversions apply. If the template-argument
4073 // represents a set of overloaded functions, the matching
4074 // function is selected from the set (13.4).
4075 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004076 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004077 // -- For a non-type template-parameter of type pointer to
4078 // member function, no conversions apply. If the
4079 // template-argument represents a set of overloaded member
4080 // functions, the matching member function is selected from
4081 // the set (13.4).
4082 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004083 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00004084 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004085
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004086 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004087 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004088 true,
4089 FoundResult)) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004090 if (DiagnoseUseOfDecl(Fn, Arg->getLocStart()))
John Wiegley429bb272011-04-08 18:41:53 +00004091 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004092
4093 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4094 ArgType = Arg->getType();
4095 } else
John Wiegley429bb272011-04-08 18:41:53 +00004096 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004097 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004098
John Wiegley429bb272011-04-08 18:41:53 +00004099 if (!ParamType->isMemberPointerType()) {
4100 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4101 ParamType,
4102 Arg, Converted))
4103 return ExprError();
4104 return Owned(Arg);
4105 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004106
John McCallf85e1932011-06-15 23:02:42 +00004107 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00004108 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00004109 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004110 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4111 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004112 } else if (!Context.hasSameUnqualifiedType(ArgType,
4113 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00004114 // We can't perform this conversion.
Daniel Dunbar96a00142012-03-09 18:35:03 +00004115 Diag(Arg->getLocStart(),
Douglas Gregora35284b2009-02-11 00:19:33 +00004116 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004117 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00004118 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004119 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004120 }
Mike Stump1eb44332009-09-09 15:08:12 +00004121
John Wiegley429bb272011-04-08 18:41:53 +00004122 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4123 return ExprError();
4124 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00004125 }
4126
Chris Lattnerfe90de72009-02-20 21:37:53 +00004127 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004128 // -- for a non-type template-parameter of type pointer to
4129 // object, qualification conversions (4.4) and the
4130 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00004131 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00004132 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004133 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004134
John Wiegley429bb272011-04-08 18:41:53 +00004135 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4136 ParamType,
4137 Arg, Converted))
4138 return ExprError();
4139 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004140 }
Mike Stump1eb44332009-09-09 15:08:12 +00004141
Ted Kremenek6217b802009-07-29 21:53:49 +00004142 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004143 // -- For a non-type template-parameter of type reference to
4144 // object, no conversions apply. The type referred to by the
4145 // reference may be more cv-qualified than the (otherwise
4146 // identical) type of the template-argument. The
4147 // template-parameter is bound directly to the
4148 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00004149 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004150 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004151
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004152 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004153 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
4154 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004155 true,
4156 FoundResult)) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004157 if (DiagnoseUseOfDecl(Fn, Arg->getLocStart()))
John Wiegley429bb272011-04-08 18:41:53 +00004158 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004159
4160 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4161 ArgType = Arg->getType();
4162 } else
John Wiegley429bb272011-04-08 18:41:53 +00004163 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004164 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004165
John Wiegley429bb272011-04-08 18:41:53 +00004166 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4167 ParamType,
4168 Arg, Converted))
4169 return ExprError();
4170 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004171 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004172
4173 // -- For a non-type template-parameter of type pointer to data
4174 // member, qualification conversions (4.4) are applied.
4175 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4176
John McCallf85e1932011-06-15 23:02:42 +00004177 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004178 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004179 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004180 } else if (IsQualificationConversion(ArgType, ParamType, false,
4181 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004182 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4183 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004184 } else {
4185 // We can't perform this conversion.
Daniel Dunbar96a00142012-03-09 18:35:03 +00004186 Diag(Arg->getLocStart(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004187 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004188 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004189 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004190 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004191 }
4192
John Wiegley429bb272011-04-08 18:41:53 +00004193 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4194 return ExprError();
4195 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004196}
4197
4198/// \brief Check a template argument against its corresponding
4199/// template template parameter.
4200///
4201/// This routine implements the semantics of C++ [temp.arg.template].
4202/// It returns true if an error occurred, and false otherwise.
4203bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004204 const TemplateArgumentLoc &Arg) {
4205 TemplateName Name = Arg.getArgument().getAsTemplate();
4206 TemplateDecl *Template = Name.getAsTemplateDecl();
4207 if (!Template) {
4208 // Any dependent template name is fine.
4209 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4210 return false;
4211 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004212
Richard Smith3e4c6c42011-05-05 21:57:07 +00004213 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004214 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004215 // the name of a class template or an alias template, expressed as an
4216 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004217 // primary class templates are considered when matching the
4218 // template template argument with the corresponding parameter;
4219 // partial specializations are not considered even if their
4220 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004221 //
4222 // Note that we also allow template template parameters here, which
4223 // will happen when we are dealing with, e.g., class template
4224 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004225 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004226 !isa<TemplateTemplateParmDecl>(Template) &&
4227 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004228 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004229 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004230 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004231 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004232 << Template;
4233 }
4234
4235 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4236 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004237 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004238 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004239 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004240}
4241
Douglas Gregor02024a92010-03-28 02:42:43 +00004242/// \brief Given a non-type template argument that refers to a
4243/// declaration and the type of its corresponding non-type template
4244/// parameter, produce an expression that properly refers to that
4245/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004246ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004247Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4248 QualType ParamType,
4249 SourceLocation Loc) {
4250 assert(Arg.getKind() == TemplateArgument::Declaration &&
4251 "Only declaration template arguments permitted here");
4252 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4253
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004254 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004255 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4256 // If the value is a class member, we might have a pointer-to-member.
4257 // Determine whether the non-type template template parameter is of
4258 // pointer-to-member type. If so, we need to build an appropriate
4259 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4260 // would refer to the member itself.
4261 if (ParamType->isMemberPointerType()) {
4262 QualType ClassType
4263 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4264 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004265 = NestedNameSpecifier::Create(Context, 0, false,
4266 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004267 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004268 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004269
4270 // The actual value-ness of this is unimportant, but for
4271 // internal consistency's sake, references to instance methods
4272 // are r-values.
4273 ExprValueKind VK = VK_LValue;
4274 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4275 VK = VK_RValue;
4276
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004277 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004278 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004279 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004280 Loc,
4281 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004282 if (RefExpr.isInvalid())
4283 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004284
John McCall2de56d12010-08-25 11:45:40 +00004285 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004286
Douglas Gregorc0c83002010-04-30 21:46:38 +00004287 // We might need to perform a trailing qualification conversion, since
4288 // the element type on the parameter could be more qualified than the
4289 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004290 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004291 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004292 ParamType.getUnqualifiedType(), false,
4293 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004294 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004295
Douglas Gregor02024a92010-03-28 02:42:43 +00004296 assert(!RefExpr.isInvalid() &&
4297 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004298 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004299 return move(RefExpr);
4300 }
4301 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004302
Douglas Gregor02024a92010-03-28 02:42:43 +00004303 QualType T = VD->getType().getNonReferenceType();
4304 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004305 // When the non-type template parameter is a pointer, take the
4306 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004307 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004308 if (RefExpr.isInvalid())
4309 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004310
4311 if (T->isFunctionType() || T->isArrayType()) {
4312 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004313 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4314 if (RefExpr.isInvalid())
4315 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004316
4317 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004318 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004319
Douglas Gregorb7a09262010-04-01 18:32:35 +00004320 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004321 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004322 }
4323
John McCallf89e55a2010-11-18 06:31:45 +00004324 ExprValueKind VK = VK_RValue;
4325
Douglas Gregor02024a92010-03-28 02:42:43 +00004326 // If the non-type template parameter has reference type, qualify the
4327 // resulting declaration reference with the extra qualifiers on the
4328 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004329 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4330 VK = VK_LValue;
4331 T = Context.getQualifiedType(T,
4332 TargetRef->getPointeeType().getQualifiers());
4333 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004334
John McCallf89e55a2010-11-18 06:31:45 +00004335 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004336}
4337
4338/// \brief Construct a new expression that refers to the given
4339/// integral template argument with the given source-location
4340/// information.
4341///
4342/// This routine takes care of the mapping from an integral template
4343/// argument (which may have any integral type) to the appropriate
4344/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004345ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004346Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4347 SourceLocation Loc) {
4348 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004349 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004350 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004351 if (T->isAnyCharacterType()) {
4352 CharacterLiteral::CharacterKind Kind;
4353 if (T->isWideCharType())
4354 Kind = CharacterLiteral::Wide;
4355 else if (T->isChar16Type())
4356 Kind = CharacterLiteral::UTF16;
4357 else if (T->isChar32Type())
4358 Kind = CharacterLiteral::UTF32;
4359 else
4360 Kind = CharacterLiteral::Ascii;
4361
Douglas Gregor02024a92010-03-28 02:42:43 +00004362 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004363 Arg.getAsIntegral()->getZExtValue(),
4364 Kind, T, Loc));
4365 }
4366
Douglas Gregor02024a92010-03-28 02:42:43 +00004367 if (T->isBooleanType())
4368 return Owned(new (Context) CXXBoolLiteralExpr(
4369 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004370 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004371
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004372 if (T->isNullPtrType())
4373 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4374
Chris Lattner223de242011-04-25 20:37:58 +00004375 // If this is an enum type that we're instantiating, we need to use an integer
4376 // type the same size as the enumerator. We don't want to build an
4377 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004378 QualType BT;
4379 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004380 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004381 else
4382 BT = T;
4383
John McCall4e9272d2011-07-15 07:47:58 +00004384 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4385 if (T->isEnumeralType()) {
4386 // FIXME: This is a hack. We need a better way to handle substituted
4387 // non-type template parameters.
4388 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4389 Context.getTrivialTypeSourceInfo(T, Loc),
4390 Loc, Loc);
4391 }
4392
4393 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004394}
4395
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004396/// \brief Match two template parameters within template parameter lists.
4397static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4398 bool Complain,
4399 Sema::TemplateParameterListEqualKind Kind,
4400 SourceLocation TemplateArgLoc) {
4401 // Check the actual kind (type, non-type, template).
4402 if (Old->getKind() != New->getKind()) {
4403 if (Complain) {
4404 unsigned NextDiag = diag::err_template_param_different_kind;
4405 if (TemplateArgLoc.isValid()) {
4406 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4407 NextDiag = diag::note_template_param_different_kind;
4408 }
4409 S.Diag(New->getLocation(), NextDiag)
4410 << (Kind != Sema::TPL_TemplateMatch);
4411 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4412 << (Kind != Sema::TPL_TemplateMatch);
4413 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004414
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004415 return false;
4416 }
4417
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004418 // Check that both are parameter packs are neither are parameter packs.
4419 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004420 // template template parameter, the template template parameter can have
4421 // a parameter pack where the template template argument does not.
4422 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4423 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4424 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004425 if (Complain) {
4426 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4427 if (TemplateArgLoc.isValid()) {
4428 S.Diag(TemplateArgLoc,
4429 diag::err_template_arg_template_params_mismatch);
4430 NextDiag = diag::note_template_parameter_pack_non_pack;
4431 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004432
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004433 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4434 : isa<NonTypeTemplateParmDecl>(New)? 1
4435 : 2;
4436 S.Diag(New->getLocation(), NextDiag)
4437 << ParamKind << New->isParameterPack();
4438 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4439 << ParamKind << Old->isParameterPack();
4440 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004441
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004442 return false;
4443 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004444
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004445 // For non-type template parameters, check the type of the parameter.
4446 if (NonTypeTemplateParmDecl *OldNTTP
4447 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4448 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004449
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004450 // If we are matching a template template argument to a template
4451 // template parameter and one of the non-type template parameter types
4452 // is dependent, then we must wait until template instantiation time
4453 // to actually compare the arguments.
4454 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4455 (OldNTTP->getType()->isDependentType() ||
4456 NewNTTP->getType()->isDependentType()))
4457 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004458
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004459 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4460 if (Complain) {
4461 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4462 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004463 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004464 diag::err_template_arg_template_params_mismatch);
4465 NextDiag = diag::note_template_nontype_parm_different_type;
4466 }
4467 S.Diag(NewNTTP->getLocation(), NextDiag)
4468 << NewNTTP->getType()
4469 << (Kind != Sema::TPL_TemplateMatch);
4470 S.Diag(OldNTTP->getLocation(),
4471 diag::note_template_nontype_parm_prev_declaration)
4472 << OldNTTP->getType();
4473 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004474
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004475 return false;
4476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004477
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004478 return true;
4479 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004480
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004481 // For template template parameters, check the template parameter types.
4482 // The template parameter lists of template template
4483 // parameters must agree.
4484 if (TemplateTemplateParmDecl *OldTTP
4485 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004486 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004487 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4488 OldTTP->getTemplateParameters(),
4489 Complain,
4490 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004491 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004492 : Kind),
4493 TemplateArgLoc);
4494 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004495
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004496 return true;
4497}
Douglas Gregor02024a92010-03-28 02:42:43 +00004498
Douglas Gregora0347822011-01-13 00:08:50 +00004499/// \brief Diagnose a known arity mismatch when comparing template argument
4500/// lists.
4501static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004502void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004503 TemplateParameterList *New,
4504 TemplateParameterList *Old,
4505 Sema::TemplateParameterListEqualKind Kind,
4506 SourceLocation TemplateArgLoc) {
4507 unsigned NextDiag = diag::err_template_param_list_different_arity;
4508 if (TemplateArgLoc.isValid()) {
4509 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4510 NextDiag = diag::note_template_param_list_different_arity;
4511 }
4512 S.Diag(New->getTemplateLoc(), NextDiag)
4513 << (New->size() > Old->size())
4514 << (Kind != Sema::TPL_TemplateMatch)
4515 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4516 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4517 << (Kind != Sema::TPL_TemplateMatch)
4518 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4519}
4520
Douglas Gregorddc29e12009-02-06 22:42:48 +00004521/// \brief Determine whether the given template parameter lists are
4522/// equivalent.
4523///
Mike Stump1eb44332009-09-09 15:08:12 +00004524/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004525/// source code as part of a new template declaration.
4526///
4527/// \param Old The old template parameter list, typically found via
4528/// name lookup of the template declared with this template parameter
4529/// list.
4530///
4531/// \param Complain If true, this routine will produce a diagnostic if
4532/// the template parameter lists are not equivalent.
4533///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004534/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004535///
4536/// \param TemplateArgLoc If this source location is valid, then we
4537/// are actually checking the template parameter list of a template
4538/// argument (New) against the template parameter list of its
4539/// corresponding template template parameter (Old). We produce
4540/// slightly different diagnostics in this scenario.
4541///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004542/// \returns True if the template parameter lists are equal, false
4543/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004544bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004545Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4546 TemplateParameterList *Old,
4547 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004548 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004549 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004550 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4551 if (Complain)
4552 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4553 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004554
4555 return false;
4556 }
4557
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004558 // C++0x [temp.arg.template]p3:
4559 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004560 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004561 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004562 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004563 // template-parameter-list of P. [...]
4564 TemplateParameterList::iterator NewParm = New->begin();
4565 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004566 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004567 OldParmEnd = Old->end();
4568 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004569 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4570 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004571 if (NewParm == NewParmEnd) {
4572 if (Complain)
4573 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4574 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004575
Douglas Gregora0347822011-01-13 00:08:50 +00004576 return false;
4577 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004578
Douglas Gregora0347822011-01-13 00:08:50 +00004579 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4580 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004581 return false;
4582
Douglas Gregora0347822011-01-13 00:08:50 +00004583 ++NewParm;
4584 continue;
4585 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004586
Douglas Gregora0347822011-01-13 00:08:50 +00004587 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004588 // [...] When P's template- parameter-list contains a template parameter
4589 // pack (14.5.3), the template parameter pack will match zero or more
4590 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004591 // template-parameter-list of A with the same type and form as the
4592 // template parameter pack in P (ignoring whether those template
4593 // parameters are template parameter packs).
4594 for (; NewParm != NewParmEnd; ++NewParm) {
4595 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4596 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004597 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004598 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004599 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004600
Douglas Gregora0347822011-01-13 00:08:50 +00004601 // Make sure we exhausted all of the arguments.
4602 if (NewParm != NewParmEnd) {
4603 if (Complain)
4604 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4605 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004606
Douglas Gregora0347822011-01-13 00:08:50 +00004607 return false;
4608 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004609
Douglas Gregorddc29e12009-02-06 22:42:48 +00004610 return true;
4611}
4612
4613/// \brief Check whether a template can be declared within this scope.
4614///
4615/// If the template declaration is valid in this scope, returns
4616/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004617bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004618Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004619 if (!S)
4620 return false;
4621
Douglas Gregorddc29e12009-02-06 22:42:48 +00004622 // Find the nearest enclosing declaration scope.
4623 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4624 (S->getFlags() & Scope::TemplateParamScope) != 0)
4625 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004626
Douglas Gregorddc29e12009-02-06 22:42:48 +00004627 // C++ [temp]p2:
4628 // A template-declaration can appear only as a namespace scope or
4629 // class scope declaration.
4630 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004631 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4632 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004633 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004634 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004635
Eli Friedman1503f772009-07-31 01:43:05 +00004636 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004637 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004638
4639 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4640 return false;
4641
Mike Stump1eb44332009-09-09 15:08:12 +00004642 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004643 diag::err_template_outside_namespace_or_class_scope)
4644 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004645}
Douglas Gregorcc636682009-02-17 23:15:12 +00004646
Douglas Gregord5cb8762009-10-07 00:13:32 +00004647/// \brief Determine what kind of template specialization the given declaration
4648/// is.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00004649static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004650 if (!D)
4651 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004652
Douglas Gregorf6b11852009-10-08 15:14:33 +00004653 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4654 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004655 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4656 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004657 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4658 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004659
Douglas Gregord5cb8762009-10-07 00:13:32 +00004660 return TSK_Undeclared;
4661}
4662
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004663/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004664/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004665///
Douglas Gregor9302da62009-10-14 23:50:59 +00004666/// This routine determines whether a template specialization can be declared
4667/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004668///
4669/// \param S the semantic analysis object for which this check is being
4670/// performed.
4671///
4672/// \param Specialized the entity being specialized or instantiated, which
4673/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004674/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004675/// member class).
4676///
4677/// \param PrevDecl the previous declaration of this entity, if any.
4678///
4679/// \param Loc the location of the explicit specialization or instantiation of
4680/// this entity.
4681///
4682/// \param IsPartialSpecialization whether this is a partial specialization of
4683/// a class template.
4684///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004685/// \returns true if there was an error that we cannot recover from, false
4686/// otherwise.
4687static bool CheckTemplateSpecializationScope(Sema &S,
4688 NamedDecl *Specialized,
4689 NamedDecl *PrevDecl,
4690 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004691 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004692 // Keep these "kind" numbers in sync with the %select statements in the
4693 // various diagnostics emitted by this routine.
4694 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004695 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004696 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004697 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004698 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004699 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004700 EntityKind = 3;
4701 else if (isa<VarDecl>(Specialized))
4702 EntityKind = 4;
4703 else if (isa<RecordDecl>(Specialized))
4704 EntityKind = 5;
Richard Smith1af83c42012-03-23 03:33:32 +00004705 else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus0x)
4706 EntityKind = 6;
Douglas Gregord5cb8762009-10-07 00:13:32 +00004707 else {
Richard Smith1af83c42012-03-23 03:33:32 +00004708 S.Diag(Loc, diag::err_template_spec_unknown_kind)
4709 << S.getLangOpts().CPlusPlus0x;
Douglas Gregor9302da62009-10-14 23:50:59 +00004710 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004711 return true;
4712 }
4713
Douglas Gregor88b70942009-02-25 22:02:03 +00004714 // C++ [temp.expl.spec]p2:
4715 // An explicit specialization shall be declared in the namespace
4716 // of which the template is a member, or, for member templates, in
4717 // the namespace of which the enclosing class or enclosing class
4718 // template is a member. An explicit specialization of a member
4719 // function, member class or static data member of a class
4720 // template shall be declared in the namespace of which the class
4721 // template is a member. Such a declaration may also be a
4722 // definition. If the declaration is not a definition, the
4723 // specialization may be defined later in the name- space in which
4724 // the explicit specialization was declared, or in a namespace
4725 // that encloses the one in which the explicit specialization was
4726 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004727 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004728 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004729 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004730 return true;
4731 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004732
Douglas Gregor0a407472009-10-07 17:30:37 +00004733 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004734 if (S.getLangOpts().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004735 // Do not warn for class scope explicit specialization during
4736 // instantiation, warning was already emitted during pattern
4737 // semantic analysis.
4738 if (!S.ActiveTemplateInstantiations.size())
4739 S.Diag(Loc, diag::ext_function_specialization_in_class)
4740 << Specialized;
4741 } else {
4742 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4743 << Specialized;
4744 return true;
4745 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004746 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004747
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004748 if (S.CurContext->isRecord() &&
4749 !S.CurContext->Equals(Specialized->getDeclContext())) {
4750 // Make sure that we're specializing in the right record context.
4751 // Otherwise, things can go horribly wrong.
4752 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4753 << Specialized;
4754 return true;
4755 }
4756
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004757 // C++ [temp.class.spec]p6:
4758 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004759 // in any namespace scope in which its definition may be defined (14.5.1
4760 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004761 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004762 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004763 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004764 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004765 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004766 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4767 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004768 // C++ [temp.exp.spec]p2:
4769 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004770 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004771 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004772 // An explicit specialization of a member function, member class or
4773 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004774 // namespace of which the class template is a member.
4775 //
4776 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004777 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004778 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004779 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4780 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4781 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4782 assert(!IsCPlusPlus0xExtension &&
4783 "DC encloses TU but isn't in enclosing namespace set");
4784 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004785 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004786 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4787 int Diag;
4788 if (!IsCPlusPlus0xExtension)
4789 Diag = diag::err_template_spec_decl_out_of_scope;
David Blaikie4e4d0842012-03-11 07:00:24 +00004790 else if (!S.getLangOpts().CPlusPlus0x)
Richard Smithebaf0e62011-10-18 20:49:44 +00004791 Diag = diag::ext_template_spec_decl_out_of_scope;
4792 else
4793 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4794 S.Diag(Loc, Diag)
4795 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4796 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004797
Douglas Gregor9302da62009-10-14 23:50:59 +00004798 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004799 ComplainedAboutScope =
David Blaikie4e4d0842012-03-11 07:00:24 +00004800 !(IsCPlusPlus0xExtension && S.getLangOpts().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004801 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004802 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004803
4804 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004805 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004806 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004807 // specializations of function templates, static data members, and member
4808 // functions, so we skip the check here for those kinds of entities.
4809 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004810 // Should we refactor that check, so that it occurs later?
4811 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004812 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4813 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004814 if (isa<TranslationUnitDecl>(SpecializedContext))
4815 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4816 << EntityKind << Specialized;
4817 else if (isa<NamespaceDecl>(SpecializedContext))
4818 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4819 << EntityKind << Specialized
4820 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004821
Douglas Gregor9302da62009-10-14 23:50:59 +00004822 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004823 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004824
Douglas Gregord5cb8762009-10-07 00:13:32 +00004825 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004826
Douglas Gregor88b70942009-02-25 22:02:03 +00004827 return false;
4828}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004829
Douglas Gregorbacb9492011-01-03 21:13:47 +00004830/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4831/// that checks non-type template partial specialization arguments.
4832static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4833 NonTypeTemplateParmDecl *Param,
4834 const TemplateArgument *Args,
4835 unsigned NumArgs) {
4836 for (unsigned I = 0; I != NumArgs; ++I) {
4837 if (Args[I].getKind() == TemplateArgument::Pack) {
4838 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004839 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004840 Args[I].pack_size()))
4841 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004842
Douglas Gregore94866f2009-06-12 21:21:02 +00004843 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004844 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004845
Douglas Gregorbacb9492011-01-03 21:13:47 +00004846 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004847 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004848 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004849 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004850
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004851 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004852 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4853 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004854
4855 // Strip off any implicit casts we added as part of type checking.
4856 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4857 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004858
Douglas Gregore94866f2009-06-12 21:21:02 +00004859 // C++ [temp.class.spec]p8:
4860 // A non-type argument is non-specialized if it is the name of a
4861 // non-type parameter. All other non-type arguments are
4862 // specialized.
4863 //
4864 // Below, we check the two conditions that only apply to
4865 // specialized non-type arguments, so skip any non-specialized
4866 // arguments.
4867 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004868 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004869 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004870
Douglas Gregore94866f2009-06-12 21:21:02 +00004871 // C++ [temp.class.spec]p9:
4872 // Within the argument list of a class template partial
4873 // specialization, the following restrictions apply:
4874 // -- A partially specialized non-type argument expression
4875 // shall not involve a template parameter of the partial
4876 // specialization except when the argument expression is a
4877 // simple identifier.
4878 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004879 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004880 diag::err_dependent_non_type_arg_in_partial_spec)
4881 << ArgExpr->getSourceRange();
4882 return true;
4883 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004884
Douglas Gregore94866f2009-06-12 21:21:02 +00004885 // -- The type of a template parameter corresponding to a
4886 // specialized non-type argument shall not be dependent on a
4887 // parameter of the specialization.
4888 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004889 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004890 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4891 << Param->getType()
4892 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004893 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004894 return true;
4895 }
4896 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004897
Douglas Gregorbacb9492011-01-03 21:13:47 +00004898 return false;
4899}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004900
Douglas Gregorbacb9492011-01-03 21:13:47 +00004901/// \brief Check the non-type template arguments of a class template
4902/// partial specialization according to C++ [temp.class.spec]p9.
4903///
4904/// \param TemplateParams the template parameters of the primary class
4905/// template.
4906///
4907/// \param TemplateArg the template arguments of the class template
4908/// partial specialization.
4909///
4910/// \returns true if there was an error, false otherwise.
4911static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4912 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004913 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004914 const TemplateArgument *ArgList = TemplateArgs.data();
4915
4916 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4917 NonTypeTemplateParmDecl *Param
4918 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4919 if (!Param)
4920 continue;
4921
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004922 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004923 &ArgList[I], 1))
4924 return true;
4925 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004926
4927 return false;
4928}
4929
John McCalld226f652010-08-21 09:40:31 +00004930DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004931Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4932 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004933 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004934 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004935 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004936 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004937 SourceLocation TemplateNameLoc,
4938 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004939 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004940 SourceLocation RAngleLoc,
4941 AttributeList *Attr,
4942 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004943 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004944
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004945 // NOTE: KWLoc is the location of the tag keyword. This will instead
4946 // store the location of the outermost template keyword in the declaration.
4947 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4948 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4949
Douglas Gregorcc636682009-02-17 23:15:12 +00004950 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004951 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004952 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004953 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4954
4955 if (!ClassTemplate) {
4956 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004957 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004958 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4959 return true;
4960 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004961
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004962 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004963 bool isPartialSpecialization = false;
4964
Douglas Gregor88b70942009-02-25 22:02:03 +00004965 // Check the validity of the template headers that introduce this
4966 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004967 // FIXME: We probably shouldn't complain about these headers for
4968 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004969 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004970 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004971 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4972 TemplateNameLoc,
4973 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004974 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004975 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004976 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004977 isExplicitSpecialization,
4978 Invalid);
4979 if (Invalid)
4980 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004981
Douglas Gregor05396e22009-08-25 17:23:04 +00004982 if (TemplateParams && TemplateParams->size() > 0) {
4983 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004984
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004985 if (TUK == TUK_Friend) {
4986 Diag(KWLoc, diag::err_partial_specialization_friend)
4987 << SourceRange(LAngleLoc, RAngleLoc);
4988 return true;
4989 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004990
Douglas Gregor05396e22009-08-25 17:23:04 +00004991 // C++ [temp.class.spec]p10:
4992 // The template parameter list of a specialization shall not
4993 // contain default template argument values.
4994 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4995 Decl *Param = TemplateParams->getParam(I);
4996 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4997 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004998 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004999 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00005000 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00005001 }
5002 } else if (NonTypeTemplateParmDecl *NTTP
5003 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
5004 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00005005 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00005006 diag::err_default_arg_in_partial_spec)
5007 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00005008 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00005009 }
5010 } else {
5011 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00005012 if (TTP->hasDefaultArgument()) {
5013 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00005014 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00005015 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00005016 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00005017 }
5018 }
5019 }
Douglas Gregora735b202009-10-13 14:39:41 +00005020 } else if (TemplateParams) {
5021 if (TUK == TUK_Friend)
5022 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00005023 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00005024 SourceRange(TemplateParams->getTemplateLoc(),
5025 TemplateParams->getRAngleLoc()))
5026 << SourceRange(LAngleLoc, RAngleLoc);
5027 else
5028 isExplicitSpecialization = true;
5029 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00005030 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00005031 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005032 isExplicitSpecialization = true;
5033 }
Douglas Gregor88b70942009-02-25 22:02:03 +00005034
Douglas Gregorcc636682009-02-17 23:15:12 +00005035 // Check that the specialization uses the same tag kind as the
5036 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005037 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5038 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005039 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005040 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005041 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005042 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00005043 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005044 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00005045 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005046 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005047 diag::note_previous_use);
5048 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5049 }
5050
Douglas Gregor40808ce2009-03-09 23:48:35 +00005051 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005052 TemplateArgumentListInfo TemplateArgs;
5053 TemplateArgs.setLAngleLoc(LAngleLoc);
5054 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005055 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00005056
Douglas Gregor925910d2011-01-03 20:35:03 +00005057 // Check for unexpanded parameter packs in any of the template arguments.
5058 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005059 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00005060 UPPC_PartialSpecialization))
5061 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005062
Douglas Gregorcc636682009-02-17 23:15:12 +00005063 // Check that the template argument list is well-formed for this
5064 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005065 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005066 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5067 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005068 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005069
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005070 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00005071 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00005072 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00005073 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00005074 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00005075 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00005076 return true;
5077
Douglas Gregor561f8122011-07-01 01:22:09 +00005078 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005079 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00005080 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005081 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00005082 TemplateArgs.size(),
5083 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00005084 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
5085 << ClassTemplate->getDeclName();
5086 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00005087 }
5088 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005089
Douglas Gregorcc636682009-02-17 23:15:12 +00005090 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005091 ClassTemplateSpecializationDecl *PrevDecl = 0;
5092
5093 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005094 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005095 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005096 = ClassTemplate->findPartialSpecialization(Converted.data(),
5097 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005098 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005099 else
5100 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005101 = ClassTemplate->findSpecialization(Converted.data(),
5102 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00005103
5104 ClassTemplateSpecializationDecl *Specialization = 0;
5105
Douglas Gregor88b70942009-02-25 22:02:03 +00005106 // Check whether we can declare a class template specialization in
5107 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005108 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005109 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
5110 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00005111 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005112 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005113
Douglas Gregorb88e8882009-07-30 17:40:51 +00005114 // The canonical type
5115 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005116 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005117 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00005118 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005119 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005120 // arguments was referenced but not declared, or we're only
5121 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005122 // declaration node as our own, updating its source location and
5123 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005124 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005125 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005126 if (TemplateParameterLists.size() > 0) {
5127 Specialization->setTemplateParameterListsInfo(Context,
5128 TemplateParameterLists.size(),
5129 (TemplateParameterList**) TemplateParameterLists.release());
5130 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005131 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00005132 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005133 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00005134 // Build the canonical type that describes the converted template
5135 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00005136 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
5137 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005138 Converted.data(),
5139 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005140
5141 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005142 ClassTemplate->getInjectedClassNameSpecialization())) {
5143 // C++ [temp.class.spec]p9b3:
5144 //
5145 // -- The argument list of the specialization shall not be identical
5146 // to the implicit argument list of the primary template.
5147 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00005148 << (TUK == TUK_Definition)
5149 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00005150 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5151 ClassTemplate->getIdentifier(),
5152 TemplateNameLoc,
5153 Attr,
5154 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005155 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005156 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005157 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005158 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005159
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005160 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005161 ClassTemplatePartialSpecializationDecl *PrevPartial
5162 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005163 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005164 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005165 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005166 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005167 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005168 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005169 TemplateParams,
5170 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005171 Converted.data(),
5172 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005173 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005174 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005175 PrevPartial,
5176 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005177 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005178 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005179 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005180 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005181 (TemplateParameterList**) TemplateParameterLists.release());
5182 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005183
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005184 if (!PrevPartial)
5185 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005186 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005187
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005188 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005189 // template specialization, make a note of that.
5190 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5191 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005192
Douglas Gregor031a5882009-06-13 00:26:55 +00005193 // Check that all of the template parameters of the class template
5194 // partial specialization are deducible from the template
5195 // arguments. If not, this class template partial specialization
5196 // will never be used.
Benjamin Kramer013b3662012-01-30 16:17:39 +00005197 llvm::SmallBitVector DeducibleParams(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005198 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005199 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005200 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005201
Benjamin Kramer013b3662012-01-30 16:17:39 +00005202 if (!DeducibleParams.all()) {
5203 unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count();
Douglas Gregor031a5882009-06-13 00:26:55 +00005204 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5205 << (NumNonDeducible > 1)
5206 << SourceRange(TemplateNameLoc, RAngleLoc);
5207 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5208 if (!DeducibleParams[I]) {
5209 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5210 if (Param->getDeclName())
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)
5213 << Param->getDeclName();
5214 else
Mike Stump1eb44332009-09-09 15:08:12 +00005215 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005216 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005217 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005218 }
5219 }
5220 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005221 } else {
5222 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005223 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005224 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005225 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005226 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005227 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005228 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005229 Converted.data(),
5230 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005231 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005232 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005233 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005234 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005235 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005236 (TemplateParameterList**) TemplateParameterLists.release());
5237 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005238
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005239 if (!PrevDecl)
5240 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005241
5242 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005243 }
5244
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005245 // C++ [temp.expl.spec]p6:
5246 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005247 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005248 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005249 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005250 // use occurs; no diagnostic is required.
5251 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005252 bool Okay = false;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005253 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005254 // Is there any previous explicit specialization declaration?
5255 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5256 Okay = true;
5257 break;
5258 }
5259 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005260
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005261 if (!Okay) {
5262 SourceRange Range(TemplateNameLoc, RAngleLoc);
5263 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5264 << Context.getTypeDeclType(Specialization) << Range;
5265
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005266 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005267 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005268 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005269 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005270 return true;
5271 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005272 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005273
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005274 // If this is not a friend, note that this is an explicit specialization.
5275 if (TUK != TUK_Friend)
5276 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005277
5278 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005279 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005280 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005281 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005282 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005283 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005284 Diag(Def->getLocation(), diag::note_previous_definition);
5285 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005286 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005287 }
5288 }
5289
John McCall7f1b9872010-12-18 03:30:47 +00005290 if (Attr)
5291 ProcessDeclAttributeList(S, Specialization, Attr);
5292
Douglas Gregord023aec2011-09-09 20:53:38 +00005293 if (ModulePrivateLoc.isValid())
5294 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5295 << (isPartialSpecialization? 1 : 0)
5296 << FixItHint::CreateRemoval(ModulePrivateLoc);
5297
Douglas Gregorfc705b82009-02-26 22:19:44 +00005298 // Build the fully-sugared type for this class template
5299 // specialization as the user wrote in the specialization
5300 // itself. This means that we'll pretty-print the type retrieved
5301 // from the specialization's declaration the way that the user
5302 // actually wrote the specialization, rather than formatting the
5303 // name based on the "canonical" representation used to store the
5304 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005305 TypeSourceInfo *WrittenTy
5306 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5307 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005308 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005309 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005310 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005311 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005312 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005313
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005314 // C++ [temp.expl.spec]p9:
5315 // A template explicit specialization is in the scope of the
5316 // namespace in which the template was defined.
5317 //
5318 // We actually implement this paragraph where we set the semantic
5319 // context (in the creation of the ClassTemplateSpecializationDecl),
5320 // but we also maintain the lexical context where the actual
5321 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005322 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005323
Douglas Gregorcc636682009-02-17 23:15:12 +00005324 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005325 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005326 Specialization->startDefinition();
5327
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005328 if (TUK == TUK_Friend) {
5329 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5330 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005331 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005332 /*FIXME:*/KWLoc);
5333 Friend->setAccess(AS_public);
5334 CurContext->addDecl(Friend);
5335 } else {
5336 // Add the specialization into its lexical context, so that it can
5337 // be seen when iterating through the list of declarations in that
5338 // context. However, specializations are not found by name lookup.
5339 CurContext->addDecl(Specialization);
5340 }
John McCalld226f652010-08-21 09:40:31 +00005341 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005342}
Douglas Gregord57959a2009-03-27 23:10:48 +00005343
John McCalld226f652010-08-21 09:40:31 +00005344Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005345 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005346 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005347 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005348}
5349
John McCalld226f652010-08-21 09:40:31 +00005350Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005351 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005352 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005353 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005354 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005355
Douglas Gregor52591bf2009-06-24 00:54:41 +00005356 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005357 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005358 }
Mike Stump1eb44332009-09-09 15:08:12 +00005359
Douglas Gregor52591bf2009-06-24 00:54:41 +00005360 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005361
Douglas Gregor45fa5602011-11-07 20:56:01 +00005362 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005363 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005364 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005365 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005366 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005367 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005368 FunctionTemplate->getTemplatedDecl());
5369 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5370 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5371 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005372}
5373
John McCall75042392010-02-11 01:33:53 +00005374/// \brief Strips various properties off an implicit instantiation
5375/// that has just been explicitly specialized.
5376static void StripImplicitInstantiation(NamedDecl *D) {
Rafael Espindola860097c2012-02-23 04:17:32 +00005377 // FIXME: "make check" is clean if the call to dropAttrs() is commented out.
Sean Huntcf807c42010-08-18 23:23:40 +00005378 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005379
5380 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5381 FD->setInlineSpecified(false);
5382 }
5383}
5384
Nico Weberd1d512a2012-01-09 19:52:25 +00005385/// \brief Compute the diagnostic location for an explicit instantiation
5386// declaration or definition.
5387static SourceLocation DiagLocForExplicitInstantiation(
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005388 NamedDecl* D, SourceLocation PointOfInstantiation) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005389 // Explicit instantiations following a specialization have no effect and
5390 // hence no PointOfInstantiation. In that case, walk decl backwards
5391 // until a valid name loc is found.
5392 SourceLocation PrevDiagLoc = PointOfInstantiation;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005393 for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
5394 Prev = Prev->getPreviousDecl()) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005395 PrevDiagLoc = Prev->getLocation();
5396 }
5397 assert(PrevDiagLoc.isValid() &&
5398 "Explicit instantiation without point of instantiation?");
5399 return PrevDiagLoc;
5400}
5401
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005402/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005403/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005404/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005405/// new specialization/instantiation will have any effect.
5406///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005407/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005408/// instantiation.
5409///
5410/// \param NewTSK the kind of the new explicit specialization or instantiation.
5411///
5412/// \param PrevDecl the previous declaration of the entity.
5413///
5414/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5415///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005416/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005417/// declaration was instantiated (either implicitly or explicitly).
5418///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005419/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005420/// specialization or instantiation has no effect and should be ignored.
5421///
5422/// \returns true if there was an error that should prevent the introduction of
5423/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005424bool
5425Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5426 TemplateSpecializationKind NewTSK,
5427 NamedDecl *PrevDecl,
5428 TemplateSpecializationKind PrevTSK,
5429 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005430 bool &HasNoEffect) {
5431 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005432
Douglas Gregor454885e2009-10-15 15:54:05 +00005433 switch (NewTSK) {
5434 case TSK_Undeclared:
5435 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005436 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005437
Douglas Gregor454885e2009-10-15 15:54:05 +00005438 case TSK_ExplicitSpecialization:
5439 switch (PrevTSK) {
5440 case TSK_Undeclared:
5441 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005442 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005443 // explicitly specialized or has merely been mentioned without any
5444 // instantiation.
5445 return false;
5446
5447 case TSK_ImplicitInstantiation:
5448 if (PrevPointOfInstantiation.isInvalid()) {
5449 // The declaration itself has not actually been instantiated, so it is
5450 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005451 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005452 return false;
5453 }
5454 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005455
Douglas Gregor454885e2009-10-15 15:54:05 +00005456 case TSK_ExplicitInstantiationDeclaration:
5457 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005458 assert((PrevTSK == TSK_ImplicitInstantiation ||
5459 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005460 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005461
Douglas Gregor454885e2009-10-15 15:54:05 +00005462 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005463 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005464 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005465 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005466 // implicit instantiation to take place, in every translation unit in
5467 // which such a use occurs; no diagnostic is required.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005468 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005469 // Is there any previous explicit specialization declaration?
5470 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5471 return false;
5472 }
5473
Douglas Gregor0d035142009-10-27 18:42:08 +00005474 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005475 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005476 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005477 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005478
Douglas Gregor454885e2009-10-15 15:54:05 +00005479 return true;
5480 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005481
Douglas Gregor454885e2009-10-15 15:54:05 +00005482 case TSK_ExplicitInstantiationDeclaration:
5483 switch (PrevTSK) {
5484 case TSK_ExplicitInstantiationDeclaration:
5485 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005486 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005487 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005488
Douglas Gregor454885e2009-10-15 15:54:05 +00005489 case TSK_Undeclared:
5490 case TSK_ImplicitInstantiation:
5491 // We're explicitly instantiating something that may have already been
5492 // implicitly instantiated; that's fine.
5493 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005494
Douglas Gregor454885e2009-10-15 15:54:05 +00005495 case TSK_ExplicitSpecialization:
5496 // C++0x [temp.explicit]p4:
5497 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005498 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005499 // specialization for that template, the explicit instantiation has no
5500 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005501 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005502 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005503
Douglas Gregor454885e2009-10-15 15:54:05 +00005504 case TSK_ExplicitInstantiationDefinition:
5505 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005506 // If an entity is the subject of both an explicit instantiation
5507 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005508 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005509 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005510 diag::err_explicit_instantiation_declaration_after_definition);
Nico Weberff91d242011-12-23 20:58:04 +00005511
5512 // Explicit instantiations following a specialization have no effect and
5513 // hence no PrevPointOfInstantiation. In that case, walk decl backwards
5514 // until a valid name loc is found.
Nico Weberd1d512a2012-01-09 19:52:25 +00005515 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
5516 diag::note_explicit_instantiation_definition_here);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005517 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005518 return false;
5519 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005520
Douglas Gregor454885e2009-10-15 15:54:05 +00005521 case TSK_ExplicitInstantiationDefinition:
5522 switch (PrevTSK) {
5523 case TSK_Undeclared:
5524 case TSK_ImplicitInstantiation:
5525 // We're explicitly instantiating something that may have already been
5526 // implicitly instantiated; that's fine.
5527 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005528
Douglas Gregor454885e2009-10-15 15:54:05 +00005529 case TSK_ExplicitSpecialization:
5530 // C++ DR 259, C++0x [temp.explicit]p4:
5531 // For a given set of template parameters, if an explicit
5532 // instantiation of a template appears after a declaration of
5533 // an explicit specialization for that template, the explicit
5534 // instantiation has no effect.
5535 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005536 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005537 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005538 // has been explicitly specialized.
David Blaikie4e4d0842012-03-11 07:00:24 +00005539 Diag(NewLoc, getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00005540 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5541 diag::ext_explicit_instantiation_after_specialization)
5542 << PrevDecl;
5543 Diag(PrevDecl->getLocation(),
5544 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005545 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005546 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005547
Douglas Gregor454885e2009-10-15 15:54:05 +00005548 case TSK_ExplicitInstantiationDeclaration:
5549 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005550 // were previously asked to suppress instantiations. That's fine.
Nico Weberff91d242011-12-23 20:58:04 +00005551
5552 // C++0x [temp.explicit]p4:
5553 // For a given set of template parameters, if an explicit instantiation
5554 // of a template appears after a declaration of an explicit
5555 // specialization for that template, the explicit instantiation has no
5556 // effect.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005557 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Nico Weberff91d242011-12-23 20:58:04 +00005558 // Is there any previous explicit specialization declaration?
5559 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5560 HasNoEffect = true;
5561 break;
5562 }
5563 }
5564
Douglas Gregor454885e2009-10-15 15:54:05 +00005565 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005566
Douglas Gregor454885e2009-10-15 15:54:05 +00005567 case TSK_ExplicitInstantiationDefinition:
5568 // C++0x [temp.spec]p5:
5569 // For a given template and a given set of template-arguments,
5570 // - an explicit instantiation definition shall appear at most once
5571 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005572 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005573 << PrevDecl;
Nico Weberd1d512a2012-01-09 19:52:25 +00005574 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
Douglas Gregor0d035142009-10-27 18:42:08 +00005575 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005576 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005577 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005578 }
Douglas Gregor454885e2009-10-15 15:54:05 +00005579 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005580
David Blaikieb219cfc2011-09-23 05:06:16 +00005581 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005582}
5583
John McCallaf2094e2010-04-08 09:05:18 +00005584/// \brief Perform semantic analysis for the given dependent function
5585/// template specialization. The only possible way to get a dependent
5586/// function template specialization is with a friend declaration,
5587/// like so:
5588///
5589/// template <class T> void foo(T);
5590/// template <class T> class A {
5591/// friend void foo<>(T);
5592/// };
5593///
5594/// There really isn't any useful analysis we can do here, so we
5595/// just store the information.
5596bool
5597Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5598 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5599 LookupResult &Previous) {
5600 // Remove anything from Previous that isn't a function template in
5601 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005602 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005603 LookupResult::Filter F = Previous.makeFilter();
5604 while (F.hasNext()) {
5605 NamedDecl *D = F.next()->getUnderlyingDecl();
5606 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005607 !FDLookupContext->InEnclosingNamespaceSetOf(
5608 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005609 F.erase();
5610 }
5611 F.done();
5612
5613 // Should this be diagnosed here?
5614 if (Previous.empty()) return true;
5615
5616 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5617 ExplicitTemplateArgs);
5618 return false;
5619}
5620
Abramo Bagnarae03db982010-05-20 15:32:11 +00005621/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005622/// specialization.
5623///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005624/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005625/// explicit function template specialization. On successful completion,
5626/// the function declaration \p FD will become a function template
5627/// specialization.
5628///
5629/// \param FD the function declaration, which will be updated to become a
5630/// function template specialization.
5631///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005632/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5633/// if any. Note that this may be valid info even when 0 arguments are
5634/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5635/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005636///
Francois Pichet59e7c562011-07-08 06:21:47 +00005637/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005638/// this function specialization.
5639bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005640Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005641 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005642 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005643 // The set of function template specializations that could match this
5644 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005645 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005646
Sebastian Redl7a126a42010-08-31 00:36:30 +00005647 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005648 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5649 I != E; ++I) {
5650 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5651 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005652 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005653 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005654 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5655 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005656 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005657
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005658 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005659 // A trailing template-argument can be left unspecified in the
5660 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005661 // provided it can be deduced from the function argument type.
5662 // Perform template argument deduction to determine whether we may be
5663 // specializing this template.
5664 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005665 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005666 FunctionDecl *Specialization = 0;
5667 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005668 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005669 FD->getType(),
5670 Specialization,
5671 Info)) {
5672 // FIXME: Template argument deduction failed; record why it failed, so
5673 // that we can provide nifty diagnostics.
5674 (void)TDK;
5675 continue;
5676 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005677
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005678 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005679 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005680 }
5681 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005682
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005683 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005684 UnresolvedSetIterator Result
5685 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005686 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005687 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005688 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005689 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005690 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005691 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005692 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005693 return true;
John McCallc373d482010-01-27 01:50:18 +00005694
5695 // Ignore access information; it doesn't figure into redeclaration checking.
5696 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005697
5698 FunctionTemplateSpecializationInfo *SpecInfo
5699 = Specialization->getTemplateSpecializationInfo();
5700 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005701
5702 // Note: do not overwrite location info if previous template
5703 // specialization kind was explicit.
5704 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
Richard Smithff234882012-02-20 23:28:05 +00005705 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) {
Francois Pichet59e7c562011-07-08 06:21:47 +00005706 Specialization->setLocation(FD->getLocation());
Richard Smithff234882012-02-20 23:28:05 +00005707 // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr
5708 // function can differ from the template declaration with respect to
5709 // the constexpr specifier.
5710 Specialization->setConstexpr(FD->isConstexpr());
5711 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005712
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005713 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005714 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005715
5716 // If this is a friend declaration, then we're not really declaring
5717 // an explicit specialization.
5718 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005719
Douglas Gregord5cb8762009-10-07 00:13:32 +00005720 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005721 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005722 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005723 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005724 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005725 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005726 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005727
5728 // C++ [temp.expl.spec]p6:
5729 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005730 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005731 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005732 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005733 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005734 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005735 if (!isFriend &&
5736 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005737 TSK_ExplicitSpecialization,
5738 Specialization,
5739 SpecInfo->getTemplateSpecializationKind(),
5740 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005741 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005742 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005743
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005744 // Mark the prior declaration as an explicit specialization, so that later
5745 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005746 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005747 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005748 MarkUnusedFileScopedDecl(Specialization);
5749 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005750
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005751 // Turn the given function declaration into a function template
5752 // specialization, with the template arguments from the previous
5753 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005754 // Take copies of (semantic and syntactic) template argument lists.
5755 const TemplateArgumentList* TemplArgs = new (Context)
5756 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005757 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005758 TemplArgs, /*InsertPos=*/0,
5759 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005760 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005761 FD->setStorageClass(Specialization->getStorageClass());
5762
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005763 // The "previous declaration" for this function template specialization is
5764 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005765 Previous.clear();
5766 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005767 return false;
5768}
5769
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005770/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005771/// specialization.
5772///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005773/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005774/// explicit member function specialization. On successful completion,
5775/// the function declaration \p FD will become a member function
5776/// specialization.
5777///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005778/// \param Member the member declaration, which will be updated to become a
5779/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005780///
John McCall68263142009-11-18 22:49:29 +00005781/// \param Previous the set of declarations, one of which may be specialized
5782/// by this function specialization; the set will be modified to contain the
5783/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005784bool
John McCall68263142009-11-18 22:49:29 +00005785Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005786 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005787
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005788 // Try to find the member we are instantiating.
5789 NamedDecl *Instantiation = 0;
5790 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005791 MemberSpecializationInfo *MSInfo = 0;
5792
John McCall68263142009-11-18 22:49:29 +00005793 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005794 // Nowhere to look anyway.
5795 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005796 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5797 I != E; ++I) {
5798 NamedDecl *D = (*I)->getUnderlyingDecl();
5799 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005800 if (Context.hasSameType(Function->getType(), Method->getType())) {
5801 Instantiation = Method;
5802 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005803 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005804 break;
5805 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005806 }
5807 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005808 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005809 VarDecl *PrevVar;
5810 if (Previous.isSingleResult() &&
5811 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005812 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005813 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005814 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005815 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005816 }
5817 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005818 CXXRecordDecl *PrevRecord;
5819 if (Previous.isSingleResult() &&
5820 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5821 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005822 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005823 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005824 }
Richard Smith1af83c42012-03-23 03:33:32 +00005825 } else if (isa<EnumDecl>(Member)) {
5826 EnumDecl *PrevEnum;
5827 if (Previous.isSingleResult() &&
5828 (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) {
5829 Instantiation = PrevEnum;
5830 InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum();
5831 MSInfo = PrevEnum->getMemberSpecializationInfo();
5832 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005833 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005834
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005835 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005836 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005837 // specializations are always out-of-line, the caller will complain about
5838 // this mismatch later.
5839 return false;
5840 }
John McCall77e8b112010-04-13 20:37:33 +00005841
5842 // If this is a friend, just bail out here before we start turning
5843 // things into explicit specializations.
5844 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5845 // Preserve instantiation information.
5846 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5847 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5848 cast<CXXMethodDecl>(InstantiatedFrom),
5849 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5850 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5851 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5852 cast<CXXRecordDecl>(InstantiatedFrom),
5853 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5854 }
5855
5856 Previous.clear();
5857 Previous.addDecl(Instantiation);
5858 return false;
5859 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005860
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005861 // Make sure that this is a specialization of a member.
5862 if (!InstantiatedFrom) {
5863 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5864 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005865 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5866 return true;
5867 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005868
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005869 // C++ [temp.expl.spec]p6:
5870 // If a template, a member template or the member of a class template is
Nico Weberff91d242011-12-23 20:58:04 +00005871 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005872 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005873 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005874 // use occurs; no diagnostic is required.
5875 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005876
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005877 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005878 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5879 TSK_ExplicitSpecialization,
5880 Instantiation,
5881 MSInfo->getTemplateSpecializationKind(),
5882 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005883 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005884 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005885
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005886 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005887 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005888 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005889 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005890 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005891 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005892
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005893 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005894 // the original declaration to note that it is an explicit specialization
5895 // (if it was previously an implicit instantiation). This latter step
5896 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005897 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005898 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5899 if (InstantiationFunction->getTemplateSpecializationKind() ==
5900 TSK_ImplicitInstantiation) {
5901 InstantiationFunction->setTemplateSpecializationKind(
5902 TSK_ExplicitSpecialization);
5903 InstantiationFunction->setLocation(Member->getLocation());
5904 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005905
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005906 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5907 cast<CXXMethodDecl>(InstantiatedFrom),
5908 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005909 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005910 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005911 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5912 if (InstantiationVar->getTemplateSpecializationKind() ==
5913 TSK_ImplicitInstantiation) {
5914 InstantiationVar->setTemplateSpecializationKind(
5915 TSK_ExplicitSpecialization);
5916 InstantiationVar->setLocation(Member->getLocation());
5917 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005918
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005919 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5920 cast<VarDecl>(InstantiatedFrom),
5921 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005922 MarkUnusedFileScopedDecl(InstantiationVar);
Richard Smith1af83c42012-03-23 03:33:32 +00005923 } else if (isa<CXXRecordDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005924 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5925 if (InstantiationClass->getTemplateSpecializationKind() ==
5926 TSK_ImplicitInstantiation) {
5927 InstantiationClass->setTemplateSpecializationKind(
5928 TSK_ExplicitSpecialization);
5929 InstantiationClass->setLocation(Member->getLocation());
5930 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005931
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005932 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005933 cast<CXXRecordDecl>(InstantiatedFrom),
5934 TSK_ExplicitSpecialization);
Richard Smith1af83c42012-03-23 03:33:32 +00005935 } else {
5936 assert(isa<EnumDecl>(Member) && "Only member enums remain");
5937 EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation);
5938 if (InstantiationEnum->getTemplateSpecializationKind() ==
5939 TSK_ImplicitInstantiation) {
5940 InstantiationEnum->setTemplateSpecializationKind(
5941 TSK_ExplicitSpecialization);
5942 InstantiationEnum->setLocation(Member->getLocation());
5943 }
5944
5945 cast<EnumDecl>(Member)->setInstantiationOfMemberEnum(
5946 cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005947 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005948
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005949 // Save the caller the trouble of having to figure out which declaration
5950 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005951 Previous.clear();
5952 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005953 return false;
5954}
5955
Douglas Gregor558c0322009-10-14 23:41:34 +00005956/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005957///
5958/// \returns true if a serious error occurs, false otherwise.
5959static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005960 SourceLocation InstLoc,
5961 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005962 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5963 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005964
Douglas Gregor669eed82010-07-13 00:10:04 +00005965 if (CurContext->isRecord()) {
5966 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5967 << D;
5968 return true;
5969 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005970
Richard Smith3e2e91e2011-10-18 02:28:33 +00005971 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005972 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005973 // template. If the name declared in the explicit instantiation is an
5974 // unqualified name, the explicit instantiation shall appear in the
5975 // namespace where its template is declared or, if that namespace is inline
5976 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005977 //
5978 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005979 if (WasQualifiedName) {
5980 if (CurContext->Encloses(OrigContext))
5981 return false;
5982 } else {
5983 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5984 return false;
5985 }
5986
5987 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5988 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005989 S.Diag(InstLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00005990 S.getLangOpts().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005991 diag::err_explicit_instantiation_out_of_scope :
5992 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005993 << D << NS;
5994 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005995 S.Diag(InstLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00005996 S.getLangOpts().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005997 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5998 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5999 << D << NS;
6000 } else
6001 S.Diag(InstLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00006002 S.getLangOpts().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00006003 diag::err_explicit_instantiation_must_be_global :
6004 diag::warn_explicit_instantiation_must_be_global_0x)
6005 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00006006 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00006007 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00006008}
6009
6010/// \brief Determine whether the given scope specifier has a template-id in it.
6011static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
6012 if (!SS.isSet())
6013 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006014
Richard Smith3e2e91e2011-10-18 02:28:33 +00006015 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006016 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006017 // or a static data member of a class template specialization, the name of
6018 // the class template specialization in the qualified-id for the member
6019 // name shall be a simple-template-id.
6020 //
6021 // C++98 has the same restriction, just worded differently.
6022 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
6023 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00006024 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00006025 if (isa<TemplateSpecializationType>(T))
6026 return true;
6027
6028 return false;
6029}
6030
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006031// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00006032DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006033Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006034 SourceLocation ExternLoc,
6035 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006036 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006037 SourceLocation KWLoc,
6038 const CXXScopeSpec &SS,
6039 TemplateTy TemplateD,
6040 SourceLocation TemplateNameLoc,
6041 SourceLocation LAngleLoc,
6042 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006043 SourceLocation RAngleLoc,
6044 AttributeList *Attr) {
6045 // Find the class template we're specializing
6046 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00006047 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006048 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
6049
6050 // Check that the specialization uses the same tag kind as the
6051 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006052 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
6053 assert(Kind != TTK_Enum &&
6054 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006055 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00006056 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006057 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00006058 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006059 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00006060 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006061 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00006062 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006063 diag::note_previous_use);
6064 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
6065 }
6066
Douglas Gregor558c0322009-10-14 23:41:34 +00006067 // C++0x [temp.explicit]p2:
6068 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006069 // definition and an explicit instantiation declaration. An explicit
6070 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00006071 TemplateSpecializationKind TSK
6072 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6073 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006074
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006075 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00006076 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00006077 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006078
6079 // Check that the template argument list is well-formed for this
6080 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00006081 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00006082 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
6083 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006084 return true;
6085
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006086 // Find the class template specialization declaration that
6087 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006088 void *InsertPos = 0;
6089 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00006090 = ClassTemplate->findSpecialization(Converted.data(),
6091 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006092
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006093 TemplateSpecializationKind PrevDecl_TSK
6094 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
6095
Douglas Gregord5cb8762009-10-07 00:13:32 +00006096 // C++0x [temp.explicit]p2:
6097 // [...] An explicit instantiation shall appear in an enclosing
6098 // namespace of its template. [...]
6099 //
6100 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00006101 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
6102 SS.isSet()))
6103 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006104
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006105 ClassTemplateSpecializationDecl *Specialization = 0;
6106
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006107 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006108 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00006109 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006110 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006111 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006112 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00006113 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006114
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006115 // Even though HasNoEffect == true means that this explicit instantiation
6116 // has no effect on semantics, we go on to put its syntax in the AST.
6117
6118 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
6119 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00006120 // Since the only prior class template specialization with these
6121 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006122 // declaration node as our own, updating the source location
6123 // for the template name to reflect our new declaration.
6124 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00006125 Specialization = PrevDecl;
6126 Specialization->setLocation(TemplateNameLoc);
6127 PrevDecl = 0;
6128 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006129 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006130
Douglas Gregor52604ab2009-09-11 21:19:12 +00006131 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006132 // Create a new class template specialization declaration node for
6133 // this explicit specialization.
6134 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00006135 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006136 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00006137 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006138 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00006139 Converted.data(),
6140 Converted.size(),
6141 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00006142 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006143
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006144 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006145 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006146 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006147 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006148 }
6149
6150 // Build the fully-sugared type for this explicit instantiation as
6151 // the user wrote in the explicit instantiation itself. This means
6152 // that we'll pretty-print the type retrieved from the
6153 // specialization's declaration the way that the user actually wrote
6154 // the explicit instantiation, rather than formatting the name based
6155 // on the "canonical" representation used to store the template
6156 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00006157 TypeSourceInfo *WrittenTy
6158 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
6159 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006160 Context.getTypeDeclType(Specialization));
6161 Specialization->setTypeAsWritten(WrittenTy);
6162 TemplateArgsIn.release();
6163
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006164 // Set source locations for keywords.
6165 Specialization->setExternLoc(ExternLoc);
6166 Specialization->setTemplateKeywordLoc(TemplateLoc);
6167
Rafael Espindola0257b7f2012-01-03 06:04:21 +00006168 if (Attr)
6169 ProcessDeclAttributeList(S, Specialization, Attr);
6170
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006171 // Add the explicit instantiation into its lexical context. However,
6172 // since explicit instantiations are never found by name lookup, we
6173 // just put it into the declaration context directly.
6174 Specialization->setLexicalDeclContext(CurContext);
6175 CurContext->addDecl(Specialization);
6176
6177 // Syntax is now OK, so return if it has no other effect on semantics.
6178 if (HasNoEffect) {
6179 // Set the template specialization kind.
6180 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006181 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00006182 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006183
6184 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006185 // A definition of a class template or class member template
6186 // shall be in scope at the point of the explicit instantiation of
6187 // the class template or class member template.
6188 //
6189 // This check comes when we actually try to perform the
6190 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006191 ClassTemplateSpecializationDecl *Def
6192 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006193 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006194 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006195 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006196 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006197 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006198 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6199 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006200
Douglas Gregor0d035142009-10-27 18:42:08 +00006201 // Instantiate the members of this class template specialization.
6202 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006203 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006204 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006205 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6206
6207 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6208 // TSK_ExplicitInstantiationDefinition
6209 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6210 TSK == TSK_ExplicitInstantiationDefinition)
6211 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006212
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006213 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006214 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006215
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006216 // Set the template specialization kind.
6217 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006218 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006219}
6220
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006221// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006222DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006223Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006224 SourceLocation ExternLoc,
6225 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006226 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006227 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006228 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006229 IdentifierInfo *Name,
6230 SourceLocation NameLoc,
6231 AttributeList *Attr) {
6232
Douglas Gregor402abb52009-05-28 23:31:59 +00006233 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006234 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006235 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006236 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006237 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006238 MultiTemplateParamsArg(*this, 0, 0),
Richard Smithbdad7a22012-01-10 01:33:14 +00006239 Owned, IsDependent, SourceLocation(), false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006240 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006241 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6242
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006243 if (!TagD)
6244 return true;
6245
John McCalld226f652010-08-21 09:40:31 +00006246 TagDecl *Tag = cast<TagDecl>(TagD);
Richard Smith1af83c42012-03-23 03:33:32 +00006247 assert(!Tag->isEnum() && "shouldn't see enumerations here");
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006248
Douglas Gregord0c87372009-05-27 17:30:49 +00006249 if (Tag->isInvalidDecl())
6250 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006251
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006252 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6253 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6254 if (!Pattern) {
6255 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6256 << Context.getTypeDeclType(Record);
6257 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6258 return true;
6259 }
6260
Douglas Gregor558c0322009-10-14 23:41:34 +00006261 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006262 // If the explicit instantiation is for a class or member class, the
6263 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006264 // simple-template-id.
6265 //
6266 // C++98 has the same restriction, just worded differently.
6267 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006268 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006269 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006270
Douglas Gregor558c0322009-10-14 23:41:34 +00006271 // C++0x [temp.explicit]p2:
6272 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006273 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006274 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006275 TemplateSpecializationKind TSK
6276 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6277 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006278
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006279 // C++0x [temp.explicit]p2:
6280 // [...] An explicit instantiation shall appear in an enclosing
6281 // namespace of its template. [...]
6282 //
6283 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006284 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006285
Douglas Gregor454885e2009-10-15 15:54:05 +00006286 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006287 CXXRecordDecl *PrevDecl
Douglas Gregoref96ee02012-01-14 16:38:05 +00006288 = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
Douglas Gregor952b0172010-02-11 01:04:33 +00006289 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006290 PrevDecl = Record;
6291 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006292 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006293 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006294 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006295 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006296 PrevDecl,
6297 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006298 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006299 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006300 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006301 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006302 return TagD;
6303 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006304
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006305 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006306 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006307 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006308 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006309 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006310 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006311 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006312 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006313 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006314 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6315 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006316 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6317 << Pattern;
6318 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006319 } else {
6320 if (InstantiateClass(NameLoc, Record, Def,
6321 getTemplateInstantiationArgs(Record),
6322 TSK))
6323 return true;
6324
Douglas Gregor952b0172010-02-11 01:04:33 +00006325 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006326 if (!RecordDef)
6327 return true;
6328 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006329 }
6330
Douglas Gregor0d035142009-10-27 18:42:08 +00006331 // Instantiate all of the members of the class.
6332 InstantiateClassMembers(NameLoc, RecordDef,
6333 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006334
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006335 if (TSK == TSK_ExplicitInstantiationDefinition)
6336 MarkVTableUsed(NameLoc, RecordDef, true);
6337
Mike Stump390b4cc2009-05-16 07:39:55 +00006338 // FIXME: We don't have any representation for explicit instantiations of
6339 // member classes. Such a representation is not needed for compilation, but it
6340 // should be available for clients that want to see all of the declarations in
6341 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006342 return TagD;
6343}
6344
John McCallf312b1e2010-08-26 23:41:50 +00006345DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6346 SourceLocation ExternLoc,
6347 SourceLocation TemplateLoc,
6348 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006349 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006350 // TODO: check if/when DNInfo should replace Name.
6351 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6352 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006353 if (!Name) {
6354 if (!D.isInvalidType())
Daniel Dunbar96a00142012-03-09 18:35:03 +00006355 Diag(D.getDeclSpec().getLocStart(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006356 diag::err_explicit_instantiation_requires_name)
6357 << D.getDeclSpec().getSourceRange()
6358 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006359
Douglas Gregord5a423b2009-09-25 18:43:00 +00006360 return true;
6361 }
6362
6363 // The scope passed in may not be a decl scope. Zip up the scope tree until
6364 // we find one that is.
6365 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6366 (S->getFlags() & Scope::TemplateParamScope) != 0)
6367 S = S->getParent();
6368
6369 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006370 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6371 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006372 if (R.isNull())
6373 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006374
Douglas Gregore885e182011-05-21 18:53:30 +00006375 // C++ [dcl.stc]p1:
6376 // A storage-class-specifier shall not be specified in [...] an explicit
6377 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006378 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006379 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6380 << Name;
6381 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006382 } else if (D.getDeclSpec().getStorageClassSpec()
6383 != DeclSpec::SCS_unspecified) {
6384 // Complain about then remove the storage class specifier.
6385 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6386 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6387
6388 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006389 }
6390
Douglas Gregor663b5a02009-10-14 20:14:33 +00006391 // C++0x [temp.explicit]p1:
6392 // [...] An explicit instantiation of a function template shall not use the
6393 // inline or constexpr specifiers.
6394 // Presumably, this also applies to member functions of class templates as
6395 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006396 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006397 Diag(D.getDeclSpec().getInlineSpecLoc(),
David Blaikie4e4d0842012-03-11 07:00:24 +00006398 getLangOpts().CPlusPlus0x ?
Richard Smith2dc7ece2011-10-18 03:44:03 +00006399 diag::err_explicit_instantiation_inline :
6400 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006401 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6402 if (D.getDeclSpec().isConstexprSpecified())
6403 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6404 // not already specified.
6405 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6406 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006407
Douglas Gregor558c0322009-10-14 23:41:34 +00006408 // C++0x [temp.explicit]p2:
6409 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006410 // definition and an explicit instantiation declaration. An explicit
6411 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006412 TemplateSpecializationKind TSK
6413 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6414 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006415
Abramo Bagnara25777432010-08-11 22:01:17 +00006416 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006417 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006418
6419 if (!R->isFunctionType()) {
6420 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006421 // A [...] static data member of a class template can be explicitly
6422 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006423 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006424 if (Previous.isAmbiguous())
6425 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006426
John McCall1bcee0a2009-12-02 08:25:40 +00006427 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006428 if (!Prev || !Prev->isStaticDataMember()) {
6429 // We expect to see a data data member here.
6430 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6431 << Name;
6432 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6433 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006434 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006435 return true;
6436 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006437
Douglas Gregord5a423b2009-09-25 18:43:00 +00006438 if (!Prev->getInstantiatedFromStaticDataMember()) {
6439 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006440 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006441 diag::err_explicit_instantiation_data_member_not_instantiated)
6442 << Prev;
6443 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6444 // FIXME: Can we provide a note showing where this was declared?
6445 return true;
6446 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006447
Douglas Gregor558c0322009-10-14 23:41:34 +00006448 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006449 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006450 // or a static data member of a class template specialization, the name of
6451 // the class template specialization in the qualified-id for the member
6452 // name shall be a simple-template-id.
6453 //
6454 // C++98 has the same restriction, just worded differently.
6455 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006456 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006457 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006458 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006459
Douglas Gregor558c0322009-10-14 23:41:34 +00006460 // Check the scope of this explicit instantiation.
6461 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006462
Douglas Gregor454885e2009-10-15 15:54:05 +00006463 // Verify that it is okay to explicitly instantiate here.
6464 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6465 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006466 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006467 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006468 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006469 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006470 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006471 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006472 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006473 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006474
Douglas Gregord5a423b2009-09-25 18:43:00 +00006475 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006476 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006477 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006478 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006479
Douglas Gregord5a423b2009-09-25 18:43:00 +00006480 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006481 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006482 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006483
6484 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006485 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006486 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006487 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006488 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6489 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006490 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6491 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006492 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6493 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006494 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006495 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006496 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006497 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006498 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006499
Douglas Gregord5a423b2009-09-25 18:43:00 +00006500 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006501 // A [...] function [...] can be explicitly instantiated from its template.
6502 // A member function [...] of a class template can be explicitly
6503 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006504 // template.
John McCallc373d482010-01-27 01:50:18 +00006505 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006506 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6507 P != PEnd; ++P) {
6508 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006509 if (!HasExplicitTemplateArgs) {
6510 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6511 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6512 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006513
John McCallc373d482010-01-27 01:50:18 +00006514 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006515 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6516 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006517 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006518 }
6519 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006520
Douglas Gregord5a423b2009-09-25 18:43:00 +00006521 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6522 if (!FunTmpl)
6523 continue;
6524
John McCall5769d612010-02-08 23:07:23 +00006525 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006526 FunctionDecl *Specialization = 0;
6527 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006528 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006529 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006530 R, Specialization, Info)) {
6531 // FIXME: Keep track of almost-matches?
6532 (void)TDK;
6533 continue;
6534 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006535
John McCallc373d482010-01-27 01:50:18 +00006536 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006537 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006538
Douglas Gregord5a423b2009-09-25 18:43:00 +00006539 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006540 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006541 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006542 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006543 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6544 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6545 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006546
John McCallc373d482010-01-27 01:50:18 +00006547 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006548 return true;
John McCallc373d482010-01-27 01:50:18 +00006549
6550 // Ignore access control bits, we don't need them for redeclaration checking.
6551 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006552
Douglas Gregor0a897e32009-10-15 17:21:20 +00006553 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006554 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006555 diag::err_explicit_instantiation_member_function_not_instantiated)
6556 << Specialization
6557 << (Specialization->getTemplateSpecializationKind() ==
6558 TSK_ExplicitSpecialization);
6559 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6560 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006561 }
6562
Douglas Gregoref96ee02012-01-14 16:38:05 +00006563 FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006564 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6565 PrevDecl = Specialization;
6566
Douglas Gregor0a897e32009-10-15 17:21:20 +00006567 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006568 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006569 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006570 PrevDecl,
6571 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006572 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006573 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006574 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006575
Douglas Gregor0a897e32009-10-15 17:21:20 +00006576 // FIXME: We may still want to build some representation of this
6577 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006578 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006579 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006580 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006581
6582 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Rafael Espindola256fc4d2012-01-04 05:40:59 +00006583 AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
6584 if (Attr)
6585 ProcessDeclAttributeList(S, Specialization, Attr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006586
Douglas Gregor0a897e32009-10-15 17:21:20 +00006587 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006588 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006589
Douglas Gregor558c0322009-10-14 23:41:34 +00006590 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006591 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006592 // or a static data member of a class template specialization, the name of
6593 // the class template specialization in the qualified-id for the member
6594 // name shall be a simple-template-id.
6595 //
6596 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006597 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006598 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006599 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006600 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006601 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006602 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006603 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006604
Douglas Gregor558c0322009-10-14 23:41:34 +00006605 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006606 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006607 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006608 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006609 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006610
Douglas Gregord5a423b2009-09-25 18:43:00 +00006611 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006612 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006613}
6614
John McCallf312b1e2010-08-26 23:41:50 +00006615TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006616Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6617 const CXXScopeSpec &SS, IdentifierInfo *Name,
6618 SourceLocation TagLoc, SourceLocation NameLoc) {
6619 // This has to hold, because SS is expected to be defined.
6620 assert(Name && "Expected a name in a dependent tag");
6621
6622 NestedNameSpecifier *NNS
6623 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6624 if (!NNS)
6625 return true;
6626
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006627 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006628
Douglas Gregor48c89f42010-04-24 16:38:41 +00006629 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6630 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006631 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006632 return true;
6633 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006634
Douglas Gregor059101f2011-03-02 00:47:37 +00006635 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006636 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006637 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6638
6639 // Create type-source location information for this type.
6640 TypeLocBuilder TLB;
6641 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006642 TL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00006643 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6644 TL.setNameLoc(NameLoc);
6645 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006646}
6647
John McCallf312b1e2010-08-26 23:41:50 +00006648TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006649Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6650 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006651 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006652 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006653 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006654
Richard Smithebaf0e62011-10-18 20:49:44 +00006655 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6656 Diag(TypenameLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00006657 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00006658 diag::warn_cxx98_compat_typename_outside_of_template :
6659 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006660 << FixItHint::CreateRemoval(TypenameLoc);
6661
Douglas Gregor2494dd02011-03-01 01:34:45 +00006662 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006663 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6664 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006665 if (T.isNull())
6666 return true;
John McCall63b43852010-04-29 23:50:39 +00006667
6668 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6669 if (isa<DependentNameType>(T)) {
6670 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006671 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006672 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006673 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006674 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006675 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006676 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006677 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006678 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006679 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006680
John McCallb3d87482010-08-24 05:47:05 +00006681 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006682}
6683
John McCallf312b1e2010-08-26 23:41:50 +00006684TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006685Sema::ActOnTypenameType(Scope *S,
6686 SourceLocation TypenameLoc,
6687 const CXXScopeSpec &SS,
6688 SourceLocation TemplateKWLoc,
Douglas Gregora02411e2011-02-27 22:46:49 +00006689 TemplateTy TemplateIn,
6690 SourceLocation TemplateNameLoc,
6691 SourceLocation LAngleLoc,
6692 ASTTemplateArgsPtr TemplateArgsIn,
6693 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006694 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6695 Diag(TypenameLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00006696 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00006697 diag::warn_cxx98_compat_typename_outside_of_template :
6698 diag::ext_typename_outside_of_template)
6699 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006700
6701 // Translate the parser's template argument list in our AST format.
6702 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6703 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6704
6705 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006706 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6707 // Construct a dependent template specialization type.
6708 assert(DTN && "dependent template has non-dependent name?");
6709 assert(DTN->getQualifier()
6710 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6711 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6712 DTN->getQualifier(),
6713 DTN->getIdentifier(),
6714 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006715
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006716 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006717 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006718 DependentTemplateSpecializationTypeLoc SpecTL
6719 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006720 SpecTL.setElaboratedKeywordLoc(TypenameLoc);
6721 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00006722 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006723 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006724 SpecTL.setLAngleLoc(LAngleLoc);
6725 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006726 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6727 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006728 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006729 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006730
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006731 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6732 if (T.isNull())
6733 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006734
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006735 // Provide source-location information for the template specialization type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006736 TypeLocBuilder Builder;
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006737 TemplateSpecializationTypeLoc SpecTL
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006738 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006739 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
6740 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006741 SpecTL.setLAngleLoc(LAngleLoc);
6742 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006743 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6744 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6745
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006746 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6747 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006748 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006749 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6750
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006751 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6752 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006753}
6754
Douglas Gregora02411e2011-02-27 22:46:49 +00006755
Douglas Gregord57959a2009-03-27 23:10:48 +00006756/// \brief Build the type that describes a C++ typename specifier,
6757/// e.g., "typename T::type".
6758QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006759Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6760 SourceLocation KeywordLoc,
6761 NestedNameSpecifierLoc QualifierLoc,
6762 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006763 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006764 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006765 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006766
John McCall77bb1aa2010-05-01 00:40:08 +00006767 DeclContext *Ctx = computeDeclContext(SS);
6768 if (!Ctx) {
6769 // If the nested-name-specifier is dependent and couldn't be
6770 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006771 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6772 return Context.getDependentNameType(Keyword,
6773 QualifierLoc.getNestedNameSpecifier(),
6774 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006775 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006776
John McCall77bb1aa2010-05-01 00:40:08 +00006777 // If the nested-name-specifier refers to the current instantiation,
6778 // the "typename" keyword itself is superfluous. In C++03, the
6779 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6780 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006781 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006782
John McCall77bb1aa2010-05-01 00:40:08 +00006783 if (RequireCompleteDeclContext(SS, Ctx))
6784 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006785
6786 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006787 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006788 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006789 unsigned DiagID = 0;
6790 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006791 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006792 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006793 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006794 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006795
6796 case LookupResult::FoundUnresolvedValue: {
6797 // We found a using declaration that is a value. Most likely, the using
6798 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006799 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006800 IILoc);
6801 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6802 << Name << Ctx << FullRange;
6803 if (UnresolvedUsingValueDecl *Using
6804 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006805 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006806 Diag(Loc, diag::note_using_value_decl_missing_typename)
6807 << FixItHint::CreateInsertion(Loc, "typename ");
6808 }
6809 }
6810 // Fall through to create a dependent typename type, from which we can recover
6811 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006812
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006813 case LookupResult::NotFoundInCurrentInstantiation:
6814 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006815 return Context.getDependentNameType(Keyword,
6816 QualifierLoc.getNestedNameSpecifier(),
6817 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006818
6819 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006820 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006821 // We found a type. Build an ElaboratedType, since the
6822 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006823 return Context.getElaboratedType(ETK_Typename,
6824 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006825 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006826 }
6827
6828 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006829 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006830 break;
6831
6832 case LookupResult::FoundOverloaded:
6833 DiagID = diag::err_typename_nested_not_type;
6834 Referenced = *Result.begin();
6835 break;
6836
John McCall6e247262009-10-10 05:48:19 +00006837 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006838 return QualType();
6839 }
6840
6841 // If we get here, it's because name lookup did not find a
6842 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006843 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006844 IILoc);
6845 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006846 if (Referenced)
6847 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6848 << Name;
6849 return QualType();
6850}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006851
6852namespace {
6853 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006854 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006855 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006856 SourceLocation Loc;
6857 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006858
Douglas Gregor4a959d82009-08-06 16:20:37 +00006859 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006860 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006861
Mike Stump1eb44332009-09-09 15:08:12 +00006862 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006863 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006864 DeclarationName Entity)
6865 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006866 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006867
6868 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006869 /// transformed.
6870 ///
6871 /// For the purposes of type reconstruction, a type has already been
6872 /// transformed if it is NULL or if it is not dependent.
6873 bool AlreadyTransformed(QualType T) {
6874 return T.isNull() || !T->isDependentType();
6875 }
Mike Stump1eb44332009-09-09 15:08:12 +00006876
6877 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006878 /// rebuilt.
6879 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006880
Douglas Gregor4a959d82009-08-06 16:20:37 +00006881 /// \brief Returns the name of the entity whose type is being rebuilt.
6882 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006883
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006884 /// \brief Sets the "base" location and entity when that
6885 /// information is known based on another transformation.
6886 void setBase(SourceLocation Loc, DeclarationName Entity) {
6887 this->Loc = Loc;
6888 this->Entity = Entity;
6889 }
Douglas Gregordfca6f52012-02-13 22:00:16 +00006890
6891 ExprResult TransformLambdaExpr(LambdaExpr *E) {
6892 // Lambdas never need to be transformed.
6893 return E;
6894 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006895 };
6896}
6897
Douglas Gregor4a959d82009-08-06 16:20:37 +00006898/// \brief Rebuilds a type within the context of the current instantiation.
6899///
Mike Stump1eb44332009-09-09 15:08:12 +00006900/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006901/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006902/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006903/// partial specialization thereof). This routine will rebuild that type now
6904/// that we have entered the declarator's scope, which may produce different
6905/// canonical types, e.g.,
6906///
6907/// \code
6908/// template<typename T>
6909/// struct X {
6910/// typedef T* pointer;
6911/// pointer data();
6912/// };
6913///
6914/// template<typename T>
6915/// typename X<T>::pointer X<T>::data() { ... }
6916/// \endcode
6917///
Douglas Gregor4714c122010-03-31 17:34:00 +00006918/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006919/// since we do not know that we can look into X<T> when we parsed the type.
6920/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006921/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006922/// as the canonical type of T*, allowing the return types of the out-of-line
6923/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006924TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6925 SourceLocation Loc,
6926 DeclarationName Name) {
6927 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006928 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006929
Douglas Gregor4a959d82009-08-06 16:20:37 +00006930 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6931 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006932}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006933
John McCall60d7b3a2010-08-24 06:29:42 +00006934ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006935 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6936 DeclarationName());
6937 return Rebuilder.TransformExpr(E);
6938}
6939
John McCall63b43852010-04-29 23:50:39 +00006940bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006941 if (SS.isInvalid())
6942 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006943
Douglas Gregor7e384942011-02-25 16:07:42 +00006944 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006945 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6946 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006947 NestedNameSpecifierLoc Rebuilt
6948 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6949 if (!Rebuilt)
6950 return true;
John McCall63b43852010-04-29 23:50:39 +00006951
Douglas Gregor7e384942011-02-25 16:07:42 +00006952 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006953 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006954}
6955
Douglas Gregor20606502011-10-14 15:31:12 +00006956/// \brief Rebuild the template parameters now that we know we're in a current
6957/// instantiation.
6958bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6959 TemplateParameterList *Params) {
6960 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6961 Decl *Param = Params->getParam(I);
6962
6963 // There is nothing to rebuild in a type parameter.
6964 if (isa<TemplateTypeParmDecl>(Param))
6965 continue;
6966
6967 // Rebuild the template parameter list of a template template parameter.
6968 if (TemplateTemplateParmDecl *TTP
6969 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6970 if (RebuildTemplateParamsInCurrentInstantiation(
6971 TTP->getTemplateParameters()))
6972 return true;
6973
6974 continue;
6975 }
6976
6977 // Rebuild the type of a non-type template parameter.
6978 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6979 TypeSourceInfo *NewTSI
6980 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6981 NTTP->getLocation(),
6982 NTTP->getDeclName());
6983 if (!NewTSI)
6984 return true;
6985
6986 if (NewTSI != NTTP->getTypeSourceInfo()) {
6987 NTTP->setTypeSourceInfo(NewTSI);
6988 NTTP->setType(NewTSI->getType());
6989 }
6990 }
6991
6992 return false;
6993}
6994
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006995/// \brief Produces a formatted string that describes the binding of
6996/// template parameters to template arguments.
6997std::string
6998Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6999 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00007000 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00007001}
7002
7003std::string
7004Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
7005 const TemplateArgument *Args,
7006 unsigned NumArgs) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00007007 SmallString<128> Str;
Douglas Gregor87dd6972010-12-20 16:52:59 +00007008 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007009
Douglas Gregor9148c3f2009-11-11 19:13:48 +00007010 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00007011 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007012
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007013 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00007014 if (I >= NumArgs)
7015 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007016
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007017 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00007018 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007019 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00007020 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007021
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007022 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00007023 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007024 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00007025 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007026 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007027
Douglas Gregor87dd6972010-12-20 16:52:59 +00007028 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00007029 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007030 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00007031
7032 Out << ']';
7033 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007034}
Francois Pichet8387e2a2011-04-22 22:18:13 +00007035
7036void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
7037 if (!FD)
7038 return;
7039 FD->setLateTemplateParsed(Flag);
7040}
7041
7042bool Sema::IsInsideALocalClassWithinATemplateFunction() {
7043 DeclContext *DC = CurContext;
7044
7045 while (DC) {
7046 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
7047 const FunctionDecl *FD = RD->isLocalClass();
7048 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
7049 } else if (DC->isTranslationUnit() || DC->isNamespace())
7050 return false;
7051
7052 DC = DC->getParent();
7053 }
7054 return false;
7055}