blob: 35022be1a381e8eecd5c960cad88a993146019d7 [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"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
Douglas Gregor312eadb2011-04-24 05:37:28 +000079void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
Douglas Gregor312eadb2011-04-24 05:37:28 +000085 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
Richard Smith3e4c6c42011-05-05 21:57:07 +000095 // is used as a template-name, the reference refers to the class
96 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +000098 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
99 if (!ClassTemplates.insert(ClassTmpl)) {
100 filter.erase();
101 continue;
102 }
John McCall8ba66912010-08-13 07:02:08 +0000103
104 // FIXME: we promote access to public here as a workaround to
105 // the fact that LookupResult doesn't let us remember that we
106 // found this template through a particular injected class name,
107 // which means we end up doing nasty things to the invariants.
108 // Pretending that access is public is *much* safer.
109 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000110 }
John McCallf7a1a742009-11-24 19:00:30 +0000111 }
112 filter.done();
113}
114
Douglas Gregor312eadb2011-04-24 05:37:28 +0000115bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
116 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
117 if (isAcceptableTemplateName(Context, *I))
118 return true;
119
Douglas Gregor3b887352011-04-27 04:48:22 +0000120 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000121}
122
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000124 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000125 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000126 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000127 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000128 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000129 TemplateTy &TemplateResult,
130 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000131 assert(getLangOptions().CPlusPlus && "No template names in C!");
132
Douglas Gregor014e88d2009-11-03 23:16:33 +0000133 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000134 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000135
Douglas Gregor014e88d2009-11-03 23:16:33 +0000136 switch (Name.getKind()) {
137 case UnqualifiedId::IK_Identifier:
138 TName = DeclarationName(Name.Identifier);
139 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000140
Douglas Gregor014e88d2009-11-03 23:16:33 +0000141 case UnqualifiedId::IK_OperatorFunctionId:
142 TName = Context.DeclarationNames.getCXXOperatorName(
143 Name.OperatorFunctionId.Operator);
144 break;
145
Sean Hunte6252d12009-11-28 08:58:14 +0000146 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000147 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
148 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000149
Douglas Gregor014e88d2009-11-03 23:16:33 +0000150 default:
151 return TNK_Non_template;
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
John McCallb3d87482010-08-24 05:47:05 +0000154 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000156 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000157 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000158 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
159 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000160 if (R.empty()) return TNK_Non_template;
161 if (R.isAmbiguous()) {
162 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000163 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000164
165 // FIXME: we might have ambiguous templates, in which case we
166 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000167 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000168 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000169
John McCall0bd6feb2009-12-02 08:04:21 +0000170 TemplateName Template;
171 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
John McCall0bd6feb2009-12-02 08:04:21 +0000173 unsigned ResultCount = R.end() - R.begin();
174 if (ResultCount > 1) {
175 // We assume that we'll preserve the qualifier from a function
176 // template name in other ways.
177 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
178 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000179
180 // We'll do this lookup again later.
181 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000182 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000183 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
184
185 if (SS.isSet() && !SS.isInvalid()) {
186 NestedNameSpecifier *Qualifier
187 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000188 Template = Context.getQualifiedTemplateName(Qualifier,
189 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000190 } else {
191 Template = TemplateName(TD);
192 }
193
John McCallb8592062010-08-13 02:23:42 +0000194 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000195 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000196
197 // We'll do this lookup again later.
198 R.suppressDiagnostics();
199 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000200 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
201 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000202 TemplateKind = TNK_Type_template;
203 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
John McCall0bd6feb2009-12-02 08:04:21 +0000206 TemplateResult = TemplateTy::make(Template);
207 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000208}
209
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000210bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000211 SourceLocation IILoc,
212 Scope *S,
213 const CXXScopeSpec *SS,
214 TemplateTy &SuggestedTemplate,
215 TemplateNameKind &SuggestedKind) {
216 // We can't recover unless there's a dependent scope specifier preceding the
217 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000218 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000219 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
220 computeDeclContext(*SS))
221 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 // The code is missing a 'template' keyword prior to the dependent template
224 // name.
225 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
226 Diag(IILoc, diag::err_template_kw_missing)
227 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000228 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000229 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000230 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
231 SuggestedKind = TNK_Dependent_template_name;
232 return true;
233}
234
John McCallf7a1a742009-11-24 19:00:30 +0000235void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000236 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000237 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000238 bool EnteringContext,
239 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000240 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000241 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000242 DeclContext *LookupCtx = 0;
243 bool isDependent = false;
244 if (!ObjectType.isNull()) {
245 // This nested-name-specifier occurs in a member access expression, e.g.,
246 // x->B::f, and we are looking into the type of the object.
247 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
248 LookupCtx = computeDeclContext(ObjectType);
249 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000251 "Caller should have completed object type");
Douglas Gregor1d7049a2012-01-12 16:11:24 +0000252
253 // Template names cannot appear inside an Objective-C class or object type.
254 if (ObjectType->isObjCObjectOrInterfaceType()) {
255 Found.clear();
256 return;
257 }
John McCallf7a1a742009-11-24 19:00:30 +0000258 } else if (SS.isSet()) {
259 // This nested-name-specifier occurs after another nested-name-specifier,
260 // so long into the context associated with the prior nested-name-specifier.
261 LookupCtx = computeDeclContext(SS, EnteringContext);
262 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000263
John McCallf7a1a742009-11-24 19:00:30 +0000264 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000265 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000266 return;
267 }
268
269 bool ObjectTypeSearchedInScope = false;
270 if (LookupCtx) {
271 // Perform "qualified" name lookup into the declaration context we
272 // computed, which is either the type of the base of a member access
273 // expression or the declaration context associated with a prior
274 // nested-name-specifier.
275 LookupQualifiedName(Found, LookupCtx);
276
277 if (!ObjectType.isNull() && Found.empty()) {
278 // C++ [basic.lookup.classref]p1:
279 // In a class member access expression (5.2.5), if the . or -> token is
280 // immediately followed by an identifier followed by a <, the
281 // identifier must be looked up to determine whether the < is the
282 // beginning of a template argument list (14.2) or a less-than operator.
283 // The identifier is first looked up in the class of the object
284 // expression. If the identifier is not found, it is then looked up in
285 // the context of the entire postfix-expression and shall name a class
286 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000287 if (S) LookupName(Found, S);
288 ObjectTypeSearchedInScope = true;
289 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000290 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000291 // We cannot look into a dependent object type or nested nme
292 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000293 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000294 return;
295 } else {
296 // Perform unqualified name lookup in the current scope.
297 LookupName(Found, S);
298 }
299
Douglas Gregor2e933882010-01-12 17:06:20 +0000300 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000301 // If we did not find any names, attempt to correct any typos.
302 DeclarationName Name = Found.getLookupName();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000303 Found.clear();
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000304 // Simple filter callback that, for keywords, only accepts the C++ *_cast
305 CorrectionCandidateCallback FilterCCC;
306 FilterCCC.WantTypeSpecifiers = false;
307 FilterCCC.WantExpressionKeywords = false;
308 FilterCCC.WantRemainingKeywords = false;
309 FilterCCC.WantCXXNamedCasts = true;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000310 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
311 Found.getLookupKind(), S, &SS,
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000312 &FilterCCC, LookupCtx)) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000313 Found.setLookupName(Corrected.getCorrection());
314 if (Corrected.getCorrectionDecl())
315 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000316 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000317 if (!Found.empty()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000318 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
319 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000320 if (LookupCtx)
321 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000322 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
323 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000324 else
325 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000326 << Name << CorrectedQuotedStr
327 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000328 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
329 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000330 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000331 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000332 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000333 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000334 }
335 }
336
Douglas Gregor312eadb2011-04-24 05:37:28 +0000337 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000338 if (Found.empty()) {
339 if (isDependent)
340 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000341 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000342 }
John McCallf7a1a742009-11-24 19:00:30 +0000343
344 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
345 // C++ [basic.lookup.classref]p1:
346 // [...] If the lookup in the class of the object expression finds a
347 // template, the name is also looked up in the context of the entire
348 // postfix-expression and [...]
349 //
350 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
351 LookupOrdinaryName);
352 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000353 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000354
John McCallf7a1a742009-11-24 19:00:30 +0000355 if (FoundOuter.empty()) {
356 // - if the name is not found, the name found in the class of the
357 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000358 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
359 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000360 // - if the name is found in the context of the entire
361 // postfix-expression and does not name a class template, the name
362 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000363 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000364 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000365 // - if the name found is a class template, it must refer to the same
366 // entity as the one found in the class of the object expression,
367 // otherwise the program is ill-formed.
368 if (!Found.isSingleResult() ||
369 Found.getFoundDecl()->getCanonicalDecl()
370 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000371 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000372 diag::ext_nested_name_member_ref_lookup_ambiguous)
373 << Found.getLookupName()
374 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000375 Diag(Found.getRepresentativeDecl()->getLocation(),
376 diag::note_ambig_member_ref_object_type)
377 << ObjectType;
378 Diag(FoundOuter.getFoundDecl()->getLocation(),
379 diag::note_ambig_member_ref_scope);
380
381 // Recover by taking the template that we found in the object
382 // expression's type.
383 }
384 }
385 }
386}
387
John McCall2f841ba2009-12-02 03:53:29 +0000388/// ActOnDependentIdExpression - Handle a dependent id-expression that
389/// was just parsed. This is only possible with an explicit scope
390/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000391ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000392Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000393 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000394 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000395 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000396 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000397
John McCall2f841ba2009-12-02 03:53:29 +0000398 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000399 isa<CXXMethodDecl>(DC) &&
400 cast<CXXMethodDecl>(DC)->isInstance()) {
401 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000402
John McCallf7a1a742009-11-24 19:00:30 +0000403 // Since the 'this' expression is synthesized, we don't need to
404 // perform the double-lookup check.
405 NamedDecl *FirstQualifierInScope = 0;
406
John McCallaa81e162009-12-01 22:10:20 +0000407 return Owned(CXXDependentScopeMemberExpr::Create(Context,
408 /*This*/ 0, ThisType,
409 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000410 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000411 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000412 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000413 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000414 TemplateArgs));
415 }
416
Abramo Bagnara25777432010-08-11 22:01:17 +0000417 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000418}
419
John McCall60d7b3a2010-08-24 06:29:42 +0000420ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000421Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000422 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000423 const TemplateArgumentListInfo *TemplateArgs) {
424 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000425 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000426 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000427 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000428}
429
Douglas Gregor72c3f312008-12-05 18:15:24 +0000430/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
431/// that the template parameter 'PrevDecl' is being shadowed by a new
432/// declaration at location Loc. Returns true to indicate that this is
433/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000434void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000435 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000436
437 // Microsoft Visual C++ permits template parameters to be shadowed.
Francois Pichet62ec1f22011-09-17 17:15:52 +0000438 if (getLangOptions().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000439 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000440
441 // C++ [temp.local]p4:
442 // A template-parameter shall not be redeclared within its
443 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000444 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000445 << cast<NamedDecl>(PrevDecl)->getDeclName();
446 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000447 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000448}
449
Douglas Gregor2943aed2009-03-03 04:44:36 +0000450/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000451/// the parameter D to reference the templated declaration and return a pointer
452/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000453TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
454 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
455 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000456 return Temp;
457 }
458 return 0;
459}
460
Douglas Gregorba68eca2011-01-05 17:40:24 +0000461ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
462 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000464 "Only template template arguments can be pack expansions here");
465 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
466 "Template template argument pack expansion without packs");
467 ParsedTemplateArgument Result(*this);
468 Result.EllipsisLoc = EllipsisLoc;
469 return Result;
470}
471
Douglas Gregor788cd062009-11-11 01:00:40 +0000472static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
473 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000474
Douglas Gregor788cd062009-11-11 01:00:40 +0000475 switch (Arg.getKind()) {
476 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000477 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000478 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000479 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000480 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000481 return TemplateArgumentLoc(TemplateArgument(T), DI);
482 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000483
Douglas Gregor788cd062009-11-11 01:00:40 +0000484 case ParsedTemplateArgument::NonType: {
485 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
486 return TemplateArgumentLoc(TemplateArgument(E), E);
487 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000488
Douglas Gregor788cd062009-11-11 01:00:40 +0000489 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000490 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000491 TemplateArgument TArg;
492 if (Arg.getEllipsisLoc().isValid())
493 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
494 else
495 TArg = Template;
496 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000497 Arg.getScopeSpec().getWithLocInContext(
498 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000499 Arg.getLocation(),
500 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000501 }
502 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000503
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000504 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000505 return TemplateArgumentLoc();
506}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000507
Douglas Gregor788cd062009-11-11 01:00:40 +0000508/// \brief Translates template arguments as provided by the parser
509/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000510void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
511 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000512 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000513 TemplateArgs.addArgument(translateTemplateArgument(*this,
514 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000515}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000516
Douglas Gregor72c3f312008-12-05 18:15:24 +0000517/// ActOnTypeParameter - Called when a C++ template type parameter
518/// (e.g., "typename T") has been parsed. Typename specifies whether
519/// the keyword "typename" was used to declare the type parameter
520/// (otherwise, "class" was used), and KeyLoc is the location of the
521/// "class" or "typename" keyword. ParamName is the name of the
522/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000523/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000524/// If the type parameter has a default argument, it will be added
525/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000526Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
527 SourceLocation EllipsisLoc,
528 SourceLocation KeyLoc,
529 IdentifierInfo *ParamName,
530 SourceLocation ParamNameLoc,
531 unsigned Depth, unsigned Position,
532 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000533 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000534 assert(S->isTemplateParamScope() &&
535 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000536 bool Invalid = false;
537
538 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000539 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000540 LookupOrdinaryName,
541 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000542 if (PrevDecl && PrevDecl->isTemplateParameter()) {
543 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
544 PrevDecl = 0;
545 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000546 }
547
Douglas Gregorddc29e12009-02-06 22:42:48 +0000548 SourceLocation Loc = ParamNameLoc;
549 if (!ParamName)
550 Loc = KeyLoc;
551
Douglas Gregor72c3f312008-12-05 18:15:24 +0000552 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000553 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000554 KeyLoc, Loc, Depth, Position, ParamName,
555 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000556 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000557 if (Invalid)
558 Param->setInvalidDecl();
559
560 if (ParamName) {
561 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000562 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000563 IdResolver.AddDecl(Param);
564 }
565
Douglas Gregor61c4d282011-01-05 15:48:55 +0000566 // C++0x [temp.param]p9:
567 // A default template-argument may be specified for any kind of
568 // template-parameter that is not a template parameter pack.
569 if (DefaultArg && Ellipsis) {
570 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
571 DefaultArg = ParsedType();
572 }
573
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000574 // Handle the default argument, if provided.
575 if (DefaultArg) {
576 TypeSourceInfo *DefaultTInfo;
577 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000578
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000579 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000580
Douglas Gregor6f526752010-12-16 08:48:57 +0000581 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000582 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000583 UPPC_DefaultArgument))
584 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000585
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000586 // Check the template argument itself.
587 if (CheckTemplateArgument(Param, DefaultTInfo)) {
588 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000589 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000590 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000591
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000592 Param->setDefaultArgument(DefaultTInfo, false);
593 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000594
John McCalld226f652010-08-21 09:40:31 +0000595 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000596}
597
Douglas Gregor2943aed2009-03-03 04:44:36 +0000598/// \brief Check that the type of a non-type template parameter is
599/// well-formed.
600///
601/// \returns the (possibly-promoted) parameter type if valid;
602/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000603QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000604Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000605 // We don't allow variably-modified types as the type of non-type template
606 // parameters.
607 if (T->isVariablyModifiedType()) {
608 Diag(Loc, diag::err_variably_modified_nontype_template_param)
609 << T;
610 return QualType();
611 }
612
Douglas Gregor2943aed2009-03-03 04:44:36 +0000613 // C++ [temp.param]p4:
614 //
615 // A non-type template-parameter shall have one of the following
616 // (optionally cv-qualified) types:
617 //
618 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000619 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000620 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000621 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000622 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000623 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000624 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000625 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000626 // -- std::nullptr_t.
627 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000628 // If T is a dependent type, we can't do the check now, so we
629 // assume that it is well-formed.
630 T->isDependentType())
631 return T;
632 // C++ [temp.param]p8:
633 //
634 // A non-type template-parameter of type "array of T" or
635 // "function returning T" is adjusted to be of type "pointer to
636 // T" or "pointer to function returning T", respectively.
637 else if (T->isArrayType())
638 // FIXME: Keep the type prior to promotion?
639 return Context.getArrayDecayedType(T);
640 else if (T->isFunctionType())
641 // FIXME: Keep the type prior to promotion?
642 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000643
Douglas Gregor2943aed2009-03-03 04:44:36 +0000644 Diag(Loc, diag::err_template_nontype_parm_bad_type)
645 << T;
646
647 return QualType();
648}
649
John McCalld226f652010-08-21 09:40:31 +0000650Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
651 unsigned Depth,
652 unsigned Position,
653 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000654 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000655 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
656 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000657
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000658 assert(S->isTemplateParamScope() &&
659 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000660 bool Invalid = false;
661
662 IdentifierInfo *ParamName = D.getIdentifier();
663 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000664 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000665 LookupOrdinaryName,
666 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000667 if (PrevDecl && PrevDecl->isTemplateParameter()) {
668 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
669 PrevDecl = 0;
670 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000671 }
672
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000673 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
674 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000675 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000676 Invalid = true;
677 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000678
Douglas Gregor10738d32010-12-23 23:51:58 +0000679 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000680 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000681 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000682 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000683 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000684 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000685 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000686 Param->setAccess(AS_public);
687
Douglas Gregor72c3f312008-12-05 18:15:24 +0000688 if (Invalid)
689 Param->setInvalidDecl();
690
691 if (D.getIdentifier()) {
692 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000693 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000694 IdResolver.AddDecl(Param);
695 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000696
Douglas Gregor61c4d282011-01-05 15:48:55 +0000697 // C++0x [temp.param]p9:
698 // A default template-argument may be specified for any kind of
699 // template-parameter that is not a template parameter pack.
700 if (Default && IsParameterPack) {
701 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
702 Default = 0;
703 }
704
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000705 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000706 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000707 // Check for unexpanded parameter packs.
708 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
709 return Param;
710
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000711 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000712 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
713 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000714 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000715 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000716 }
John Wiegley429bb272011-04-08 18:41:53 +0000717 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000718
John McCall9ae2f072010-08-23 23:25:46 +0000719 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000720 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000721
John McCalld226f652010-08-21 09:40:31 +0000722 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000723}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000724
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000725/// ActOnTemplateTemplateParameter - Called when a C++ template template
726/// parameter (e.g. T in template <template <typename> class T> class array)
727/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000728Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
729 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000730 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000731 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000732 IdentifierInfo *Name,
733 SourceLocation NameLoc,
734 unsigned Depth,
735 unsigned Position,
736 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000737 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000738 assert(S->isTemplateParamScope() &&
739 "Template template parameter not in template parameter scope!");
740
741 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000742 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000743 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000744 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000745 NameLoc.isInvalid()? TmpLoc : NameLoc,
746 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000747 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000748 Param->setAccess(AS_public);
749
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000750 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000751 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000752 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000753 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000754 IdResolver.AddDecl(Param);
755 }
756
Douglas Gregor6f526752010-12-16 08:48:57 +0000757 if (Params->size() == 0) {
758 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
759 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
760 Param->setInvalidDecl();
761 }
762
Douglas Gregor61c4d282011-01-05 15:48:55 +0000763 // C++0x [temp.param]p9:
764 // A default template-argument may be specified for any kind of
765 // template-parameter that is not a template parameter pack.
766 if (IsParameterPack && !Default.isInvalid()) {
767 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
768 Default = ParsedTemplateArgument();
769 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000770
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000771 if (!Default.isInvalid()) {
772 // Check only that we have a template template argument. We don't want to
773 // try to check well-formedness now, because our template template parameter
774 // might have dependent types in its template parameters, which we wouldn't
775 // be able to match now.
776 //
777 // If none of the template template parameter's template arguments mention
778 // other template parameters, we could actually perform more checking here.
779 // However, it isn't worth doing.
780 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
781 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
782 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
783 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000784 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000785 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000786
Douglas Gregor6f526752010-12-16 08:48:57 +0000787 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000788 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000789 DefaultArg.getArgument().getAsTemplate(),
790 UPPC_DefaultArgument))
791 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000792
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000793 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000794 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000795
John McCalld226f652010-08-21 09:40:31 +0000796 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000797}
798
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000799/// ActOnTemplateParameterList - Builds a TemplateParameterList that
800/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000801TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000802Sema::ActOnTemplateParameterList(unsigned Depth,
803 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000804 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000805 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000806 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000807 SourceLocation RAngleLoc) {
808 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000809 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000810
Douglas Gregorddc29e12009-02-06 22:42:48 +0000811 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000812 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000813 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000814}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000815
John McCallb6217662010-03-15 10:12:16 +0000816static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
817 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000818 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000819}
820
John McCallf312b1e2010-08-26 23:41:50 +0000821DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000822Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000823 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000824 IdentifierInfo *Name, SourceLocation NameLoc,
825 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000826 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000827 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000828 unsigned NumOuterTemplateParamLists,
829 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000830 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000831 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000832 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000833 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000834
835 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000836 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000837 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000838
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000839 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
840 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841
842 // There is no such thing as an unnamed class template.
843 if (!Name) {
844 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000845 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000846 }
847
848 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000849 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000850 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000851 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000852 if (SS.isNotEmpty() && !SS.isInvalid()) {
853 SemanticContext = computeDeclContext(SS, true);
854 if (!SemanticContext) {
855 // FIXME: Produce a reasonable diagnostic here
856 return true;
857 }
Mike Stump1eb44332009-09-09 15:08:12 +0000858
John McCall77bb1aa2010-05-01 00:40:08 +0000859 if (RequireCompleteDeclContext(SS, SemanticContext))
860 return true;
861
Douglas Gregor20606502011-10-14 15:31:12 +0000862 // If we're adding a template to a dependent context, we may need to
863 // rebuilding some of the types used within the template parameter list,
864 // now that we know what the current instantiation is.
865 if (SemanticContext->isDependentContext()) {
866 ContextRAII SavedContext(*this, SemanticContext);
867 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
868 Invalid = true;
869 }
870
John McCalla24dc2e2009-11-17 02:14:36 +0000871 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000872 } else {
873 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000874 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Douglas Gregor57265e32010-04-12 16:00:01 +0000877 if (Previous.isAmbiguous())
878 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000879
Douglas Gregorddc29e12009-02-06 22:42:48 +0000880 NamedDecl *PrevDecl = 0;
881 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000882 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000883
Douglas Gregorddc29e12009-02-06 22:42:48 +0000884 // If there is a previous declaration with the same name, check
885 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000886 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000887 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000888
889 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000890 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000891 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000892 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000893 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
894 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000896 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
897 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
898 PrevClassTemplate
899 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
900 ->getSpecializedTemplate();
901 }
902 }
903
John McCall65c49462009-12-18 11:25:59 +0000904 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000905 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000906 // [...] When looking for a prior declaration of a class or a function
907 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000908 // function is neither a qualified name nor a template-id, scopes outside
909 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000910 if (!SS.isSet()) {
911 DeclContext *OutermostContext = CurContext;
912 while (!OutermostContext->isFileContext())
913 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000914
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000915 if (PrevDecl &&
916 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
917 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
918 SemanticContext = PrevDecl->getDeclContext();
919 } else {
920 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000921 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000922 // declaration.
923 PrevDecl = PrevClassTemplate = 0;
924 SemanticContext = OutermostContext;
925 }
John McCalle129d442009-12-17 23:21:11 +0000926 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000927
John McCalle129d442009-12-17 23:21:11 +0000928 if (CurContext->isDependentContext()) {
929 // If this is a dependent context, we don't want to link the friend
930 // class template to the template in scope, because that would perform
931 // checking of the template parameter lists that can't be performed
932 // until the outer context is instantiated.
933 PrevDecl = PrevClassTemplate = 0;
934 }
935 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
936 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000937
Douglas Gregorddc29e12009-02-06 22:42:48 +0000938 if (PrevClassTemplate) {
939 // Ensure that the template parameter lists are compatible.
940 if (!TemplateParameterListsAreEqual(TemplateParams,
941 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000942 /*Complain=*/true,
943 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000944 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000945
946 // C++ [temp.class]p4:
947 // In a redeclaration, partial specialization, explicit
948 // specialization or explicit instantiation of a class template,
949 // the class-key shall agree in kind with the original class
950 // template declaration (7.1.5.3).
951 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000952 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
953 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000954 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000955 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000956 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000957 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000958 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000959 }
960
Douglas Gregorddc29e12009-02-06 22:42:48 +0000961 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000962 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000963 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000964 Diag(NameLoc, diag::err_redefinition) << Name;
965 Diag(Def->getLocation(), diag::note_previous_definition);
966 // FIXME: Would it make sense to try to "forget" the previous
967 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000968 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000969 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000970 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000971 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
972 // Maybe we will complain about the shadowed template parameter.
973 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
974 // Just pretend that we didn't see the previous declaration.
975 PrevDecl = 0;
976 } else if (PrevDecl) {
977 // C++ [temp]p5:
978 // A class template shall not have the same name as any other
979 // template, class, function, object, enumeration, enumerator,
980 // namespace, or type in the same scope (3.3), except as specified
981 // in (14.5.4).
982 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
983 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000984 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000985 }
986
Douglas Gregord684b002009-02-10 19:49:53 +0000987 // Check the template parameter list of this declaration, possibly
988 // merging in the template parameter list from the previous class
989 // template declaration.
990 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000991 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000992 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000993 SemanticContext->isRecord() &&
994 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000995 ? TPC_ClassTemplateMember
996 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000997 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Douglas Gregor57265e32010-04-12 16:00:01 +0000999 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001000 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +00001001 // template out-of-line.
1002 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001003 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +00001004 Diag(NameLoc, diag::err_member_def_does_not_match)
1005 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001006 Invalid = true;
1007 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001008 }
1009
Mike Stump1eb44332009-09-09 15:08:12 +00001010 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001011 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001012 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001013 PrevClassTemplate->getTemplatedDecl() : 0,
1014 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001015 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001016 if (NumOuterTemplateParamLists > 0)
1017 NewClass->setTemplateParameterListsInfo(Context,
1018 NumOuterTemplateParamLists,
1019 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001020
1021 ClassTemplateDecl *NewTemplate
1022 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1023 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001024 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001025 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001026
Douglas Gregor2ccd89c2011-12-20 18:11:52 +00001027 if (ModulePrivateLoc.isValid())
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001028 NewTemplate->setModulePrivate();
Douglas Gregor8d267c52011-09-09 02:06:17 +00001029
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001030 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001031 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001032 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001033 assert(T->isDependentType() && "Class template type is not dependent?");
1034 (void)T;
1035
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001036 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001037 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001038 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001039 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1040 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001041
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001042 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001043 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001044 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Douglas Gregorddc29e12009-02-06 22:42:48 +00001046 // Set the lexical context of these templates
1047 NewClass->setLexicalDeclContext(CurContext);
1048 NewTemplate->setLexicalDeclContext(CurContext);
1049
John McCall0f434ec2009-07-31 02:45:11 +00001050 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001051 NewClass->startDefinition();
1052
1053 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001054 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001055
John McCall05b23ea2009-09-14 21:59:20 +00001056 if (TUK != TUK_Friend)
1057 PushOnScopeChains(NewTemplate, S);
1058 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001059 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001060 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001061 NewClass->setAccess(PrevClassTemplate->getAccess());
1062 }
John McCall05b23ea2009-09-14 21:59:20 +00001063
Douglas Gregord85bea22009-09-26 06:47:28 +00001064 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1065 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001066
John McCall05b23ea2009-09-14 21:59:20 +00001067 // Friend templates are visible in fairly strange ways.
1068 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001069 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001070 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1071 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1072 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001073 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001074 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001075
Douglas Gregord85bea22009-09-26 06:47:28 +00001076 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1077 NewClass->getLocation(),
1078 NewTemplate,
1079 /*FIXME:*/NewClass->getLocation());
1080 Friend->setAccess(AS_public);
1081 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001082 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001083
Douglas Gregord684b002009-02-10 19:49:53 +00001084 if (Invalid) {
1085 NewTemplate->setInvalidDecl();
1086 NewClass->setInvalidDecl();
1087 }
John McCalld226f652010-08-21 09:40:31 +00001088 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001089}
1090
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001091/// \brief Diagnose the presence of a default template argument on a
1092/// template parameter, which is ill-formed in certain contexts.
1093///
1094/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001095static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001096 Sema::TemplateParamListContext TPC,
1097 SourceLocation ParamLoc,
1098 SourceRange DefArgRange) {
1099 switch (TPC) {
1100 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001101 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001102 return false;
1103
1104 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001105 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001106 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001107 // A default template-argument shall not be specified in a
1108 // function template declaration or a function template
1109 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001110 // If a friend function template declaration specifies a default
1111 // template-argument, that declaration shall be a definition and shall be
1112 // the only declaration of the function template in the translation unit.
1113 // (C++98/03 doesn't have this wording; see DR226).
Richard Smithebaf0e62011-10-18 20:49:44 +00001114 S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
1115 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1116 : diag::ext_template_parameter_default_in_function_template)
1117 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001118 return false;
1119
1120 case Sema::TPC_ClassTemplateMember:
1121 // C++0x [temp.param]p9:
1122 // A default template-argument shall not be specified in the
1123 // template-parameter-lists of the definition of a member of a
1124 // class template that appears outside of the member's class.
1125 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1126 << DefArgRange;
1127 return true;
1128
1129 case Sema::TPC_FriendFunctionTemplate:
1130 // C++ [temp.param]p9:
1131 // A default template-argument shall not be specified in a
1132 // friend template declaration.
1133 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1134 << DefArgRange;
1135 return true;
1136
1137 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1138 // for friend function templates if there is only a single
1139 // declaration (and it is a definition). Strange!
1140 }
1141
1142 return false;
1143}
1144
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001145/// \brief Check for unexpanded parameter packs within the template parameters
1146/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001147static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1148 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001149 TemplateParameterList *Params = TTP->getTemplateParameters();
1150 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1151 NamedDecl *P = Params->getParam(I);
1152 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001153 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001154 NTTP->getTypeSourceInfo(),
1155 Sema::UPPC_NonTypeTemplateParameterType))
1156 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001157
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001158 continue;
1159 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001160
1161 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001162 = dyn_cast<TemplateTemplateParmDecl>(P))
1163 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1164 return true;
1165 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001166
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001167 return false;
1168}
1169
Douglas Gregord684b002009-02-10 19:49:53 +00001170/// \brief Checks the validity of a template parameter list, possibly
1171/// considering the template parameter list from a previous
1172/// declaration.
1173///
1174/// If an "old" template parameter list is provided, it must be
1175/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1176/// template parameter list.
1177///
1178/// \param NewParams Template parameter list for a new template
1179/// declaration. This template parameter list will be updated with any
1180/// default arguments that are carried through from the previous
1181/// template parameter list.
1182///
1183/// \param OldParams If provided, template parameter list from a
1184/// previous declaration of the same template. Default template
1185/// arguments will be merged from the old template parameter list to
1186/// the new template parameter list.
1187///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001188/// \param TPC Describes the context in which we are checking the given
1189/// template parameter list.
1190///
Douglas Gregord684b002009-02-10 19:49:53 +00001191/// \returns true if an error occurred, false otherwise.
1192bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001193 TemplateParameterList *OldParams,
1194 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001195 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Douglas Gregord684b002009-02-10 19:49:53 +00001197 // C++ [temp.param]p10:
1198 // The set of default template-arguments available for use with a
1199 // template declaration or definition is obtained by merging the
1200 // default arguments from the definition (if in scope) and all
1201 // declarations in scope in the same way default function
1202 // arguments are (8.3.6).
1203 bool SawDefaultArgument = false;
1204 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001205
Mike Stump1a35fde2009-02-11 23:03:27 +00001206 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001207 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001208 if (OldParams)
1209 OldParam = OldParams->begin();
1210
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001211 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001212 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1213 NewParamEnd = NewParams->end();
1214 NewParam != NewParamEnd; ++NewParam) {
1215 // Variables used to diagnose redundant default arguments
1216 bool RedundantDefaultArg = false;
1217 SourceLocation OldDefaultLoc;
1218 SourceLocation NewDefaultLoc;
1219
David Blaikie1368e582011-10-19 05:19:50 +00001220 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001221 bool MissingDefaultArg = false;
1222
David Blaikie1368e582011-10-19 05:19:50 +00001223 // Variable used to diagnose non-final parameter packs
1224 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001225
Douglas Gregord684b002009-02-10 19:49:53 +00001226 if (TemplateTypeParmDecl *NewTypeParm
1227 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001228 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001229 if (NewTypeParm->hasDefaultArgument() &&
1230 DiagnoseDefaultTemplateArgument(*this, TPC,
1231 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001232 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001233 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001234 NewTypeParm->removeDefaultArgument();
1235
1236 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001237 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001238 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Anders Carlsson49d25572009-06-12 23:20:15 +00001240 if (NewTypeParm->isParameterPack()) {
1241 assert(!NewTypeParm->hasDefaultArgument() &&
1242 "Parameter packs can't have a default argument!");
1243 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001244 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001245 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001246 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1247 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1248 SawDefaultArgument = true;
1249 RedundantDefaultArg = true;
1250 PreviousDefaultArgLoc = NewDefaultLoc;
1251 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1252 // Merge the default argument from the old declaration to the
1253 // new declaration.
1254 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001255 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001256 true);
1257 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1258 } else if (NewTypeParm->hasDefaultArgument()) {
1259 SawDefaultArgument = true;
1260 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1261 } else if (SawDefaultArgument)
1262 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001263 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001264 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001265 // Check for unexpanded parameter packs.
1266 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001267 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001268 UPPC_NonTypeTemplateParameterType)) {
1269 Invalid = true;
1270 continue;
1271 }
1272
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001273 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001274 if (NewNonTypeParm->hasDefaultArgument() &&
1275 DiagnoseDefaultTemplateArgument(*this, TPC,
1276 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001277 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001278 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001279 }
1280
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001281 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001282 NonTypeTemplateParmDecl *OldNonTypeParm
1283 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001284 if (NewNonTypeParm->isParameterPack()) {
1285 assert(!NewNonTypeParm->hasDefaultArgument() &&
1286 "Parameter packs can't have a default argument!");
1287 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001288 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001289 NewNonTypeParm->hasDefaultArgument()) {
1290 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1291 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1292 SawDefaultArgument = true;
1293 RedundantDefaultArg = true;
1294 PreviousDefaultArgLoc = NewDefaultLoc;
1295 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1296 // Merge the default argument from the old declaration to the
1297 // new declaration.
1298 SawDefaultArgument = true;
1299 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001300 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001301 // parameter.
1302 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001303 OldNonTypeParm->getDefaultArgument(),
1304 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001305 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1306 } else if (NewNonTypeParm->hasDefaultArgument()) {
1307 SawDefaultArgument = true;
1308 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1309 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001310 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001311 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001312 TemplateTemplateParmDecl *NewTemplateParm
1313 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001314
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001315 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001316 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001317 Invalid = true;
1318 continue;
1319 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001320
David Blaikie1368e582011-10-19 05:19:50 +00001321 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001322 if (NewTemplateParm->hasDefaultArgument() &&
1323 DiagnoseDefaultTemplateArgument(*this, TPC,
1324 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001325 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001326 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001327
1328 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001329 TemplateTemplateParmDecl *OldTemplateParm
1330 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001331 if (NewTemplateParm->isParameterPack()) {
1332 assert(!NewTemplateParm->hasDefaultArgument() &&
1333 "Parameter packs can't have a default argument!");
1334 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001335 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001336 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001337 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1338 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001339 SawDefaultArgument = true;
1340 RedundantDefaultArg = true;
1341 PreviousDefaultArgLoc = NewDefaultLoc;
1342 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1343 // Merge the default argument from the old declaration to the
1344 // new declaration.
1345 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001346 // FIXME: We need to create a new kind of "default argument" expression
1347 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001348 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001349 OldTemplateParm->getDefaultArgument(),
1350 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001351 PreviousDefaultArgLoc
1352 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001353 } else if (NewTemplateParm->hasDefaultArgument()) {
1354 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001355 PreviousDefaultArgLoc
1356 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001357 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001358 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001359 }
1360
David Blaikie1368e582011-10-19 05:19:50 +00001361 // C++0x [temp.param]p11:
1362 // If a template parameter of a primary class template or alias template
1363 // is a template parameter pack, it shall be the last template parameter.
1364 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1365 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1366 Diag((*NewParam)->getLocation(),
1367 diag::err_template_param_pack_must_be_last_template_parameter);
1368 Invalid = true;
1369 }
1370
Douglas Gregord684b002009-02-10 19:49:53 +00001371 if (RedundantDefaultArg) {
1372 // C++ [temp.param]p12:
1373 // A template-parameter shall not be given default arguments
1374 // by two different declarations in the same scope.
1375 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1376 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1377 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001378 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001379 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001380 // If a template-parameter of a class template has a default
1381 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001382 // have a default template-argument supplied or be a template parameter
1383 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001384 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001385 diag::err_template_param_default_arg_missing);
1386 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1387 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001388 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001389 }
1390
1391 // If we have an old template parameter list that we're merging
1392 // in, move on to the next parameter.
1393 if (OldParams)
1394 ++OldParam;
1395 }
1396
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001397 // We were missing some default arguments at the end of the list, so remove
1398 // all of the default arguments.
1399 if (RemoveDefaultArguments) {
1400 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1401 NewParamEnd = NewParams->end();
1402 NewParam != NewParamEnd; ++NewParam) {
1403 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1404 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001405 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001406 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1407 NTTP->removeDefaultArgument();
1408 else
1409 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1410 }
1411 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001412
Douglas Gregord684b002009-02-10 19:49:53 +00001413 return Invalid;
1414}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001415
John McCall4e2cbb22010-10-20 05:44:58 +00001416namespace {
1417
1418/// A class which looks for a use of a certain level of template
1419/// parameter.
1420struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1421 typedef RecursiveASTVisitor<DependencyChecker> super;
1422
1423 unsigned Depth;
1424 bool Match;
1425
1426 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1427 NamedDecl *ND = Params->getParam(0);
1428 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1429 Depth = PD->getDepth();
1430 } else if (NonTypeTemplateParmDecl *PD =
1431 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1432 Depth = PD->getDepth();
1433 } else {
1434 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1435 }
1436 }
1437
1438 bool Matches(unsigned ParmDepth) {
1439 if (ParmDepth >= Depth) {
1440 Match = true;
1441 return true;
1442 }
1443 return false;
1444 }
1445
1446 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1447 return !Matches(T->getDepth());
1448 }
1449
1450 bool TraverseTemplateName(TemplateName N) {
1451 if (TemplateTemplateParmDecl *PD =
1452 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1453 if (Matches(PD->getDepth())) return false;
1454 return super::TraverseTemplateName(N);
1455 }
1456
1457 bool VisitDeclRefExpr(DeclRefExpr *E) {
1458 if (NonTypeTemplateParmDecl *PD =
1459 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1460 if (PD->getDepth() == Depth) {
1461 Match = true;
1462 return false;
1463 }
1464 }
1465 return super::VisitDeclRefExpr(E);
1466 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001467
1468 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1469 return TraverseType(T->getInjectedSpecializationType());
1470 }
John McCall4e2cbb22010-10-20 05:44:58 +00001471};
1472}
1473
Douglas Gregorc8406492011-05-10 18:27:06 +00001474/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001475/// list.
1476static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001477DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001478 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001479 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001480 return Checker.Match;
1481}
1482
Douglas Gregorc8406492011-05-10 18:27:06 +00001483// Find the source range corresponding to the named type in the given
1484// nested-name-specifier, if any.
1485static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1486 QualType T,
1487 const CXXScopeSpec &SS) {
1488 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1489 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1490 if (const Type *CurType = NNS->getAsType()) {
1491 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1492 return NNSLoc.getTypeLoc().getSourceRange();
1493 } else
1494 break;
1495
1496 NNSLoc = NNSLoc.getPrefix();
1497 }
1498
1499 return SourceRange();
1500}
1501
Mike Stump1eb44332009-09-09 15:08:12 +00001502/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503/// specifier, returning the template parameter list that applies to the
1504/// name.
1505///
1506/// \param DeclStartLoc the start of the declaration that has a scope
1507/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001508///
Douglas Gregorc8406492011-05-10 18:27:06 +00001509/// \param DeclLoc The location of the declaration itself.
1510///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001511/// \param SS the scope specifier that will be matched to the given template
1512/// parameter lists. This scope specifier precedes a qualified name that is
1513/// being declared.
1514///
1515/// \param ParamLists the template parameter lists, from the outermost to the
1516/// innermost template parameter lists.
1517///
1518/// \param NumParamLists the number of template parameter lists in ParamLists.
1519///
John McCall77e8b112010-04-13 20:37:33 +00001520/// \param IsFriend Whether to apply the slightly different rules for
1521/// matching template parameters to scope specifiers in friend
1522/// declarations.
1523///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001524/// \param IsExplicitSpecialization will be set true if the entity being
1525/// declared is an explicit specialization, false otherwise.
1526///
Mike Stump1eb44332009-09-09 15:08:12 +00001527/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001528/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001529/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001530/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001531/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001532/// itself a template).
1533TemplateParameterList *
1534Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001535 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001536 const CXXScopeSpec &SS,
1537 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001538 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001539 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001540 bool &IsExplicitSpecialization,
1541 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001542 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001543 Invalid = false;
1544
1545 // The sequence of nested types to which we will match up the template
1546 // parameter lists. We first build this list by starting with the type named
1547 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001548 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001549 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001550 if (SS.getScopeRep()) {
1551 if (CXXRecordDecl *Record
1552 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1553 T = Context.getTypeDeclType(Record);
1554 else
1555 T = QualType(SS.getScopeRep()->getAsType(), 0);
1556 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001557
1558 // If we found an explicit specialization that prevents us from needing
1559 // 'template<>' headers, this will be set to the location of that
1560 // explicit specialization.
1561 SourceLocation ExplicitSpecLoc;
1562
1563 while (!T.isNull()) {
1564 NestedTypes.push_back(T);
1565
1566 // Retrieve the parent of a record type.
1567 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1568 // If this type is an explicit specialization, we're done.
1569 if (ClassTemplateSpecializationDecl *Spec
1570 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1571 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1572 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1573 ExplicitSpecLoc = Spec->getLocation();
1574 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001575 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001576 } else if (Record->getTemplateSpecializationKind()
1577 == TSK_ExplicitSpecialization) {
1578 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001579 break;
1580 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001581
1582 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1583 T = Context.getTypeDeclType(Parent);
1584 else
1585 T = QualType();
1586 continue;
1587 }
1588
1589 if (const TemplateSpecializationType *TST
1590 = T->getAs<TemplateSpecializationType>()) {
1591 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1592 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1593 T = Context.getTypeDeclType(Parent);
1594 else
1595 T = QualType();
1596 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001597 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001598 }
1599
1600 // Look one step prior in a dependent template specialization type.
1601 if (const DependentTemplateSpecializationType *DependentTST
1602 = T->getAs<DependentTemplateSpecializationType>()) {
1603 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1604 T = QualType(NNS->getAsType(), 0);
1605 else
1606 T = QualType();
1607 continue;
1608 }
1609
1610 // Look one step prior in a dependent name type.
1611 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1612 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1613 T = QualType(NNS->getAsType(), 0);
1614 else
1615 T = QualType();
1616 continue;
1617 }
1618
1619 // Retrieve the parent of an enumeration type.
1620 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1621 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1622 // check here.
1623 EnumDecl *Enum = EnumT->getDecl();
1624
1625 // Get to the parent type.
1626 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1627 T = Context.getTypeDeclType(Parent);
1628 else
1629 T = QualType();
1630 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001631 }
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Douglas Gregorc8406492011-05-10 18:27:06 +00001633 T = QualType();
1634 }
1635 // Reverse the nested types list, since we want to traverse from the outermost
1636 // to the innermost while checking template-parameter-lists.
1637 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001638
Douglas Gregorc8406492011-05-10 18:27:06 +00001639 // C++0x [temp.expl.spec]p17:
1640 // A member or a member template may be nested within many
1641 // enclosing class templates. In an explicit specialization for
1642 // such a member, the member declaration shall be preceded by a
1643 // template<> for each enclosing class template that is
1644 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001645 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001646 unsigned ParamIdx = 0;
1647 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1648 ++TypeIdx) {
1649 T = NestedTypes[TypeIdx];
1650
1651 // Whether we expect a 'template<>' header.
1652 bool NeedEmptyTemplateHeader = false;
1653
1654 // Whether we expect a template header with parameters.
1655 bool NeedNonemptyTemplateHeader = false;
1656
1657 // For a dependent type, the set of template parameters that we
1658 // expect to see.
1659 TemplateParameterList *ExpectedTemplateParams = 0;
1660
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001661 // C++0x [temp.expl.spec]p15:
1662 // A member or a member template may be nested within many enclosing
1663 // class templates. In an explicit specialization for such a member, the
1664 // member declaration shall be preceded by a template<> for each
1665 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001666 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1667 if (ClassTemplatePartialSpecializationDecl *Partial
1668 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1669 ExpectedTemplateParams = Partial->getTemplateParameters();
1670 NeedNonemptyTemplateHeader = true;
1671 } else if (Record->isDependentType()) {
1672 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001673 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001674 ->getTemplateParameters();
1675 NeedNonemptyTemplateHeader = true;
1676 }
1677 } else if (ClassTemplateSpecializationDecl *Spec
1678 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1679 // C++0x [temp.expl.spec]p4:
1680 // Members of an explicitly specialized class template are defined
1681 // in the same manner as members of normal classes, and not using
1682 // the template<> syntax.
1683 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1684 NeedEmptyTemplateHeader = true;
1685 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001686 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001687 } else if (Record->getTemplateSpecializationKind()) {
1688 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001689 != TSK_ExplicitSpecialization &&
1690 TypeIdx == NumTypes - 1)
1691 IsExplicitSpecialization = true;
1692
1693 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001694 }
1695 } else if (const TemplateSpecializationType *TST
1696 = T->getAs<TemplateSpecializationType>()) {
1697 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1698 ExpectedTemplateParams = Template->getTemplateParameters();
1699 NeedNonemptyTemplateHeader = true;
1700 }
1701 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1702 // FIXME: We actually could/should check the template arguments here
1703 // against the corresponding template parameter list.
1704 NeedNonemptyTemplateHeader = false;
1705 }
1706
Douglas Gregor89b9f102011-06-06 15:22:55 +00001707 // C++ [temp.expl.spec]p16:
1708 // In an explicit specialization declaration for a member of a class
1709 // template or a member template that ap- pears in namespace scope, the
1710 // member template and some of its enclosing class templates may remain
1711 // unspecialized, except that the declaration shall not explicitly
1712 // specialize a class member template if its en- closing class templates
1713 // are not explicitly specialized as well.
1714 if (ParamIdx < NumParamLists) {
1715 if (ParamLists[ParamIdx]->size() == 0) {
1716 if (SawNonEmptyTemplateParameterList) {
1717 Diag(DeclLoc, diag::err_specialize_member_of_template)
1718 << ParamLists[ParamIdx]->getSourceRange();
1719 Invalid = true;
1720 IsExplicitSpecialization = false;
1721 return 0;
1722 }
1723 } else
1724 SawNonEmptyTemplateParameterList = true;
1725 }
1726
Douglas Gregorc8406492011-05-10 18:27:06 +00001727 if (NeedEmptyTemplateHeader) {
1728 // If we're on the last of the types, and we need a 'template<>' header
1729 // here, then it's an explicit specialization.
1730 if (TypeIdx == NumTypes - 1)
1731 IsExplicitSpecialization = true;
1732
1733 if (ParamIdx < NumParamLists) {
1734 if (ParamLists[ParamIdx]->size() > 0) {
1735 // The header has template parameters when it shouldn't. Complain.
1736 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1737 diag::err_template_param_list_matches_nontemplate)
1738 << T
1739 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1740 ParamLists[ParamIdx]->getRAngleLoc())
1741 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1742 Invalid = true;
1743 return 0;
1744 }
1745
1746 // Consume this template header.
1747 ++ParamIdx;
1748 continue;
1749 }
1750
1751 if (!IsFriend) {
1752 // We don't have a template header, but we should.
1753 SourceLocation ExpectedTemplateLoc;
1754 if (NumParamLists > 0)
1755 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1756 else
1757 ExpectedTemplateLoc = DeclStartLoc;
1758
1759 Diag(DeclLoc, diag::err_template_spec_needs_header)
1760 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1761 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1762 }
1763
1764 continue;
1765 }
1766
1767 if (NeedNonemptyTemplateHeader) {
1768 // In friend declarations we can have template-ids which don't
1769 // depend on the corresponding template parameter lists. But
1770 // assume that empty parameter lists are supposed to match this
1771 // template-id.
1772 if (IsFriend && T->isDependentType()) {
1773 if (ParamIdx < NumParamLists &&
1774 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1775 ExpectedTemplateParams = 0;
1776 else
1777 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001778 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001779
Douglas Gregorc8406492011-05-10 18:27:06 +00001780 if (ParamIdx < NumParamLists) {
1781 // Check the template parameter list, if we can.
1782 if (ExpectedTemplateParams &&
1783 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1784 ExpectedTemplateParams,
1785 true, TPL_TemplateMatch))
1786 Invalid = true;
1787
1788 if (!Invalid &&
1789 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1790 TPC_ClassTemplateMember))
1791 Invalid = true;
1792
1793 ++ParamIdx;
1794 continue;
1795 }
1796
1797 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1798 << T
1799 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1800 Invalid = true;
1801 continue;
1802 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001803 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001804
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001805 // If there were at least as many template-ids as there were template
1806 // parameter lists, then there are no template parameter lists remaining for
1807 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001808 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001809 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001811 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001812 if (ParamIdx < NumParamLists - 1) {
1813 bool HasAnyExplicitSpecHeader = false;
1814 bool AllExplicitSpecHeaders = true;
1815 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1816 if (ParamLists[I]->size() == 0)
1817 HasAnyExplicitSpecHeader = true;
1818 else
1819 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001820 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001821
1822 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1823 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1824 : diag::err_template_spec_extra_headers)
1825 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1826 ParamLists[NumParamLists - 2]->getRAngleLoc());
1827
1828 // If there was a specialization somewhere, such that 'template<>' is
1829 // not required, and there were any 'template<>' headers, note where the
1830 // specialization occurred.
1831 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1832 Diag(ExplicitSpecLoc,
1833 diag::note_explicit_template_spec_does_not_need_header)
1834 << NestedTypes.back();
1835
1836 // We have a template parameter list with no corresponding scope, which
1837 // means that the resulting template declaration can't be instantiated
1838 // properly (we'll end up with dependent nodes when we shouldn't).
1839 if (!AllExplicitSpecHeaders)
1840 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001841 }
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Douglas Gregor89b9f102011-06-06 15:22:55 +00001843 // C++ [temp.expl.spec]p16:
1844 // In an explicit specialization declaration for a member of a class
1845 // template or a member template that ap- pears in namespace scope, the
1846 // member template and some of its enclosing class templates may remain
1847 // unspecialized, except that the declaration shall not explicitly
1848 // specialize a class member template if its en- closing class templates
1849 // are not explicitly specialized as well.
1850 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1851 SawNonEmptyTemplateParameterList) {
1852 Diag(DeclLoc, diag::err_specialize_member_of_template)
1853 << ParamLists[ParamIdx]->getSourceRange();
1854 Invalid = true;
1855 IsExplicitSpecialization = false;
1856 return 0;
1857 }
1858
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001859 // Return the last template parameter list, which corresponds to the
1860 // entity being declared.
1861 return ParamLists[NumParamLists - 1];
1862}
1863
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001864void Sema::NoteAllFoundTemplates(TemplateName Name) {
1865 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1866 Diag(Template->getLocation(), diag::note_template_declared_here)
1867 << (isa<FunctionTemplateDecl>(Template)? 0
1868 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001869 : isa<TypeAliasTemplateDecl>(Template)? 2
1870 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001871 << Template->getDeclName();
1872 return;
1873 }
1874
1875 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1876 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1877 IEnd = OST->end();
1878 I != IEnd; ++I)
1879 Diag((*I)->getLocation(), diag::note_template_declared_here)
1880 << 0 << (*I)->getDeclName();
1881
1882 return;
1883 }
1884}
1885
1886
Douglas Gregor7532dc62009-03-30 22:58:21 +00001887QualType Sema::CheckTemplateIdType(TemplateName Name,
1888 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001889 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001890 DependentTemplateName *DTN
1891 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001892 if (DTN && DTN->isIdentifier())
1893 // When building a template-id where the template-name is dependent,
1894 // assume the template is a type template. Either our assumption is
1895 // correct, or the code is ill-formed and will be diagnosed when the
1896 // dependent name is substituted.
1897 return Context.getDependentTemplateSpecializationType(ETK_None,
1898 DTN->getQualifier(),
1899 DTN->getIdentifier(),
1900 TemplateArgs);
1901
Douglas Gregor7532dc62009-03-30 22:58:21 +00001902 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001903 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1904 // We might have a substituted template template parameter pack. If so,
1905 // build a template specialization type for it.
1906 if (Name.getAsSubstTemplateTemplateParmPack())
1907 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001908
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001909 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1910 << Name;
1911 NoteAllFoundTemplates(Name);
1912 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001913 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001914
Douglas Gregor40808ce2009-03-09 23:48:35 +00001915 // Check that the template argument list is well-formed for this
1916 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001917 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001918 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001919 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001920 return QualType();
1921
Douglas Gregor910f8002010-11-07 23:05:16 +00001922 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001923 "Converted template argument list is too short!");
1924
1925 QualType CanonType;
1926
Douglas Gregor561f8122011-07-01 01:22:09 +00001927 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001928 if (TypeAliasTemplateDecl *AliasTemplate
1929 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1930 // Find the canonical type for this type alias template specialization.
1931 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1932 if (Pattern->isInvalidDecl())
1933 return QualType();
1934
1935 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1936 Converted.data(), Converted.size());
1937
1938 // Only substitute for the innermost template argument list.
1939 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001940 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001941 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1942 for (unsigned I = 0; I < Depth; ++I)
1943 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001944
1945 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1946 CanonType = SubstType(Pattern->getUnderlyingType(),
1947 TemplateArgLists, AliasTemplate->getLocation(),
1948 AliasTemplate->getDeclName());
1949 if (CanonType.isNull())
1950 return QualType();
1951 } else if (Name.isDependent() ||
1952 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001953 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001954 // This class template specialization is a dependent
1955 // type. Therefore, its canonical type is another class template
1956 // specialization type that contains all of the converted
1957 // arguments in canonical form. This ensures that, e.g., A<T> and
1958 // A<T, T> have identical types when A is declared as:
1959 //
1960 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001961 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001962 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001963 Converted.data(),
1964 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Douglas Gregor1275ae02009-07-28 23:00:59 +00001966 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001967 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001968 // In the future, we need to teach getTemplateSpecializationType to only
1969 // build the canonical type and return that to us.
1970 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001971
1972 // This might work out to be a current instantiation, in which
1973 // case the canonical type needs to be the InjectedClassNameType.
1974 //
1975 // TODO: in theory this could be a simple hashtable lookup; most
1976 // changes to CurContext don't change the set of current
1977 // instantiations.
1978 if (isa<ClassTemplateDecl>(Template)) {
1979 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1980 // If we get out to a namespace, we're done.
1981 if (Ctx->isFileContext()) break;
1982
1983 // If this isn't a record, keep looking.
1984 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1985 if (!Record) continue;
1986
1987 // Look for one of the two cases with InjectedClassNameTypes
1988 // and check whether it's the same template.
1989 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1990 !Record->getDescribedClassTemplate())
1991 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001992
John McCall31f17ec2010-04-27 00:57:59 +00001993 // Fetch the injected class name type and check whether its
1994 // injected type is equal to the type we just built.
1995 QualType ICNT = Context.getTypeDeclType(Record);
1996 QualType Injected = cast<InjectedClassNameType>(ICNT)
1997 ->getInjectedSpecializationType();
1998
1999 if (CanonType != Injected->getCanonicalTypeInternal())
2000 continue;
2001
2002 // If so, the canonical type of this TST is the injected
2003 // class name type of the record we just found.
2004 assert(ICNT.isCanonical());
2005 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002006 break;
2007 }
2008 }
Mike Stump1eb44332009-09-09 15:08:12 +00002009 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002010 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002011 // Find the class template specialization declaration that
2012 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002013 void *InsertPos = 0;
2014 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002015 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002016 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002017 if (!Decl) {
2018 // This is the first time we have referenced this class template
2019 // specialization. Create the canonical declaration and add it to
2020 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002021 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002022 ClassTemplate->getTemplatedDecl()->getTagKind(),
2023 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002024 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002025 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002026 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002027 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002028 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002029 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002030 Decl->setLexicalDeclContext(CurContext);
2031 }
2032
2033 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002034 assert(isa<RecordType>(CanonType) &&
2035 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002036 }
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Douglas Gregor40808ce2009-03-09 23:48:35 +00002038 // Build the fully-sugared type for this class template
2039 // specialization, which refers back to the class template
2040 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002041 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002042}
2043
John McCallf312b1e2010-08-26 23:41:50 +00002044TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002045Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2046 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002047 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002048 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002049 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002050 if (SS.isInvalid())
2051 return true;
2052
Douglas Gregor7532dc62009-03-30 22:58:21 +00002053 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002054
Douglas Gregor40808ce2009-03-09 23:48:35 +00002055 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002056 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002057 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002058
Douglas Gregora88f09f2011-02-28 17:23:35 +00002059 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2060 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2061 DTN->getQualifier(),
2062 DTN->getIdentifier(),
2063 TemplateArgs);
2064
2065 // Build type-source information.
2066 TypeLocBuilder TLB;
2067 DependentTemplateSpecializationTypeLoc SpecTL
2068 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002069 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002070 SpecTL.setNameLoc(TemplateLoc);
2071 SpecTL.setLAngleLoc(LAngleLoc);
2072 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002073 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002074 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2075 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2076 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2077 }
2078
John McCalld5532b62009-11-23 01:53:49 +00002079 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002080 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002081
2082 if (Result.isNull())
2083 return true;
2084
Douglas Gregor059101f2011-03-02 00:47:37 +00002085 // Build type-source information.
2086 TypeLocBuilder TLB;
2087 TemplateSpecializationTypeLoc SpecTL
2088 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2089 SpecTL.setTemplateNameLoc(TemplateLoc);
2090 SpecTL.setLAngleLoc(LAngleLoc);
2091 SpecTL.setRAngleLoc(RAngleLoc);
2092 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2093 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002094
Douglas Gregor059101f2011-03-02 00:47:37 +00002095 if (SS.isNotEmpty()) {
2096 // Create an elaborated-type-specifier containing the nested-name-specifier.
2097 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2098 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2099 ElabTL.setKeywordLoc(SourceLocation());
2100 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2101 }
2102
2103 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002104}
John McCallf1bbbb42009-09-04 01:14:41 +00002105
Douglas Gregor059101f2011-03-02 00:47:37 +00002106TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002107 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002108 SourceLocation TagLoc,
2109 CXXScopeSpec &SS,
2110 TemplateTy TemplateD,
2111 SourceLocation TemplateLoc,
2112 SourceLocation LAngleLoc,
2113 ASTTemplateArgsPtr TemplateArgsIn,
2114 SourceLocation RAngleLoc) {
2115 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2116
2117 // Translate the parser's template argument list in our AST format.
2118 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2119 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2120
2121 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002122 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002123 ElaboratedTypeKeyword Keyword
2124 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Douglas Gregor059101f2011-03-02 00:47:37 +00002126 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2127 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2128 DTN->getQualifier(),
2129 DTN->getIdentifier(),
2130 TemplateArgs);
2131
2132 // Build type-source information.
2133 TypeLocBuilder TLB;
2134 DependentTemplateSpecializationTypeLoc SpecTL
2135 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2136 SpecTL.setKeywordLoc(TagLoc);
2137 SpecTL.setNameLoc(TemplateLoc);
2138 SpecTL.setLAngleLoc(LAngleLoc);
2139 SpecTL.setRAngleLoc(RAngleLoc);
2140 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2141 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2142 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2143 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2144 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002145
2146 if (TypeAliasTemplateDecl *TAT =
2147 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2148 // C++0x [dcl.type.elab]p2:
2149 // If the identifier resolves to a typedef-name or the simple-template-id
2150 // resolves to an alias template specialization, the
2151 // elaborated-type-specifier is ill-formed.
2152 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2153 Diag(TAT->getLocation(), diag::note_declared_at);
2154 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002155
2156 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2157 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002158 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002159
2160 // Check the tag kind
2161 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002162 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002163
John McCall6b2becf2009-09-08 17:47:29 +00002164 IdentifierInfo *Id = D->getIdentifier();
2165 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002166
Richard Trieubbf34c02011-06-10 03:11:26 +00002167 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2168 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002169 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002170 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002171 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002172 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002173 }
2174 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002175
2176 // Provide source-location information for the template specialization.
2177 TypeLocBuilder TLB;
2178 TemplateSpecializationTypeLoc SpecTL
2179 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2180 SpecTL.setTemplateNameLoc(TemplateLoc);
2181 SpecTL.setLAngleLoc(LAngleLoc);
2182 SpecTL.setRAngleLoc(RAngleLoc);
2183 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2184 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002185
Douglas Gregor059101f2011-03-02 00:47:37 +00002186 // Construct an elaborated type containing the nested-name-specifier (if any)
2187 // and keyword.
2188 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2189 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2190 ElabTL.setKeywordLoc(TagLoc);
2191 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2192 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002193}
2194
John McCall60d7b3a2010-08-24 06:29:42 +00002195ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002196 LookupResult &R,
2197 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002198 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002199 // FIXME: Can we do any checking at this point? I guess we could check the
2200 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002201 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002202 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002203 // foo<int> could identify a single function unambiguously
2204 // This approach does NOT work, since f<int>(1);
2205 // gets resolved prior to resorting to overload resolution
2206 // i.e., template<class T> void f(double);
2207 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002208
2209 // These should be filtered out by our callers.
2210 assert(!R.empty() && "empty lookup results when building templateid");
2211 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2212
John McCallc373d482010-01-27 01:50:18 +00002213 // We don't want lookup warnings at this point.
2214 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002215
John McCallf7a1a742009-11-24 19:00:30 +00002216 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002217 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002218 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002219 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002220 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002221 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002222
2223 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002224}
2225
John McCallf7a1a742009-11-24 19:00:30 +00002226// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002227ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002228Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002229 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002230 const TemplateArgumentListInfo &TemplateArgs) {
2231 DeclContext *DC;
2232 if (!(DC = computeDeclContext(SS, false)) ||
2233 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002234 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002235 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002236
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002237 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002238 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002239 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2240 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002241
John McCallf7a1a742009-11-24 19:00:30 +00002242 if (R.isAmbiguous())
2243 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002244
John McCallf7a1a742009-11-24 19:00:30 +00002245 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002246 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2247 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002248 return ExprError();
2249 }
2250
2251 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002252 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2253 << (NestedNameSpecifier*) SS.getScopeRep()
2254 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002255 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2256 return ExprError();
2257 }
2258
2259 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002260}
2261
Douglas Gregorc45c2322009-03-31 00:43:58 +00002262/// \brief Form a dependent template name.
2263///
2264/// This action forms a dependent template name given the template
2265/// name and its (presumably dependent) scope specifier. For
2266/// example, given "MetaFun::template apply", the scope specifier \p
2267/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2268/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002269TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002270 SourceLocation TemplateKWLoc,
2271 CXXScopeSpec &SS,
2272 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002273 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002274 bool EnteringContext,
2275 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002276 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2277 Diag(TemplateKWLoc,
2278 getLangOptions().CPlusPlus0x ?
2279 diag::warn_cxx98_compat_template_outside_of_template :
2280 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002281 << FixItHint::CreateRemoval(TemplateKWLoc);
2282
Douglas Gregor0707bc52010-01-19 16:01:07 +00002283 DeclContext *LookupCtx = 0;
2284 if (SS.isSet())
2285 LookupCtx = computeDeclContext(SS, EnteringContext);
2286 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002287 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002288 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002289 // C++0x [temp.names]p5:
2290 // If a name prefixed by the keyword template is not the name of
2291 // a template, the program is ill-formed. [Note: the keyword
2292 // template may not be applied to non-template members of class
2293 // templates. -end note ] [ Note: as is the case with the
2294 // typename prefix, the template prefix is allowed in cases
2295 // where it is not strictly necessary; i.e., when the
2296 // nested-name-specifier or the expression on the left of the ->
2297 // or . is not dependent on a template-parameter, or the use
2298 // does not appear in the scope of a template. -end note]
2299 //
2300 // Note: C++03 was more strict here, because it banned the use of
2301 // the "template" keyword prior to a template-name that was not a
2302 // dependent name. C++ DR468 relaxed this requirement (the
2303 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002304 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002305 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002306 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2307 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002308 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002309 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2310 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002311 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2312 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002313 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002314 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002315 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002316 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002317 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002318 << Name.getSourceRange()
2319 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002320 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002321 } else {
2322 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002323 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002324 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002325 }
2326
Mike Stump1eb44332009-09-09 15:08:12 +00002327 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002328 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002329
Douglas Gregor014e88d2009-11-03 23:16:33 +00002330 switch (Name.getKind()) {
2331 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002332 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002333 Name.Identifier));
2334 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002335
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002336 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002337 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002338 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002339 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002340
2341 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002342 llvm_unreachable(
2343 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002344
Douglas Gregor014e88d2009-11-03 23:16:33 +00002345 default:
2346 break;
2347 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002348
2349 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002350 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002351 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002352 << Name.getSourceRange()
2353 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002354 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002355}
2356
Mike Stump1eb44332009-09-09 15:08:12 +00002357bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002358 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002359 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002360 const TemplateArgument &Arg = AL.getArgument();
2361
Anders Carlsson436b1562009-06-13 00:33:33 +00002362 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002363 switch(Arg.getKind()) {
2364 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002365 // C++ [temp.arg.type]p1:
2366 // A template-argument for a template-parameter which is a
2367 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002368 break;
2369 case TemplateArgument::Template: {
2370 // We have a template type parameter but the template argument
2371 // is a template without any arguments.
2372 SourceRange SR = AL.getSourceRange();
2373 TemplateName Name = Arg.getAsTemplate();
2374 Diag(SR.getBegin(), diag::err_template_missing_args)
2375 << Name << SR;
2376 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2377 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002378
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002379 return true;
2380 }
2381 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002382 // We have a template type parameter but the template argument
2383 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002384 SourceRange SR = AL.getSourceRange();
2385 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002386 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Anders Carlsson436b1562009-06-13 00:33:33 +00002388 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002389 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002390 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002391
John McCalla93c9342009-12-07 02:54:59 +00002392 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002393 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002394
Anders Carlsson436b1562009-06-13 00:33:33 +00002395 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002396 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2397
2398 // Objective-C ARC:
2399 // If an explicitly-specified template argument type is a lifetime type
2400 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2401 if (getLangOptions().ObjCAutoRefCount &&
2402 ArgType->isObjCLifetimeType() &&
2403 !ArgType.getObjCLifetime()) {
2404 Qualifiers Qs;
2405 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2406 ArgType = Context.getQualifiedType(ArgType, Qs);
2407 }
2408
2409 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002410 return false;
2411}
2412
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002413/// \brief Substitute template arguments into the default template argument for
2414/// the given template type parameter.
2415///
2416/// \param SemaRef the semantic analysis object for which we are performing
2417/// the substitution.
2418///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002419/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002420/// for.
2421///
2422/// \param TemplateLoc the location of the template name that started the
2423/// template-id we are checking.
2424///
2425/// \param RAngleLoc the location of the right angle bracket ('>') that
2426/// terminates the template-id.
2427///
2428/// \param Param the template template parameter whose default we are
2429/// substituting into.
2430///
2431/// \param Converted the list of template arguments provided for template
2432/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002433/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002434static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002435SubstDefaultTemplateArgument(Sema &SemaRef,
2436 TemplateDecl *Template,
2437 SourceLocation TemplateLoc,
2438 SourceLocation RAngleLoc,
2439 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002440 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002441 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002442
2443 // If the argument type is dependent, instantiate it now based
2444 // on the previously-computed template arguments.
2445 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002446 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002447 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002448
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002449 MultiLevelTemplateArgumentList AllTemplateArgs
2450 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2451
2452 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002453 Template, Converted.data(),
2454 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002455 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002456
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002457 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2458 Param->getDefaultArgumentLoc(),
2459 Param->getDeclName());
2460 }
2461
2462 return ArgType;
2463}
2464
2465/// \brief Substitute template arguments into the default template argument for
2466/// the given non-type template parameter.
2467///
2468/// \param SemaRef the semantic analysis object for which we are performing
2469/// the substitution.
2470///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002471/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002472/// for.
2473///
2474/// \param TemplateLoc the location of the template name that started the
2475/// template-id we are checking.
2476///
2477/// \param RAngleLoc the location of the right angle bracket ('>') that
2478/// terminates the template-id.
2479///
Douglas Gregor788cd062009-11-11 01:00:40 +00002480/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002481/// substituting into.
2482///
2483/// \param Converted the list of template arguments provided for template
2484/// parameters that precede \p Param in the template parameter list.
2485///
2486/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002487static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002488SubstDefaultTemplateArgument(Sema &SemaRef,
2489 TemplateDecl *Template,
2490 SourceLocation TemplateLoc,
2491 SourceLocation RAngleLoc,
2492 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002493 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002494 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002495 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002496
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002497 MultiLevelTemplateArgumentList AllTemplateArgs
2498 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002499
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002500 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002501 Template, Converted.data(),
2502 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002503 SourceRange(TemplateLoc, RAngleLoc));
2504
2505 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2506}
2507
Douglas Gregor788cd062009-11-11 01:00:40 +00002508/// \brief Substitute template arguments into the default template argument for
2509/// the given template template parameter.
2510///
2511/// \param SemaRef the semantic analysis object for which we are performing
2512/// the substitution.
2513///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002514/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002515/// for.
2516///
2517/// \param TemplateLoc the location of the template name that started the
2518/// template-id we are checking.
2519///
2520/// \param RAngleLoc the location of the right angle bracket ('>') that
2521/// terminates the template-id.
2522///
2523/// \param Param the template template parameter whose default we are
2524/// substituting into.
2525///
2526/// \param Converted the list of template arguments provided for template
2527/// parameters that precede \p Param in the template parameter list.
2528///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002529/// \param QualifierLoc Will be set to the nested-name-specifier (with
2530/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002531///
Douglas Gregor788cd062009-11-11 01:00:40 +00002532/// \returns the substituted template argument, or NULL if an error occurred.
2533static TemplateName
2534SubstDefaultTemplateArgument(Sema &SemaRef,
2535 TemplateDecl *Template,
2536 SourceLocation TemplateLoc,
2537 SourceLocation RAngleLoc,
2538 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002539 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002540 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002541 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002542 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002543
Douglas Gregor788cd062009-11-11 01:00:40 +00002544 MultiLevelTemplateArgumentList AllTemplateArgs
2545 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002546
Douglas Gregor788cd062009-11-11 01:00:40 +00002547 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002548 Template, Converted.data(),
2549 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002550 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002551
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002552 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002553 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002554 if (QualifierLoc) {
2555 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2556 AllTemplateArgs);
2557 if (!QualifierLoc)
2558 return TemplateName();
2559 }
2560
Douglas Gregor1d752d72011-03-02 18:46:51 +00002561 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002562 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002563 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002564 AllTemplateArgs);
2565}
2566
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002567/// \brief If the given template parameter has a default template
2568/// argument, substitute into that default template argument and
2569/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002570TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002571Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2572 SourceLocation TemplateLoc,
2573 SourceLocation RAngleLoc,
2574 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002575 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002576 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002577 if (!TypeParm->hasDefaultArgument())
2578 return TemplateArgumentLoc();
2579
John McCalla93c9342009-12-07 02:54:59 +00002580 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002581 TemplateLoc,
2582 RAngleLoc,
2583 TypeParm,
2584 Converted);
2585 if (DI)
2586 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2587
2588 return TemplateArgumentLoc();
2589 }
2590
2591 if (NonTypeTemplateParmDecl *NonTypeParm
2592 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2593 if (!NonTypeParm->hasDefaultArgument())
2594 return TemplateArgumentLoc();
2595
John McCall60d7b3a2010-08-24 06:29:42 +00002596 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002597 TemplateLoc,
2598 RAngleLoc,
2599 NonTypeParm,
2600 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002601 if (Arg.isInvalid())
2602 return TemplateArgumentLoc();
2603
2604 Expr *ArgE = Arg.takeAs<Expr>();
2605 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2606 }
2607
2608 TemplateTemplateParmDecl *TempTempParm
2609 = cast<TemplateTemplateParmDecl>(Param);
2610 if (!TempTempParm->hasDefaultArgument())
2611 return TemplateArgumentLoc();
2612
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002613
Douglas Gregor1d752d72011-03-02 18:46:51 +00002614 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002615 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002616 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002617 RAngleLoc,
2618 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002619 Converted,
2620 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002621 if (TName.isNull())
2622 return TemplateArgumentLoc();
2623
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002624 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002625 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002626 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2627}
2628
Douglas Gregore7526412009-11-11 19:31:23 +00002629/// \brief Check that the given template argument corresponds to the given
2630/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002631///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002632/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002633/// checked.
2634///
2635/// \param Arg The template argument.
2636///
2637/// \param Template The template in which the template argument resides.
2638///
2639/// \param TemplateLoc The location of the template name for the template
2640/// whose argument list we're matching.
2641///
2642/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2643/// the template argument list.
2644///
2645/// \param ArgumentPackIndex The index into the argument pack where this
2646/// argument will be placed. Only valid if the parameter is a parameter pack.
2647///
2648/// \param Converted The checked, converted argument will be added to the
2649/// end of this small vector.
2650///
2651/// \param CTAK Describes how we arrived at this particular template argument:
2652/// explicitly written, deduced, etc.
2653///
2654/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002655bool Sema::CheckTemplateArgument(NamedDecl *Param,
2656 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002657 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002658 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002659 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002660 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002661 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002662 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002663 // Check template type parameters.
2664 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002665 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002666
Douglas Gregord9e15302009-11-11 19:41:09 +00002667 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002668 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002669 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002670 // with the template arguments we've seen thus far. But if the
2671 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002672 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002673 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2674 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002675
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002676 if (NTTPType->isDependentType() &&
2677 !isa<TemplateTemplateParmDecl>(Template) &&
2678 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002679 // Do substitution on the type of the non-type template parameter.
2680 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002681 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002682 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002683
2684 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002685 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002686 NTTPType = SubstType(NTTPType,
2687 MultiLevelTemplateArgumentList(TemplateArgs),
2688 NTTP->getLocation(),
2689 NTTP->getDeclName());
2690 // If that worked, check the non-type template parameter type
2691 // for validity.
2692 if (!NTTPType.isNull())
2693 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2694 NTTP->getLocation());
2695 if (NTTPType.isNull())
2696 return true;
2697 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002698
Douglas Gregore7526412009-11-11 19:31:23 +00002699 switch (Arg.getArgument().getKind()) {
2700 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002701 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002702
Douglas Gregore7526412009-11-11 19:31:23 +00002703 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002704 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002705 ExprResult Res =
2706 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2707 Result, CTAK);
2708 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002709 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002710
Douglas Gregor910f8002010-11-07 23:05:16 +00002711 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002712 break;
2713 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002714
Douglas Gregore7526412009-11-11 19:31:23 +00002715 case TemplateArgument::Declaration:
2716 case TemplateArgument::Integral:
2717 // We've already checked this template argument, so just copy
2718 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002719 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002720 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002721
Douglas Gregore7526412009-11-11 19:31:23 +00002722 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002723 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002724 // We were given a template template argument. It may not be ill-formed;
2725 // see below.
2726 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002727 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2728 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002729 // We have a template argument such as \c T::template X, which we
2730 // parsed as a template template argument. However, since we now
2731 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002732 // template name into an expression.
2733
2734 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2735 Arg.getTemplateNameLoc());
2736
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002737 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002738 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002739 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002740 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002741 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002742
Douglas Gregora7fc9012011-01-05 18:58:31 +00002743 // If we parsed the template argument as a pack expansion, create a
2744 // pack expansion expression.
2745 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002746 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2747 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002748 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002749 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002750
Douglas Gregore7526412009-11-11 19:31:23 +00002751 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002752 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2753 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002754 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002755
Douglas Gregor910f8002010-11-07 23:05:16 +00002756 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002757 break;
2758 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002759
Douglas Gregore7526412009-11-11 19:31:23 +00002760 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002761 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002762 // therefore cannot be a non-type template argument.
2763 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2764 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002765
Douglas Gregore7526412009-11-11 19:31:23 +00002766 Diag(Param->getLocation(), diag::note_template_param_here);
2767 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002768
Douglas Gregore7526412009-11-11 19:31:23 +00002769 case TemplateArgument::Type: {
2770 // We have a non-type template parameter but the template
2771 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002772
Douglas Gregore7526412009-11-11 19:31:23 +00002773 // C++ [temp.arg]p2:
2774 // In a template-argument, an ambiguity between a type-id and
2775 // an expression is resolved to a type-id, regardless of the
2776 // form of the corresponding template-parameter.
2777 //
2778 // We warn specifically about this case, since it can be rather
2779 // confusing for users.
2780 QualType T = Arg.getArgument().getAsType();
2781 SourceRange SR = Arg.getSourceRange();
2782 if (T->isFunctionType())
2783 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2784 else
2785 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2786 Diag(Param->getLocation(), diag::note_template_param_here);
2787 return true;
2788 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789
Douglas Gregore7526412009-11-11 19:31:23 +00002790 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002791 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002792 break;
2793 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002794
Douglas Gregore7526412009-11-11 19:31:23 +00002795 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002796 }
2797
2798
Douglas Gregore7526412009-11-11 19:31:23 +00002799 // Check template template parameters.
2800 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002801
Douglas Gregore7526412009-11-11 19:31:23 +00002802 // Substitute into the template parameter list of the template
2803 // template parameter, since previously-supplied template arguments
2804 // may appear within the template template parameter.
2805 {
2806 // Set up a template instantiation context.
2807 LocalInstantiationScope Scope(*this);
2808 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002809 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002810 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002811
2812 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002813 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002814 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002815 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002816 MultiLevelTemplateArgumentList(TemplateArgs)));
2817 if (!TempParm)
2818 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002819 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002820
Douglas Gregore7526412009-11-11 19:31:23 +00002821 switch (Arg.getArgument().getKind()) {
2822 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002823 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002824
Douglas Gregore7526412009-11-11 19:31:23 +00002825 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002826 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002827 if (CheckTemplateArgument(TempParm, Arg))
2828 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002829
Douglas Gregor910f8002010-11-07 23:05:16 +00002830 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002831 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002832
Douglas Gregore7526412009-11-11 19:31:23 +00002833 case TemplateArgument::Expression:
2834 case TemplateArgument::Type:
2835 // We have a template template parameter but the template
2836 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002837 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2838 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002839 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002840
Douglas Gregore7526412009-11-11 19:31:23 +00002841 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002842 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002843 "Declaration argument with template template parameter");
2844 break;
2845 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002846 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002847 "Integral argument with template template parameter");
2848 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002849
Douglas Gregore7526412009-11-11 19:31:23 +00002850 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002851 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002852 break;
2853 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002854
Douglas Gregore7526412009-11-11 19:31:23 +00002855 return false;
2856}
2857
Douglas Gregorc15cb382009-02-09 23:23:08 +00002858/// \brief Check that the given template argument list is well-formed
2859/// for specializing the given template.
2860bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2861 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002862 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002863 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002864 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002865 TemplateParameterList *Params = Template->getTemplateParameters();
2866 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002867 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002868 bool Invalid = false;
2869
John McCalld5532b62009-11-23 01:53:49 +00002870 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2871
Mike Stump1eb44332009-09-09 15:08:12 +00002872 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002873 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002875 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002876 (NumArgs < Params->getMinRequiredArguments() &&
2877 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002878 // FIXME: point at either the first arg beyond what we can handle,
2879 // or the '>', depending on whether we have too many or too few
2880 // arguments.
2881 SourceRange Range;
2882 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002883 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002884 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2885 << (NumArgs > NumParams)
2886 << (isa<ClassTemplateDecl>(Template)? 0 :
2887 isa<FunctionTemplateDecl>(Template)? 1 :
2888 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2889 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002890 Diag(Template->getLocation(), diag::note_template_decl_here)
2891 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002892 Invalid = true;
2893 }
Mike Stump1eb44332009-09-09 15:08:12 +00002894
2895 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002896 // [...] The type and form of each template-argument specified in
2897 // a template-id shall match the type and form specified for the
2898 // corresponding parameter declared by the template in its
2899 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002900 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002901 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002902 TemplateParameterList::iterator Param = Params->begin(),
2903 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002904 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002905 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002906 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002907 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002908 // If we have an expanded parameter pack, make sure we don't have too
2909 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002910 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002911 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002912 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002913 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2914 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2915 << true
2916 << (isa<ClassTemplateDecl>(Template)? 0 :
2917 isa<FunctionTemplateDecl>(Template)? 1 :
2918 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2919 << Template;
2920 Diag(Template->getLocation(), diag::note_template_decl_here)
2921 << Params->getSourceRange();
2922 return true;
2923 }
2924 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002925
Douglas Gregorf35f8282009-11-11 21:54:23 +00002926 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002927 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2928 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002929 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002930 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002931
Douglas Gregor14be16b2010-12-20 16:57:52 +00002932 if ((*Param)->isTemplateParameterPack()) {
2933 // The template parameter was a template parameter pack, so take the
2934 // deduced argument and place it on the argument pack. Note that we
2935 // stay on the same template parameter so that we can deduce more
2936 // arguments.
2937 ArgumentPack.push_back(Converted.back());
2938 Converted.pop_back();
2939 } else {
2940 // Move to the next template parameter.
2941 ++Param;
2942 }
2943 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002944 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002945 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002946
Douglas Gregor8735b292011-06-03 02:59:40 +00002947 // If we're checking a partial template argument list, we're done.
2948 if (PartialTemplateArgs) {
2949 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2950 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2951 ArgumentPack.data(),
2952 ArgumentPack.size()));
2953
2954 return Invalid;
2955 }
2956
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002957 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002958 // arguments, just break out now and we'll fill in the argument pack below.
2959 if ((*Param)->isTemplateParameterPack())
2960 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002961
Douglas Gregorf35f8282009-11-11 21:54:23 +00002962 // We have a default template argument that we will use.
2963 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002964
Douglas Gregorf35f8282009-11-11 21:54:23 +00002965 // Retrieve the default template argument from the template
2966 // parameter. For each kind of template parameter, we substitute the
2967 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002968 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002969 // the default argument.
2970 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2971 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002972 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002973 break;
2974 }
2975
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002976 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002977 Template,
2978 TemplateLoc,
2979 RAngleLoc,
2980 TTP,
2981 Converted);
2982 if (!ArgType)
2983 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002984
Douglas Gregorf35f8282009-11-11 21:54:23 +00002985 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2986 ArgType);
2987 } else if (NonTypeTemplateParmDecl *NTTP
2988 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2989 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002990 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002991 break;
2992 }
2993
John McCall60d7b3a2010-08-24 06:29:42 +00002994 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002995 TemplateLoc,
2996 RAngleLoc,
2997 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002998 Converted);
2999 if (E.isInvalid())
3000 return true;
3001
3002 Expr *Ex = E.takeAs<Expr>();
3003 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3004 } else {
3005 TemplateTemplateParmDecl *TempParm
3006 = cast<TemplateTemplateParmDecl>(*Param);
3007
3008 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003009 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00003010 break;
3011 }
3012
Douglas Gregor1d752d72011-03-02 18:46:51 +00003013 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003014 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003015 TemplateLoc,
3016 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003017 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003018 Converted,
3019 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003020 if (Name.isNull())
3021 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003022
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003023 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3024 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003025 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003026
Douglas Gregorf35f8282009-11-11 21:54:23 +00003027 // Introduce an instantiation record that describes where we are using
3028 // the default template argument.
3029 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003030 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031 SourceRange(TemplateLoc, RAngleLoc));
3032
Douglas Gregorf35f8282009-11-11 21:54:23 +00003033 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003034 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003035 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003036 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003037
Douglas Gregor67714232011-03-03 02:41:12 +00003038 // Core issue 150 (assumed resolution): if this is a template template
3039 // parameter, keep track of the default template arguments from the
3040 // template definition.
3041 if (isTemplateTemplateParameter)
3042 TemplateArgs.addArgument(Arg);
3043
Douglas Gregor14be16b2010-12-20 16:57:52 +00003044 // Move to the next template parameter and argument.
3045 ++Param;
3046 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003047 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003048
Douglas Gregor14be16b2010-12-20 16:57:52 +00003049 // Form argument packs for each of the parameter packs remaining.
3050 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003051 // If we're checking a partial list of template arguments, don't fill
3052 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003053
3054 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003055 if (!HasParameterPack)
3056 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003057 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003058 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003059 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003060 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3061 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003062 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003063 ArgumentPack.clear();
3064 }
3065 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003066
Douglas Gregor14be16b2010-12-20 16:57:52 +00003067 ++Param;
3068 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003069
Douglas Gregorc15cb382009-02-09 23:23:08 +00003070 return Invalid;
3071}
3072
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003073namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003074 class UnnamedLocalNoLinkageFinder
3075 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003076 {
3077 Sema &S;
3078 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003079
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003080 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003081
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003082 public:
3083 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3084
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003085 bool Visit(QualType T) {
3086 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003087 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003088
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003089#define TYPE(Class, Parent) \
3090 bool Visit##Class##Type(const Class##Type *);
3091#define ABSTRACT_TYPE(Class, Parent) \
3092 bool Visit##Class##Type(const Class##Type *) { return false; }
3093#define NON_CANONICAL_TYPE(Class, Parent) \
3094 bool Visit##Class##Type(const Class##Type *) { return false; }
3095#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003096
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003097 bool VisitTagDecl(const TagDecl *Tag);
3098 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3099 };
3100}
3101
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003102bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003103 return false;
3104}
3105
3106bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3107 return Visit(T->getElementType());
3108}
3109
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003110bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003111 return Visit(T->getPointeeType());
3112}
3113
3114bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003115 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003116 return Visit(T->getPointeeType());
3117}
3118
3119bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003120 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003121 return Visit(T->getPointeeType());
3122}
3123
3124bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003125 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003126 return Visit(T->getPointeeType());
3127}
3128
3129bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003130 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003131 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3132}
3133
3134bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003135 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003136 return Visit(T->getElementType());
3137}
3138
3139bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003140 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003141 return Visit(T->getElementType());
3142}
3143
3144bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003145 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003146 return Visit(T->getElementType());
3147}
3148
3149bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003150 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003151 return Visit(T->getElementType());
3152}
3153
3154bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003155 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003156 return Visit(T->getElementType());
3157}
3158
3159bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3160 return Visit(T->getElementType());
3161}
3162
3163bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3164 return Visit(T->getElementType());
3165}
3166
3167bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3168 const FunctionProtoType* T) {
3169 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003170 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003171 A != AEnd; ++A) {
3172 if (Visit(*A))
3173 return true;
3174 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003175
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003176 return Visit(T->getResultType());
3177}
3178
3179bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3180 const FunctionNoProtoType* T) {
3181 return Visit(T->getResultType());
3182}
3183
3184bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3185 const UnresolvedUsingType*) {
3186 return false;
3187}
3188
3189bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3190 return false;
3191}
3192
3193bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3194 return Visit(T->getUnderlyingType());
3195}
3196
3197bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3198 return false;
3199}
3200
Sean Huntca63c202011-05-24 22:41:36 +00003201bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3202 const UnaryTransformType*) {
3203 return false;
3204}
3205
Richard Smith34b41d92011-02-20 03:19:35 +00003206bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3207 return Visit(T->getDeducedType());
3208}
3209
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003210bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3211 return VisitTagDecl(T->getDecl());
3212}
3213
3214bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3215 return VisitTagDecl(T->getDecl());
3216}
3217
3218bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3219 const TemplateTypeParmType*) {
3220 return false;
3221}
3222
Douglas Gregorc3069d62011-01-14 02:55:32 +00003223bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3224 const SubstTemplateTypeParmPackType *) {
3225 return false;
3226}
3227
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003228bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3229 const TemplateSpecializationType*) {
3230 return false;
3231}
3232
3233bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3234 const InjectedClassNameType* T) {
3235 return VisitTagDecl(T->getDecl());
3236}
3237
3238bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3239 const DependentNameType* T) {
3240 return VisitNestedNameSpecifier(T->getQualifier());
3241}
3242
3243bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3244 const DependentTemplateSpecializationType* T) {
3245 return VisitNestedNameSpecifier(T->getQualifier());
3246}
3247
Douglas Gregor7536dd52010-12-20 02:24:11 +00003248bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3249 const PackExpansionType* T) {
3250 return Visit(T->getPattern());
3251}
3252
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003253bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3254 return false;
3255}
3256
3257bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3258 const ObjCInterfaceType *) {
3259 return false;
3260}
3261
3262bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3263 const ObjCObjectPointerType *) {
3264 return false;
3265}
3266
Eli Friedmanb001de72011-10-06 23:00:33 +00003267bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3268 return Visit(T->getValueType());
3269}
3270
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003271bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3272 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003273 S.Diag(SR.getBegin(),
3274 S.getLangOptions().CPlusPlus0x ?
3275 diag::warn_cxx98_compat_template_arg_local_type :
3276 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003277 << S.Context.getTypeDeclType(Tag) << SR;
3278 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003279 }
3280
Richard Smith162e1c12011-04-15 14:24:37 +00003281 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003282 S.Diag(SR.getBegin(),
3283 S.getLangOptions().CPlusPlus0x ?
3284 diag::warn_cxx98_compat_template_arg_unnamed_type :
3285 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003286 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3287 return true;
3288 }
3289
3290 return false;
3291}
3292
3293bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3294 NestedNameSpecifier *NNS) {
3295 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3296 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003297
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003298 switch (NNS->getKind()) {
3299 case NestedNameSpecifier::Identifier:
3300 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003301 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003302 case NestedNameSpecifier::Global:
3303 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003304
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003305 case NestedNameSpecifier::TypeSpec:
3306 case NestedNameSpecifier::TypeSpecWithTemplate:
3307 return Visit(QualType(NNS->getAsType(), 0));
3308 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003309 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003310}
3311
3312
Douglas Gregorc15cb382009-02-09 23:23:08 +00003313/// \brief Check a template argument against its corresponding
3314/// template type parameter.
3315///
3316/// This routine implements the semantics of C++ [temp.arg.type]. It
3317/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003318bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003319 TypeSourceInfo *ArgInfo) {
3320 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003321 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003322 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003323
3324 if (Arg->isVariablyModifiedType()) {
3325 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003326 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003327 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003328 }
3329
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003330 // C++03 [temp.arg.type]p2:
3331 // A local type, a type with no linkage, an unnamed type or a type
3332 // compounded from any of these types shall not be used as a
3333 // template-argument for a template type-parameter.
3334 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003335 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003336 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003337 if (LangOpts.CPlusPlus0x ?
3338 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3339 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3340 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3341 SR.getBegin()) != DiagnosticsEngine::Ignored :
3342 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003343 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3344 (void)Finder.Visit(Context.getCanonicalType(Arg));
3345 }
3346
Douglas Gregorc15cb382009-02-09 23:23:08 +00003347 return false;
3348}
3349
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003350/// \brief Checks whether the given template argument is the address
3351/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003352static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003353CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3354 NonTypeTemplateParmDecl *Param,
3355 QualType ParamType,
3356 Expr *ArgIn,
3357 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003358 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003359 Expr *Arg = ArgIn;
3360 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003361
3362 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003363 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003364
3365 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003366 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003367 // A template-argument for a non-type, non-template
3368 // template-parameter shall be one of: [...]
3369 //
3370 // -- the address of an object or function with external
3371 // linkage, including function templates and function
3372 // template-ids but excluding non-static class members,
3373 // expressed as & id-expression where the & is optional if
3374 // the name refers to a function or array, or if the
3375 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003376
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003377 // In C++98/03 mode, give an extension warning on any extra parentheses.
3378 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3379 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003380 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003381 if (!Invalid && !ExtraParens) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003382 S.Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003383 S.getLangOptions().CPlusPlus0x ?
3384 diag::warn_cxx98_compat_template_arg_extra_parens :
3385 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003386 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003387 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003388 }
3389
3390 Arg = Parens->getSubExpr();
3391 }
3392
John McCall91a57552011-07-15 05:09:51 +00003393 while (SubstNonTypeTemplateParmExpr *subst =
3394 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3395 Arg = subst->getReplacement()->IgnoreImpCasts();
3396
Douglas Gregorb7a09262010-04-01 18:32:35 +00003397 bool AddressTaken = false;
3398 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003399 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003400 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003401 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003402 AddressTaken = true;
3403 AddrOpLoc = UnOp->getOperatorLoc();
3404 }
Francois Picheta343a412011-04-29 09:08:14 +00003405 }
John McCall91a57552011-07-15 05:09:51 +00003406
Francois Pichet62ec1f22011-09-17 17:15:52 +00003407 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003408 Converted = TemplateArgument(ArgIn);
3409 return false;
3410 }
3411
3412 while (SubstNonTypeTemplateParmExpr *subst =
3413 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3414 Arg = subst->getReplacement()->IgnoreImpCasts();
3415
3416 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003417 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003418 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3419 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003420 S.Diag(Param->getLocation(), diag::note_template_param_here);
3421 return true;
3422 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003423
3424 // Stop checking the precise nature of the argument if it is value dependent,
3425 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003426 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003427 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003428 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003429 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003430
Douglas Gregorb7a09262010-04-01 18:32:35 +00003431 if (!isa<ValueDecl>(DRE->getDecl())) {
3432 S.Diag(Arg->getSourceRange().getBegin(),
3433 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003434 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003435 S.Diag(Param->getLocation(), diag::note_template_param_here);
3436 return true;
3437 }
3438
3439 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003440
3441 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003442 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3443 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003444 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003445 S.Diag(Param->getLocation(), diag::note_template_param_here);
3446 return true;
3447 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003448
3449 // Cannot refer to non-static member functions
3450 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003451 if (!Method->isStatic()) {
3452 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003453 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003454 S.Diag(Param->getLocation(), diag::note_template_param_here);
3455 return true;
3456 }
Mike Stump1eb44332009-09-09 15:08:12 +00003457
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003458 // Functions must have external linkage.
3459 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003460 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003461 S.Diag(Arg->getSourceRange().getBegin(),
3462 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003463 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003464 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003465 << true;
3466 return true;
3467 }
3468
3469 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003470 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003471
Douglas Gregorb7a09262010-04-01 18:32:35 +00003472 // If the template parameter has pointer type, the function decays.
3473 if (ParamType->isPointerType() && !AddressTaken)
3474 ArgType = S.Context.getPointerType(Func->getType());
3475 else if (AddressTaken && ParamType->isReferenceType()) {
3476 // If we originally had an address-of operator, but the
3477 // parameter has reference type, complain and (if things look
3478 // like they will work) drop the address-of operator.
3479 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3480 ParamType.getNonReferenceType())) {
3481 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3482 << ParamType;
3483 S.Diag(Param->getLocation(), diag::note_template_param_here);
3484 return true;
3485 }
3486
3487 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3488 << ParamType
3489 << FixItHint::CreateRemoval(AddrOpLoc);
3490 S.Diag(Param->getLocation(), diag::note_template_param_here);
3491
3492 ArgType = Func->getType();
3493 }
3494 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003495 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003496 S.Diag(Arg->getSourceRange().getBegin(),
3497 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003498 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003499 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003500 << true;
3501 return true;
3502 }
3503
Douglas Gregorb7a09262010-04-01 18:32:35 +00003504 // A value of reference type is not an object.
3505 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003506 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003507 diag::err_template_arg_reference_var)
3508 << Var->getType() << Arg->getSourceRange();
3509 S.Diag(Param->getLocation(), diag::note_template_param_here);
3510 return true;
3511 }
3512
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003513 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003514 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003515
3516 // If the template parameter has pointer type, we must have taken
3517 // the address of this object.
3518 if (ParamType->isReferenceType()) {
3519 if (AddressTaken) {
3520 // If we originally had an address-of operator, but the
3521 // parameter has reference type, complain and (if things look
3522 // like they will work) drop the address-of operator.
3523 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3524 ParamType.getNonReferenceType())) {
3525 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3526 << ParamType;
3527 S.Diag(Param->getLocation(), diag::note_template_param_here);
3528 return true;
3529 }
3530
3531 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3532 << ParamType
3533 << FixItHint::CreateRemoval(AddrOpLoc);
3534 S.Diag(Param->getLocation(), diag::note_template_param_here);
3535
3536 ArgType = Var->getType();
3537 }
3538 } else if (!AddressTaken && ParamType->isPointerType()) {
3539 if (Var->getType()->isArrayType()) {
3540 // Array-to-pointer decay.
3541 ArgType = S.Context.getArrayDecayedType(Var->getType());
3542 } else {
3543 // If the template parameter has pointer type but the address of
3544 // this object was not taken, complain and (possibly) recover by
3545 // taking the address of the entity.
3546 ArgType = S.Context.getPointerType(Var->getType());
3547 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3548 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3549 << ParamType;
3550 S.Diag(Param->getLocation(), diag::note_template_param_here);
3551 return true;
3552 }
3553
3554 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3555 << ParamType
3556 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3557
3558 S.Diag(Param->getLocation(), diag::note_template_param_here);
3559 }
3560 }
3561 } else {
3562 // We found something else, but we don't know specifically what it is.
3563 S.Diag(Arg->getSourceRange().getBegin(),
3564 diag::err_template_arg_not_object_or_func)
3565 << Arg->getSourceRange();
3566 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3567 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003568 }
Mike Stump1eb44332009-09-09 15:08:12 +00003569
John McCallf85e1932011-06-15 23:02:42 +00003570 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003571 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003572 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003573 S.IsQualificationConversion(ArgType, ParamType, false,
3574 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003575 // For pointer-to-object types, qualification conversions are
3576 // permitted.
3577 } else {
3578 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3579 if (!ParamRef->getPointeeType()->isFunctionType()) {
3580 // C++ [temp.arg.nontype]p5b3:
3581 // For a non-type template-parameter of type reference to
3582 // object, no conversions apply. The type referred to by the
3583 // reference may be more cv-qualified than the (otherwise
3584 // identical) type of the template- argument. The
3585 // template-parameter is bound directly to the
3586 // template-argument, which shall be an lvalue.
3587
3588 // FIXME: Other qualifiers?
3589 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3590 unsigned ArgQuals = ArgType.getCVRQualifiers();
3591
3592 if ((ParamQuals | ArgQuals) != ParamQuals) {
3593 S.Diag(Arg->getSourceRange().getBegin(),
3594 diag::err_template_arg_ref_bind_ignores_quals)
3595 << ParamType << Arg->getType()
3596 << Arg->getSourceRange();
3597 S.Diag(Param->getLocation(), diag::note_template_param_here);
3598 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003599 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003600 }
3601 }
3602
3603 // At this point, the template argument refers to an object or
3604 // function with external linkage. We now need to check whether the
3605 // argument and parameter types are compatible.
3606 if (!S.Context.hasSameUnqualifiedType(ArgType,
3607 ParamType.getNonReferenceType())) {
3608 // We can't perform this conversion or binding.
3609 if (ParamType->isReferenceType())
3610 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003611 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003612 else
3613 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003614 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003615 S.Diag(Param->getLocation(), diag::note_template_param_here);
3616 return true;
3617 }
3618 }
3619
3620 // Create the template argument.
3621 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003622 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003623 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003624}
3625
3626/// \brief Checks whether the given template argument is a pointer to
3627/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003628bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003629 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003630 bool Invalid = false;
3631
3632 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003633 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003634 Arg = Cast->getSubExpr();
3635
3636 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003637 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003638 // A template-argument for a non-type, non-template
3639 // template-parameter shall be one of: [...]
3640 //
3641 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003642 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003643
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003644 // In C++98/03 mode, give an extension warning on any extra parentheses.
3645 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3646 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003647 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003648 if (!Invalid && !ExtraParens) {
Mike Stump1eb44332009-09-09 15:08:12 +00003649 Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003650 getLangOptions().CPlusPlus0x ?
3651 diag::warn_cxx98_compat_template_arg_extra_parens :
3652 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003653 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003654 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003655 }
3656
3657 Arg = Parens->getSubExpr();
3658 }
3659
John McCall91a57552011-07-15 05:09:51 +00003660 while (SubstNonTypeTemplateParmExpr *subst =
3661 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3662 Arg = subst->getReplacement()->IgnoreImpCasts();
3663
Douglas Gregorcaddba02009-11-12 18:38:13 +00003664 // A pointer-to-member constant written &Class::member.
3665 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003666 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003667 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3668 if (DRE && !DRE->getQualifier())
3669 DRE = 0;
3670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003671 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003672 // A constant of pointer-to-member type.
3673 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3674 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3675 if (VD->getType()->isMemberPointerType()) {
3676 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003677 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003678 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3679 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003680 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003681 else
3682 Converted = TemplateArgument(VD->getCanonicalDecl());
3683 return Invalid;
3684 }
3685 }
3686 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003687
Douglas Gregorcaddba02009-11-12 18:38:13 +00003688 DRE = 0;
3689 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003690
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003691 if (!DRE)
3692 return Diag(Arg->getSourceRange().getBegin(),
3693 diag::err_template_arg_not_pointer_to_member_form)
3694 << Arg->getSourceRange();
3695
3696 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3697 assert((isa<FieldDecl>(DRE->getDecl()) ||
3698 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3699 "Only non-static member pointers can make it here");
3700
3701 // Okay: this is the address of a non-static member, and therefore
3702 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003703 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003704 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003705 else
3706 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003707 return Invalid;
3708 }
3709
3710 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003711 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003712 diag::err_template_arg_not_pointer_to_member_form)
3713 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003714 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003715 diag::note_template_arg_refers_here);
3716 return true;
3717}
3718
Douglas Gregorc15cb382009-02-09 23:23:08 +00003719/// \brief Check a template argument against its corresponding
3720/// non-type template parameter.
3721///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003722/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003723/// If an error occurred, it returns ExprError(); otherwise, it
3724/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003725/// InstantiatedParamType is the type of the non-type template
3726/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003727ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3728 QualType InstantiatedParamType, Expr *Arg,
3729 TemplateArgument &Converted,
3730 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003731 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3732
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003733 // If either the parameter has a dependent type or the argument is
3734 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003735 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3736 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003737 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003738 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003739 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003740
3741 // C++ [temp.arg.nontype]p5:
3742 // The following conversions are performed on each expression used
3743 // as a non-type template-argument. If a non-type
3744 // template-argument cannot be converted to the type of the
3745 // corresponding template-parameter then the program is
3746 // ill-formed.
3747 //
3748 // -- for a non-type template-parameter of integral or
3749 // enumeration type, integral promotions (4.5) and integral
3750 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003751 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003752 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith4f870622011-10-27 22:11:44 +00003753 // FIXME: In C++11, the argument is a converted constant expression of the
3754 // type of the template parameter.
3755 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3756 if (ArgResult.isInvalid())
3757 return ExprError();
3758 Arg = ArgResult.take();
3759
3760 QualType ArgType = Arg->getType();
3761
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003762 // C++ [temp.arg.nontype]p1:
3763 // A template-argument for a non-type, non-template
3764 // template-parameter shall be one of:
3765 //
3766 // -- an integral constant-expression of integral or enumeration
3767 // type; or
3768 // -- the name of a non-type template-parameter; or
3769 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003770 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003771 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003772 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003773 diag::err_template_arg_not_integral_or_enumeral)
3774 << ArgType << Arg->getSourceRange();
3775 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003776 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003777 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003778 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003779 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3780 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003781 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003782 }
3783
Douglas Gregor02024a92010-03-28 02:42:43 +00003784 // From here on out, all we care about are the unqualified forms
3785 // of the parameter and argument types.
3786 ParamType = ParamType.getUnqualifiedType();
3787 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003788
3789 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003790 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003791 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003792 } else if (CTAK == CTAK_Deduced) {
3793 // C++ [temp.deduct.type]p17:
3794 // If, in the declaration of a function template with a non-type
3795 // template-parameter, the non-type template- parameter is used
3796 // in an expression in the function parameter-list and, if the
3797 // corresponding template-argument is deduced, the
3798 // template-argument type shall match the type of the
3799 // template-parameter exactly, except that a template-argument
3800 // deduced from an array bound may be of any integral type.
3801 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3802 << ArgType << ParamType;
3803 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003804 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003805 } else if (ParamType->isBooleanType()) {
3806 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003807 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003808 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3809 !ParamType->isEnumeralType()) {
3810 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003811 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003812 } else {
3813 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003814 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003815 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003816 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003817 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003818 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003819 }
3820
Douglas Gregorc7469372011-05-04 21:55:00 +00003821 // Add the value of this argument to the list of converted
3822 // arguments. We use the bitwidth and signedness of the template
3823 // parameter.
3824 if (Arg->isValueDependent()) {
3825 // The argument is value-dependent. Create a new
3826 // TemplateArgument with the converted expression.
3827 Converted = TemplateArgument(Arg);
3828 return Owned(Arg);
3829 }
3830
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003831 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003832 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003833 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003834
Douglas Gregorc7469372011-05-04 21:55:00 +00003835 if (ParamType->isBooleanType()) {
3836 // Value must be zero or one.
3837 Value = Value != 0;
3838 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3839 if (Value.getBitWidth() != AllowedBits)
3840 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003841 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003842 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003843 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003844
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003845 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003846 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003847 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003848 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003849 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003850 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003851
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003852 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003853 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003854 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003855 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3856 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3857 << Arg->getSourceRange();
3858 Diag(Param->getLocation(), diag::note_template_param_here);
3859 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003860
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003861 // Complain if we overflowed the template parameter's type.
3862 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003863 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003864 RequiredBits = OldValue.getActiveBits();
3865 else if (OldValue.isUnsigned())
3866 RequiredBits = OldValue.getActiveBits() + 1;
3867 else
3868 RequiredBits = OldValue.getMinSignedBits();
3869 if (RequiredBits > AllowedBits) {
3870 Diag(Arg->getSourceRange().getBegin(),
3871 diag::warn_template_arg_too_large)
3872 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3873 << Arg->getSourceRange();
3874 Diag(Param->getLocation(), diag::note_template_param_here);
3875 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003876 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003877
John McCall833ca992009-10-29 08:12:44 +00003878 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003879 ParamType->isEnumeralType()
3880 ? Context.getCanonicalType(ParamType)
3881 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003882 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003883 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003884
Richard Smith4f870622011-10-27 22:11:44 +00003885 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00003886 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3887
Douglas Gregorb7a09262010-04-01 18:32:35 +00003888 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3889 // from a template argument of type std::nullptr_t to a non-type
3890 // template parameter of type pointer to object, pointer to
3891 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003892 if (ArgType->isNullPtrType()) {
3893 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3894 Converted = TemplateArgument((NamedDecl *)0);
3895 return Owned(Arg);
3896 }
3897
3898 if (ParamType->isNullPtrType()) {
3899 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3900 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3901 return Owned(Arg);
3902 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003903 }
3904
Douglas Gregorb86b0572009-02-11 01:18:59 +00003905 // Handle pointer-to-function, reference-to-function, and
3906 // pointer-to-member-function all in (roughly) the same way.
3907 if (// -- For a non-type template-parameter of type pointer to
3908 // function, only the function-to-pointer conversion (4.3) is
3909 // applied. If the template-argument represents a set of
3910 // overloaded functions (or a pointer to such), the matching
3911 // function is selected from the set (13.4).
3912 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003913 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003914 // -- For a non-type template-parameter of type reference to
3915 // function, no conversions apply. If the template-argument
3916 // represents a set of overloaded functions, the matching
3917 // function is selected from the set (13.4).
3918 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003919 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003920 // -- For a non-type template-parameter of type pointer to
3921 // member function, no conversions apply. If the
3922 // template-argument represents a set of overloaded member
3923 // functions, the matching member function is selected from
3924 // the set (13.4).
3925 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003926 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003927 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003928
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003929 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003930 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003931 true,
3932 FoundResult)) {
3933 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003934 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003935
3936 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3937 ArgType = Arg->getType();
3938 } else
John Wiegley429bb272011-04-08 18:41:53 +00003939 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003940 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003941
John Wiegley429bb272011-04-08 18:41:53 +00003942 if (!ParamType->isMemberPointerType()) {
3943 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3944 ParamType,
3945 Arg, Converted))
3946 return ExprError();
3947 return Owned(Arg);
3948 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003949
John McCallf85e1932011-06-15 23:02:42 +00003950 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003951 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003952 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00003953 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
3954 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003955 } else if (!Context.hasSameUnqualifiedType(ArgType,
3956 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003957 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003958 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003959 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003960 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003961 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003962 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003963 }
Mike Stump1eb44332009-09-09 15:08:12 +00003964
John Wiegley429bb272011-04-08 18:41:53 +00003965 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3966 return ExprError();
3967 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003968 }
3969
Chris Lattnerfe90de72009-02-20 21:37:53 +00003970 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003971 // -- for a non-type template-parameter of type pointer to
3972 // object, qualification conversions (4.4) and the
3973 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003974 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003975 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003976 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003977
John Wiegley429bb272011-04-08 18:41:53 +00003978 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3979 ParamType,
3980 Arg, Converted))
3981 return ExprError();
3982 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003983 }
Mike Stump1eb44332009-09-09 15:08:12 +00003984
Ted Kremenek6217b802009-07-29 21:53:49 +00003985 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003986 // -- For a non-type template-parameter of type reference to
3987 // object, no conversions apply. The type referred to by the
3988 // reference may be more cv-qualified than the (otherwise
3989 // identical) type of the template-argument. The
3990 // template-parameter is bound directly to the
3991 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003992 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003993 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003994
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003995 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003996 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3997 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003998 true,
3999 FoundResult)) {
4000 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00004001 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004002
4003 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4004 ArgType = Arg->getType();
4005 } else
John Wiegley429bb272011-04-08 18:41:53 +00004006 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004007 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004008
John Wiegley429bb272011-04-08 18:41:53 +00004009 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4010 ParamType,
4011 Arg, Converted))
4012 return ExprError();
4013 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004014 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004015
4016 // -- For a non-type template-parameter of type pointer to data
4017 // member, qualification conversions (4.4) are applied.
4018 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4019
John McCallf85e1932011-06-15 23:02:42 +00004020 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004021 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004022 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004023 } else if (IsQualificationConversion(ArgType, ParamType, false,
4024 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004025 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4026 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004027 } else {
4028 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004029 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004030 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004031 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004032 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004033 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004034 }
4035
John Wiegley429bb272011-04-08 18:41:53 +00004036 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4037 return ExprError();
4038 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004039}
4040
4041/// \brief Check a template argument against its corresponding
4042/// template template parameter.
4043///
4044/// This routine implements the semantics of C++ [temp.arg.template].
4045/// It returns true if an error occurred, and false otherwise.
4046bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004047 const TemplateArgumentLoc &Arg) {
4048 TemplateName Name = Arg.getArgument().getAsTemplate();
4049 TemplateDecl *Template = Name.getAsTemplateDecl();
4050 if (!Template) {
4051 // Any dependent template name is fine.
4052 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4053 return false;
4054 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004055
Richard Smith3e4c6c42011-05-05 21:57:07 +00004056 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004057 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004058 // the name of a class template or an alias template, expressed as an
4059 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004060 // primary class templates are considered when matching the
4061 // template template argument with the corresponding parameter;
4062 // partial specializations are not considered even if their
4063 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004064 //
4065 // Note that we also allow template template parameters here, which
4066 // will happen when we are dealing with, e.g., class template
4067 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004068 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004069 !isa<TemplateTemplateParmDecl>(Template) &&
4070 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004071 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004072 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004073 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004074 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004075 << Template;
4076 }
4077
4078 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4079 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004080 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004081 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004082 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004083}
4084
Douglas Gregor02024a92010-03-28 02:42:43 +00004085/// \brief Given a non-type template argument that refers to a
4086/// declaration and the type of its corresponding non-type template
4087/// parameter, produce an expression that properly refers to that
4088/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004089ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004090Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4091 QualType ParamType,
4092 SourceLocation Loc) {
4093 assert(Arg.getKind() == TemplateArgument::Declaration &&
4094 "Only declaration template arguments permitted here");
4095 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4096
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004097 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004098 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4099 // If the value is a class member, we might have a pointer-to-member.
4100 // Determine whether the non-type template template parameter is of
4101 // pointer-to-member type. If so, we need to build an appropriate
4102 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4103 // would refer to the member itself.
4104 if (ParamType->isMemberPointerType()) {
4105 QualType ClassType
4106 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4107 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004108 = NestedNameSpecifier::Create(Context, 0, false,
4109 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004110 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004111 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004112
4113 // The actual value-ness of this is unimportant, but for
4114 // internal consistency's sake, references to instance methods
4115 // are r-values.
4116 ExprValueKind VK = VK_LValue;
4117 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4118 VK = VK_RValue;
4119
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004120 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004121 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004122 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004123 Loc,
4124 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004125 if (RefExpr.isInvalid())
4126 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004127
John McCall2de56d12010-08-25 11:45:40 +00004128 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004129
Douglas Gregorc0c83002010-04-30 21:46:38 +00004130 // We might need to perform a trailing qualification conversion, since
4131 // the element type on the parameter could be more qualified than the
4132 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004133 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004134 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004135 ParamType.getUnqualifiedType(), false,
4136 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004137 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004138
Douglas Gregor02024a92010-03-28 02:42:43 +00004139 assert(!RefExpr.isInvalid() &&
4140 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004141 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004142 return move(RefExpr);
4143 }
4144 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004145
Douglas Gregor02024a92010-03-28 02:42:43 +00004146 QualType T = VD->getType().getNonReferenceType();
4147 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004148 // When the non-type template parameter is a pointer, take the
4149 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004150 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004151 if (RefExpr.isInvalid())
4152 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004153
4154 if (T->isFunctionType() || T->isArrayType()) {
4155 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004156 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4157 if (RefExpr.isInvalid())
4158 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004159
4160 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004161 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004162
Douglas Gregorb7a09262010-04-01 18:32:35 +00004163 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004164 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004165 }
4166
John McCallf89e55a2010-11-18 06:31:45 +00004167 ExprValueKind VK = VK_RValue;
4168
Douglas Gregor02024a92010-03-28 02:42:43 +00004169 // If the non-type template parameter has reference type, qualify the
4170 // resulting declaration reference with the extra qualifiers on the
4171 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004172 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4173 VK = VK_LValue;
4174 T = Context.getQualifiedType(T,
4175 TargetRef->getPointeeType().getQualifiers());
4176 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004177
John McCallf89e55a2010-11-18 06:31:45 +00004178 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004179}
4180
4181/// \brief Construct a new expression that refers to the given
4182/// integral template argument with the given source-location
4183/// information.
4184///
4185/// This routine takes care of the mapping from an integral template
4186/// argument (which may have any integral type) to the appropriate
4187/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004188ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004189Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4190 SourceLocation Loc) {
4191 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004192 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004193 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004194 if (T->isAnyCharacterType()) {
4195 CharacterLiteral::CharacterKind Kind;
4196 if (T->isWideCharType())
4197 Kind = CharacterLiteral::Wide;
4198 else if (T->isChar16Type())
4199 Kind = CharacterLiteral::UTF16;
4200 else if (T->isChar32Type())
4201 Kind = CharacterLiteral::UTF32;
4202 else
4203 Kind = CharacterLiteral::Ascii;
4204
Douglas Gregor02024a92010-03-28 02:42:43 +00004205 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004206 Arg.getAsIntegral()->getZExtValue(),
4207 Kind, T, Loc));
4208 }
4209
Douglas Gregor02024a92010-03-28 02:42:43 +00004210 if (T->isBooleanType())
4211 return Owned(new (Context) CXXBoolLiteralExpr(
4212 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004213 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004214
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004215 if (T->isNullPtrType())
4216 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4217
Chris Lattner223de242011-04-25 20:37:58 +00004218 // If this is an enum type that we're instantiating, we need to use an integer
4219 // type the same size as the enumerator. We don't want to build an
4220 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004221 QualType BT;
4222 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004223 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004224 else
4225 BT = T;
4226
John McCall4e9272d2011-07-15 07:47:58 +00004227 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4228 if (T->isEnumeralType()) {
4229 // FIXME: This is a hack. We need a better way to handle substituted
4230 // non-type template parameters.
4231 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4232 Context.getTrivialTypeSourceInfo(T, Loc),
4233 Loc, Loc);
4234 }
4235
4236 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004237}
4238
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004239/// \brief Match two template parameters within template parameter lists.
4240static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4241 bool Complain,
4242 Sema::TemplateParameterListEqualKind Kind,
4243 SourceLocation TemplateArgLoc) {
4244 // Check the actual kind (type, non-type, template).
4245 if (Old->getKind() != New->getKind()) {
4246 if (Complain) {
4247 unsigned NextDiag = diag::err_template_param_different_kind;
4248 if (TemplateArgLoc.isValid()) {
4249 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4250 NextDiag = diag::note_template_param_different_kind;
4251 }
4252 S.Diag(New->getLocation(), NextDiag)
4253 << (Kind != Sema::TPL_TemplateMatch);
4254 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4255 << (Kind != Sema::TPL_TemplateMatch);
4256 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004257
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004258 return false;
4259 }
4260
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004261 // Check that both are parameter packs are neither are parameter packs.
4262 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004263 // template template parameter, the template template parameter can have
4264 // a parameter pack where the template template argument does not.
4265 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4266 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4267 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004268 if (Complain) {
4269 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4270 if (TemplateArgLoc.isValid()) {
4271 S.Diag(TemplateArgLoc,
4272 diag::err_template_arg_template_params_mismatch);
4273 NextDiag = diag::note_template_parameter_pack_non_pack;
4274 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004275
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004276 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4277 : isa<NonTypeTemplateParmDecl>(New)? 1
4278 : 2;
4279 S.Diag(New->getLocation(), NextDiag)
4280 << ParamKind << New->isParameterPack();
4281 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4282 << ParamKind << Old->isParameterPack();
4283 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004284
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004285 return false;
4286 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004287
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004288 // For non-type template parameters, check the type of the parameter.
4289 if (NonTypeTemplateParmDecl *OldNTTP
4290 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4291 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004292
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004293 // If we are matching a template template argument to a template
4294 // template parameter and one of the non-type template parameter types
4295 // is dependent, then we must wait until template instantiation time
4296 // to actually compare the arguments.
4297 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4298 (OldNTTP->getType()->isDependentType() ||
4299 NewNTTP->getType()->isDependentType()))
4300 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004301
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004302 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4303 if (Complain) {
4304 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4305 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004306 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004307 diag::err_template_arg_template_params_mismatch);
4308 NextDiag = diag::note_template_nontype_parm_different_type;
4309 }
4310 S.Diag(NewNTTP->getLocation(), NextDiag)
4311 << NewNTTP->getType()
4312 << (Kind != Sema::TPL_TemplateMatch);
4313 S.Diag(OldNTTP->getLocation(),
4314 diag::note_template_nontype_parm_prev_declaration)
4315 << OldNTTP->getType();
4316 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004317
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004318 return false;
4319 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004320
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004321 return true;
4322 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004323
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004324 // For template template parameters, check the template parameter types.
4325 // The template parameter lists of template template
4326 // parameters must agree.
4327 if (TemplateTemplateParmDecl *OldTTP
4328 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004329 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004330 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4331 OldTTP->getTemplateParameters(),
4332 Complain,
4333 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004334 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004335 : Kind),
4336 TemplateArgLoc);
4337 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004338
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004339 return true;
4340}
Douglas Gregor02024a92010-03-28 02:42:43 +00004341
Douglas Gregora0347822011-01-13 00:08:50 +00004342/// \brief Diagnose a known arity mismatch when comparing template argument
4343/// lists.
4344static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004345void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004346 TemplateParameterList *New,
4347 TemplateParameterList *Old,
4348 Sema::TemplateParameterListEqualKind Kind,
4349 SourceLocation TemplateArgLoc) {
4350 unsigned NextDiag = diag::err_template_param_list_different_arity;
4351 if (TemplateArgLoc.isValid()) {
4352 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4353 NextDiag = diag::note_template_param_list_different_arity;
4354 }
4355 S.Diag(New->getTemplateLoc(), NextDiag)
4356 << (New->size() > Old->size())
4357 << (Kind != Sema::TPL_TemplateMatch)
4358 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4359 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4360 << (Kind != Sema::TPL_TemplateMatch)
4361 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4362}
4363
Douglas Gregorddc29e12009-02-06 22:42:48 +00004364/// \brief Determine whether the given template parameter lists are
4365/// equivalent.
4366///
Mike Stump1eb44332009-09-09 15:08:12 +00004367/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004368/// source code as part of a new template declaration.
4369///
4370/// \param Old The old template parameter list, typically found via
4371/// name lookup of the template declared with this template parameter
4372/// list.
4373///
4374/// \param Complain If true, this routine will produce a diagnostic if
4375/// the template parameter lists are not equivalent.
4376///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004377/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004378///
4379/// \param TemplateArgLoc If this source location is valid, then we
4380/// are actually checking the template parameter list of a template
4381/// argument (New) against the template parameter list of its
4382/// corresponding template template parameter (Old). We produce
4383/// slightly different diagnostics in this scenario.
4384///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004385/// \returns True if the template parameter lists are equal, false
4386/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004387bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004388Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4389 TemplateParameterList *Old,
4390 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004391 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004392 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004393 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4394 if (Complain)
4395 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4396 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004397
4398 return false;
4399 }
4400
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004401 // C++0x [temp.arg.template]p3:
4402 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004403 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004404 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004405 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004406 // template-parameter-list of P. [...]
4407 TemplateParameterList::iterator NewParm = New->begin();
4408 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004409 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004410 OldParmEnd = Old->end();
4411 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004412 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4413 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004414 if (NewParm == NewParmEnd) {
4415 if (Complain)
4416 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4417 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004418
Douglas Gregora0347822011-01-13 00:08:50 +00004419 return false;
4420 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004421
Douglas Gregora0347822011-01-13 00:08:50 +00004422 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4423 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004424 return false;
4425
Douglas Gregora0347822011-01-13 00:08:50 +00004426 ++NewParm;
4427 continue;
4428 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004429
Douglas Gregora0347822011-01-13 00:08:50 +00004430 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004431 // [...] When P's template- parameter-list contains a template parameter
4432 // pack (14.5.3), the template parameter pack will match zero or more
4433 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004434 // template-parameter-list of A with the same type and form as the
4435 // template parameter pack in P (ignoring whether those template
4436 // parameters are template parameter packs).
4437 for (; NewParm != NewParmEnd; ++NewParm) {
4438 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4439 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004440 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004441 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004442 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004443
Douglas Gregora0347822011-01-13 00:08:50 +00004444 // Make sure we exhausted all of the arguments.
4445 if (NewParm != NewParmEnd) {
4446 if (Complain)
4447 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4448 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004449
Douglas Gregora0347822011-01-13 00:08:50 +00004450 return false;
4451 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004452
Douglas Gregorddc29e12009-02-06 22:42:48 +00004453 return true;
4454}
4455
4456/// \brief Check whether a template can be declared within this scope.
4457///
4458/// If the template declaration is valid in this scope, returns
4459/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004460bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004461Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004462 if (!S)
4463 return false;
4464
Douglas Gregorddc29e12009-02-06 22:42:48 +00004465 // Find the nearest enclosing declaration scope.
4466 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4467 (S->getFlags() & Scope::TemplateParamScope) != 0)
4468 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004469
Douglas Gregorddc29e12009-02-06 22:42:48 +00004470 // C++ [temp]p2:
4471 // A template-declaration can appear only as a namespace scope or
4472 // class scope declaration.
4473 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004474 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4475 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004476 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004477 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004478
Eli Friedman1503f772009-07-31 01:43:05 +00004479 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004480 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004481
4482 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4483 return false;
4484
Mike Stump1eb44332009-09-09 15:08:12 +00004485 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004486 diag::err_template_outside_namespace_or_class_scope)
4487 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004488}
Douglas Gregorcc636682009-02-17 23:15:12 +00004489
Douglas Gregord5cb8762009-10-07 00:13:32 +00004490/// \brief Determine what kind of template specialization the given declaration
4491/// is.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00004492static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004493 if (!D)
4494 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004495
Douglas Gregorf6b11852009-10-08 15:14:33 +00004496 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4497 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004498 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4499 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004500 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4501 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004502
Douglas Gregord5cb8762009-10-07 00:13:32 +00004503 return TSK_Undeclared;
4504}
4505
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004506/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004507/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004508///
Douglas Gregor9302da62009-10-14 23:50:59 +00004509/// This routine determines whether a template specialization can be declared
4510/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004511///
4512/// \param S the semantic analysis object for which this check is being
4513/// performed.
4514///
4515/// \param Specialized the entity being specialized or instantiated, which
4516/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004517/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004518/// member class).
4519///
4520/// \param PrevDecl the previous declaration of this entity, if any.
4521///
4522/// \param Loc the location of the explicit specialization or instantiation of
4523/// this entity.
4524///
4525/// \param IsPartialSpecialization whether this is a partial specialization of
4526/// a class template.
4527///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004528/// \returns true if there was an error that we cannot recover from, false
4529/// otherwise.
4530static bool CheckTemplateSpecializationScope(Sema &S,
4531 NamedDecl *Specialized,
4532 NamedDecl *PrevDecl,
4533 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004534 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004535 // Keep these "kind" numbers in sync with the %select statements in the
4536 // various diagnostics emitted by this routine.
4537 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004538 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004539 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004540 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004541 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004542 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004543 EntityKind = 3;
4544 else if (isa<VarDecl>(Specialized))
4545 EntityKind = 4;
4546 else if (isa<RecordDecl>(Specialized))
4547 EntityKind = 5;
4548 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004549 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4550 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004551 return true;
4552 }
4553
Douglas Gregor88b70942009-02-25 22:02:03 +00004554 // C++ [temp.expl.spec]p2:
4555 // An explicit specialization shall be declared in the namespace
4556 // of which the template is a member, or, for member templates, in
4557 // the namespace of which the enclosing class or enclosing class
4558 // template is a member. An explicit specialization of a member
4559 // function, member class or static data member of a class
4560 // template shall be declared in the namespace of which the class
4561 // template is a member. Such a declaration may also be a
4562 // definition. If the declaration is not a definition, the
4563 // specialization may be defined later in the name- space in which
4564 // the explicit specialization was declared, or in a namespace
4565 // that encloses the one in which the explicit specialization was
4566 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004567 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004568 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004569 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004570 return true;
4571 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004572
Douglas Gregor0a407472009-10-07 17:30:37 +00004573 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004574 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004575 // Do not warn for class scope explicit specialization during
4576 // instantiation, warning was already emitted during pattern
4577 // semantic analysis.
4578 if (!S.ActiveTemplateInstantiations.size())
4579 S.Diag(Loc, diag::ext_function_specialization_in_class)
4580 << Specialized;
4581 } else {
4582 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4583 << Specialized;
4584 return true;
4585 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004586 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004587
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004588 if (S.CurContext->isRecord() &&
4589 !S.CurContext->Equals(Specialized->getDeclContext())) {
4590 // Make sure that we're specializing in the right record context.
4591 // Otherwise, things can go horribly wrong.
4592 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4593 << Specialized;
4594 return true;
4595 }
4596
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004597 // C++ [temp.class.spec]p6:
4598 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004599 // in any namespace scope in which its definition may be defined (14.5.1
4600 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004601 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004602 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004603 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004604 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004605 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004606 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4607 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004608 // C++ [temp.exp.spec]p2:
4609 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004610 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004611 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004612 // An explicit specialization of a member function, member class or
4613 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004614 // namespace of which the class template is a member.
4615 //
4616 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004617 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004618 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004619 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4620 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4621 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4622 assert(!IsCPlusPlus0xExtension &&
4623 "DC encloses TU but isn't in enclosing namespace set");
4624 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004625 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004626 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4627 int Diag;
4628 if (!IsCPlusPlus0xExtension)
4629 Diag = diag::err_template_spec_decl_out_of_scope;
4630 else if (!S.getLangOptions().CPlusPlus0x)
4631 Diag = diag::ext_template_spec_decl_out_of_scope;
4632 else
4633 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4634 S.Diag(Loc, Diag)
4635 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4636 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004637
Douglas Gregor9302da62009-10-14 23:50:59 +00004638 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004639 ComplainedAboutScope =
4640 !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004641 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004642 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004643
4644 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004645 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004646 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004647 // specializations of function templates, static data members, and member
4648 // functions, so we skip the check here for those kinds of entities.
4649 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004650 // Should we refactor that check, so that it occurs later?
4651 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004652 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4653 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004654 if (isa<TranslationUnitDecl>(SpecializedContext))
4655 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4656 << EntityKind << Specialized;
4657 else if (isa<NamespaceDecl>(SpecializedContext))
4658 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4659 << EntityKind << Specialized
4660 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004661
Douglas Gregor9302da62009-10-14 23:50:59 +00004662 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004663 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004664
Douglas Gregord5cb8762009-10-07 00:13:32 +00004665 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004666
Douglas Gregor88b70942009-02-25 22:02:03 +00004667 return false;
4668}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004669
Douglas Gregorbacb9492011-01-03 21:13:47 +00004670/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4671/// that checks non-type template partial specialization arguments.
4672static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4673 NonTypeTemplateParmDecl *Param,
4674 const TemplateArgument *Args,
4675 unsigned NumArgs) {
4676 for (unsigned I = 0; I != NumArgs; ++I) {
4677 if (Args[I].getKind() == TemplateArgument::Pack) {
4678 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004679 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004680 Args[I].pack_size()))
4681 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004682
Douglas Gregore94866f2009-06-12 21:21:02 +00004683 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004684 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004685
Douglas Gregorbacb9492011-01-03 21:13:47 +00004686 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004687 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004688 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004689 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004690
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004691 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004692 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4693 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004694
4695 // Strip off any implicit casts we added as part of type checking.
4696 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4697 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004698
Douglas Gregore94866f2009-06-12 21:21:02 +00004699 // C++ [temp.class.spec]p8:
4700 // A non-type argument is non-specialized if it is the name of a
4701 // non-type parameter. All other non-type arguments are
4702 // specialized.
4703 //
4704 // Below, we check the two conditions that only apply to
4705 // specialized non-type arguments, so skip any non-specialized
4706 // arguments.
4707 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004708 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004709 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004710
Douglas Gregore94866f2009-06-12 21:21:02 +00004711 // C++ [temp.class.spec]p9:
4712 // Within the argument list of a class template partial
4713 // specialization, the following restrictions apply:
4714 // -- A partially specialized non-type argument expression
4715 // shall not involve a template parameter of the partial
4716 // specialization except when the argument expression is a
4717 // simple identifier.
4718 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004719 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004720 diag::err_dependent_non_type_arg_in_partial_spec)
4721 << ArgExpr->getSourceRange();
4722 return true;
4723 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004724
Douglas Gregore94866f2009-06-12 21:21:02 +00004725 // -- The type of a template parameter corresponding to a
4726 // specialized non-type argument shall not be dependent on a
4727 // parameter of the specialization.
4728 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004729 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004730 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4731 << Param->getType()
4732 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004733 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004734 return true;
4735 }
4736 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004737
Douglas Gregorbacb9492011-01-03 21:13:47 +00004738 return false;
4739}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004740
Douglas Gregorbacb9492011-01-03 21:13:47 +00004741/// \brief Check the non-type template arguments of a class template
4742/// partial specialization according to C++ [temp.class.spec]p9.
4743///
4744/// \param TemplateParams the template parameters of the primary class
4745/// template.
4746///
4747/// \param TemplateArg the template arguments of the class template
4748/// partial specialization.
4749///
4750/// \returns true if there was an error, false otherwise.
4751static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4752 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004753 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004754 const TemplateArgument *ArgList = TemplateArgs.data();
4755
4756 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4757 NonTypeTemplateParmDecl *Param
4758 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4759 if (!Param)
4760 continue;
4761
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004762 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004763 &ArgList[I], 1))
4764 return true;
4765 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004766
4767 return false;
4768}
4769
John McCalld226f652010-08-21 09:40:31 +00004770DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004771Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4772 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004773 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004774 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004775 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004776 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004777 SourceLocation TemplateNameLoc,
4778 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004779 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004780 SourceLocation RAngleLoc,
4781 AttributeList *Attr,
4782 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004783 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004784
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004785 // NOTE: KWLoc is the location of the tag keyword. This will instead
4786 // store the location of the outermost template keyword in the declaration.
4787 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4788 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4789
Douglas Gregorcc636682009-02-17 23:15:12 +00004790 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004791 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004792 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004793 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4794
4795 if (!ClassTemplate) {
4796 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004797 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004798 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4799 return true;
4800 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004801
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004802 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004803 bool isPartialSpecialization = false;
4804
Douglas Gregor88b70942009-02-25 22:02:03 +00004805 // Check the validity of the template headers that introduce this
4806 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004807 // FIXME: We probably shouldn't complain about these headers for
4808 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004809 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004810 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004811 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4812 TemplateNameLoc,
4813 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004814 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004815 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004816 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004817 isExplicitSpecialization,
4818 Invalid);
4819 if (Invalid)
4820 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004821
Douglas Gregor05396e22009-08-25 17:23:04 +00004822 if (TemplateParams && TemplateParams->size() > 0) {
4823 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004824
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004825 if (TUK == TUK_Friend) {
4826 Diag(KWLoc, diag::err_partial_specialization_friend)
4827 << SourceRange(LAngleLoc, RAngleLoc);
4828 return true;
4829 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004830
Douglas Gregor05396e22009-08-25 17:23:04 +00004831 // C++ [temp.class.spec]p10:
4832 // The template parameter list of a specialization shall not
4833 // contain default template argument values.
4834 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4835 Decl *Param = TemplateParams->getParam(I);
4836 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4837 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004838 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004839 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004840 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004841 }
4842 } else if (NonTypeTemplateParmDecl *NTTP
4843 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4844 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004845 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004846 diag::err_default_arg_in_partial_spec)
4847 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004848 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004849 }
4850 } else {
4851 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004852 if (TTP->hasDefaultArgument()) {
4853 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004854 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004855 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004856 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004857 }
4858 }
4859 }
Douglas Gregora735b202009-10-13 14:39:41 +00004860 } else if (TemplateParams) {
4861 if (TUK == TUK_Friend)
4862 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004863 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004864 SourceRange(TemplateParams->getTemplateLoc(),
4865 TemplateParams->getRAngleLoc()))
4866 << SourceRange(LAngleLoc, RAngleLoc);
4867 else
4868 isExplicitSpecialization = true;
4869 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004870 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004871 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004872 isExplicitSpecialization = true;
4873 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004874
Douglas Gregorcc636682009-02-17 23:15:12 +00004875 // Check that the specialization uses the same tag kind as the
4876 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004877 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4878 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004879 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004880 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004881 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004882 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004883 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004884 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004885 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004886 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004887 diag::note_previous_use);
4888 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4889 }
4890
Douglas Gregor40808ce2009-03-09 23:48:35 +00004891 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004892 TemplateArgumentListInfo TemplateArgs;
4893 TemplateArgs.setLAngleLoc(LAngleLoc);
4894 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004895 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004896
Douglas Gregor925910d2011-01-03 20:35:03 +00004897 // Check for unexpanded parameter packs in any of the template arguments.
4898 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004899 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004900 UPPC_PartialSpecialization))
4901 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004902
Douglas Gregorcc636682009-02-17 23:15:12 +00004903 // Check that the template argument list is well-formed for this
4904 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004905 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004906 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4907 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004908 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004909
Douglas Gregor910f8002010-11-07 23:05:16 +00004910 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004911 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004912
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004913 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004914 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004915 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004916 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004917 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004918 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004919 return true;
4920
Douglas Gregor561f8122011-07-01 01:22:09 +00004921 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004922 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004923 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004924 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004925 TemplateArgs.size(),
4926 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004927 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4928 << ClassTemplate->getDeclName();
4929 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004930 }
4931 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004932
Douglas Gregorcc636682009-02-17 23:15:12 +00004933 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004934 ClassTemplateSpecializationDecl *PrevDecl = 0;
4935
4936 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004937 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004938 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004939 = ClassTemplate->findPartialSpecialization(Converted.data(),
4940 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004941 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004942 else
4943 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004944 = ClassTemplate->findSpecialization(Converted.data(),
4945 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004946
4947 ClassTemplateSpecializationDecl *Specialization = 0;
4948
Douglas Gregor88b70942009-02-25 22:02:03 +00004949 // Check whether we can declare a class template specialization in
4950 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004951 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004952 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4953 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004954 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004955 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004956
Douglas Gregorb88e8882009-07-30 17:40:51 +00004957 // The canonical type
4958 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004959 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004960 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004961 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004962 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004963 // arguments was referenced but not declared, or we're only
4964 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004965 // declaration node as our own, updating its source location and
4966 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004967 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004968 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004969 if (TemplateParameterLists.size() > 0) {
4970 Specialization->setTemplateParameterListsInfo(Context,
4971 TemplateParameterLists.size(),
4972 (TemplateParameterList**) TemplateParameterLists.release());
4973 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004974 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004975 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004976 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004977 // Build the canonical type that describes the converted template
4978 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004979 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4980 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004981 Converted.data(),
4982 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004983
4984 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004985 ClassTemplate->getInjectedClassNameSpecialization())) {
4986 // C++ [temp.class.spec]p9b3:
4987 //
4988 // -- The argument list of the specialization shall not be identical
4989 // to the implicit argument list of the primary template.
4990 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00004991 << (TUK == TUK_Definition)
4992 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00004993 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4994 ClassTemplate->getIdentifier(),
4995 TemplateNameLoc,
4996 Attr,
4997 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00004998 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004999 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005000 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005001 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005002
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005003 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005004 ClassTemplatePartialSpecializationDecl *PrevPartial
5005 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005006 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005007 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005008 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005009 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005010 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005011 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005012 TemplateParams,
5013 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005014 Converted.data(),
5015 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005016 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005017 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005018 PrevPartial,
5019 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005020 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005021 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005022 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005023 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005024 (TemplateParameterList**) TemplateParameterLists.release());
5025 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005026
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005027 if (!PrevPartial)
5028 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005029 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005030
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005031 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005032 // template specialization, make a note of that.
5033 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5034 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005035
Douglas Gregor031a5882009-06-13 00:26:55 +00005036 // Check that all of the template parameters of the class template
5037 // partial specialization are deducible from the template
5038 // arguments. If not, this class template partial specialization
5039 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005040 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00005041 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005042 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005043 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005044 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005045 unsigned NumNonDeducible = 0;
5046 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
5047 if (!DeducibleParams[I])
5048 ++NumNonDeducible;
5049
5050 if (NumNonDeducible) {
5051 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5052 << (NumNonDeducible > 1)
5053 << SourceRange(TemplateNameLoc, RAngleLoc);
5054 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5055 if (!DeducibleParams[I]) {
5056 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5057 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005058 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005059 diag::note_partial_spec_unused_parameter)
5060 << Param->getDeclName();
5061 else
Mike Stump1eb44332009-09-09 15:08:12 +00005062 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005063 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005064 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005065 }
5066 }
5067 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005068 } else {
5069 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005070 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005071 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005072 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005073 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005074 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005075 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005076 Converted.data(),
5077 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005078 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005079 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005080 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005081 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005082 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005083 (TemplateParameterList**) TemplateParameterLists.release());
5084 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005085
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005086 if (!PrevDecl)
5087 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005088
5089 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005090 }
5091
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005092 // C++ [temp.expl.spec]p6:
5093 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005094 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005095 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005096 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005097 // use occurs; no diagnostic is required.
5098 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005099 bool Okay = false;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005100 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005101 // Is there any previous explicit specialization declaration?
5102 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5103 Okay = true;
5104 break;
5105 }
5106 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005107
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005108 if (!Okay) {
5109 SourceRange Range(TemplateNameLoc, RAngleLoc);
5110 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5111 << Context.getTypeDeclType(Specialization) << Range;
5112
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005113 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005114 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005115 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005116 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005117 return true;
5118 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005119 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005120
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005121 // If this is not a friend, note that this is an explicit specialization.
5122 if (TUK != TUK_Friend)
5123 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005124
5125 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005126 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005127 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005128 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005129 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005130 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005131 Diag(Def->getLocation(), diag::note_previous_definition);
5132 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005133 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005134 }
5135 }
5136
John McCall7f1b9872010-12-18 03:30:47 +00005137 if (Attr)
5138 ProcessDeclAttributeList(S, Specialization, Attr);
5139
Douglas Gregord023aec2011-09-09 20:53:38 +00005140 if (ModulePrivateLoc.isValid())
5141 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5142 << (isPartialSpecialization? 1 : 0)
5143 << FixItHint::CreateRemoval(ModulePrivateLoc);
5144
Douglas Gregorfc705b82009-02-26 22:19:44 +00005145 // Build the fully-sugared type for this class template
5146 // specialization as the user wrote in the specialization
5147 // itself. This means that we'll pretty-print the type retrieved
5148 // from the specialization's declaration the way that the user
5149 // actually wrote the specialization, rather than formatting the
5150 // name based on the "canonical" representation used to store the
5151 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005152 TypeSourceInfo *WrittenTy
5153 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5154 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005155 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005156 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005157 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005158 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005159 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005160
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005161 // C++ [temp.expl.spec]p9:
5162 // A template explicit specialization is in the scope of the
5163 // namespace in which the template was defined.
5164 //
5165 // We actually implement this paragraph where we set the semantic
5166 // context (in the creation of the ClassTemplateSpecializationDecl),
5167 // but we also maintain the lexical context where the actual
5168 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005169 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005170
Douglas Gregorcc636682009-02-17 23:15:12 +00005171 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005172 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005173 Specialization->startDefinition();
5174
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005175 if (TUK == TUK_Friend) {
5176 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5177 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005178 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005179 /*FIXME:*/KWLoc);
5180 Friend->setAccess(AS_public);
5181 CurContext->addDecl(Friend);
5182 } else {
5183 // Add the specialization into its lexical context, so that it can
5184 // be seen when iterating through the list of declarations in that
5185 // context. However, specializations are not found by name lookup.
5186 CurContext->addDecl(Specialization);
5187 }
John McCalld226f652010-08-21 09:40:31 +00005188 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005189}
Douglas Gregord57959a2009-03-27 23:10:48 +00005190
John McCalld226f652010-08-21 09:40:31 +00005191Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005192 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005193 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005194 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005195}
5196
John McCalld226f652010-08-21 09:40:31 +00005197Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005198 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005199 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005200 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005201 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005202
Douglas Gregor52591bf2009-06-24 00:54:41 +00005203 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005204 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005205 }
Mike Stump1eb44332009-09-09 15:08:12 +00005206
Douglas Gregor52591bf2009-06-24 00:54:41 +00005207 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005208
Douglas Gregor45fa5602011-11-07 20:56:01 +00005209 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005210 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005211 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005212 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005213 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005214 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005215 FunctionTemplate->getTemplatedDecl());
5216 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5217 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5218 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005219}
5220
John McCall75042392010-02-11 01:33:53 +00005221/// \brief Strips various properties off an implicit instantiation
5222/// that has just been explicitly specialized.
5223static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005224 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005225
5226 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5227 FD->setInlineSpecified(false);
5228 }
5229}
5230
Nico Weberd1d512a2012-01-09 19:52:25 +00005231/// \brief Compute the diagnostic location for an explicit instantiation
5232// declaration or definition.
5233static SourceLocation DiagLocForExplicitInstantiation(
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005234 NamedDecl* D, SourceLocation PointOfInstantiation) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005235 // Explicit instantiations following a specialization have no effect and
5236 // hence no PointOfInstantiation. In that case, walk decl backwards
5237 // until a valid name loc is found.
5238 SourceLocation PrevDiagLoc = PointOfInstantiation;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005239 for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
5240 Prev = Prev->getPreviousDecl()) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005241 PrevDiagLoc = Prev->getLocation();
5242 }
5243 assert(PrevDiagLoc.isValid() &&
5244 "Explicit instantiation without point of instantiation?");
5245 return PrevDiagLoc;
5246}
5247
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005248/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005249/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005250/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005251/// new specialization/instantiation will have any effect.
5252///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005253/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005254/// instantiation.
5255///
5256/// \param NewTSK the kind of the new explicit specialization or instantiation.
5257///
5258/// \param PrevDecl the previous declaration of the entity.
5259///
5260/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5261///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005262/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005263/// declaration was instantiated (either implicitly or explicitly).
5264///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005265/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005266/// specialization or instantiation has no effect and should be ignored.
5267///
5268/// \returns true if there was an error that should prevent the introduction of
5269/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005270bool
5271Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5272 TemplateSpecializationKind NewTSK,
5273 NamedDecl *PrevDecl,
5274 TemplateSpecializationKind PrevTSK,
5275 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005276 bool &HasNoEffect) {
5277 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005278
Douglas Gregor454885e2009-10-15 15:54:05 +00005279 switch (NewTSK) {
5280 case TSK_Undeclared:
5281 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005282 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005283
Douglas Gregor454885e2009-10-15 15:54:05 +00005284 case TSK_ExplicitSpecialization:
5285 switch (PrevTSK) {
5286 case TSK_Undeclared:
5287 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005288 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005289 // explicitly specialized or has merely been mentioned without any
5290 // instantiation.
5291 return false;
5292
5293 case TSK_ImplicitInstantiation:
5294 if (PrevPointOfInstantiation.isInvalid()) {
5295 // The declaration itself has not actually been instantiated, so it is
5296 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005297 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005298 return false;
5299 }
5300 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005301
Douglas Gregor454885e2009-10-15 15:54:05 +00005302 case TSK_ExplicitInstantiationDeclaration:
5303 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005304 assert((PrevTSK == TSK_ImplicitInstantiation ||
5305 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005306 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005307
Douglas Gregor454885e2009-10-15 15:54:05 +00005308 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005309 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005310 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005311 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005312 // implicit instantiation to take place, in every translation unit in
5313 // which such a use occurs; no diagnostic is required.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005314 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005315 // Is there any previous explicit specialization declaration?
5316 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5317 return false;
5318 }
5319
Douglas Gregor0d035142009-10-27 18:42:08 +00005320 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005321 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005322 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005323 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005324
Douglas Gregor454885e2009-10-15 15:54:05 +00005325 return true;
5326 }
5327 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005328
Douglas Gregor454885e2009-10-15 15:54:05 +00005329 case TSK_ExplicitInstantiationDeclaration:
5330 switch (PrevTSK) {
5331 case TSK_ExplicitInstantiationDeclaration:
5332 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005333 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005334 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005335
Douglas Gregor454885e2009-10-15 15:54:05 +00005336 case TSK_Undeclared:
5337 case TSK_ImplicitInstantiation:
5338 // We're explicitly instantiating something that may have already been
5339 // implicitly instantiated; that's fine.
5340 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005341
Douglas Gregor454885e2009-10-15 15:54:05 +00005342 case TSK_ExplicitSpecialization:
5343 // C++0x [temp.explicit]p4:
5344 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005345 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005346 // specialization for that template, the explicit instantiation has no
5347 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005348 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005349 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005350
Douglas Gregor454885e2009-10-15 15:54:05 +00005351 case TSK_ExplicitInstantiationDefinition:
5352 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005353 // If an entity is the subject of both an explicit instantiation
5354 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005355 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005356 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005357 diag::err_explicit_instantiation_declaration_after_definition);
Nico Weberff91d242011-12-23 20:58:04 +00005358
5359 // Explicit instantiations following a specialization have no effect and
5360 // hence no PrevPointOfInstantiation. In that case, walk decl backwards
5361 // until a valid name loc is found.
Nico Weberd1d512a2012-01-09 19:52:25 +00005362 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
5363 diag::note_explicit_instantiation_definition_here);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005364 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005365 return false;
5366 }
5367 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005368
Douglas Gregor454885e2009-10-15 15:54:05 +00005369 case TSK_ExplicitInstantiationDefinition:
5370 switch (PrevTSK) {
5371 case TSK_Undeclared:
5372 case TSK_ImplicitInstantiation:
5373 // We're explicitly instantiating something that may have already been
5374 // implicitly instantiated; that's fine.
5375 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005376
Douglas Gregor454885e2009-10-15 15:54:05 +00005377 case TSK_ExplicitSpecialization:
5378 // C++ DR 259, C++0x [temp.explicit]p4:
5379 // For a given set of template parameters, if an explicit
5380 // instantiation of a template appears after a declaration of
5381 // an explicit specialization for that template, the explicit
5382 // instantiation has no effect.
5383 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005384 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005385 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005386 // has been explicitly specialized.
Richard Smithebaf0e62011-10-18 20:49:44 +00005387 Diag(NewLoc, getLangOptions().CPlusPlus0x ?
5388 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5389 diag::ext_explicit_instantiation_after_specialization)
5390 << PrevDecl;
5391 Diag(PrevDecl->getLocation(),
5392 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005393 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005394 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005395
Douglas Gregor454885e2009-10-15 15:54:05 +00005396 case TSK_ExplicitInstantiationDeclaration:
5397 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005398 // were previously asked to suppress instantiations. That's fine.
Nico Weberff91d242011-12-23 20:58:04 +00005399
5400 // C++0x [temp.explicit]p4:
5401 // For a given set of template parameters, if an explicit instantiation
5402 // of a template appears after a declaration of an explicit
5403 // specialization for that template, the explicit instantiation has no
5404 // effect.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005405 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Nico Weberff91d242011-12-23 20:58:04 +00005406 // Is there any previous explicit specialization declaration?
5407 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5408 HasNoEffect = true;
5409 break;
5410 }
5411 }
5412
Douglas Gregor454885e2009-10-15 15:54:05 +00005413 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005414
Douglas Gregor454885e2009-10-15 15:54:05 +00005415 case TSK_ExplicitInstantiationDefinition:
5416 // C++0x [temp.spec]p5:
5417 // For a given template and a given set of template-arguments,
5418 // - an explicit instantiation definition shall appear at most once
5419 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005420 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005421 << PrevDecl;
Nico Weberd1d512a2012-01-09 19:52:25 +00005422 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
Douglas Gregor0d035142009-10-27 18:42:08 +00005423 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005424 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005425 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005426 }
5427 break;
5428 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005429
David Blaikieb219cfc2011-09-23 05:06:16 +00005430 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005431}
5432
John McCallaf2094e2010-04-08 09:05:18 +00005433/// \brief Perform semantic analysis for the given dependent function
5434/// template specialization. The only possible way to get a dependent
5435/// function template specialization is with a friend declaration,
5436/// like so:
5437///
5438/// template <class T> void foo(T);
5439/// template <class T> class A {
5440/// friend void foo<>(T);
5441/// };
5442///
5443/// There really isn't any useful analysis we can do here, so we
5444/// just store the information.
5445bool
5446Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5447 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5448 LookupResult &Previous) {
5449 // Remove anything from Previous that isn't a function template in
5450 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005451 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005452 LookupResult::Filter F = Previous.makeFilter();
5453 while (F.hasNext()) {
5454 NamedDecl *D = F.next()->getUnderlyingDecl();
5455 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005456 !FDLookupContext->InEnclosingNamespaceSetOf(
5457 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005458 F.erase();
5459 }
5460 F.done();
5461
5462 // Should this be diagnosed here?
5463 if (Previous.empty()) return true;
5464
5465 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5466 ExplicitTemplateArgs);
5467 return false;
5468}
5469
Abramo Bagnarae03db982010-05-20 15:32:11 +00005470/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005471/// specialization.
5472///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005473/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005474/// explicit function template specialization. On successful completion,
5475/// the function declaration \p FD will become a function template
5476/// specialization.
5477///
5478/// \param FD the function declaration, which will be updated to become a
5479/// function template specialization.
5480///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005481/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5482/// if any. Note that this may be valid info even when 0 arguments are
5483/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5484/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005485///
Francois Pichet59e7c562011-07-08 06:21:47 +00005486/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005487/// this function specialization.
5488bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005489Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005490 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005491 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005492 // The set of function template specializations that could match this
5493 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005494 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005495
Sebastian Redl7a126a42010-08-31 00:36:30 +00005496 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005497 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5498 I != E; ++I) {
5499 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5500 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005501 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005502 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005503 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5504 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005505 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005506
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005507 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005508 // A trailing template-argument can be left unspecified in the
5509 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005510 // provided it can be deduced from the function argument type.
5511 // Perform template argument deduction to determine whether we may be
5512 // specializing this template.
5513 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005514 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005515 FunctionDecl *Specialization = 0;
5516 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005517 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005518 FD->getType(),
5519 Specialization,
5520 Info)) {
5521 // FIXME: Template argument deduction failed; record why it failed, so
5522 // that we can provide nifty diagnostics.
5523 (void)TDK;
5524 continue;
5525 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005526
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005527 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005528 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005529 }
5530 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005531
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005532 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005533 UnresolvedSetIterator Result
5534 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005535 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005536 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005537 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005538 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005539 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005540 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005541 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005542 return true;
John McCallc373d482010-01-27 01:50:18 +00005543
5544 // Ignore access information; it doesn't figure into redeclaration checking.
5545 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005546
5547 FunctionTemplateSpecializationInfo *SpecInfo
5548 = Specialization->getTemplateSpecializationInfo();
5549 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005550
5551 // Note: do not overwrite location info if previous template
5552 // specialization kind was explicit.
5553 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5554 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5555 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005556
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005557 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005558 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005559
5560 // If this is a friend declaration, then we're not really declaring
5561 // an explicit specialization.
5562 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005563
Douglas Gregord5cb8762009-10-07 00:13:32 +00005564 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005565 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005566 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005567 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005568 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005569 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005570 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005571
5572 // C++ [temp.expl.spec]p6:
5573 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005574 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005575 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005576 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005577 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005578 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005579 if (!isFriend &&
5580 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005581 TSK_ExplicitSpecialization,
5582 Specialization,
5583 SpecInfo->getTemplateSpecializationKind(),
5584 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005585 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005586 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005587
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005588 // Mark the prior declaration as an explicit specialization, so that later
5589 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005590 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005591 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005592 MarkUnusedFileScopedDecl(Specialization);
5593 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005594
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005595 // Turn the given function declaration into a function template
5596 // specialization, with the template arguments from the previous
5597 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005598 // Take copies of (semantic and syntactic) template argument lists.
5599 const TemplateArgumentList* TemplArgs = new (Context)
5600 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005601 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005602 TemplArgs, /*InsertPos=*/0,
5603 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005604 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005605 FD->setStorageClass(Specialization->getStorageClass());
5606
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005607 // The "previous declaration" for this function template specialization is
5608 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005609 Previous.clear();
5610 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005611 return false;
5612}
5613
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005614/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005615/// specialization.
5616///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005617/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005618/// explicit member function specialization. On successful completion,
5619/// the function declaration \p FD will become a member function
5620/// specialization.
5621///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005622/// \param Member the member declaration, which will be updated to become a
5623/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005624///
John McCall68263142009-11-18 22:49:29 +00005625/// \param Previous the set of declarations, one of which may be specialized
5626/// by this function specialization; the set will be modified to contain the
5627/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005628bool
John McCall68263142009-11-18 22:49:29 +00005629Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005630 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005631
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005632 // Try to find the member we are instantiating.
5633 NamedDecl *Instantiation = 0;
5634 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005635 MemberSpecializationInfo *MSInfo = 0;
5636
John McCall68263142009-11-18 22:49:29 +00005637 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005638 // Nowhere to look anyway.
5639 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005640 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5641 I != E; ++I) {
5642 NamedDecl *D = (*I)->getUnderlyingDecl();
5643 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005644 if (Context.hasSameType(Function->getType(), Method->getType())) {
5645 Instantiation = Method;
5646 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005647 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005648 break;
5649 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005650 }
5651 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005652 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005653 VarDecl *PrevVar;
5654 if (Previous.isSingleResult() &&
5655 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005656 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005657 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005658 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005659 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005660 }
5661 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005662 CXXRecordDecl *PrevRecord;
5663 if (Previous.isSingleResult() &&
5664 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5665 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005666 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005667 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005668 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005669 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005670
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005671 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005672 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005673 // specializations are always out-of-line, the caller will complain about
5674 // this mismatch later.
5675 return false;
5676 }
John McCall77e8b112010-04-13 20:37:33 +00005677
5678 // If this is a friend, just bail out here before we start turning
5679 // things into explicit specializations.
5680 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5681 // Preserve instantiation information.
5682 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5683 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5684 cast<CXXMethodDecl>(InstantiatedFrom),
5685 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5686 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5687 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5688 cast<CXXRecordDecl>(InstantiatedFrom),
5689 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5690 }
5691
5692 Previous.clear();
5693 Previous.addDecl(Instantiation);
5694 return false;
5695 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005696
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005697 // Make sure that this is a specialization of a member.
5698 if (!InstantiatedFrom) {
5699 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5700 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005701 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5702 return true;
5703 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005704
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005705 // C++ [temp.expl.spec]p6:
5706 // If a template, a member template or the member of a class template is
Nico Weberff91d242011-12-23 20:58:04 +00005707 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005708 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005709 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005710 // use occurs; no diagnostic is required.
5711 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005712
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005713 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005714 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5715 TSK_ExplicitSpecialization,
5716 Instantiation,
5717 MSInfo->getTemplateSpecializationKind(),
5718 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005719 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005720 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005721
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005722 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005723 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005724 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005725 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005726 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005727 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005728
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005729 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005730 // the original declaration to note that it is an explicit specialization
5731 // (if it was previously an implicit instantiation). This latter step
5732 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005733 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005734 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5735 if (InstantiationFunction->getTemplateSpecializationKind() ==
5736 TSK_ImplicitInstantiation) {
5737 InstantiationFunction->setTemplateSpecializationKind(
5738 TSK_ExplicitSpecialization);
5739 InstantiationFunction->setLocation(Member->getLocation());
5740 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005741
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005742 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5743 cast<CXXMethodDecl>(InstantiatedFrom),
5744 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005745 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005746 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005747 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5748 if (InstantiationVar->getTemplateSpecializationKind() ==
5749 TSK_ImplicitInstantiation) {
5750 InstantiationVar->setTemplateSpecializationKind(
5751 TSK_ExplicitSpecialization);
5752 InstantiationVar->setLocation(Member->getLocation());
5753 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005754
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005755 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5756 cast<VarDecl>(InstantiatedFrom),
5757 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005758 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005759 } else {
5760 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005761 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5762 if (InstantiationClass->getTemplateSpecializationKind() ==
5763 TSK_ImplicitInstantiation) {
5764 InstantiationClass->setTemplateSpecializationKind(
5765 TSK_ExplicitSpecialization);
5766 InstantiationClass->setLocation(Member->getLocation());
5767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005768
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005769 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005770 cast<CXXRecordDecl>(InstantiatedFrom),
5771 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005773
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005774 // Save the caller the trouble of having to figure out which declaration
5775 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005776 Previous.clear();
5777 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005778 return false;
5779}
5780
Douglas Gregor558c0322009-10-14 23:41:34 +00005781/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005782///
5783/// \returns true if a serious error occurs, false otherwise.
5784static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005785 SourceLocation InstLoc,
5786 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005787 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5788 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005789
Douglas Gregor669eed82010-07-13 00:10:04 +00005790 if (CurContext->isRecord()) {
5791 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5792 << D;
5793 return true;
5794 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005795
Richard Smith3e2e91e2011-10-18 02:28:33 +00005796 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005797 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005798 // template. If the name declared in the explicit instantiation is an
5799 // unqualified name, the explicit instantiation shall appear in the
5800 // namespace where its template is declared or, if that namespace is inline
5801 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005802 //
5803 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005804 if (WasQualifiedName) {
5805 if (CurContext->Encloses(OrigContext))
5806 return false;
5807 } else {
5808 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5809 return false;
5810 }
5811
5812 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5813 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005814 S.Diag(InstLoc,
5815 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005816 diag::err_explicit_instantiation_out_of_scope :
5817 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005818 << D << NS;
5819 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005820 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005821 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005822 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5823 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5824 << D << NS;
5825 } else
5826 S.Diag(InstLoc,
5827 S.getLangOptions().CPlusPlus0x?
5828 diag::err_explicit_instantiation_must_be_global :
5829 diag::warn_explicit_instantiation_must_be_global_0x)
5830 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005831 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005832 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005833}
5834
5835/// \brief Determine whether the given scope specifier has a template-id in it.
5836static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5837 if (!SS.isSet())
5838 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005839
Richard Smith3e2e91e2011-10-18 02:28:33 +00005840 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005841 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005842 // or a static data member of a class template specialization, the name of
5843 // the class template specialization in the qualified-id for the member
5844 // name shall be a simple-template-id.
5845 //
5846 // C++98 has the same restriction, just worded differently.
5847 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5848 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005849 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005850 if (isa<TemplateSpecializationType>(T))
5851 return true;
5852
5853 return false;
5854}
5855
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005856// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005857DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005858Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005859 SourceLocation ExternLoc,
5860 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005861 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005862 SourceLocation KWLoc,
5863 const CXXScopeSpec &SS,
5864 TemplateTy TemplateD,
5865 SourceLocation TemplateNameLoc,
5866 SourceLocation LAngleLoc,
5867 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005868 SourceLocation RAngleLoc,
5869 AttributeList *Attr) {
5870 // Find the class template we're specializing
5871 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005872 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005873 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5874
5875 // Check that the specialization uses the same tag kind as the
5876 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005877 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5878 assert(Kind != TTK_Enum &&
5879 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005880 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005881 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005882 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005883 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005884 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005885 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005886 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005887 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005888 diag::note_previous_use);
5889 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5890 }
5891
Douglas Gregor558c0322009-10-14 23:41:34 +00005892 // C++0x [temp.explicit]p2:
5893 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005894 // definition and an explicit instantiation declaration. An explicit
5895 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005896 TemplateSpecializationKind TSK
5897 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5898 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005899
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005900 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005901 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005902 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005903
5904 // Check that the template argument list is well-formed for this
5905 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005906 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005907 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5908 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005909 return true;
5910
Douglas Gregor910f8002010-11-07 23:05:16 +00005911 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005912 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005913
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005914 // Find the class template specialization declaration that
5915 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005916 void *InsertPos = 0;
5917 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005918 = ClassTemplate->findSpecialization(Converted.data(),
5919 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005920
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005921 TemplateSpecializationKind PrevDecl_TSK
5922 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5923
Douglas Gregord5cb8762009-10-07 00:13:32 +00005924 // C++0x [temp.explicit]p2:
5925 // [...] An explicit instantiation shall appear in an enclosing
5926 // namespace of its template. [...]
5927 //
5928 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005929 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5930 SS.isSet()))
5931 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005932
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005933 ClassTemplateSpecializationDecl *Specialization = 0;
5934
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005935 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005936 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005937 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005938 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005939 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005940 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005941 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005942
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005943 // Even though HasNoEffect == true means that this explicit instantiation
5944 // has no effect on semantics, we go on to put its syntax in the AST.
5945
5946 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5947 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005948 // Since the only prior class template specialization with these
5949 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005950 // declaration node as our own, updating the source location
5951 // for the template name to reflect our new declaration.
5952 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005953 Specialization = PrevDecl;
5954 Specialization->setLocation(TemplateNameLoc);
5955 PrevDecl = 0;
5956 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005957 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005958
Douglas Gregor52604ab2009-09-11 21:19:12 +00005959 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005960 // Create a new class template specialization declaration node for
5961 // this explicit specialization.
5962 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005963 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005964 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005965 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005966 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005967 Converted.data(),
5968 Converted.size(),
5969 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005970 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005971
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005972 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005973 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005974 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005975 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005976 }
5977
5978 // Build the fully-sugared type for this explicit instantiation as
5979 // the user wrote in the explicit instantiation itself. This means
5980 // that we'll pretty-print the type retrieved from the
5981 // specialization's declaration the way that the user actually wrote
5982 // the explicit instantiation, rather than formatting the name based
5983 // on the "canonical" representation used to store the template
5984 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005985 TypeSourceInfo *WrittenTy
5986 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5987 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005988 Context.getTypeDeclType(Specialization));
5989 Specialization->setTypeAsWritten(WrittenTy);
5990 TemplateArgsIn.release();
5991
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005992 // Set source locations for keywords.
5993 Specialization->setExternLoc(ExternLoc);
5994 Specialization->setTemplateKeywordLoc(TemplateLoc);
5995
Rafael Espindola0257b7f2012-01-03 06:04:21 +00005996 if (Attr)
5997 ProcessDeclAttributeList(S, Specialization, Attr);
5998
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005999 // Add the explicit instantiation into its lexical context. However,
6000 // since explicit instantiations are never found by name lookup, we
6001 // just put it into the declaration context directly.
6002 Specialization->setLexicalDeclContext(CurContext);
6003 CurContext->addDecl(Specialization);
6004
6005 // Syntax is now OK, so return if it has no other effect on semantics.
6006 if (HasNoEffect) {
6007 // Set the template specialization kind.
6008 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006009 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00006010 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006011
6012 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006013 // A definition of a class template or class member template
6014 // shall be in scope at the point of the explicit instantiation of
6015 // the class template or class member template.
6016 //
6017 // This check comes when we actually try to perform the
6018 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006019 ClassTemplateSpecializationDecl *Def
6020 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006021 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006022 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006023 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006024 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006025 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006026 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6027 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006028
Douglas Gregor0d035142009-10-27 18:42:08 +00006029 // Instantiate the members of this class template specialization.
6030 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006031 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006032 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006033 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6034
6035 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6036 // TSK_ExplicitInstantiationDefinition
6037 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6038 TSK == TSK_ExplicitInstantiationDefinition)
6039 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006040
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006041 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006042 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006043
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006044 // Set the template specialization kind.
6045 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006046 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006047}
6048
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006049// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006050DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006051Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006052 SourceLocation ExternLoc,
6053 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006054 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006055 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006056 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006057 IdentifierInfo *Name,
6058 SourceLocation NameLoc,
6059 AttributeList *Attr) {
6060
Douglas Gregor402abb52009-05-28 23:31:59 +00006061 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006062 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006063 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006064 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006065 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006066 MultiTemplateParamsArg(*this, 0, 0),
Richard Smithbdad7a22012-01-10 01:33:14 +00006067 Owned, IsDependent, SourceLocation(), false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006068 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006069 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6070
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006071 if (!TagD)
6072 return true;
6073
John McCalld226f652010-08-21 09:40:31 +00006074 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006075 if (Tag->isEnum()) {
6076 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6077 << Context.getTypeDeclType(Tag);
6078 return true;
6079 }
6080
Douglas Gregord0c87372009-05-27 17:30:49 +00006081 if (Tag->isInvalidDecl())
6082 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006083
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006084 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6085 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6086 if (!Pattern) {
6087 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6088 << Context.getTypeDeclType(Record);
6089 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6090 return true;
6091 }
6092
Douglas Gregor558c0322009-10-14 23:41:34 +00006093 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006094 // If the explicit instantiation is for a class or member class, the
6095 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006096 // simple-template-id.
6097 //
6098 // C++98 has the same restriction, just worded differently.
6099 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006100 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006101 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006102
Douglas Gregor558c0322009-10-14 23:41:34 +00006103 // C++0x [temp.explicit]p2:
6104 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006105 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006106 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006107 TemplateSpecializationKind TSK
6108 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6109 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006110
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006111 // C++0x [temp.explicit]p2:
6112 // [...] An explicit instantiation shall appear in an enclosing
6113 // namespace of its template. [...]
6114 //
6115 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006116 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006117
Douglas Gregor454885e2009-10-15 15:54:05 +00006118 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006119 CXXRecordDecl *PrevDecl
Douglas Gregoref96ee02012-01-14 16:38:05 +00006120 = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
Douglas Gregor952b0172010-02-11 01:04:33 +00006121 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006122 PrevDecl = Record;
6123 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006124 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006125 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006126 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006127 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006128 PrevDecl,
6129 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006130 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006131 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006132 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006133 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006134 return TagD;
6135 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006136
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006137 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006138 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006139 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006140 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006141 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006142 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006143 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006144 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006145 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006146 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6147 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006148 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6149 << Pattern;
6150 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006151 } else {
6152 if (InstantiateClass(NameLoc, Record, Def,
6153 getTemplateInstantiationArgs(Record),
6154 TSK))
6155 return true;
6156
Douglas Gregor952b0172010-02-11 01:04:33 +00006157 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006158 if (!RecordDef)
6159 return true;
6160 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006161 }
6162
Douglas Gregor0d035142009-10-27 18:42:08 +00006163 // Instantiate all of the members of the class.
6164 InstantiateClassMembers(NameLoc, RecordDef,
6165 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006166
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006167 if (TSK == TSK_ExplicitInstantiationDefinition)
6168 MarkVTableUsed(NameLoc, RecordDef, true);
6169
Mike Stump390b4cc2009-05-16 07:39:55 +00006170 // FIXME: We don't have any representation for explicit instantiations of
6171 // member classes. Such a representation is not needed for compilation, but it
6172 // should be available for clients that want to see all of the declarations in
6173 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006174 return TagD;
6175}
6176
John McCallf312b1e2010-08-26 23:41:50 +00006177DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6178 SourceLocation ExternLoc,
6179 SourceLocation TemplateLoc,
6180 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006181 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006182 // TODO: check if/when DNInfo should replace Name.
6183 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6184 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006185 if (!Name) {
6186 if (!D.isInvalidType())
6187 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6188 diag::err_explicit_instantiation_requires_name)
6189 << D.getDeclSpec().getSourceRange()
6190 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006191
Douglas Gregord5a423b2009-09-25 18:43:00 +00006192 return true;
6193 }
6194
6195 // The scope passed in may not be a decl scope. Zip up the scope tree until
6196 // we find one that is.
6197 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6198 (S->getFlags() & Scope::TemplateParamScope) != 0)
6199 S = S->getParent();
6200
6201 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006202 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6203 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006204 if (R.isNull())
6205 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006206
Douglas Gregore885e182011-05-21 18:53:30 +00006207 // C++ [dcl.stc]p1:
6208 // A storage-class-specifier shall not be specified in [...] an explicit
6209 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006210 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006211 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6212 << Name;
6213 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006214 } else if (D.getDeclSpec().getStorageClassSpec()
6215 != DeclSpec::SCS_unspecified) {
6216 // Complain about then remove the storage class specifier.
6217 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6218 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6219
6220 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006221 }
6222
Douglas Gregor663b5a02009-10-14 20:14:33 +00006223 // C++0x [temp.explicit]p1:
6224 // [...] An explicit instantiation of a function template shall not use the
6225 // inline or constexpr specifiers.
6226 // Presumably, this also applies to member functions of class templates as
6227 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006228 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006229 Diag(D.getDeclSpec().getInlineSpecLoc(),
Richard Smith2dc7ece2011-10-18 03:44:03 +00006230 getLangOptions().CPlusPlus0x ?
6231 diag::err_explicit_instantiation_inline :
6232 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006233 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6234 if (D.getDeclSpec().isConstexprSpecified())
6235 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6236 // not already specified.
6237 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6238 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006239
Douglas Gregor558c0322009-10-14 23:41:34 +00006240 // C++0x [temp.explicit]p2:
6241 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006242 // definition and an explicit instantiation declaration. An explicit
6243 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006244 TemplateSpecializationKind TSK
6245 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6246 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006247
Abramo Bagnara25777432010-08-11 22:01:17 +00006248 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006249 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006250
6251 if (!R->isFunctionType()) {
6252 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006253 // A [...] static data member of a class template can be explicitly
6254 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006255 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006256 if (Previous.isAmbiguous())
6257 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006258
John McCall1bcee0a2009-12-02 08:25:40 +00006259 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006260 if (!Prev || !Prev->isStaticDataMember()) {
6261 // We expect to see a data data member here.
6262 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6263 << Name;
6264 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6265 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006266 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006267 return true;
6268 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006269
Douglas Gregord5a423b2009-09-25 18:43:00 +00006270 if (!Prev->getInstantiatedFromStaticDataMember()) {
6271 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006272 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006273 diag::err_explicit_instantiation_data_member_not_instantiated)
6274 << Prev;
6275 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6276 // FIXME: Can we provide a note showing where this was declared?
6277 return true;
6278 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006279
Douglas Gregor558c0322009-10-14 23:41:34 +00006280 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006281 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006282 // or a static data member of a class template specialization, the name of
6283 // the class template specialization in the qualified-id for the member
6284 // name shall be a simple-template-id.
6285 //
6286 // C++98 has the same restriction, just worded differently.
6287 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006288 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006289 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006290 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006291
Douglas Gregor558c0322009-10-14 23:41:34 +00006292 // Check the scope of this explicit instantiation.
6293 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006294
Douglas Gregor454885e2009-10-15 15:54:05 +00006295 // Verify that it is okay to explicitly instantiate here.
6296 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6297 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006298 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006299 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006300 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006301 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006302 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006303 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006304 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006305 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006306
Douglas Gregord5a423b2009-09-25 18:43:00 +00006307 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006308 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006309 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006310 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006311
Douglas Gregord5a423b2009-09-25 18:43:00 +00006312 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006313 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006314 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006315
6316 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006317 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006318 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006319 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006320 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6321 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006322 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6323 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006324 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6325 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006326 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006327 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006328 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006329 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006330 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006331
Douglas Gregord5a423b2009-09-25 18:43:00 +00006332 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006333 // A [...] function [...] can be explicitly instantiated from its template.
6334 // A member function [...] of a class template can be explicitly
6335 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006336 // template.
John McCallc373d482010-01-27 01:50:18 +00006337 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006338 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6339 P != PEnd; ++P) {
6340 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006341 if (!HasExplicitTemplateArgs) {
6342 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6343 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6344 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006345
John McCallc373d482010-01-27 01:50:18 +00006346 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006347 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6348 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006349 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006350 }
6351 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006352
Douglas Gregord5a423b2009-09-25 18:43:00 +00006353 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6354 if (!FunTmpl)
6355 continue;
6356
John McCall5769d612010-02-08 23:07:23 +00006357 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006358 FunctionDecl *Specialization = 0;
6359 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006360 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006361 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006362 R, Specialization, Info)) {
6363 // FIXME: Keep track of almost-matches?
6364 (void)TDK;
6365 continue;
6366 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006367
John McCallc373d482010-01-27 01:50:18 +00006368 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006369 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006370
Douglas Gregord5a423b2009-09-25 18:43:00 +00006371 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006372 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006373 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006374 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006375 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6376 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6377 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006378
John McCallc373d482010-01-27 01:50:18 +00006379 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006380 return true;
John McCallc373d482010-01-27 01:50:18 +00006381
6382 // Ignore access control bits, we don't need them for redeclaration checking.
6383 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006384
Douglas Gregor0a897e32009-10-15 17:21:20 +00006385 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006386 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006387 diag::err_explicit_instantiation_member_function_not_instantiated)
6388 << Specialization
6389 << (Specialization->getTemplateSpecializationKind() ==
6390 TSK_ExplicitSpecialization);
6391 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6392 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006393 }
6394
Douglas Gregoref96ee02012-01-14 16:38:05 +00006395 FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006396 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6397 PrevDecl = Specialization;
6398
Douglas Gregor0a897e32009-10-15 17:21:20 +00006399 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006400 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006401 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006402 PrevDecl,
6403 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006404 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006405 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006406 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006407
Douglas Gregor0a897e32009-10-15 17:21:20 +00006408 // FIXME: We may still want to build some representation of this
6409 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006410 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006411 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006412 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006413
6414 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Rafael Espindola256fc4d2012-01-04 05:40:59 +00006415 AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
6416 if (Attr)
6417 ProcessDeclAttributeList(S, Specialization, Attr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006418
Douglas Gregor0a897e32009-10-15 17:21:20 +00006419 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006420 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006421
Douglas Gregor558c0322009-10-14 23:41:34 +00006422 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006423 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006424 // or a static data member of a class template specialization, the name of
6425 // the class template specialization in the qualified-id for the member
6426 // name shall be a simple-template-id.
6427 //
6428 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006429 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006430 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006431 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006432 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006433 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006434 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006435 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006436
Douglas Gregor558c0322009-10-14 23:41:34 +00006437 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006438 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006439 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006440 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006441 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006442
Douglas Gregord5a423b2009-09-25 18:43:00 +00006443 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006444 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006445}
6446
John McCallf312b1e2010-08-26 23:41:50 +00006447TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006448Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6449 const CXXScopeSpec &SS, IdentifierInfo *Name,
6450 SourceLocation TagLoc, SourceLocation NameLoc) {
6451 // This has to hold, because SS is expected to be defined.
6452 assert(Name && "Expected a name in a dependent tag");
6453
6454 NestedNameSpecifier *NNS
6455 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6456 if (!NNS)
6457 return true;
6458
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006459 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006460
Douglas Gregor48c89f42010-04-24 16:38:41 +00006461 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6462 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006463 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006464 return true;
6465 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006466
Douglas Gregor059101f2011-03-02 00:47:37 +00006467 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006468 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006469 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6470
6471 // Create type-source location information for this type.
6472 TypeLocBuilder TLB;
6473 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6474 TL.setKeywordLoc(TagLoc);
6475 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6476 TL.setNameLoc(NameLoc);
6477 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006478}
6479
John McCallf312b1e2010-08-26 23:41:50 +00006480TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006481Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6482 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006483 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006484 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006485 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006486
Richard Smithebaf0e62011-10-18 20:49:44 +00006487 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6488 Diag(TypenameLoc,
6489 getLangOptions().CPlusPlus0x ?
6490 diag::warn_cxx98_compat_typename_outside_of_template :
6491 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006492 << FixItHint::CreateRemoval(TypenameLoc);
6493
Douglas Gregor2494dd02011-03-01 01:34:45 +00006494 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006495 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6496 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006497 if (T.isNull())
6498 return true;
John McCall63b43852010-04-29 23:50:39 +00006499
6500 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6501 if (isa<DependentNameType>(T)) {
6502 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006503 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006504 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006505 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006506 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006507 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006508 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006509 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006510 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006511 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006512
John McCallb3d87482010-08-24 05:47:05 +00006513 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006514}
6515
John McCallf312b1e2010-08-26 23:41:50 +00006516TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006517Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6518 const CXXScopeSpec &SS,
6519 SourceLocation TemplateLoc,
6520 TemplateTy TemplateIn,
6521 SourceLocation TemplateNameLoc,
6522 SourceLocation LAngleLoc,
6523 ASTTemplateArgsPtr TemplateArgsIn,
6524 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006525 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6526 Diag(TypenameLoc,
6527 getLangOptions().CPlusPlus0x ?
6528 diag::warn_cxx98_compat_typename_outside_of_template :
6529 diag::ext_typename_outside_of_template)
6530 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006531
6532 // Translate the parser's template argument list in our AST format.
6533 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6534 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6535
6536 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006537 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6538 // Construct a dependent template specialization type.
6539 assert(DTN && "dependent template has non-dependent name?");
6540 assert(DTN->getQualifier()
6541 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6542 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6543 DTN->getQualifier(),
6544 DTN->getIdentifier(),
6545 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006546
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006547 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006548 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006549 DependentTemplateSpecializationTypeLoc SpecTL
6550 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006551 SpecTL.setLAngleLoc(LAngleLoc);
6552 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006553 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006554 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006555 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006556 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6557 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006558 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006559 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006560
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006561 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6562 if (T.isNull())
6563 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006564
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006565 // Provide source-location information for the template specialization
6566 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006567 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006568 TemplateSpecializationTypeLoc SpecTL
6569 = Builder.push<TemplateSpecializationTypeLoc>(T);
6570
6571 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006572 SpecTL.setLAngleLoc(LAngleLoc);
6573 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006574 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006575 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6576 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6577
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006578 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6579 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6580 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006581 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6582
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006583 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6584 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006585}
6586
Douglas Gregora02411e2011-02-27 22:46:49 +00006587
Douglas Gregord57959a2009-03-27 23:10:48 +00006588/// \brief Build the type that describes a C++ typename specifier,
6589/// e.g., "typename T::type".
6590QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006591Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6592 SourceLocation KeywordLoc,
6593 NestedNameSpecifierLoc QualifierLoc,
6594 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006595 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006596 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006597 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006598
John McCall77bb1aa2010-05-01 00:40:08 +00006599 DeclContext *Ctx = computeDeclContext(SS);
6600 if (!Ctx) {
6601 // If the nested-name-specifier is dependent and couldn't be
6602 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006603 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6604 return Context.getDependentNameType(Keyword,
6605 QualifierLoc.getNestedNameSpecifier(),
6606 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006607 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006608
John McCall77bb1aa2010-05-01 00:40:08 +00006609 // If the nested-name-specifier refers to the current instantiation,
6610 // the "typename" keyword itself is superfluous. In C++03, the
6611 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6612 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006613 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006614
John McCall77bb1aa2010-05-01 00:40:08 +00006615 if (RequireCompleteDeclContext(SS, Ctx))
6616 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006617
6618 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006619 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006620 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006621 unsigned DiagID = 0;
6622 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006623 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006624 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006625 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006626 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006627
6628 case LookupResult::FoundUnresolvedValue: {
6629 // We found a using declaration that is a value. Most likely, the using
6630 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006631 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006632 IILoc);
6633 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6634 << Name << Ctx << FullRange;
6635 if (UnresolvedUsingValueDecl *Using
6636 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006637 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006638 Diag(Loc, diag::note_using_value_decl_missing_typename)
6639 << FixItHint::CreateInsertion(Loc, "typename ");
6640 }
6641 }
6642 // Fall through to create a dependent typename type, from which we can recover
6643 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006644
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006645 case LookupResult::NotFoundInCurrentInstantiation:
6646 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006647 return Context.getDependentNameType(Keyword,
6648 QualifierLoc.getNestedNameSpecifier(),
6649 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006650
6651 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006652 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006653 // We found a type. Build an ElaboratedType, since the
6654 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006655 return Context.getElaboratedType(ETK_Typename,
6656 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006657 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006658 }
6659
6660 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006661 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006662 break;
6663
6664 case LookupResult::FoundOverloaded:
6665 DiagID = diag::err_typename_nested_not_type;
6666 Referenced = *Result.begin();
6667 break;
6668
John McCall6e247262009-10-10 05:48:19 +00006669 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006670 return QualType();
6671 }
6672
6673 // If we get here, it's because name lookup did not find a
6674 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006675 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006676 IILoc);
6677 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006678 if (Referenced)
6679 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6680 << Name;
6681 return QualType();
6682}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006683
6684namespace {
6685 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006686 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006687 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006688 SourceLocation Loc;
6689 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006690
Douglas Gregor4a959d82009-08-06 16:20:37 +00006691 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006692 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006693
Mike Stump1eb44332009-09-09 15:08:12 +00006694 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006695 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006696 DeclarationName Entity)
6697 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006698 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006699
6700 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006701 /// transformed.
6702 ///
6703 /// For the purposes of type reconstruction, a type has already been
6704 /// transformed if it is NULL or if it is not dependent.
6705 bool AlreadyTransformed(QualType T) {
6706 return T.isNull() || !T->isDependentType();
6707 }
Mike Stump1eb44332009-09-09 15:08:12 +00006708
6709 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006710 /// rebuilt.
6711 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006712
Douglas Gregor4a959d82009-08-06 16:20:37 +00006713 /// \brief Returns the name of the entity whose type is being rebuilt.
6714 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006715
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006716 /// \brief Sets the "base" location and entity when that
6717 /// information is known based on another transformation.
6718 void setBase(SourceLocation Loc, DeclarationName Entity) {
6719 this->Loc = Loc;
6720 this->Entity = Entity;
6721 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006722 };
6723}
6724
Douglas Gregor4a959d82009-08-06 16:20:37 +00006725/// \brief Rebuilds a type within the context of the current instantiation.
6726///
Mike Stump1eb44332009-09-09 15:08:12 +00006727/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006728/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006729/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006730/// partial specialization thereof). This routine will rebuild that type now
6731/// that we have entered the declarator's scope, which may produce different
6732/// canonical types, e.g.,
6733///
6734/// \code
6735/// template<typename T>
6736/// struct X {
6737/// typedef T* pointer;
6738/// pointer data();
6739/// };
6740///
6741/// template<typename T>
6742/// typename X<T>::pointer X<T>::data() { ... }
6743/// \endcode
6744///
Douglas Gregor4714c122010-03-31 17:34:00 +00006745/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006746/// since we do not know that we can look into X<T> when we parsed the type.
6747/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006748/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006749/// as the canonical type of T*, allowing the return types of the out-of-line
6750/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006751TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6752 SourceLocation Loc,
6753 DeclarationName Name) {
6754 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006755 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006756
Douglas Gregor4a959d82009-08-06 16:20:37 +00006757 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6758 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006759}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006760
John McCall60d7b3a2010-08-24 06:29:42 +00006761ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006762 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6763 DeclarationName());
6764 return Rebuilder.TransformExpr(E);
6765}
6766
John McCall63b43852010-04-29 23:50:39 +00006767bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006768 if (SS.isInvalid())
6769 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006770
Douglas Gregor7e384942011-02-25 16:07:42 +00006771 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006772 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6773 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006774 NestedNameSpecifierLoc Rebuilt
6775 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6776 if (!Rebuilt)
6777 return true;
John McCall63b43852010-04-29 23:50:39 +00006778
Douglas Gregor7e384942011-02-25 16:07:42 +00006779 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006780 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006781}
6782
Douglas Gregor20606502011-10-14 15:31:12 +00006783/// \brief Rebuild the template parameters now that we know we're in a current
6784/// instantiation.
6785bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6786 TemplateParameterList *Params) {
6787 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6788 Decl *Param = Params->getParam(I);
6789
6790 // There is nothing to rebuild in a type parameter.
6791 if (isa<TemplateTypeParmDecl>(Param))
6792 continue;
6793
6794 // Rebuild the template parameter list of a template template parameter.
6795 if (TemplateTemplateParmDecl *TTP
6796 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6797 if (RebuildTemplateParamsInCurrentInstantiation(
6798 TTP->getTemplateParameters()))
6799 return true;
6800
6801 continue;
6802 }
6803
6804 // Rebuild the type of a non-type template parameter.
6805 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6806 TypeSourceInfo *NewTSI
6807 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6808 NTTP->getLocation(),
6809 NTTP->getDeclName());
6810 if (!NewTSI)
6811 return true;
6812
6813 if (NewTSI != NTTP->getTypeSourceInfo()) {
6814 NTTP->setTypeSourceInfo(NewTSI);
6815 NTTP->setType(NewTSI->getType());
6816 }
6817 }
6818
6819 return false;
6820}
6821
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006822/// \brief Produces a formatted string that describes the binding of
6823/// template parameters to template arguments.
6824std::string
6825Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6826 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006827 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006828}
6829
6830std::string
6831Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6832 const TemplateArgument *Args,
6833 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006834 llvm::SmallString<128> Str;
6835 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006836
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006837 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006838 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006839
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006840 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006841 if (I >= NumArgs)
6842 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006843
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006844 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006845 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006846 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006847 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006848
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006849 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006850 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006851 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006852 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006853 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006854
Douglas Gregor87dd6972010-12-20 16:52:59 +00006855 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006856 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006857 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006858
6859 Out << ']';
6860 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006861}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006862
6863void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6864 if (!FD)
6865 return;
6866 FD->setLateTemplateParsed(Flag);
6867}
6868
6869bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6870 DeclContext *DC = CurContext;
6871
6872 while (DC) {
6873 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6874 const FunctionDecl *FD = RD->isLocalClass();
6875 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6876 } else if (DC->isTranslationUnit() || DC->isNamespace())
6877 return false;
6878
6879 DC = DC->getParent();
6880 }
6881 return false;
6882}