blob: ca34ada132436e64489767822334698f67e0d429 [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Benjamin Kramer013b3662012-01-30 16:17:39 +000029#include "llvm/ADT/SmallBitVector.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000030#include "llvm/ADT/SmallString.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000031#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000032using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000033using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000034
John McCall78b81052010-11-10 02:40:36 +000035// Exported for use by Parser.
36SourceRange
37clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
38 unsigned N) {
39 if (!N) return SourceRange();
40 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
41}
42
Douglas Gregor2dd078a2009-09-02 22:59:36 +000043/// \brief Determine whether the declaration found is acceptable as the name
44/// of a template and, if so, return that template declaration. Otherwise,
45/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000046static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
47 NamedDecl *Orig) {
48 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000049
Douglas Gregor2dd078a2009-09-02 22:59:36 +000050 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000051 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000052
Douglas Gregor2dd078a2009-09-02 22:59:36 +000053 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
54 // C++ [temp.local]p1:
55 // Like normal (non-template) classes, class templates have an
56 // injected-class-name (Clause 9). The injected-class-name
57 // can be used with or without a template-argument-list. When
58 // it is used without a template-argument-list, it is
59 // equivalent to the injected-class-name followed by the
60 // template-parameters of the class template enclosed in
61 // <>. When it is used with a template-argument-list, it
62 // refers to the specified class template specialization,
63 // which could be the current specialization or another
64 // specialization.
65 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000066 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000067 if (Record->getDescribedClassTemplate())
68 return Record->getDescribedClassTemplate();
69
70 if (ClassTemplateSpecializationDecl *Spec
71 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
72 return Spec->getSpecializedTemplate();
73 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor2dd078a2009-09-02 22:59:36 +000075 return 0;
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Douglas Gregor2dd078a2009-09-02 22:59:36 +000078 return 0;
79}
80
Douglas Gregor312eadb2011-04-24 05:37:28 +000081void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000082 // The set of class templates we've already seen.
83 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000084 LookupResult::Filter filter = R.makeFilter();
85 while (filter.hasNext()) {
86 NamedDecl *Orig = filter.next();
Douglas Gregor312eadb2011-04-24 05:37:28 +000087 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000088 if (!Repl)
89 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000090 else if (Repl != Orig) {
91
92 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000094 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000095 // one base class). If all of the injected-class-names that are found
96 // refer to specializations of the same class template, and if the name
Richard Smith3e4c6c42011-05-05 21:57:07 +000097 // is used as a template-name, the reference refers to the class
98 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000099 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000100 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
101 if (!ClassTemplates.insert(ClassTmpl)) {
102 filter.erase();
103 continue;
104 }
John McCall8ba66912010-08-13 07:02:08 +0000105
106 // FIXME: we promote access to public here as a workaround to
107 // the fact that LookupResult doesn't let us remember that we
108 // found this template through a particular injected class name,
109 // which means we end up doing nasty things to the invariants.
110 // Pretending that access is public is *much* safer.
111 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000112 }
John McCallf7a1a742009-11-24 19:00:30 +0000113 }
114 filter.done();
115}
116
Douglas Gregor312eadb2011-04-24 05:37:28 +0000117bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
118 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
119 if (isAcceptableTemplateName(Context, *I))
120 return true;
121
Douglas Gregor3b887352011-04-27 04:48:22 +0000122 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000123}
124
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000125TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000126 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000127 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000128 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000129 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000130 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000131 TemplateTy &TemplateResult,
132 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000133 assert(getLangOptions().CPlusPlus && "No template names in C!");
134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000136 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000137
Douglas Gregor014e88d2009-11-03 23:16:33 +0000138 switch (Name.getKind()) {
139 case UnqualifiedId::IK_Identifier:
140 TName = DeclarationName(Name.Identifier);
141 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000142
Douglas Gregor014e88d2009-11-03 23:16:33 +0000143 case UnqualifiedId::IK_OperatorFunctionId:
144 TName = Context.DeclarationNames.getCXXOperatorName(
145 Name.OperatorFunctionId.Operator);
146 break;
147
Sean Hunte6252d12009-11-28 08:58:14 +0000148 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000149 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
150 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000151
Douglas Gregor014e88d2009-11-03 23:16:33 +0000152 default:
153 return TNK_Non_template;
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
John McCallb3d87482010-08-24 05:47:05 +0000156 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000157
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000158 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000159 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000160 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
161 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000162 if (R.empty()) return TNK_Non_template;
163 if (R.isAmbiguous()) {
164 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000165 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000166
167 // FIXME: we might have ambiguous templates, in which case we
168 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000169 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000170 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000171
John McCall0bd6feb2009-12-02 08:04:21 +0000172 TemplateName Template;
173 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
John McCall0bd6feb2009-12-02 08:04:21 +0000175 unsigned ResultCount = R.end() - R.begin();
176 if (ResultCount > 1) {
177 // We assume that we'll preserve the qualifier from a function
178 // template name in other ways.
179 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
180 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000181
182 // We'll do this lookup again later.
183 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000184 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000185 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
186
187 if (SS.isSet() && !SS.isInvalid()) {
188 NestedNameSpecifier *Qualifier
189 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000190 Template = Context.getQualifiedTemplateName(Qualifier,
191 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000192 } else {
193 Template = TemplateName(TD);
194 }
195
John McCallb8592062010-08-13 02:23:42 +0000196 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000197 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000198
199 // We'll do this lookup again later.
200 R.suppressDiagnostics();
201 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000202 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
203 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000204 TemplateKind = TNK_Type_template;
205 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
John McCall0bd6feb2009-12-02 08:04:21 +0000208 TemplateResult = TemplateTy::make(Template);
209 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000210}
211
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000212bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000213 SourceLocation IILoc,
214 Scope *S,
215 const CXXScopeSpec *SS,
216 TemplateTy &SuggestedTemplate,
217 TemplateNameKind &SuggestedKind) {
218 // We can't recover unless there's a dependent scope specifier preceding the
219 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000220 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000221 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
222 computeDeclContext(*SS))
223 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000224
Douglas Gregor84d0a192010-01-12 21:28:44 +0000225 // The code is missing a 'template' keyword prior to the dependent template
226 // name.
227 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
228 Diag(IILoc, diag::err_template_kw_missing)
229 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000230 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000231 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000232 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
233 SuggestedKind = TNK_Dependent_template_name;
234 return true;
235}
236
John McCallf7a1a742009-11-24 19:00:30 +0000237void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000238 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000239 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000240 bool EnteringContext,
241 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000242 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000243 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000244 DeclContext *LookupCtx = 0;
245 bool isDependent = false;
246 if (!ObjectType.isNull()) {
247 // This nested-name-specifier occurs in a member access expression, e.g.,
248 // x->B::f, and we are looking into the type of the object.
249 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
250 LookupCtx = computeDeclContext(ObjectType);
251 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000252 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000253 "Caller should have completed object type");
Douglas Gregor1d7049a2012-01-12 16:11:24 +0000254
255 // Template names cannot appear inside an Objective-C class or object type.
256 if (ObjectType->isObjCObjectOrInterfaceType()) {
257 Found.clear();
258 return;
259 }
John McCallf7a1a742009-11-24 19:00:30 +0000260 } else if (SS.isSet()) {
261 // This nested-name-specifier occurs after another nested-name-specifier,
262 // so long into the context associated with the prior nested-name-specifier.
263 LookupCtx = computeDeclContext(SS, EnteringContext);
264 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000265
John McCallf7a1a742009-11-24 19:00:30 +0000266 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000267 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000268 return;
269 }
270
271 bool ObjectTypeSearchedInScope = false;
272 if (LookupCtx) {
273 // Perform "qualified" name lookup into the declaration context we
274 // computed, which is either the type of the base of a member access
275 // expression or the declaration context associated with a prior
276 // nested-name-specifier.
277 LookupQualifiedName(Found, LookupCtx);
278
279 if (!ObjectType.isNull() && Found.empty()) {
280 // C++ [basic.lookup.classref]p1:
281 // In a class member access expression (5.2.5), if the . or -> token is
282 // immediately followed by an identifier followed by a <, the
283 // identifier must be looked up to determine whether the < is the
284 // beginning of a template argument list (14.2) or a less-than operator.
285 // The identifier is first looked up in the class of the object
286 // expression. If the identifier is not found, it is then looked up in
287 // the context of the entire postfix-expression and shall name a class
288 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000289 if (S) LookupName(Found, S);
290 ObjectTypeSearchedInScope = true;
291 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000292 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000293 // We cannot look into a dependent object type or nested nme
294 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000295 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000296 return;
297 } else {
298 // Perform unqualified name lookup in the current scope.
299 LookupName(Found, S);
300 }
301
Douglas Gregor2e933882010-01-12 17:06:20 +0000302 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000303 // If we did not find any names, attempt to correct any typos.
304 DeclarationName Name = Found.getLookupName();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000305 Found.clear();
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000306 // Simple filter callback that, for keywords, only accepts the C++ *_cast
307 CorrectionCandidateCallback FilterCCC;
308 FilterCCC.WantTypeSpecifiers = false;
309 FilterCCC.WantExpressionKeywords = false;
310 FilterCCC.WantRemainingKeywords = false;
311 FilterCCC.WantCXXNamedCasts = true;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000312 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
313 Found.getLookupKind(), S, &SS,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000314 FilterCCC, LookupCtx)) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000315 Found.setLookupName(Corrected.getCorrection());
316 if (Corrected.getCorrectionDecl())
317 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000318 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000319 if (!Found.empty()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000320 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
321 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000322 if (LookupCtx)
323 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000324 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
325 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000326 else
327 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000328 << Name << CorrectedQuotedStr
329 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000330 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
331 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000332 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000333 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000334 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000335 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000336 }
337 }
338
Douglas Gregor312eadb2011-04-24 05:37:28 +0000339 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000340 if (Found.empty()) {
341 if (isDependent)
342 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000343 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000344 }
John McCallf7a1a742009-11-24 19:00:30 +0000345
346 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
347 // C++ [basic.lookup.classref]p1:
348 // [...] If the lookup in the class of the object expression finds a
349 // template, the name is also looked up in the context of the entire
350 // postfix-expression and [...]
351 //
352 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
353 LookupOrdinaryName);
354 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000355 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000356
John McCallf7a1a742009-11-24 19:00:30 +0000357 if (FoundOuter.empty()) {
358 // - if the name is not found, the name found in the class of the
359 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000360 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
361 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000362 // - if the name is found in the context of the entire
363 // postfix-expression and does not name a class template, the name
364 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000365 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000366 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000367 // - if the name found is a class template, it must refer to the same
368 // entity as the one found in the class of the object expression,
369 // otherwise the program is ill-formed.
370 if (!Found.isSingleResult() ||
371 Found.getFoundDecl()->getCanonicalDecl()
372 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000373 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000374 diag::ext_nested_name_member_ref_lookup_ambiguous)
375 << Found.getLookupName()
376 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000377 Diag(Found.getRepresentativeDecl()->getLocation(),
378 diag::note_ambig_member_ref_object_type)
379 << ObjectType;
380 Diag(FoundOuter.getFoundDecl()->getLocation(),
381 diag::note_ambig_member_ref_scope);
382
383 // Recover by taking the template that we found in the object
384 // expression's type.
385 }
386 }
387 }
388}
389
John McCall2f841ba2009-12-02 03:53:29 +0000390/// ActOnDependentIdExpression - Handle a dependent id-expression that
391/// was just parsed. This is only possible with an explicit scope
392/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000393ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000394Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000395 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000396 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000397 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000398 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000399 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000400
John McCall2f841ba2009-12-02 03:53:29 +0000401 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000402 isa<CXXMethodDecl>(DC) &&
403 cast<CXXMethodDecl>(DC)->isInstance()) {
404 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000405
John McCallf7a1a742009-11-24 19:00:30 +0000406 // Since the 'this' expression is synthesized, we don't need to
407 // perform the double-lookup check.
408 NamedDecl *FirstQualifierInScope = 0;
409
John McCallaa81e162009-12-01 22:10:20 +0000410 return Owned(CXXDependentScopeMemberExpr::Create(Context,
411 /*This*/ 0, ThisType,
412 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000413 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000414 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000415 TemplateKWLoc,
John McCallf7a1a742009-11-24 19:00:30 +0000416 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000417 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000418 TemplateArgs));
419 }
420
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000421 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000422}
423
John McCall60d7b3a2010-08-24 06:29:42 +0000424ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000425Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000426 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000427 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000428 const TemplateArgumentListInfo *TemplateArgs) {
429 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000430 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000431 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000432 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000433 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000434}
435
Douglas Gregor72c3f312008-12-05 18:15:24 +0000436/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
437/// that the template parameter 'PrevDecl' is being shadowed by a new
438/// declaration at location Loc. Returns true to indicate that this is
439/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000440void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000441 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000442
443 // Microsoft Visual C++ permits template parameters to be shadowed.
Francois Pichet62ec1f22011-09-17 17:15:52 +0000444 if (getLangOptions().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000445 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000446
447 // C++ [temp.local]p4:
448 // A template-parameter shall not be redeclared within its
449 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000450 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000451 << cast<NamedDecl>(PrevDecl)->getDeclName();
452 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000453 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000454}
455
Douglas Gregor2943aed2009-03-03 04:44:36 +0000456/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000457/// the parameter D to reference the templated declaration and return a pointer
458/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000459TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
460 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
461 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000462 return Temp;
463 }
464 return 0;
465}
466
Douglas Gregorba68eca2011-01-05 17:40:24 +0000467ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
468 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000469 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000470 "Only template template arguments can be pack expansions here");
471 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
472 "Template template argument pack expansion without packs");
473 ParsedTemplateArgument Result(*this);
474 Result.EllipsisLoc = EllipsisLoc;
475 return Result;
476}
477
Douglas Gregor788cd062009-11-11 01:00:40 +0000478static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
479 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000480
Douglas Gregor788cd062009-11-11 01:00:40 +0000481 switch (Arg.getKind()) {
482 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000483 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000484 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000485 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000486 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000487 return TemplateArgumentLoc(TemplateArgument(T), DI);
488 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000489
Douglas Gregor788cd062009-11-11 01:00:40 +0000490 case ParsedTemplateArgument::NonType: {
491 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
492 return TemplateArgumentLoc(TemplateArgument(E), E);
493 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000494
Douglas Gregor788cd062009-11-11 01:00:40 +0000495 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000496 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000497 TemplateArgument TArg;
498 if (Arg.getEllipsisLoc().isValid())
499 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
500 else
501 TArg = Template;
502 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000503 Arg.getScopeSpec().getWithLocInContext(
504 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000505 Arg.getLocation(),
506 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000507 }
508 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000509
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000510 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000511}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000512
Douglas Gregor788cd062009-11-11 01:00:40 +0000513/// \brief Translates template arguments as provided by the parser
514/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000515void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
516 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000517 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000518 TemplateArgs.addArgument(translateTemplateArgument(*this,
519 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000520}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000521
Douglas Gregor72c3f312008-12-05 18:15:24 +0000522/// ActOnTypeParameter - Called when a C++ template type parameter
523/// (e.g., "typename T") has been parsed. Typename specifies whether
524/// the keyword "typename" was used to declare the type parameter
525/// (otherwise, "class" was used), and KeyLoc is the location of the
526/// "class" or "typename" keyword. ParamName is the name of the
527/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000528/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000529/// If the type parameter has a default argument, it will be added
530/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000531Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
532 SourceLocation EllipsisLoc,
533 SourceLocation KeyLoc,
534 IdentifierInfo *ParamName,
535 SourceLocation ParamNameLoc,
536 unsigned Depth, unsigned Position,
537 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000538 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000539 assert(S->isTemplateParamScope() &&
540 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000541 bool Invalid = false;
542
543 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000544 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000545 LookupOrdinaryName,
546 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000547 if (PrevDecl && PrevDecl->isTemplateParameter()) {
548 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
549 PrevDecl = 0;
550 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000551 }
552
Douglas Gregorddc29e12009-02-06 22:42:48 +0000553 SourceLocation Loc = ParamNameLoc;
554 if (!ParamName)
555 Loc = KeyLoc;
556
Douglas Gregor72c3f312008-12-05 18:15:24 +0000557 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000558 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000559 KeyLoc, Loc, Depth, Position, ParamName,
560 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000561 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000562 if (Invalid)
563 Param->setInvalidDecl();
564
565 if (ParamName) {
566 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000567 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000568 IdResolver.AddDecl(Param);
569 }
570
Douglas Gregor61c4d282011-01-05 15:48:55 +0000571 // C++0x [temp.param]p9:
572 // A default template-argument may be specified for any kind of
573 // template-parameter that is not a template parameter pack.
574 if (DefaultArg && Ellipsis) {
575 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
576 DefaultArg = ParsedType();
577 }
578
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000579 // Handle the default argument, if provided.
580 if (DefaultArg) {
581 TypeSourceInfo *DefaultTInfo;
582 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000583
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000584 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000585
Douglas Gregor6f526752010-12-16 08:48:57 +0000586 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000587 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000588 UPPC_DefaultArgument))
589 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000590
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000591 // Check the template argument itself.
592 if (CheckTemplateArgument(Param, DefaultTInfo)) {
593 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000594 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000595 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000596
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000597 Param->setDefaultArgument(DefaultTInfo, false);
598 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000599
John McCalld226f652010-08-21 09:40:31 +0000600 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000601}
602
Douglas Gregor2943aed2009-03-03 04:44:36 +0000603/// \brief Check that the type of a non-type template parameter is
604/// well-formed.
605///
606/// \returns the (possibly-promoted) parameter type if valid;
607/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000608QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000609Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000610 // We don't allow variably-modified types as the type of non-type template
611 // parameters.
612 if (T->isVariablyModifiedType()) {
613 Diag(Loc, diag::err_variably_modified_nontype_template_param)
614 << T;
615 return QualType();
616 }
617
Douglas Gregor2943aed2009-03-03 04:44:36 +0000618 // C++ [temp.param]p4:
619 //
620 // A non-type template-parameter shall have one of the following
621 // (optionally cv-qualified) types:
622 //
623 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000624 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000625 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000626 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000627 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000628 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000629 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000630 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000631 // -- std::nullptr_t.
632 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000633 // If T is a dependent type, we can't do the check now, so we
634 // assume that it is well-formed.
635 T->isDependentType())
636 return T;
637 // C++ [temp.param]p8:
638 //
639 // A non-type template-parameter of type "array of T" or
640 // "function returning T" is adjusted to be of type "pointer to
641 // T" or "pointer to function returning T", respectively.
642 else if (T->isArrayType())
643 // FIXME: Keep the type prior to promotion?
644 return Context.getArrayDecayedType(T);
645 else if (T->isFunctionType())
646 // FIXME: Keep the type prior to promotion?
647 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000648
Douglas Gregor2943aed2009-03-03 04:44:36 +0000649 Diag(Loc, diag::err_template_nontype_parm_bad_type)
650 << T;
651
652 return QualType();
653}
654
John McCalld226f652010-08-21 09:40:31 +0000655Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
656 unsigned Depth,
657 unsigned Position,
658 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000659 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000660 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
661 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000662
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000663 assert(S->isTemplateParamScope() &&
664 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000665 bool Invalid = false;
666
667 IdentifierInfo *ParamName = D.getIdentifier();
668 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000669 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000670 LookupOrdinaryName,
671 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000672 if (PrevDecl && PrevDecl->isTemplateParameter()) {
673 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
674 PrevDecl = 0;
675 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000676 }
677
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000678 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
679 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000680 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000681 Invalid = true;
682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683
Douglas Gregor10738d32010-12-23 23:51:58 +0000684 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000685 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000686 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000687 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000688 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000689 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000690 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000691 Param->setAccess(AS_public);
692
Douglas Gregor72c3f312008-12-05 18:15:24 +0000693 if (Invalid)
694 Param->setInvalidDecl();
695
696 if (D.getIdentifier()) {
697 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000698 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000699 IdResolver.AddDecl(Param);
700 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000701
Douglas Gregor61c4d282011-01-05 15:48:55 +0000702 // C++0x [temp.param]p9:
703 // A default template-argument may be specified for any kind of
704 // template-parameter that is not a template parameter pack.
705 if (Default && IsParameterPack) {
706 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
707 Default = 0;
708 }
709
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000710 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000711 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000712 // Check for unexpanded parameter packs.
713 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
714 return Param;
715
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000716 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000717 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
718 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000719 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000720 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000721 }
John Wiegley429bb272011-04-08 18:41:53 +0000722 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000723
John McCall9ae2f072010-08-23 23:25:46 +0000724 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000725 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000726
John McCalld226f652010-08-21 09:40:31 +0000727 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000728}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000729
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000730/// ActOnTemplateTemplateParameter - Called when a C++ template template
731/// parameter (e.g. T in template <template <typename> class T> class array)
732/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000733Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
734 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000735 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000736 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000737 IdentifierInfo *Name,
738 SourceLocation NameLoc,
739 unsigned Depth,
740 unsigned Position,
741 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000742 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000743 assert(S->isTemplateParamScope() &&
744 "Template template parameter not in template parameter scope!");
745
746 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000747 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000748 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000749 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000750 NameLoc.isInvalid()? TmpLoc : NameLoc,
751 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000752 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000753 Param->setAccess(AS_public);
754
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000755 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000756 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000757 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000758 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000759 IdResolver.AddDecl(Param);
760 }
761
Douglas Gregor6f526752010-12-16 08:48:57 +0000762 if (Params->size() == 0) {
763 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
764 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
765 Param->setInvalidDecl();
766 }
767
Douglas Gregor61c4d282011-01-05 15:48:55 +0000768 // C++0x [temp.param]p9:
769 // A default template-argument may be specified for any kind of
770 // template-parameter that is not a template parameter pack.
771 if (IsParameterPack && !Default.isInvalid()) {
772 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
773 Default = ParsedTemplateArgument();
774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000776 if (!Default.isInvalid()) {
777 // Check only that we have a template template argument. We don't want to
778 // try to check well-formedness now, because our template template parameter
779 // might have dependent types in its template parameters, which we wouldn't
780 // be able to match now.
781 //
782 // If none of the template template parameter's template arguments mention
783 // other template parameters, we could actually perform more checking here.
784 // However, it isn't worth doing.
785 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
786 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
787 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
788 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000789 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000790 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000791
Douglas Gregor6f526752010-12-16 08:48:57 +0000792 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000793 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000794 DefaultArg.getArgument().getAsTemplate(),
795 UPPC_DefaultArgument))
796 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000797
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000798 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000799 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000800
John McCalld226f652010-08-21 09:40:31 +0000801 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000802}
803
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000804/// ActOnTemplateParameterList - Builds a TemplateParameterList that
805/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000806TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000807Sema::ActOnTemplateParameterList(unsigned Depth,
808 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000809 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000810 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000811 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000812 SourceLocation RAngleLoc) {
813 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000814 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000815
Douglas Gregorddc29e12009-02-06 22:42:48 +0000816 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000817 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000818 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000819}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000820
John McCallb6217662010-03-15 10:12:16 +0000821static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
822 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000823 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000824}
825
John McCallf312b1e2010-08-26 23:41:50 +0000826DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000827Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000828 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000829 IdentifierInfo *Name, SourceLocation NameLoc,
830 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000831 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000832 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000833 unsigned NumOuterTemplateParamLists,
834 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000835 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000836 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000837 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000838 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000839
840 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000841 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000842 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000843
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000844 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
845 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000846
847 // There is no such thing as an unnamed class template.
848 if (!Name) {
849 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000850 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000851 }
852
853 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000854 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000855 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000856 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000857 if (SS.isNotEmpty() && !SS.isInvalid()) {
858 SemanticContext = computeDeclContext(SS, true);
859 if (!SemanticContext) {
860 // FIXME: Produce a reasonable diagnostic here
861 return true;
862 }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
John McCall77bb1aa2010-05-01 00:40:08 +0000864 if (RequireCompleteDeclContext(SS, SemanticContext))
865 return true;
866
Douglas Gregor20606502011-10-14 15:31:12 +0000867 // If we're adding a template to a dependent context, we may need to
868 // rebuilding some of the types used within the template parameter list,
869 // now that we know what the current instantiation is.
870 if (SemanticContext->isDependentContext()) {
871 ContextRAII SavedContext(*this, SemanticContext);
872 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
873 Invalid = true;
874 }
875
John McCalla24dc2e2009-11-17 02:14:36 +0000876 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000877 } else {
878 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000879 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Douglas Gregor57265e32010-04-12 16:00:01 +0000882 if (Previous.isAmbiguous())
883 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884
Douglas Gregorddc29e12009-02-06 22:42:48 +0000885 NamedDecl *PrevDecl = 0;
886 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000887 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000888
Douglas Gregorddc29e12009-02-06 22:42:48 +0000889 // If there is a previous declaration with the same name, check
890 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000891 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000892 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000893
894 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000896 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000897 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000898 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
899 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000900 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000901 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
902 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
903 PrevClassTemplate
904 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
905 ->getSpecializedTemplate();
906 }
907 }
908
John McCall65c49462009-12-18 11:25:59 +0000909 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000910 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000911 // [...] When looking for a prior declaration of a class or a function
912 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000913 // function is neither a qualified name nor a template-id, scopes outside
914 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000915 if (!SS.isSet()) {
916 DeclContext *OutermostContext = CurContext;
917 while (!OutermostContext->isFileContext())
918 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000919
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000920 if (PrevDecl &&
921 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
922 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
923 SemanticContext = PrevDecl->getDeclContext();
924 } else {
925 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000926 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000927 // declaration.
928 PrevDecl = PrevClassTemplate = 0;
929 SemanticContext = OutermostContext;
930 }
John McCalle129d442009-12-17 23:21:11 +0000931 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000932
John McCalle129d442009-12-17 23:21:11 +0000933 if (CurContext->isDependentContext()) {
934 // If this is a dependent context, we don't want to link the friend
935 // class template to the template in scope, because that would perform
936 // checking of the template parameter lists that can't be performed
937 // until the outer context is instantiated.
938 PrevDecl = PrevClassTemplate = 0;
939 }
940 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
941 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000942
Douglas Gregorddc29e12009-02-06 22:42:48 +0000943 if (PrevClassTemplate) {
944 // Ensure that the template parameter lists are compatible.
945 if (!TemplateParameterListsAreEqual(TemplateParams,
946 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000947 /*Complain=*/true,
948 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000949 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000950
951 // C++ [temp.class]p4:
952 // In a redeclaration, partial specialization, explicit
953 // specialization or explicit instantiation of a class template,
954 // the class-key shall agree in kind with the original class
955 // template declaration (7.1.5.3).
956 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000957 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
958 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000959 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000960 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000961 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000962 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000963 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000964 }
965
Douglas Gregorddc29e12009-02-06 22:42:48 +0000966 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000967 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000968 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000969 Diag(NameLoc, diag::err_redefinition) << Name;
970 Diag(Def->getLocation(), diag::note_previous_definition);
971 // FIXME: Would it make sense to try to "forget" the previous
972 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000973 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000974 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000975 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000976 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
977 // Maybe we will complain about the shadowed template parameter.
978 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
979 // Just pretend that we didn't see the previous declaration.
980 PrevDecl = 0;
981 } else if (PrevDecl) {
982 // C++ [temp]p5:
983 // A class template shall not have the same name as any other
984 // template, class, function, object, enumeration, enumerator,
985 // namespace, or type in the same scope (3.3), except as specified
986 // in (14.5.4).
987 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
988 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000989 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000990 }
991
Douglas Gregord684b002009-02-10 19:49:53 +0000992 // Check the template parameter list of this declaration, possibly
993 // merging in the template parameter list from the previous class
994 // template declaration.
995 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000996 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000997 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000998 SemanticContext->isRecord() &&
999 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +00001000 ? TPC_ClassTemplateMember
1001 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +00001002 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Douglas Gregor57265e32010-04-12 16:00:01 +00001004 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001005 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +00001006 // template out-of-line.
1007 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001008 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +00001009 Diag(NameLoc, diag::err_member_def_does_not_match)
1010 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001011 Invalid = true;
1012 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001013 }
1014
Mike Stump1eb44332009-09-09 15:08:12 +00001015 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001016 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001017 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001018 PrevClassTemplate->getTemplatedDecl() : 0,
1019 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001020 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001021 if (NumOuterTemplateParamLists > 0)
1022 NewClass->setTemplateParameterListsInfo(Context,
1023 NumOuterTemplateParamLists,
1024 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001025
1026 ClassTemplateDecl *NewTemplate
1027 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1028 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001029 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001030 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001031
Douglas Gregor2ccd89c2011-12-20 18:11:52 +00001032 if (ModulePrivateLoc.isValid())
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001033 NewTemplate->setModulePrivate();
Douglas Gregor8d267c52011-09-09 02:06:17 +00001034
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001035 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001036 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001037 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001038 assert(T->isDependentType() && "Class template type is not dependent?");
1039 (void)T;
1040
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001041 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001042 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001043 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001044 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1045 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001046
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001047 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001048 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001049 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Douglas Gregorddc29e12009-02-06 22:42:48 +00001051 // Set the lexical context of these templates
1052 NewClass->setLexicalDeclContext(CurContext);
1053 NewTemplate->setLexicalDeclContext(CurContext);
1054
John McCall0f434ec2009-07-31 02:45:11 +00001055 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001056 NewClass->startDefinition();
1057
1058 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001059 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001060
John McCall05b23ea2009-09-14 21:59:20 +00001061 if (TUK != TUK_Friend)
1062 PushOnScopeChains(NewTemplate, S);
1063 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001064 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001065 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001066 NewClass->setAccess(PrevClassTemplate->getAccess());
1067 }
John McCall05b23ea2009-09-14 21:59:20 +00001068
Douglas Gregord85bea22009-09-26 06:47:28 +00001069 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1070 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001071
John McCall05b23ea2009-09-14 21:59:20 +00001072 // Friend templates are visible in fairly strange ways.
1073 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001074 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001075 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1076 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1077 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001078 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001079 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001080
Douglas Gregord85bea22009-09-26 06:47:28 +00001081 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1082 NewClass->getLocation(),
1083 NewTemplate,
1084 /*FIXME:*/NewClass->getLocation());
1085 Friend->setAccess(AS_public);
1086 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001087 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001088
Douglas Gregord684b002009-02-10 19:49:53 +00001089 if (Invalid) {
1090 NewTemplate->setInvalidDecl();
1091 NewClass->setInvalidDecl();
1092 }
John McCalld226f652010-08-21 09:40:31 +00001093 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001094}
1095
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001096/// \brief Diagnose the presence of a default template argument on a
1097/// template parameter, which is ill-formed in certain contexts.
1098///
1099/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001100static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001101 Sema::TemplateParamListContext TPC,
1102 SourceLocation ParamLoc,
1103 SourceRange DefArgRange) {
1104 switch (TPC) {
1105 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001106 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001107 return false;
1108
1109 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001110 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001111 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001112 // A default template-argument shall not be specified in a
1113 // function template declaration or a function template
1114 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001115 // If a friend function template declaration specifies a default
1116 // template-argument, that declaration shall be a definition and shall be
1117 // the only declaration of the function template in the translation unit.
1118 // (C++98/03 doesn't have this wording; see DR226).
Richard Smithebaf0e62011-10-18 20:49:44 +00001119 S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
1120 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1121 : diag::ext_template_parameter_default_in_function_template)
1122 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001123 return false;
1124
1125 case Sema::TPC_ClassTemplateMember:
1126 // C++0x [temp.param]p9:
1127 // A default template-argument shall not be specified in the
1128 // template-parameter-lists of the definition of a member of a
1129 // class template that appears outside of the member's class.
1130 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1131 << DefArgRange;
1132 return true;
1133
1134 case Sema::TPC_FriendFunctionTemplate:
1135 // C++ [temp.param]p9:
1136 // A default template-argument shall not be specified in a
1137 // friend template declaration.
1138 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1139 << DefArgRange;
1140 return true;
1141
1142 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1143 // for friend function templates if there is only a single
1144 // declaration (and it is a definition). Strange!
1145 }
1146
David Blaikie7530c032012-01-17 06:56:22 +00001147 llvm_unreachable("Invalid TemplateParamListContext!");
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001148}
1149
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001150/// \brief Check for unexpanded parameter packs within the template parameters
1151/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001152static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1153 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001154 TemplateParameterList *Params = TTP->getTemplateParameters();
1155 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1156 NamedDecl *P = Params->getParam(I);
1157 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001158 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001159 NTTP->getTypeSourceInfo(),
1160 Sema::UPPC_NonTypeTemplateParameterType))
1161 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001162
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001163 continue;
1164 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001165
1166 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001167 = dyn_cast<TemplateTemplateParmDecl>(P))
1168 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1169 return true;
1170 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001171
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001172 return false;
1173}
1174
Douglas Gregord684b002009-02-10 19:49:53 +00001175/// \brief Checks the validity of a template parameter list, possibly
1176/// considering the template parameter list from a previous
1177/// declaration.
1178///
1179/// If an "old" template parameter list is provided, it must be
1180/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1181/// template parameter list.
1182///
1183/// \param NewParams Template parameter list for a new template
1184/// declaration. This template parameter list will be updated with any
1185/// default arguments that are carried through from the previous
1186/// template parameter list.
1187///
1188/// \param OldParams If provided, template parameter list from a
1189/// previous declaration of the same template. Default template
1190/// arguments will be merged from the old template parameter list to
1191/// the new template parameter list.
1192///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001193/// \param TPC Describes the context in which we are checking the given
1194/// template parameter list.
1195///
Douglas Gregord684b002009-02-10 19:49:53 +00001196/// \returns true if an error occurred, false otherwise.
1197bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001198 TemplateParameterList *OldParams,
1199 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001200 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Douglas Gregord684b002009-02-10 19:49:53 +00001202 // C++ [temp.param]p10:
1203 // The set of default template-arguments available for use with a
1204 // template declaration or definition is obtained by merging the
1205 // default arguments from the definition (if in scope) and all
1206 // declarations in scope in the same way default function
1207 // arguments are (8.3.6).
1208 bool SawDefaultArgument = false;
1209 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001210
Mike Stump1a35fde2009-02-11 23:03:27 +00001211 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001212 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001213 if (OldParams)
1214 OldParam = OldParams->begin();
1215
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001216 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001217 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1218 NewParamEnd = NewParams->end();
1219 NewParam != NewParamEnd; ++NewParam) {
1220 // Variables used to diagnose redundant default arguments
1221 bool RedundantDefaultArg = false;
1222 SourceLocation OldDefaultLoc;
1223 SourceLocation NewDefaultLoc;
1224
David Blaikie1368e582011-10-19 05:19:50 +00001225 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001226 bool MissingDefaultArg = false;
1227
David Blaikie1368e582011-10-19 05:19:50 +00001228 // Variable used to diagnose non-final parameter packs
1229 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001230
Douglas Gregord684b002009-02-10 19:49:53 +00001231 if (TemplateTypeParmDecl *NewTypeParm
1232 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001233 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001234 if (NewTypeParm->hasDefaultArgument() &&
1235 DiagnoseDefaultTemplateArgument(*this, TPC,
1236 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001237 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001238 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001239 NewTypeParm->removeDefaultArgument();
1240
1241 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001242 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001243 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Anders Carlsson49d25572009-06-12 23:20:15 +00001245 if (NewTypeParm->isParameterPack()) {
1246 assert(!NewTypeParm->hasDefaultArgument() &&
1247 "Parameter packs can't have a default argument!");
1248 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001249 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001250 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001251 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1252 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1253 SawDefaultArgument = true;
1254 RedundantDefaultArg = true;
1255 PreviousDefaultArgLoc = NewDefaultLoc;
1256 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1257 // Merge the default argument from the old declaration to the
1258 // new declaration.
1259 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001260 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001261 true);
1262 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1263 } else if (NewTypeParm->hasDefaultArgument()) {
1264 SawDefaultArgument = true;
1265 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1266 } else if (SawDefaultArgument)
1267 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001268 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001269 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001270 // Check for unexpanded parameter packs.
1271 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001272 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001273 UPPC_NonTypeTemplateParameterType)) {
1274 Invalid = true;
1275 continue;
1276 }
1277
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001278 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001279 if (NewNonTypeParm->hasDefaultArgument() &&
1280 DiagnoseDefaultTemplateArgument(*this, TPC,
1281 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001282 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001283 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001284 }
1285
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001286 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001287 NonTypeTemplateParmDecl *OldNonTypeParm
1288 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001289 if (NewNonTypeParm->isParameterPack()) {
1290 assert(!NewNonTypeParm->hasDefaultArgument() &&
1291 "Parameter packs can't have a default argument!");
1292 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001293 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001294 NewNonTypeParm->hasDefaultArgument()) {
1295 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1296 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1297 SawDefaultArgument = true;
1298 RedundantDefaultArg = true;
1299 PreviousDefaultArgLoc = NewDefaultLoc;
1300 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1301 // Merge the default argument from the old declaration to the
1302 // new declaration.
1303 SawDefaultArgument = true;
1304 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001305 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001306 // parameter.
1307 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001308 OldNonTypeParm->getDefaultArgument(),
1309 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001310 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1311 } else if (NewNonTypeParm->hasDefaultArgument()) {
1312 SawDefaultArgument = true;
1313 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1314 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001315 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001316 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001317 TemplateTemplateParmDecl *NewTemplateParm
1318 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001319
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001320 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001321 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001322 Invalid = true;
1323 continue;
1324 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001325
David Blaikie1368e582011-10-19 05:19:50 +00001326 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001327 if (NewTemplateParm->hasDefaultArgument() &&
1328 DiagnoseDefaultTemplateArgument(*this, TPC,
1329 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001330 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001331 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001332
1333 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001334 TemplateTemplateParmDecl *OldTemplateParm
1335 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001336 if (NewTemplateParm->isParameterPack()) {
1337 assert(!NewTemplateParm->hasDefaultArgument() &&
1338 "Parameter packs can't have a default argument!");
1339 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001340 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001341 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001342 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1343 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001344 SawDefaultArgument = true;
1345 RedundantDefaultArg = true;
1346 PreviousDefaultArgLoc = NewDefaultLoc;
1347 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1348 // Merge the default argument from the old declaration to the
1349 // new declaration.
1350 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001351 // FIXME: We need to create a new kind of "default argument" expression
1352 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001353 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001354 OldTemplateParm->getDefaultArgument(),
1355 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001356 PreviousDefaultArgLoc
1357 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001358 } else if (NewTemplateParm->hasDefaultArgument()) {
1359 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001360 PreviousDefaultArgLoc
1361 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001362 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001363 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001364 }
1365
David Blaikie1368e582011-10-19 05:19:50 +00001366 // C++0x [temp.param]p11:
1367 // If a template parameter of a primary class template or alias template
1368 // is a template parameter pack, it shall be the last template parameter.
1369 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1370 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1371 Diag((*NewParam)->getLocation(),
1372 diag::err_template_param_pack_must_be_last_template_parameter);
1373 Invalid = true;
1374 }
1375
Douglas Gregord684b002009-02-10 19:49:53 +00001376 if (RedundantDefaultArg) {
1377 // C++ [temp.param]p12:
1378 // A template-parameter shall not be given default arguments
1379 // by two different declarations in the same scope.
1380 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1381 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1382 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001383 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001384 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001385 // If a template-parameter of a class template has a default
1386 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001387 // have a default template-argument supplied or be a template parameter
1388 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001389 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001390 diag::err_template_param_default_arg_missing);
1391 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1392 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001393 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001394 }
1395
1396 // If we have an old template parameter list that we're merging
1397 // in, move on to the next parameter.
1398 if (OldParams)
1399 ++OldParam;
1400 }
1401
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001402 // We were missing some default arguments at the end of the list, so remove
1403 // all of the default arguments.
1404 if (RemoveDefaultArguments) {
1405 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1406 NewParamEnd = NewParams->end();
1407 NewParam != NewParamEnd; ++NewParam) {
1408 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1409 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001410 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001411 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1412 NTTP->removeDefaultArgument();
1413 else
1414 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1415 }
1416 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001417
Douglas Gregord684b002009-02-10 19:49:53 +00001418 return Invalid;
1419}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001420
John McCall4e2cbb22010-10-20 05:44:58 +00001421namespace {
1422
1423/// A class which looks for a use of a certain level of template
1424/// parameter.
1425struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1426 typedef RecursiveASTVisitor<DependencyChecker> super;
1427
1428 unsigned Depth;
1429 bool Match;
1430
1431 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1432 NamedDecl *ND = Params->getParam(0);
1433 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1434 Depth = PD->getDepth();
1435 } else if (NonTypeTemplateParmDecl *PD =
1436 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1437 Depth = PD->getDepth();
1438 } else {
1439 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1440 }
1441 }
1442
1443 bool Matches(unsigned ParmDepth) {
1444 if (ParmDepth >= Depth) {
1445 Match = true;
1446 return true;
1447 }
1448 return false;
1449 }
1450
1451 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1452 return !Matches(T->getDepth());
1453 }
1454
1455 bool TraverseTemplateName(TemplateName N) {
1456 if (TemplateTemplateParmDecl *PD =
1457 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1458 if (Matches(PD->getDepth())) return false;
1459 return super::TraverseTemplateName(N);
1460 }
1461
1462 bool VisitDeclRefExpr(DeclRefExpr *E) {
1463 if (NonTypeTemplateParmDecl *PD =
1464 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1465 if (PD->getDepth() == Depth) {
1466 Match = true;
1467 return false;
1468 }
1469 }
1470 return super::VisitDeclRefExpr(E);
1471 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001472
1473 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1474 return TraverseType(T->getInjectedSpecializationType());
1475 }
John McCall4e2cbb22010-10-20 05:44:58 +00001476};
1477}
1478
Douglas Gregorc8406492011-05-10 18:27:06 +00001479/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001480/// list.
1481static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001482DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001483 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001484 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001485 return Checker.Match;
1486}
1487
Douglas Gregorc8406492011-05-10 18:27:06 +00001488// Find the source range corresponding to the named type in the given
1489// nested-name-specifier, if any.
1490static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1491 QualType T,
1492 const CXXScopeSpec &SS) {
1493 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1494 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1495 if (const Type *CurType = NNS->getAsType()) {
1496 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1497 return NNSLoc.getTypeLoc().getSourceRange();
1498 } else
1499 break;
1500
1501 NNSLoc = NNSLoc.getPrefix();
1502 }
1503
1504 return SourceRange();
1505}
1506
Mike Stump1eb44332009-09-09 15:08:12 +00001507/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001508/// specifier, returning the template parameter list that applies to the
1509/// name.
1510///
1511/// \param DeclStartLoc the start of the declaration that has a scope
1512/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001513///
Douglas Gregorc8406492011-05-10 18:27:06 +00001514/// \param DeclLoc The location of the declaration itself.
1515///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001516/// \param SS the scope specifier that will be matched to the given template
1517/// parameter lists. This scope specifier precedes a qualified name that is
1518/// being declared.
1519///
1520/// \param ParamLists the template parameter lists, from the outermost to the
1521/// innermost template parameter lists.
1522///
1523/// \param NumParamLists the number of template parameter lists in ParamLists.
1524///
John McCall77e8b112010-04-13 20:37:33 +00001525/// \param IsFriend Whether to apply the slightly different rules for
1526/// matching template parameters to scope specifiers in friend
1527/// declarations.
1528///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001529/// \param IsExplicitSpecialization will be set true if the entity being
1530/// declared is an explicit specialization, false otherwise.
1531///
Mike Stump1eb44332009-09-09 15:08:12 +00001532/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001533/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001534/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001535/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001536/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001537/// itself a template).
1538TemplateParameterList *
1539Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001540 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001541 const CXXScopeSpec &SS,
1542 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001543 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001544 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001545 bool &IsExplicitSpecialization,
1546 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001547 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001548 Invalid = false;
1549
1550 // The sequence of nested types to which we will match up the template
1551 // parameter lists. We first build this list by starting with the type named
1552 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001553 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001554 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001555 if (SS.getScopeRep()) {
1556 if (CXXRecordDecl *Record
1557 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1558 T = Context.getTypeDeclType(Record);
1559 else
1560 T = QualType(SS.getScopeRep()->getAsType(), 0);
1561 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001562
1563 // If we found an explicit specialization that prevents us from needing
1564 // 'template<>' headers, this will be set to the location of that
1565 // explicit specialization.
1566 SourceLocation ExplicitSpecLoc;
1567
1568 while (!T.isNull()) {
1569 NestedTypes.push_back(T);
1570
1571 // Retrieve the parent of a record type.
1572 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1573 // If this type is an explicit specialization, we're done.
1574 if (ClassTemplateSpecializationDecl *Spec
1575 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1576 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1577 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1578 ExplicitSpecLoc = Spec->getLocation();
1579 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001580 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001581 } else if (Record->getTemplateSpecializationKind()
1582 == TSK_ExplicitSpecialization) {
1583 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001584 break;
1585 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001586
1587 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1588 T = Context.getTypeDeclType(Parent);
1589 else
1590 T = QualType();
1591 continue;
1592 }
1593
1594 if (const TemplateSpecializationType *TST
1595 = T->getAs<TemplateSpecializationType>()) {
1596 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1597 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1598 T = Context.getTypeDeclType(Parent);
1599 else
1600 T = QualType();
1601 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001602 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001603 }
1604
1605 // Look one step prior in a dependent template specialization type.
1606 if (const DependentTemplateSpecializationType *DependentTST
1607 = T->getAs<DependentTemplateSpecializationType>()) {
1608 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1609 T = QualType(NNS->getAsType(), 0);
1610 else
1611 T = QualType();
1612 continue;
1613 }
1614
1615 // Look one step prior in a dependent name type.
1616 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1617 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1618 T = QualType(NNS->getAsType(), 0);
1619 else
1620 T = QualType();
1621 continue;
1622 }
1623
1624 // Retrieve the parent of an enumeration type.
1625 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1626 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1627 // check here.
1628 EnumDecl *Enum = EnumT->getDecl();
1629
1630 // Get to the parent type.
1631 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1632 T = Context.getTypeDeclType(Parent);
1633 else
1634 T = QualType();
1635 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001636 }
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Douglas Gregorc8406492011-05-10 18:27:06 +00001638 T = QualType();
1639 }
1640 // Reverse the nested types list, since we want to traverse from the outermost
1641 // to the innermost while checking template-parameter-lists.
1642 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001643
Douglas Gregorc8406492011-05-10 18:27:06 +00001644 // C++0x [temp.expl.spec]p17:
1645 // A member or a member template may be nested within many
1646 // enclosing class templates. In an explicit specialization for
1647 // such a member, the member declaration shall be preceded by a
1648 // template<> for each enclosing class template that is
1649 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001650 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001651 unsigned ParamIdx = 0;
1652 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1653 ++TypeIdx) {
1654 T = NestedTypes[TypeIdx];
1655
1656 // Whether we expect a 'template<>' header.
1657 bool NeedEmptyTemplateHeader = false;
1658
1659 // Whether we expect a template header with parameters.
1660 bool NeedNonemptyTemplateHeader = false;
1661
1662 // For a dependent type, the set of template parameters that we
1663 // expect to see.
1664 TemplateParameterList *ExpectedTemplateParams = 0;
1665
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001666 // C++0x [temp.expl.spec]p15:
1667 // A member or a member template may be nested within many enclosing
1668 // class templates. In an explicit specialization for such a member, the
1669 // member declaration shall be preceded by a template<> for each
1670 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001671 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1672 if (ClassTemplatePartialSpecializationDecl *Partial
1673 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1674 ExpectedTemplateParams = Partial->getTemplateParameters();
1675 NeedNonemptyTemplateHeader = true;
1676 } else if (Record->isDependentType()) {
1677 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001678 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001679 ->getTemplateParameters();
1680 NeedNonemptyTemplateHeader = true;
1681 }
1682 } else if (ClassTemplateSpecializationDecl *Spec
1683 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1684 // C++0x [temp.expl.spec]p4:
1685 // Members of an explicitly specialized class template are defined
1686 // in the same manner as members of normal classes, and not using
1687 // the template<> syntax.
1688 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1689 NeedEmptyTemplateHeader = true;
1690 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001691 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001692 } else if (Record->getTemplateSpecializationKind()) {
1693 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001694 != TSK_ExplicitSpecialization &&
1695 TypeIdx == NumTypes - 1)
1696 IsExplicitSpecialization = true;
1697
1698 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001699 }
1700 } else if (const TemplateSpecializationType *TST
1701 = T->getAs<TemplateSpecializationType>()) {
1702 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1703 ExpectedTemplateParams = Template->getTemplateParameters();
1704 NeedNonemptyTemplateHeader = true;
1705 }
1706 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1707 // FIXME: We actually could/should check the template arguments here
1708 // against the corresponding template parameter list.
1709 NeedNonemptyTemplateHeader = false;
1710 }
1711
Douglas Gregor89b9f102011-06-06 15:22:55 +00001712 // C++ [temp.expl.spec]p16:
1713 // In an explicit specialization declaration for a member of a class
1714 // template or a member template that ap- pears in namespace scope, the
1715 // member template and some of its enclosing class templates may remain
1716 // unspecialized, except that the declaration shall not explicitly
1717 // specialize a class member template if its en- closing class templates
1718 // are not explicitly specialized as well.
1719 if (ParamIdx < NumParamLists) {
1720 if (ParamLists[ParamIdx]->size() == 0) {
1721 if (SawNonEmptyTemplateParameterList) {
1722 Diag(DeclLoc, diag::err_specialize_member_of_template)
1723 << ParamLists[ParamIdx]->getSourceRange();
1724 Invalid = true;
1725 IsExplicitSpecialization = false;
1726 return 0;
1727 }
1728 } else
1729 SawNonEmptyTemplateParameterList = true;
1730 }
1731
Douglas Gregorc8406492011-05-10 18:27:06 +00001732 if (NeedEmptyTemplateHeader) {
1733 // If we're on the last of the types, and we need a 'template<>' header
1734 // here, then it's an explicit specialization.
1735 if (TypeIdx == NumTypes - 1)
1736 IsExplicitSpecialization = true;
1737
1738 if (ParamIdx < NumParamLists) {
1739 if (ParamLists[ParamIdx]->size() > 0) {
1740 // The header has template parameters when it shouldn't. Complain.
1741 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1742 diag::err_template_param_list_matches_nontemplate)
1743 << T
1744 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1745 ParamLists[ParamIdx]->getRAngleLoc())
1746 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1747 Invalid = true;
1748 return 0;
1749 }
1750
1751 // Consume this template header.
1752 ++ParamIdx;
1753 continue;
1754 }
1755
1756 if (!IsFriend) {
1757 // We don't have a template header, but we should.
1758 SourceLocation ExpectedTemplateLoc;
1759 if (NumParamLists > 0)
1760 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1761 else
1762 ExpectedTemplateLoc = DeclStartLoc;
1763
1764 Diag(DeclLoc, diag::err_template_spec_needs_header)
1765 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1766 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1767 }
1768
1769 continue;
1770 }
1771
1772 if (NeedNonemptyTemplateHeader) {
1773 // In friend declarations we can have template-ids which don't
1774 // depend on the corresponding template parameter lists. But
1775 // assume that empty parameter lists are supposed to match this
1776 // template-id.
1777 if (IsFriend && T->isDependentType()) {
1778 if (ParamIdx < NumParamLists &&
1779 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1780 ExpectedTemplateParams = 0;
1781 else
1782 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001783 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001784
Douglas Gregorc8406492011-05-10 18:27:06 +00001785 if (ParamIdx < NumParamLists) {
1786 // Check the template parameter list, if we can.
1787 if (ExpectedTemplateParams &&
1788 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1789 ExpectedTemplateParams,
1790 true, TPL_TemplateMatch))
1791 Invalid = true;
1792
1793 if (!Invalid &&
1794 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1795 TPC_ClassTemplateMember))
1796 Invalid = true;
1797
1798 ++ParamIdx;
1799 continue;
1800 }
1801
1802 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1803 << T
1804 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1805 Invalid = true;
1806 continue;
1807 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001808 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001809
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001810 // If there were at least as many template-ids as there were template
1811 // parameter lists, then there are no template parameter lists remaining for
1812 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001813 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001814 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001816 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001817 if (ParamIdx < NumParamLists - 1) {
1818 bool HasAnyExplicitSpecHeader = false;
1819 bool AllExplicitSpecHeaders = true;
1820 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1821 if (ParamLists[I]->size() == 0)
1822 HasAnyExplicitSpecHeader = true;
1823 else
1824 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001825 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001826
1827 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1828 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1829 : diag::err_template_spec_extra_headers)
1830 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1831 ParamLists[NumParamLists - 2]->getRAngleLoc());
1832
1833 // If there was a specialization somewhere, such that 'template<>' is
1834 // not required, and there were any 'template<>' headers, note where the
1835 // specialization occurred.
1836 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1837 Diag(ExplicitSpecLoc,
1838 diag::note_explicit_template_spec_does_not_need_header)
1839 << NestedTypes.back();
1840
1841 // We have a template parameter list with no corresponding scope, which
1842 // means that the resulting template declaration can't be instantiated
1843 // properly (we'll end up with dependent nodes when we shouldn't).
1844 if (!AllExplicitSpecHeaders)
1845 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001846 }
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Douglas Gregor89b9f102011-06-06 15:22:55 +00001848 // C++ [temp.expl.spec]p16:
1849 // In an explicit specialization declaration for a member of a class
1850 // template or a member template that ap- pears in namespace scope, the
1851 // member template and some of its enclosing class templates may remain
1852 // unspecialized, except that the declaration shall not explicitly
1853 // specialize a class member template if its en- closing class templates
1854 // are not explicitly specialized as well.
1855 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1856 SawNonEmptyTemplateParameterList) {
1857 Diag(DeclLoc, diag::err_specialize_member_of_template)
1858 << ParamLists[ParamIdx]->getSourceRange();
1859 Invalid = true;
1860 IsExplicitSpecialization = false;
1861 return 0;
1862 }
1863
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001864 // Return the last template parameter list, which corresponds to the
1865 // entity being declared.
1866 return ParamLists[NumParamLists - 1];
1867}
1868
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001869void Sema::NoteAllFoundTemplates(TemplateName Name) {
1870 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1871 Diag(Template->getLocation(), diag::note_template_declared_here)
1872 << (isa<FunctionTemplateDecl>(Template)? 0
1873 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001874 : isa<TypeAliasTemplateDecl>(Template)? 2
1875 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001876 << Template->getDeclName();
1877 return;
1878 }
1879
1880 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1881 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1882 IEnd = OST->end();
1883 I != IEnd; ++I)
1884 Diag((*I)->getLocation(), diag::note_template_declared_here)
1885 << 0 << (*I)->getDeclName();
1886
1887 return;
1888 }
1889}
1890
Douglas Gregor7532dc62009-03-30 22:58:21 +00001891QualType Sema::CheckTemplateIdType(TemplateName Name,
1892 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001893 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001894 DependentTemplateName *DTN
1895 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001896 if (DTN && DTN->isIdentifier())
1897 // When building a template-id where the template-name is dependent,
1898 // assume the template is a type template. Either our assumption is
1899 // correct, or the code is ill-formed and will be diagnosed when the
1900 // dependent name is substituted.
1901 return Context.getDependentTemplateSpecializationType(ETK_None,
1902 DTN->getQualifier(),
1903 DTN->getIdentifier(),
1904 TemplateArgs);
1905
Douglas Gregor7532dc62009-03-30 22:58:21 +00001906 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001907 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1908 // We might have a substituted template template parameter pack. If so,
1909 // build a template specialization type for it.
1910 if (Name.getAsSubstTemplateTemplateParmPack())
1911 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001912
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001913 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1914 << Name;
1915 NoteAllFoundTemplates(Name);
1916 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001917 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001918
Douglas Gregor40808ce2009-03-09 23:48:35 +00001919 // Check that the template argument list is well-formed for this
1920 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001921 SmallVector<TemplateArgument, 4> Converted;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001922 bool ExpansionIntoFixedList = false;
John McCalld5532b62009-11-23 01:53:49 +00001923 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00001924 false, Converted, &ExpansionIntoFixedList))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001925 return QualType();
1926
Douglas Gregor40808ce2009-03-09 23:48:35 +00001927 QualType CanonType;
1928
Douglas Gregor561f8122011-07-01 01:22:09 +00001929 bool InstantiationDependent = false;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001930 TypeAliasTemplateDecl *AliasTemplate = 0;
1931 if (!ExpansionIntoFixedList &&
1932 (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001933 // Find the canonical type for this type alias template specialization.
1934 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1935 if (Pattern->isInvalidDecl())
1936 return QualType();
1937
1938 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1939 Converted.data(), Converted.size());
1940
1941 // Only substitute for the innermost template argument list.
1942 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001943 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001944 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1945 for (unsigned I = 0; I < Depth; ++I)
1946 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001947
1948 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1949 CanonType = SubstType(Pattern->getUnderlyingType(),
1950 TemplateArgLists, AliasTemplate->getLocation(),
1951 AliasTemplate->getDeclName());
1952 if (CanonType.isNull())
1953 return QualType();
1954 } else if (Name.isDependent() ||
1955 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001956 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001957 // This class template specialization is a dependent
1958 // type. Therefore, its canonical type is another class template
1959 // specialization type that contains all of the converted
1960 // arguments in canonical form. This ensures that, e.g., A<T> and
1961 // A<T, T> have identical types when A is declared as:
1962 //
1963 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001964 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001965 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001966 Converted.data(),
1967 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Douglas Gregor1275ae02009-07-28 23:00:59 +00001969 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001970 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001971 // In the future, we need to teach getTemplateSpecializationType to only
1972 // build the canonical type and return that to us.
1973 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001974
1975 // This might work out to be a current instantiation, in which
1976 // case the canonical type needs to be the InjectedClassNameType.
1977 //
1978 // TODO: in theory this could be a simple hashtable lookup; most
1979 // changes to CurContext don't change the set of current
1980 // instantiations.
1981 if (isa<ClassTemplateDecl>(Template)) {
1982 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1983 // If we get out to a namespace, we're done.
1984 if (Ctx->isFileContext()) break;
1985
1986 // If this isn't a record, keep looking.
1987 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1988 if (!Record) continue;
1989
1990 // Look for one of the two cases with InjectedClassNameTypes
1991 // and check whether it's the same template.
1992 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1993 !Record->getDescribedClassTemplate())
1994 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001995
John McCall31f17ec2010-04-27 00:57:59 +00001996 // Fetch the injected class name type and check whether its
1997 // injected type is equal to the type we just built.
1998 QualType ICNT = Context.getTypeDeclType(Record);
1999 QualType Injected = cast<InjectedClassNameType>(ICNT)
2000 ->getInjectedSpecializationType();
2001
2002 if (CanonType != Injected->getCanonicalTypeInternal())
2003 continue;
2004
2005 // If so, the canonical type of this TST is the injected
2006 // class name type of the record we just found.
2007 assert(ICNT.isCanonical());
2008 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002009 break;
2010 }
2011 }
Mike Stump1eb44332009-09-09 15:08:12 +00002012 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002013 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002014 // Find the class template specialization declaration that
2015 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002016 void *InsertPos = 0;
2017 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002018 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002019 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002020 if (!Decl) {
2021 // This is the first time we have referenced this class template
2022 // specialization. Create the canonical declaration and add it to
2023 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002024 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002025 ClassTemplate->getTemplatedDecl()->getTagKind(),
2026 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002027 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002028 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002029 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002030 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002031 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002032 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002033 Decl->setLexicalDeclContext(CurContext);
2034 }
2035
2036 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002037 assert(isa<RecordType>(CanonType) &&
2038 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002039 }
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Douglas Gregor40808ce2009-03-09 23:48:35 +00002041 // Build the fully-sugared type for this class template
2042 // specialization, which refers back to the class template
2043 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002044 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002045}
2046
John McCallf312b1e2010-08-26 23:41:50 +00002047TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002048Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00002049 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002050 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002051 ASTTemplateArgsPtr TemplateArgsIn,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002052 SourceLocation RAngleLoc,
2053 bool IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002054 if (SS.isInvalid())
2055 return true;
2056
Douglas Gregor7532dc62009-03-30 22:58:21 +00002057 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002058
Douglas Gregor40808ce2009-03-09 23:48:35 +00002059 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002060 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002061 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002062
Douglas Gregora88f09f2011-02-28 17:23:35 +00002063 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002064 QualType T
2065 = Context.getDependentTemplateSpecializationType(ETK_None,
2066 DTN->getQualifier(),
2067 DTN->getIdentifier(),
2068 TemplateArgs);
2069 // Build type-source information.
Douglas Gregora88f09f2011-02-28 17:23:35 +00002070 TypeLocBuilder TLB;
2071 DependentTemplateSpecializationTypeLoc SpecTL
2072 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002073 SpecTL.setElaboratedKeywordLoc(SourceLocation());
2074 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002075 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002076 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002077 SpecTL.setLAngleLoc(LAngleLoc);
2078 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002079 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2080 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2081 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2082 }
2083
John McCalld5532b62009-11-23 01:53:49 +00002084 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002085 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002086
2087 if (Result.isNull())
2088 return true;
2089
Douglas Gregor059101f2011-03-02 00:47:37 +00002090 // Build type-source information.
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002091 TypeLocBuilder TLB;
Douglas Gregor059101f2011-03-02 00:47:37 +00002092 TemplateSpecializationTypeLoc SpecTL
2093 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002094 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002095 SpecTL.setTemplateNameLoc(TemplateLoc);
2096 SpecTL.setLAngleLoc(LAngleLoc);
2097 SpecTL.setRAngleLoc(RAngleLoc);
2098 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2099 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002100
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002101 // NOTE: avoid constructing an ElaboratedTypeLoc if this is a
2102 // constructor or destructor name (in such a case, the scope specifier
2103 // will be attached to the enclosing Decl or Expr node).
2104 if (SS.isNotEmpty() && !IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002105 // Create an elaborated-type-specifier containing the nested-name-specifier.
2106 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2107 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002108 ElabTL.setElaboratedKeywordLoc(SourceLocation());
Douglas Gregor059101f2011-03-02 00:47:37 +00002109 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2110 }
2111
2112 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002113}
John McCallf1bbbb42009-09-04 01:14:41 +00002114
Douglas Gregor059101f2011-03-02 00:47:37 +00002115TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002116 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002117 SourceLocation TagLoc,
2118 CXXScopeSpec &SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002119 SourceLocation TemplateKWLoc,
2120 TemplateTy TemplateD,
Douglas Gregor059101f2011-03-02 00:47:37 +00002121 SourceLocation TemplateLoc,
2122 SourceLocation LAngleLoc,
2123 ASTTemplateArgsPtr TemplateArgsIn,
2124 SourceLocation RAngleLoc) {
2125 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2126
2127 // Translate the parser's template argument list in our AST format.
2128 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2129 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2130
2131 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002132 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002133 ElaboratedTypeKeyword Keyword
2134 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Douglas Gregor059101f2011-03-02 00:47:37 +00002136 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2137 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2138 DTN->getQualifier(),
2139 DTN->getIdentifier(),
2140 TemplateArgs);
2141
2142 // Build type-source information.
2143 TypeLocBuilder TLB;
2144 DependentTemplateSpecializationTypeLoc SpecTL
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002145 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2146 SpecTL.setElaboratedKeywordLoc(TagLoc);
2147 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002148 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002149 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002150 SpecTL.setLAngleLoc(LAngleLoc);
2151 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002152 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2153 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2154 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2155 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002156
2157 if (TypeAliasTemplateDecl *TAT =
2158 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2159 // C++0x [dcl.type.elab]p2:
2160 // If the identifier resolves to a typedef-name or the simple-template-id
2161 // resolves to an alias template specialization, the
2162 // elaborated-type-specifier is ill-formed.
2163 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2164 Diag(TAT->getLocation(), diag::note_declared_at);
2165 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002166
2167 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2168 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002169 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002170
2171 // Check the tag kind
2172 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002173 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002174
John McCall6b2becf2009-09-08 17:47:29 +00002175 IdentifierInfo *Id = D->getIdentifier();
2176 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002177
Richard Trieubbf34c02011-06-10 03:11:26 +00002178 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2179 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002180 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002181 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002182 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002183 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002184 }
2185 }
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002186
Douglas Gregor059101f2011-03-02 00:47:37 +00002187 // Provide source-location information for the template specialization.
2188 TypeLocBuilder TLB;
2189 TemplateSpecializationTypeLoc SpecTL
2190 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002191 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002192 SpecTL.setTemplateNameLoc(TemplateLoc);
2193 SpecTL.setLAngleLoc(LAngleLoc);
2194 SpecTL.setRAngleLoc(RAngleLoc);
2195 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2196 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002197
Douglas Gregor059101f2011-03-02 00:47:37 +00002198 // Construct an elaborated type containing the nested-name-specifier (if any)
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002199 // and tag keyword.
Douglas Gregor059101f2011-03-02 00:47:37 +00002200 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2201 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002202 ElabTL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002203 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2204 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002205}
2206
John McCall60d7b3a2010-08-24 06:29:42 +00002207ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002208 SourceLocation TemplateKWLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002209 LookupResult &R,
2210 bool RequiresADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002211 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002212 // FIXME: Can we do any checking at this point? I guess we could check the
2213 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002214 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002215 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002216 // foo<int> could identify a single function unambiguously
2217 // This approach does NOT work, since f<int>(1);
2218 // gets resolved prior to resorting to overload resolution
2219 // i.e., template<class T> void f(double);
2220 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002221
2222 // These should be filtered out by our callers.
2223 assert(!R.empty() && "empty lookup results when building templateid");
2224 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2225
John McCallc373d482010-01-27 01:50:18 +00002226 // We don't want lookup warnings at this point.
2227 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002228
John McCallf7a1a742009-11-24 19:00:30 +00002229 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002230 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002231 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002232 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002233 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002234 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002235 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002236
2237 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002238}
2239
John McCallf7a1a742009-11-24 19:00:30 +00002240// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002241ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002242Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002243 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002244 const DeclarationNameInfo &NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002245 const TemplateArgumentListInfo *TemplateArgs) {
2246 assert(TemplateArgs || TemplateKWLoc.isValid());
John McCallf7a1a742009-11-24 19:00:30 +00002247 DeclContext *DC;
2248 if (!(DC = computeDeclContext(SS, false)) ||
2249 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002250 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002251 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002253 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002254 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002255 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2256 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002257
John McCallf7a1a742009-11-24 19:00:30 +00002258 if (R.isAmbiguous())
2259 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002260
John McCallf7a1a742009-11-24 19:00:30 +00002261 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002262 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2263 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002264 return ExprError();
2265 }
2266
2267 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002268 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2269 << (NestedNameSpecifier*) SS.getScopeRep()
2270 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002271 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2272 return ExprError();
2273 }
2274
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002275 return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002276}
2277
Douglas Gregorc45c2322009-03-31 00:43:58 +00002278/// \brief Form a dependent template name.
2279///
2280/// This action forms a dependent template name given the template
2281/// name and its (presumably dependent) scope specifier. For
2282/// example, given "MetaFun::template apply", the scope specifier \p
2283/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2284/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002285TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002286 CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002287 SourceLocation TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002288 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002289 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002290 bool EnteringContext,
2291 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002292 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2293 Diag(TemplateKWLoc,
2294 getLangOptions().CPlusPlus0x ?
2295 diag::warn_cxx98_compat_template_outside_of_template :
2296 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002297 << FixItHint::CreateRemoval(TemplateKWLoc);
2298
Douglas Gregor0707bc52010-01-19 16:01:07 +00002299 DeclContext *LookupCtx = 0;
2300 if (SS.isSet())
2301 LookupCtx = computeDeclContext(SS, EnteringContext);
2302 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002303 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002304 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002305 // C++0x [temp.names]p5:
2306 // If a name prefixed by the keyword template is not the name of
2307 // a template, the program is ill-formed. [Note: the keyword
2308 // template may not be applied to non-template members of class
2309 // templates. -end note ] [ Note: as is the case with the
2310 // typename prefix, the template prefix is allowed in cases
2311 // where it is not strictly necessary; i.e., when the
2312 // nested-name-specifier or the expression on the left of the ->
2313 // or . is not dependent on a template-parameter, or the use
2314 // does not appear in the scope of a template. -end note]
2315 //
2316 // Note: C++03 was more strict here, because it banned the use of
2317 // the "template" keyword prior to a template-name that was not a
2318 // dependent name. C++ DR468 relaxed this requirement (the
2319 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002320 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002321 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002322 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2323 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002324 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002325 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2326 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002327 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2328 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002329 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002330 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002331 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002332 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002333 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002334 << Name.getSourceRange()
2335 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002336 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002337 } else {
2338 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002339 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002340 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002341 }
2342
Mike Stump1eb44332009-09-09 15:08:12 +00002343 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002344 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002345
Douglas Gregor014e88d2009-11-03 23:16:33 +00002346 switch (Name.getKind()) {
2347 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002348 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002349 Name.Identifier));
2350 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002351
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002352 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002353 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002354 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002355 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002356
2357 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002358 llvm_unreachable(
2359 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002360
Douglas Gregor014e88d2009-11-03 23:16:33 +00002361 default:
2362 break;
2363 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002364
2365 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002366 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002367 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002368 << Name.getSourceRange()
2369 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002370 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002371}
2372
Mike Stump1eb44332009-09-09 15:08:12 +00002373bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002374 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002375 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002376 const TemplateArgument &Arg = AL.getArgument();
2377
Anders Carlsson436b1562009-06-13 00:33:33 +00002378 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002379 switch(Arg.getKind()) {
2380 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002381 // C++ [temp.arg.type]p1:
2382 // A template-argument for a template-parameter which is a
2383 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002384 break;
2385 case TemplateArgument::Template: {
2386 // We have a template type parameter but the template argument
2387 // is a template without any arguments.
2388 SourceRange SR = AL.getSourceRange();
2389 TemplateName Name = Arg.getAsTemplate();
2390 Diag(SR.getBegin(), diag::err_template_missing_args)
2391 << Name << SR;
2392 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2393 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002394
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002395 return true;
2396 }
2397 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002398 // We have a template type parameter but the template argument
2399 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002400 SourceRange SR = AL.getSourceRange();
2401 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002402 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002403
Anders Carlsson436b1562009-06-13 00:33:33 +00002404 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002405 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002406 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002407
John McCalla93c9342009-12-07 02:54:59 +00002408 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002409 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002410
Anders Carlsson436b1562009-06-13 00:33:33 +00002411 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002412 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2413
2414 // Objective-C ARC:
2415 // If an explicitly-specified template argument type is a lifetime type
2416 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2417 if (getLangOptions().ObjCAutoRefCount &&
2418 ArgType->isObjCLifetimeType() &&
2419 !ArgType.getObjCLifetime()) {
2420 Qualifiers Qs;
2421 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2422 ArgType = Context.getQualifiedType(ArgType, Qs);
2423 }
2424
2425 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002426 return false;
2427}
2428
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002429/// \brief Substitute template arguments into the default template argument for
2430/// the given template type parameter.
2431///
2432/// \param SemaRef the semantic analysis object for which we are performing
2433/// the substitution.
2434///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002435/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002436/// for.
2437///
2438/// \param TemplateLoc the location of the template name that started the
2439/// template-id we are checking.
2440///
2441/// \param RAngleLoc the location of the right angle bracket ('>') that
2442/// terminates the template-id.
2443///
2444/// \param Param the template template parameter whose default we are
2445/// substituting into.
2446///
2447/// \param Converted the list of template arguments provided for template
2448/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002449/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002450static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002451SubstDefaultTemplateArgument(Sema &SemaRef,
2452 TemplateDecl *Template,
2453 SourceLocation TemplateLoc,
2454 SourceLocation RAngleLoc,
2455 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002456 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002457 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002458
2459 // If the argument type is dependent, instantiate it now based
2460 // on the previously-computed template arguments.
2461 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002462 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002463 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002464
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002465 MultiLevelTemplateArgumentList AllTemplateArgs
2466 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2467
2468 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002469 Template, Converted.data(),
2470 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002471 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002472
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002473 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2474 Param->getDefaultArgumentLoc(),
2475 Param->getDeclName());
2476 }
2477
2478 return ArgType;
2479}
2480
2481/// \brief Substitute template arguments into the default template argument for
2482/// the given non-type template parameter.
2483///
2484/// \param SemaRef the semantic analysis object for which we are performing
2485/// the substitution.
2486///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002487/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002488/// for.
2489///
2490/// \param TemplateLoc the location of the template name that started the
2491/// template-id we are checking.
2492///
2493/// \param RAngleLoc the location of the right angle bracket ('>') that
2494/// terminates the template-id.
2495///
Douglas Gregor788cd062009-11-11 01:00:40 +00002496/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002497/// substituting into.
2498///
2499/// \param Converted the list of template arguments provided for template
2500/// parameters that precede \p Param in the template parameter list.
2501///
2502/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002503static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002504SubstDefaultTemplateArgument(Sema &SemaRef,
2505 TemplateDecl *Template,
2506 SourceLocation TemplateLoc,
2507 SourceLocation RAngleLoc,
2508 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002509 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002510 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002511 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002512
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002513 MultiLevelTemplateArgumentList AllTemplateArgs
2514 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002515
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002516 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002517 Template, Converted.data(),
2518 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002519 SourceRange(TemplateLoc, RAngleLoc));
2520
2521 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2522}
2523
Douglas Gregor788cd062009-11-11 01:00:40 +00002524/// \brief Substitute template arguments into the default template argument for
2525/// the given template template parameter.
2526///
2527/// \param SemaRef the semantic analysis object for which we are performing
2528/// the substitution.
2529///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002530/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002531/// for.
2532///
2533/// \param TemplateLoc the location of the template name that started the
2534/// template-id we are checking.
2535///
2536/// \param RAngleLoc the location of the right angle bracket ('>') that
2537/// terminates the template-id.
2538///
2539/// \param Param the template template parameter whose default we are
2540/// substituting into.
2541///
2542/// \param Converted the list of template arguments provided for template
2543/// parameters that precede \p Param in the template parameter list.
2544///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002545/// \param QualifierLoc Will be set to the nested-name-specifier (with
2546/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002547///
Douglas Gregor788cd062009-11-11 01:00:40 +00002548/// \returns the substituted template argument, or NULL if an error occurred.
2549static TemplateName
2550SubstDefaultTemplateArgument(Sema &SemaRef,
2551 TemplateDecl *Template,
2552 SourceLocation TemplateLoc,
2553 SourceLocation RAngleLoc,
2554 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002555 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002556 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002557 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002558 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002559
Douglas Gregor788cd062009-11-11 01:00:40 +00002560 MultiLevelTemplateArgumentList AllTemplateArgs
2561 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002562
Douglas Gregor788cd062009-11-11 01:00:40 +00002563 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002564 Template, Converted.data(),
2565 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002566 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002567
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002568 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002569 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002570 if (QualifierLoc) {
2571 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2572 AllTemplateArgs);
2573 if (!QualifierLoc)
2574 return TemplateName();
2575 }
2576
Douglas Gregor1d752d72011-03-02 18:46:51 +00002577 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002578 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002579 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002580 AllTemplateArgs);
2581}
2582
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002583/// \brief If the given template parameter has a default template
2584/// argument, substitute into that default template argument and
2585/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002586TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002587Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2588 SourceLocation TemplateLoc,
2589 SourceLocation RAngleLoc,
2590 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002591 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002592 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002593 if (!TypeParm->hasDefaultArgument())
2594 return TemplateArgumentLoc();
2595
John McCalla93c9342009-12-07 02:54:59 +00002596 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002597 TemplateLoc,
2598 RAngleLoc,
2599 TypeParm,
2600 Converted);
2601 if (DI)
2602 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2603
2604 return TemplateArgumentLoc();
2605 }
2606
2607 if (NonTypeTemplateParmDecl *NonTypeParm
2608 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2609 if (!NonTypeParm->hasDefaultArgument())
2610 return TemplateArgumentLoc();
2611
John McCall60d7b3a2010-08-24 06:29:42 +00002612 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002613 TemplateLoc,
2614 RAngleLoc,
2615 NonTypeParm,
2616 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002617 if (Arg.isInvalid())
2618 return TemplateArgumentLoc();
2619
2620 Expr *ArgE = Arg.takeAs<Expr>();
2621 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2622 }
2623
2624 TemplateTemplateParmDecl *TempTempParm
2625 = cast<TemplateTemplateParmDecl>(Param);
2626 if (!TempTempParm->hasDefaultArgument())
2627 return TemplateArgumentLoc();
2628
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002629
Douglas Gregor1d752d72011-03-02 18:46:51 +00002630 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002631 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002632 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002633 RAngleLoc,
2634 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002635 Converted,
2636 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002637 if (TName.isNull())
2638 return TemplateArgumentLoc();
2639
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002640 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002641 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002642 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2643}
2644
Douglas Gregore7526412009-11-11 19:31:23 +00002645/// \brief Check that the given template argument corresponds to the given
2646/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002647///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002648/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002649/// checked.
2650///
2651/// \param Arg The template argument.
2652///
2653/// \param Template The template in which the template argument resides.
2654///
2655/// \param TemplateLoc The location of the template name for the template
2656/// whose argument list we're matching.
2657///
2658/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2659/// the template argument list.
2660///
2661/// \param ArgumentPackIndex The index into the argument pack where this
2662/// argument will be placed. Only valid if the parameter is a parameter pack.
2663///
2664/// \param Converted The checked, converted argument will be added to the
2665/// end of this small vector.
2666///
2667/// \param CTAK Describes how we arrived at this particular template argument:
2668/// explicitly written, deduced, etc.
2669///
2670/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002671bool Sema::CheckTemplateArgument(NamedDecl *Param,
2672 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002673 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002674 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002675 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002676 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002677 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002678 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002679 // Check template type parameters.
2680 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002681 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002682
Douglas Gregord9e15302009-11-11 19:41:09 +00002683 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002684 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002685 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002686 // with the template arguments we've seen thus far. But if the
2687 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002688 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002689 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2690 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002691
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002692 if (NTTPType->isDependentType() &&
2693 !isa<TemplateTemplateParmDecl>(Template) &&
2694 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002695 // Do substitution on the type of the non-type template parameter.
2696 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002697 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002698 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002699
2700 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002701 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002702 NTTPType = SubstType(NTTPType,
2703 MultiLevelTemplateArgumentList(TemplateArgs),
2704 NTTP->getLocation(),
2705 NTTP->getDeclName());
2706 // If that worked, check the non-type template parameter type
2707 // for validity.
2708 if (!NTTPType.isNull())
2709 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2710 NTTP->getLocation());
2711 if (NTTPType.isNull())
2712 return true;
2713 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002714
Douglas Gregore7526412009-11-11 19:31:23 +00002715 switch (Arg.getArgument().getKind()) {
2716 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002717 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002718
Douglas Gregore7526412009-11-11 19:31:23 +00002719 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002720 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002721 ExprResult Res =
2722 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2723 Result, CTAK);
2724 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002725 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002726
Douglas Gregor910f8002010-11-07 23:05:16 +00002727 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002728 break;
2729 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002730
Douglas Gregore7526412009-11-11 19:31:23 +00002731 case TemplateArgument::Declaration:
2732 case TemplateArgument::Integral:
2733 // We've already checked this template argument, so just copy
2734 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002735 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002736 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002737
Douglas Gregore7526412009-11-11 19:31:23 +00002738 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002739 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002740 // We were given a template template argument. It may not be ill-formed;
2741 // see below.
2742 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002743 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2744 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002745 // We have a template argument such as \c T::template X, which we
2746 // parsed as a template template argument. However, since we now
2747 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002748 // template name into an expression.
2749
2750 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2751 Arg.getTemplateNameLoc());
2752
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002753 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002754 SS.Adopt(Arg.getTemplateQualifierLoc());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002755 // FIXME: the template-template arg was a DependentTemplateName,
2756 // so it was provided with a template keyword. However, its source
2757 // location is not stored in the template argument structure.
2758 SourceLocation TemplateKWLoc;
John Wiegley429bb272011-04-08 18:41:53 +00002759 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002760 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002761 TemplateKWLoc,
2762 NameInfo, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002763
Douglas Gregora7fc9012011-01-05 18:58:31 +00002764 // If we parsed the template argument as a pack expansion, create a
2765 // pack expansion expression.
2766 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002767 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2768 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002769 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002770 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002771
Douglas Gregore7526412009-11-11 19:31:23 +00002772 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002773 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2774 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002775 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002776
Douglas Gregor910f8002010-11-07 23:05:16 +00002777 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002778 break;
2779 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002780
Douglas Gregore7526412009-11-11 19:31:23 +00002781 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002782 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002783 // therefore cannot be a non-type template argument.
2784 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2785 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002786
Douglas Gregore7526412009-11-11 19:31:23 +00002787 Diag(Param->getLocation(), diag::note_template_param_here);
2788 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789
Douglas Gregore7526412009-11-11 19:31:23 +00002790 case TemplateArgument::Type: {
2791 // We have a non-type template parameter but the template
2792 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002793
Douglas Gregore7526412009-11-11 19:31:23 +00002794 // C++ [temp.arg]p2:
2795 // In a template-argument, an ambiguity between a type-id and
2796 // an expression is resolved to a type-id, regardless of the
2797 // form of the corresponding template-parameter.
2798 //
2799 // We warn specifically about this case, since it can be rather
2800 // confusing for users.
2801 QualType T = Arg.getArgument().getAsType();
2802 SourceRange SR = Arg.getSourceRange();
2803 if (T->isFunctionType())
2804 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2805 else
2806 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2807 Diag(Param->getLocation(), diag::note_template_param_here);
2808 return true;
2809 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002810
Douglas Gregore7526412009-11-11 19:31:23 +00002811 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002812 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002813 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002814
Douglas Gregore7526412009-11-11 19:31:23 +00002815 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002816 }
2817
2818
Douglas Gregore7526412009-11-11 19:31:23 +00002819 // Check template template parameters.
2820 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002821
Douglas Gregore7526412009-11-11 19:31:23 +00002822 // Substitute into the template parameter list of the template
2823 // template parameter, since previously-supplied template arguments
2824 // may appear within the template template parameter.
2825 {
2826 // Set up a template instantiation context.
2827 LocalInstantiationScope Scope(*this);
2828 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002829 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002830 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002831
2832 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002833 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002834 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002835 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002836 MultiLevelTemplateArgumentList(TemplateArgs)));
2837 if (!TempParm)
2838 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002839 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002840
Douglas Gregore7526412009-11-11 19:31:23 +00002841 switch (Arg.getArgument().getKind()) {
2842 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002843 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002844
Douglas Gregore7526412009-11-11 19:31:23 +00002845 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002846 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002847 if (CheckTemplateArgument(TempParm, Arg))
2848 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002849
Douglas Gregor910f8002010-11-07 23:05:16 +00002850 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002851 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002852
Douglas Gregore7526412009-11-11 19:31:23 +00002853 case TemplateArgument::Expression:
2854 case TemplateArgument::Type:
2855 // We have a template template parameter but the template
2856 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002857 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2858 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002859 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002860
Douglas Gregore7526412009-11-11 19:31:23 +00002861 case TemplateArgument::Declaration:
David Blaikie7530c032012-01-17 06:56:22 +00002862 llvm_unreachable("Declaration argument with template template parameter");
Douglas Gregore7526412009-11-11 19:31:23 +00002863 case TemplateArgument::Integral:
David Blaikie7530c032012-01-17 06:56:22 +00002864 llvm_unreachable("Integral argument with template template parameter");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002865
Douglas Gregore7526412009-11-11 19:31:23 +00002866 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002867 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002868 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002869
Douglas Gregore7526412009-11-11 19:31:23 +00002870 return false;
2871}
2872
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002873/// \brief Diagnose an arity mismatch in the
2874static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template,
2875 SourceLocation TemplateLoc,
2876 TemplateArgumentListInfo &TemplateArgs) {
2877 TemplateParameterList *Params = Template->getTemplateParameters();
2878 unsigned NumParams = Params->size();
2879 unsigned NumArgs = TemplateArgs.size();
2880
2881 SourceRange Range;
2882 if (NumArgs > NumParams)
2883 Range = SourceRange(TemplateArgs[NumParams].getLocation(),
2884 TemplateArgs.getRAngleLoc());
2885 S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2886 << (NumArgs > NumParams)
2887 << (isa<ClassTemplateDecl>(Template)? 0 :
2888 isa<FunctionTemplateDecl>(Template)? 1 :
2889 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2890 << Template << Range;
2891 S.Diag(Template->getLocation(), diag::note_template_decl_here)
2892 << Params->getSourceRange();
2893 return true;
2894}
2895
Douglas Gregorc15cb382009-02-09 23:23:08 +00002896/// \brief Check that the given template argument list is well-formed
2897/// for specializing the given template.
2898bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2899 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002900 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002901 bool PartialTemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00002902 SmallVectorImpl<TemplateArgument> &Converted,
2903 bool *ExpansionIntoFixedList) {
2904 if (ExpansionIntoFixedList)
2905 *ExpansionIntoFixedList = false;
2906
Douglas Gregorc15cb382009-02-09 23:23:08 +00002907 TemplateParameterList *Params = Template->getTemplateParameters();
2908 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002909 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002910 bool Invalid = false;
2911
John McCalld5532b62009-11-23 01:53:49 +00002912 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2913
Mike Stump1eb44332009-09-09 15:08:12 +00002914 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002915 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Douglas Gregorb70126a2012-02-03 17:16:23 +00002916
Mike Stump1eb44332009-09-09 15:08:12 +00002917 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002918 // [...] The type and form of each template-argument specified in
2919 // a template-id shall match the type and form specified for the
2920 // corresponding parameter declared by the template in its
2921 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002922 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002923 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002924 TemplateParameterList::iterator Param = Params->begin(),
2925 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002926 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002927 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002928 bool SawPackExpansion = false;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002929 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002930 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002931 // If we have an expanded parameter pack, make sure we don't have too
2932 // many arguments.
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002933 // FIXME: This really should fall out from the normal arity checking.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002934 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002935 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002936 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002937 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2938 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2939 << true
2940 << (isa<ClassTemplateDecl>(Template)? 0 :
2941 isa<FunctionTemplateDecl>(Template)? 1 :
2942 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2943 << Template;
2944 Diag(Template->getLocation(), diag::note_template_decl_here)
2945 << Params->getSourceRange();
2946 return true;
2947 }
2948 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002949
Douglas Gregorf35f8282009-11-11 21:54:23 +00002950 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002951 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2952 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002953 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002954 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002955
Douglas Gregor14be16b2010-12-20 16:57:52 +00002956 if ((*Param)->isTemplateParameterPack()) {
2957 // The template parameter was a template parameter pack, so take the
2958 // deduced argument and place it on the argument pack. Note that we
2959 // stay on the same template parameter so that we can deduce more
2960 // arguments.
2961 ArgumentPack.push_back(Converted.back());
2962 Converted.pop_back();
2963 } else {
2964 // Move to the next template parameter.
2965 ++Param;
2966 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002967
2968 // If this template argument is a pack expansion, record that fact
2969 // and break out; we can't actually check any more.
2970 if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) {
2971 SawPackExpansion = true;
2972 ++ArgIdx;
2973 break;
2974 }
2975
Douglas Gregor14be16b2010-12-20 16:57:52 +00002976 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002977 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002978 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002979
Douglas Gregor8735b292011-06-03 02:59:40 +00002980 // If we're checking a partial template argument list, we're done.
2981 if (PartialTemplateArgs) {
2982 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2983 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2984 ArgumentPack.data(),
2985 ArgumentPack.size()));
2986
2987 return Invalid;
2988 }
2989
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002990 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002991 // arguments, just break out now and we'll fill in the argument pack below.
2992 if ((*Param)->isTemplateParameterPack())
2993 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002994
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002995 // Check whether we have a default argument.
Douglas Gregorf35f8282009-11-11 21:54:23 +00002996 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002997
Douglas Gregorf35f8282009-11-11 21:54:23 +00002998 // Retrieve the default template argument from the template
2999 // parameter. For each kind of template parameter, we substitute the
3000 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003001 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00003002 // the default argument.
3003 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003004 if (!TTP->hasDefaultArgument())
3005 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3006 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003007
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003008 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003009 Template,
3010 TemplateLoc,
3011 RAngleLoc,
3012 TTP,
3013 Converted);
3014 if (!ArgType)
3015 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003016
Douglas Gregorf35f8282009-11-11 21:54:23 +00003017 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
3018 ArgType);
3019 } else if (NonTypeTemplateParmDecl *NTTP
3020 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003021 if (!NTTP->hasDefaultArgument())
3022 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3023 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003024
John McCall60d7b3a2010-08-24 06:29:42 +00003025 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003026 TemplateLoc,
3027 RAngleLoc,
3028 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003029 Converted);
3030 if (E.isInvalid())
3031 return true;
3032
3033 Expr *Ex = E.takeAs<Expr>();
3034 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3035 } else {
3036 TemplateTemplateParmDecl *TempParm
3037 = cast<TemplateTemplateParmDecl>(*Param);
3038
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003039 if (!TempParm->hasDefaultArgument())
3040 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3041 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003042
Douglas Gregor1d752d72011-03-02 18:46:51 +00003043 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003044 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003045 TemplateLoc,
3046 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003047 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003048 Converted,
3049 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003050 if (Name.isNull())
3051 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003052
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003053 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3054 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003055 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003056
Douglas Gregorf35f8282009-11-11 21:54:23 +00003057 // Introduce an instantiation record that describes where we are using
3058 // the default template argument.
3059 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003060 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003061 SourceRange(TemplateLoc, RAngleLoc));
3062
Douglas Gregorf35f8282009-11-11 21:54:23 +00003063 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003064 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003065 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003066 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003067
Douglas Gregor67714232011-03-03 02:41:12 +00003068 // Core issue 150 (assumed resolution): if this is a template template
3069 // parameter, keep track of the default template arguments from the
3070 // template definition.
3071 if (isTemplateTemplateParameter)
3072 TemplateArgs.addArgument(Arg);
3073
Douglas Gregor14be16b2010-12-20 16:57:52 +00003074 // Move to the next template parameter and argument.
3075 ++Param;
3076 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003077 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003078
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003079 // If we saw a pack expansion, then directly convert the remaining arguments,
3080 // because we don't know what parameters they'll match up with.
3081 if (SawPackExpansion) {
3082 bool AddToArgumentPack
3083 = Param != ParamEnd && (*Param)->isTemplateParameterPack();
3084 while (ArgIdx < NumArgs) {
3085 if (AddToArgumentPack)
3086 ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument());
3087 else
3088 Converted.push_back(TemplateArgs[ArgIdx].getArgument());
3089 ++ArgIdx;
3090 }
3091
3092 // Push the argument pack onto the list of converted arguments.
3093 if (AddToArgumentPack) {
3094 if (ArgumentPack.empty())
3095 Converted.push_back(TemplateArgument(0, 0));
3096 else {
3097 Converted.push_back(
3098 TemplateArgument::CreatePackCopy(Context,
3099 ArgumentPack.data(),
3100 ArgumentPack.size()));
3101 ArgumentPack.clear();
3102 }
Douglas Gregorb70126a2012-02-03 17:16:23 +00003103 } else if (ExpansionIntoFixedList) {
3104 // We have expanded a pack into a fixed list.
3105 *ExpansionIntoFixedList = true;
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003106 }
3107
3108 return Invalid;
3109 }
3110
3111 // If we have any leftover arguments, then there were too many arguments.
3112 // Complain and fail.
3113 if (ArgIdx < NumArgs)
3114 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
3115
3116 // If we have an expanded parameter pack, make sure we don't have too
3117 // many arguments.
3118 // FIXME: This really should fall out from the normal arity checking.
3119 if (Param != ParamEnd) {
3120 if (NonTypeTemplateParmDecl *NTTP
3121 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
3122 if (NTTP->isExpandedParameterPack() &&
3123 ArgumentPack.size() < NTTP->getNumExpansionTypes()) {
3124 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
3125 << false
3126 << (isa<ClassTemplateDecl>(Template)? 0 :
3127 isa<FunctionTemplateDecl>(Template)? 1 :
3128 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
3129 << Template;
3130 Diag(Template->getLocation(), diag::note_template_decl_here)
3131 << Params->getSourceRange();
3132 return true;
3133 }
3134 }
3135 }
3136
Douglas Gregor14be16b2010-12-20 16:57:52 +00003137 // Form argument packs for each of the parameter packs remaining.
3138 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003139 // If we're checking a partial list of template arguments, don't fill
3140 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003141 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003142 if (!HasParameterPack)
3143 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003144 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003145 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003146 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003147 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3148 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003149 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003150 ArgumentPack.clear();
3151 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003152 } else if (!PartialTemplateArgs)
3153 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003154
Douglas Gregor14be16b2010-12-20 16:57:52 +00003155 ++Param;
3156 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003157
Douglas Gregorc15cb382009-02-09 23:23:08 +00003158 return Invalid;
3159}
3160
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003161namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003162 class UnnamedLocalNoLinkageFinder
3163 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003164 {
3165 Sema &S;
3166 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003167
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003168 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003169
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003170 public:
3171 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3172
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003173 bool Visit(QualType T) {
3174 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003175 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003176
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003177#define TYPE(Class, Parent) \
3178 bool Visit##Class##Type(const Class##Type *);
3179#define ABSTRACT_TYPE(Class, Parent) \
3180 bool Visit##Class##Type(const Class##Type *) { return false; }
3181#define NON_CANONICAL_TYPE(Class, Parent) \
3182 bool Visit##Class##Type(const Class##Type *) { return false; }
3183#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003184
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003185 bool VisitTagDecl(const TagDecl *Tag);
3186 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3187 };
3188}
3189
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003190bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003191 return false;
3192}
3193
3194bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3195 return Visit(T->getElementType());
3196}
3197
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003198bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003199 return Visit(T->getPointeeType());
3200}
3201
3202bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003203 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003204 return Visit(T->getPointeeType());
3205}
3206
3207bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003208 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003209 return Visit(T->getPointeeType());
3210}
3211
3212bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003213 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003214 return Visit(T->getPointeeType());
3215}
3216
3217bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003218 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003219 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3220}
3221
3222bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003223 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003224 return Visit(T->getElementType());
3225}
3226
3227bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003228 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003229 return Visit(T->getElementType());
3230}
3231
3232bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003233 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003234 return Visit(T->getElementType());
3235}
3236
3237bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003238 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003239 return Visit(T->getElementType());
3240}
3241
3242bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003243 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003244 return Visit(T->getElementType());
3245}
3246
3247bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3248 return Visit(T->getElementType());
3249}
3250
3251bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3252 return Visit(T->getElementType());
3253}
3254
3255bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3256 const FunctionProtoType* T) {
3257 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003258 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003259 A != AEnd; ++A) {
3260 if (Visit(*A))
3261 return true;
3262 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003263
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003264 return Visit(T->getResultType());
3265}
3266
3267bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3268 const FunctionNoProtoType* T) {
3269 return Visit(T->getResultType());
3270}
3271
3272bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3273 const UnresolvedUsingType*) {
3274 return false;
3275}
3276
3277bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3278 return false;
3279}
3280
3281bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3282 return Visit(T->getUnderlyingType());
3283}
3284
3285bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3286 return false;
3287}
3288
Sean Huntca63c202011-05-24 22:41:36 +00003289bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3290 const UnaryTransformType*) {
3291 return false;
3292}
3293
Richard Smith34b41d92011-02-20 03:19:35 +00003294bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3295 return Visit(T->getDeducedType());
3296}
3297
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003298bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3299 return VisitTagDecl(T->getDecl());
3300}
3301
3302bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3303 return VisitTagDecl(T->getDecl());
3304}
3305
3306bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3307 const TemplateTypeParmType*) {
3308 return false;
3309}
3310
Douglas Gregorc3069d62011-01-14 02:55:32 +00003311bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3312 const SubstTemplateTypeParmPackType *) {
3313 return false;
3314}
3315
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003316bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3317 const TemplateSpecializationType*) {
3318 return false;
3319}
3320
3321bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3322 const InjectedClassNameType* T) {
3323 return VisitTagDecl(T->getDecl());
3324}
3325
3326bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3327 const DependentNameType* T) {
3328 return VisitNestedNameSpecifier(T->getQualifier());
3329}
3330
3331bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3332 const DependentTemplateSpecializationType* T) {
3333 return VisitNestedNameSpecifier(T->getQualifier());
3334}
3335
Douglas Gregor7536dd52010-12-20 02:24:11 +00003336bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3337 const PackExpansionType* T) {
3338 return Visit(T->getPattern());
3339}
3340
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003341bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3342 return false;
3343}
3344
3345bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3346 const ObjCInterfaceType *) {
3347 return false;
3348}
3349
3350bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3351 const ObjCObjectPointerType *) {
3352 return false;
3353}
3354
Eli Friedmanb001de72011-10-06 23:00:33 +00003355bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3356 return Visit(T->getValueType());
3357}
3358
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003359bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3360 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003361 S.Diag(SR.getBegin(),
3362 S.getLangOptions().CPlusPlus0x ?
3363 diag::warn_cxx98_compat_template_arg_local_type :
3364 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003365 << S.Context.getTypeDeclType(Tag) << SR;
3366 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003367 }
3368
Richard Smith162e1c12011-04-15 14:24:37 +00003369 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003370 S.Diag(SR.getBegin(),
3371 S.getLangOptions().CPlusPlus0x ?
3372 diag::warn_cxx98_compat_template_arg_unnamed_type :
3373 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003374 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3375 return true;
3376 }
3377
3378 return false;
3379}
3380
3381bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3382 NestedNameSpecifier *NNS) {
3383 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3384 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003385
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003386 switch (NNS->getKind()) {
3387 case NestedNameSpecifier::Identifier:
3388 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003389 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003390 case NestedNameSpecifier::Global:
3391 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003392
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003393 case NestedNameSpecifier::TypeSpec:
3394 case NestedNameSpecifier::TypeSpecWithTemplate:
3395 return Visit(QualType(NNS->getAsType(), 0));
3396 }
David Blaikie7530c032012-01-17 06:56:22 +00003397 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003398}
3399
3400
Douglas Gregorc15cb382009-02-09 23:23:08 +00003401/// \brief Check a template argument against its corresponding
3402/// template type parameter.
3403///
3404/// This routine implements the semantics of C++ [temp.arg.type]. It
3405/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003406bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003407 TypeSourceInfo *ArgInfo) {
3408 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003409 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003410 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003411
3412 if (Arg->isVariablyModifiedType()) {
3413 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003414 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003415 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003416 }
3417
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003418 // C++03 [temp.arg.type]p2:
3419 // A local type, a type with no linkage, an unnamed type or a type
3420 // compounded from any of these types shall not be used as a
3421 // template-argument for a template type-parameter.
3422 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003423 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003424 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003425 if (LangOpts.CPlusPlus0x ?
3426 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3427 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3428 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3429 SR.getBegin()) != DiagnosticsEngine::Ignored :
3430 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003431 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3432 (void)Finder.Visit(Context.getCanonicalType(Arg));
3433 }
3434
Douglas Gregorc15cb382009-02-09 23:23:08 +00003435 return false;
3436}
3437
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003438/// \brief Checks whether the given template argument is the address
3439/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003440static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003441CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3442 NonTypeTemplateParmDecl *Param,
3443 QualType ParamType,
3444 Expr *ArgIn,
3445 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003446 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003447 Expr *Arg = ArgIn;
3448 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003449
3450 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003451 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003452
3453 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003454 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003455 // A template-argument for a non-type, non-template
3456 // template-parameter shall be one of: [...]
3457 //
3458 // -- the address of an object or function with external
3459 // linkage, including function templates and function
3460 // template-ids but excluding non-static class members,
3461 // expressed as & id-expression where the & is optional if
3462 // the name refers to a function or array, or if the
3463 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003464
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003465 // In C++98/03 mode, give an extension warning on any extra parentheses.
3466 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3467 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003468 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003469 if (!Invalid && !ExtraParens) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003470 S.Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003471 S.getLangOptions().CPlusPlus0x ?
3472 diag::warn_cxx98_compat_template_arg_extra_parens :
3473 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003474 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003475 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003476 }
3477
3478 Arg = Parens->getSubExpr();
3479 }
3480
John McCall91a57552011-07-15 05:09:51 +00003481 while (SubstNonTypeTemplateParmExpr *subst =
3482 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3483 Arg = subst->getReplacement()->IgnoreImpCasts();
3484
Douglas Gregorb7a09262010-04-01 18:32:35 +00003485 bool AddressTaken = false;
3486 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003487 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003488 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003489 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003490 AddressTaken = true;
3491 AddrOpLoc = UnOp->getOperatorLoc();
3492 }
Francois Picheta343a412011-04-29 09:08:14 +00003493 }
John McCall91a57552011-07-15 05:09:51 +00003494
Francois Pichet62ec1f22011-09-17 17:15:52 +00003495 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003496 Converted = TemplateArgument(ArgIn);
3497 return false;
3498 }
3499
3500 while (SubstNonTypeTemplateParmExpr *subst =
3501 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3502 Arg = subst->getReplacement()->IgnoreImpCasts();
3503
3504 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003505 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003506 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3507 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003508 S.Diag(Param->getLocation(), diag::note_template_param_here);
3509 return true;
3510 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003511
3512 // Stop checking the precise nature of the argument if it is value dependent,
3513 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003514 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003515 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003516 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003517 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003518
Douglas Gregorb7a09262010-04-01 18:32:35 +00003519 if (!isa<ValueDecl>(DRE->getDecl())) {
3520 S.Diag(Arg->getSourceRange().getBegin(),
3521 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003522 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003523 S.Diag(Param->getLocation(), diag::note_template_param_here);
3524 return true;
3525 }
3526
3527 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003528
3529 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003530 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3531 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003532 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003533 S.Diag(Param->getLocation(), diag::note_template_param_here);
3534 return true;
3535 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003536
3537 // Cannot refer to non-static member functions
3538 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003539 if (!Method->isStatic()) {
3540 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003541 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003542 S.Diag(Param->getLocation(), diag::note_template_param_here);
3543 return true;
3544 }
Mike Stump1eb44332009-09-09 15:08:12 +00003545
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003546 // Functions must have external linkage.
3547 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003548 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003549 S.Diag(Arg->getSourceRange().getBegin(),
3550 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003551 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003552 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003553 << true;
3554 return true;
3555 }
3556
3557 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003558 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003559
Douglas Gregorb7a09262010-04-01 18:32:35 +00003560 // If the template parameter has pointer type, the function decays.
3561 if (ParamType->isPointerType() && !AddressTaken)
3562 ArgType = S.Context.getPointerType(Func->getType());
3563 else if (AddressTaken && ParamType->isReferenceType()) {
3564 // If we originally had an address-of operator, but the
3565 // parameter has reference type, complain and (if things look
3566 // like they will work) drop the address-of operator.
3567 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3568 ParamType.getNonReferenceType())) {
3569 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3570 << ParamType;
3571 S.Diag(Param->getLocation(), diag::note_template_param_here);
3572 return true;
3573 }
3574
3575 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3576 << ParamType
3577 << FixItHint::CreateRemoval(AddrOpLoc);
3578 S.Diag(Param->getLocation(), diag::note_template_param_here);
3579
3580 ArgType = Func->getType();
3581 }
3582 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003583 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003584 S.Diag(Arg->getSourceRange().getBegin(),
3585 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003586 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003587 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003588 << true;
3589 return true;
3590 }
3591
Douglas Gregorb7a09262010-04-01 18:32:35 +00003592 // A value of reference type is not an object.
3593 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003594 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003595 diag::err_template_arg_reference_var)
3596 << Var->getType() << Arg->getSourceRange();
3597 S.Diag(Param->getLocation(), diag::note_template_param_here);
3598 return true;
3599 }
3600
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003601 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003602 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003603
3604 // If the template parameter has pointer type, we must have taken
3605 // the address of this object.
3606 if (ParamType->isReferenceType()) {
3607 if (AddressTaken) {
3608 // If we originally had an address-of operator, but the
3609 // parameter has reference type, complain and (if things look
3610 // like they will work) drop the address-of operator.
3611 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3612 ParamType.getNonReferenceType())) {
3613 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3614 << ParamType;
3615 S.Diag(Param->getLocation(), diag::note_template_param_here);
3616 return true;
3617 }
3618
3619 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3620 << ParamType
3621 << FixItHint::CreateRemoval(AddrOpLoc);
3622 S.Diag(Param->getLocation(), diag::note_template_param_here);
3623
3624 ArgType = Var->getType();
3625 }
3626 } else if (!AddressTaken && ParamType->isPointerType()) {
3627 if (Var->getType()->isArrayType()) {
3628 // Array-to-pointer decay.
3629 ArgType = S.Context.getArrayDecayedType(Var->getType());
3630 } else {
3631 // If the template parameter has pointer type but the address of
3632 // this object was not taken, complain and (possibly) recover by
3633 // taking the address of the entity.
3634 ArgType = S.Context.getPointerType(Var->getType());
3635 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3636 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3637 << ParamType;
3638 S.Diag(Param->getLocation(), diag::note_template_param_here);
3639 return true;
3640 }
3641
3642 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3643 << ParamType
3644 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3645
3646 S.Diag(Param->getLocation(), diag::note_template_param_here);
3647 }
3648 }
3649 } else {
3650 // We found something else, but we don't know specifically what it is.
3651 S.Diag(Arg->getSourceRange().getBegin(),
3652 diag::err_template_arg_not_object_or_func)
3653 << Arg->getSourceRange();
3654 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3655 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003656 }
Mike Stump1eb44332009-09-09 15:08:12 +00003657
John McCallf85e1932011-06-15 23:02:42 +00003658 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003659 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003660 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003661 S.IsQualificationConversion(ArgType, ParamType, false,
3662 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003663 // For pointer-to-object types, qualification conversions are
3664 // permitted.
3665 } else {
3666 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3667 if (!ParamRef->getPointeeType()->isFunctionType()) {
3668 // C++ [temp.arg.nontype]p5b3:
3669 // For a non-type template-parameter of type reference to
3670 // object, no conversions apply. The type referred to by the
3671 // reference may be more cv-qualified than the (otherwise
3672 // identical) type of the template- argument. The
3673 // template-parameter is bound directly to the
3674 // template-argument, which shall be an lvalue.
3675
3676 // FIXME: Other qualifiers?
3677 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3678 unsigned ArgQuals = ArgType.getCVRQualifiers();
3679
3680 if ((ParamQuals | ArgQuals) != ParamQuals) {
3681 S.Diag(Arg->getSourceRange().getBegin(),
3682 diag::err_template_arg_ref_bind_ignores_quals)
3683 << ParamType << Arg->getType()
3684 << Arg->getSourceRange();
3685 S.Diag(Param->getLocation(), diag::note_template_param_here);
3686 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003687 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003688 }
3689 }
3690
3691 // At this point, the template argument refers to an object or
3692 // function with external linkage. We now need to check whether the
3693 // argument and parameter types are compatible.
3694 if (!S.Context.hasSameUnqualifiedType(ArgType,
3695 ParamType.getNonReferenceType())) {
3696 // We can't perform this conversion or binding.
3697 if (ParamType->isReferenceType())
3698 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003699 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003700 else
3701 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003702 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003703 S.Diag(Param->getLocation(), diag::note_template_param_here);
3704 return true;
3705 }
3706 }
3707
3708 // Create the template argument.
3709 Converted = TemplateArgument(Entity->getCanonicalDecl());
Eli Friedman5f2987c2012-02-02 03:46:19 +00003710 S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003711 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003712}
3713
3714/// \brief Checks whether the given template argument is a pointer to
3715/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003716bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003717 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003718 bool Invalid = false;
3719
3720 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003721 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003722 Arg = Cast->getSubExpr();
3723
3724 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003725 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003726 // A template-argument for a non-type, non-template
3727 // template-parameter shall be one of: [...]
3728 //
3729 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003730 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003731
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003732 // In C++98/03 mode, give an extension warning on any extra parentheses.
3733 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3734 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003735 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003736 if (!Invalid && !ExtraParens) {
Mike Stump1eb44332009-09-09 15:08:12 +00003737 Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003738 getLangOptions().CPlusPlus0x ?
3739 diag::warn_cxx98_compat_template_arg_extra_parens :
3740 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003741 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003742 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003743 }
3744
3745 Arg = Parens->getSubExpr();
3746 }
3747
John McCall91a57552011-07-15 05:09:51 +00003748 while (SubstNonTypeTemplateParmExpr *subst =
3749 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3750 Arg = subst->getReplacement()->IgnoreImpCasts();
3751
Douglas Gregorcaddba02009-11-12 18:38:13 +00003752 // A pointer-to-member constant written &Class::member.
3753 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003754 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003755 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3756 if (DRE && !DRE->getQualifier())
3757 DRE = 0;
3758 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003759 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003760 // A constant of pointer-to-member type.
3761 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3762 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3763 if (VD->getType()->isMemberPointerType()) {
3764 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003765 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003766 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3767 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003768 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003769 else
3770 Converted = TemplateArgument(VD->getCanonicalDecl());
3771 return Invalid;
3772 }
3773 }
3774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003775
Douglas Gregorcaddba02009-11-12 18:38:13 +00003776 DRE = 0;
3777 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003778
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003779 if (!DRE)
3780 return Diag(Arg->getSourceRange().getBegin(),
3781 diag::err_template_arg_not_pointer_to_member_form)
3782 << Arg->getSourceRange();
3783
3784 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3785 assert((isa<FieldDecl>(DRE->getDecl()) ||
3786 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3787 "Only non-static member pointers can make it here");
3788
3789 // Okay: this is the address of a non-static member, and therefore
3790 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003791 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003792 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003793 else
3794 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003795 return Invalid;
3796 }
3797
3798 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003799 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003800 diag::err_template_arg_not_pointer_to_member_form)
3801 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003802 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003803 diag::note_template_arg_refers_here);
3804 return true;
3805}
3806
Douglas Gregorc15cb382009-02-09 23:23:08 +00003807/// \brief Check a template argument against its corresponding
3808/// non-type template parameter.
3809///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003810/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003811/// If an error occurred, it returns ExprError(); otherwise, it
3812/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003813/// InstantiatedParamType is the type of the non-type template
3814/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003815ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3816 QualType InstantiatedParamType, Expr *Arg,
3817 TemplateArgument &Converted,
3818 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003819 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3820
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003821 // If either the parameter has a dependent type or the argument is
3822 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003823 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3824 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003825 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003826 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003827 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003828
3829 // C++ [temp.arg.nontype]p5:
3830 // The following conversions are performed on each expression used
3831 // as a non-type template-argument. If a non-type
3832 // template-argument cannot be converted to the type of the
3833 // corresponding template-parameter then the program is
3834 // ill-formed.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003835 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003836 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003837 // C++11:
3838 // -- for a non-type template-parameter of integral or
3839 // enumeration type, conversions permitted in a converted
3840 // constant expression are applied.
3841 //
3842 // C++98:
3843 // -- for a non-type template-parameter of integral or
3844 // enumeration type, integral promotions (4.5) and integral
3845 // conversions (4.7) are applied.
3846
3847 if (CTAK == CTAK_Deduced &&
3848 !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) {
3849 // C++ [temp.deduct.type]p17:
3850 // If, in the declaration of a function template with a non-type
3851 // template-parameter, the non-type template-parameter is used
3852 // in an expression in the function parameter-list and, if the
3853 // corresponding template-argument is deduced, the
3854 // template-argument type shall match the type of the
3855 // template-parameter exactly, except that a template-argument
3856 // deduced from an array bound may be of any integral type.
3857 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3858 << Arg->getType().getUnqualifiedType()
3859 << ParamType.getUnqualifiedType();
3860 Diag(Param->getLocation(), diag::note_template_param_here);
3861 return ExprError();
3862 }
3863
3864 if (getLangOptions().CPlusPlus0x) {
3865 // We can't check arbitrary value-dependent arguments.
3866 // FIXME: If there's no viable conversion to the template parameter type,
3867 // we should be able to diagnose that prior to instantiation.
3868 if (Arg->isValueDependent()) {
3869 Converted = TemplateArgument(Arg);
3870 return Owned(Arg);
3871 }
3872
3873 // C++ [temp.arg.nontype]p1:
3874 // A template-argument for a non-type, non-template template-parameter
3875 // shall be one of:
3876 //
3877 // -- for a non-type template-parameter of integral or enumeration
3878 // type, a converted constant expression of the type of the
3879 // template-parameter; or
3880 llvm::APSInt Value;
3881 ExprResult ArgResult =
3882 CheckConvertedConstantExpression(Arg, ParamType, Value,
3883 CCEK_TemplateArg);
3884 if (ArgResult.isInvalid())
3885 return ExprError();
3886
3887 // Widen the argument value to sizeof(parameter type). This is almost
3888 // always a no-op, except when the parameter type is bool. In
3889 // that case, this may extend the argument from 1 bit to 8 bits.
3890 QualType IntegerType = ParamType;
3891 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
3892 IntegerType = Enum->getDecl()->getIntegerType();
3893 Value = Value.extOrTrunc(Context.getTypeSize(IntegerType));
3894
3895 Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType));
3896 return ArgResult;
3897 }
3898
Richard Smith4f870622011-10-27 22:11:44 +00003899 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3900 if (ArgResult.isInvalid())
3901 return ExprError();
3902 Arg = ArgResult.take();
3903
3904 QualType ArgType = Arg->getType();
3905
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003906 // C++ [temp.arg.nontype]p1:
3907 // A template-argument for a non-type, non-template
3908 // template-parameter shall be one of:
3909 //
3910 // -- an integral constant-expression of integral or enumeration
3911 // type; or
3912 // -- the name of a non-type template-parameter; or
3913 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003914 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003915 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003916 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003917 diag::err_template_arg_not_integral_or_enumeral)
3918 << ArgType << Arg->getSourceRange();
3919 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003920 return ExprError();
Richard Smith282e7e62012-02-04 09:53:13 +00003921 } else if (!Arg->isValueDependent()) {
3922 Arg = VerifyIntegerConstantExpression(Arg, &Value,
3923 PDiag(diag::err_template_arg_not_ice) << ArgType, false).take();
3924 if (!Arg)
3925 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003926 }
3927
Douglas Gregor02024a92010-03-28 02:42:43 +00003928 // From here on out, all we care about are the unqualified forms
3929 // of the parameter and argument types.
3930 ParamType = ParamType.getUnqualifiedType();
3931 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003932
3933 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003934 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003935 // Okay: no conversion necessary
John McCalldaa8e4e2010-11-15 09:13:47 +00003936 } else if (ParamType->isBooleanType()) {
3937 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003938 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003939 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3940 !ParamType->isEnumeralType()) {
3941 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003942 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003943 } else {
3944 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003945 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003946 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003947 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003948 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003949 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003950 }
3951
Douglas Gregorc7469372011-05-04 21:55:00 +00003952 // Add the value of this argument to the list of converted
3953 // arguments. We use the bitwidth and signedness of the template
3954 // parameter.
3955 if (Arg->isValueDependent()) {
3956 // The argument is value-dependent. Create a new
3957 // TemplateArgument with the converted expression.
3958 Converted = TemplateArgument(Arg);
3959 return Owned(Arg);
3960 }
3961
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003962 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003963 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003964 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003965
Douglas Gregorc7469372011-05-04 21:55:00 +00003966 if (ParamType->isBooleanType()) {
3967 // Value must be zero or one.
3968 Value = Value != 0;
3969 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3970 if (Value.getBitWidth() != AllowedBits)
3971 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003972 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003973 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003974 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003975
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003976 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003977 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003978 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003979 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003980 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003981 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003982
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003983 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003984 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003985 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003986 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3987 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3988 << Arg->getSourceRange();
3989 Diag(Param->getLocation(), diag::note_template_param_here);
3990 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003991
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003992 // Complain if we overflowed the template parameter's type.
3993 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003994 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003995 RequiredBits = OldValue.getActiveBits();
3996 else if (OldValue.isUnsigned())
3997 RequiredBits = OldValue.getActiveBits() + 1;
3998 else
3999 RequiredBits = OldValue.getMinSignedBits();
4000 if (RequiredBits > AllowedBits) {
4001 Diag(Arg->getSourceRange().getBegin(),
4002 diag::warn_template_arg_too_large)
4003 << OldValue.toString(10) << Value.toString(10) << Param->getType()
4004 << Arg->getSourceRange();
4005 Diag(Param->getLocation(), diag::note_template_param_here);
4006 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00004007 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00004008
John McCall833ca992009-10-29 08:12:44 +00004009 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00004010 ParamType->isEnumeralType()
4011 ? Context.getCanonicalType(ParamType)
4012 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00004013 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00004014 }
Douglas Gregora35284b2009-02-11 00:19:33 +00004015
Richard Smith4f870622011-10-27 22:11:44 +00004016 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00004017 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
4018
Douglas Gregorb7a09262010-04-01 18:32:35 +00004019 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
4020 // from a template argument of type std::nullptr_t to a non-type
4021 // template parameter of type pointer to object, pointer to
4022 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004023 if (ArgType->isNullPtrType()) {
4024 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
4025 Converted = TemplateArgument((NamedDecl *)0);
4026 return Owned(Arg);
4027 }
4028
4029 if (ParamType->isNullPtrType()) {
4030 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
4031 Converted = TemplateArgument(Zero, Context.NullPtrTy);
4032 return Owned(Arg);
4033 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004034 }
4035
Douglas Gregorb86b0572009-02-11 01:18:59 +00004036 // Handle pointer-to-function, reference-to-function, and
4037 // pointer-to-member-function all in (roughly) the same way.
4038 if (// -- For a non-type template-parameter of type pointer to
4039 // function, only the function-to-pointer conversion (4.3) is
4040 // applied. If the template-argument represents a set of
4041 // overloaded functions (or a pointer to such), the matching
4042 // function is selected from the set (13.4).
4043 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004044 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004045 // -- For a non-type template-parameter of type reference to
4046 // function, no conversions apply. If the template-argument
4047 // represents a set of overloaded functions, the matching
4048 // function is selected from the set (13.4).
4049 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004050 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004051 // -- For a non-type template-parameter of type pointer to
4052 // member function, no conversions apply. If the
4053 // template-argument represents a set of overloaded member
4054 // functions, the matching member function is selected from
4055 // the set (13.4).
4056 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004057 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00004058 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004059
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004060 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004061 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004062 true,
4063 FoundResult)) {
4064 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00004065 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004066
4067 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4068 ArgType = Arg->getType();
4069 } else
John Wiegley429bb272011-04-08 18:41:53 +00004070 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004071 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004072
John Wiegley429bb272011-04-08 18:41:53 +00004073 if (!ParamType->isMemberPointerType()) {
4074 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4075 ParamType,
4076 Arg, Converted))
4077 return ExprError();
4078 return Owned(Arg);
4079 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004080
John McCallf85e1932011-06-15 23:02:42 +00004081 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00004082 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00004083 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004084 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4085 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004086 } else if (!Context.hasSameUnqualifiedType(ArgType,
4087 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00004088 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004089 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00004090 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004091 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00004092 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004093 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004094 }
Mike Stump1eb44332009-09-09 15:08:12 +00004095
John Wiegley429bb272011-04-08 18:41:53 +00004096 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4097 return ExprError();
4098 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00004099 }
4100
Chris Lattnerfe90de72009-02-20 21:37:53 +00004101 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004102 // -- for a non-type template-parameter of type pointer to
4103 // object, qualification conversions (4.4) and the
4104 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00004105 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00004106 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004107 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004108
John Wiegley429bb272011-04-08 18:41:53 +00004109 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4110 ParamType,
4111 Arg, Converted))
4112 return ExprError();
4113 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004114 }
Mike Stump1eb44332009-09-09 15:08:12 +00004115
Ted Kremenek6217b802009-07-29 21:53:49 +00004116 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004117 // -- For a non-type template-parameter of type reference to
4118 // object, no conversions apply. The type referred to by the
4119 // reference may be more cv-qualified than the (otherwise
4120 // identical) type of the template-argument. The
4121 // template-parameter is bound directly to the
4122 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00004123 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004124 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004125
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004126 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004127 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
4128 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004129 true,
4130 FoundResult)) {
4131 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00004132 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004133
4134 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4135 ArgType = Arg->getType();
4136 } else
John Wiegley429bb272011-04-08 18:41:53 +00004137 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004138 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004139
John Wiegley429bb272011-04-08 18:41:53 +00004140 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4141 ParamType,
4142 Arg, Converted))
4143 return ExprError();
4144 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004145 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004146
4147 // -- For a non-type template-parameter of type pointer to data
4148 // member, qualification conversions (4.4) are applied.
4149 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4150
John McCallf85e1932011-06-15 23:02:42 +00004151 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004152 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004153 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004154 } else if (IsQualificationConversion(ArgType, ParamType, false,
4155 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004156 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4157 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004158 } else {
4159 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004160 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004161 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004162 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004163 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004164 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004165 }
4166
John Wiegley429bb272011-04-08 18:41:53 +00004167 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4168 return ExprError();
4169 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004170}
4171
4172/// \brief Check a template argument against its corresponding
4173/// template template parameter.
4174///
4175/// This routine implements the semantics of C++ [temp.arg.template].
4176/// It returns true if an error occurred, and false otherwise.
4177bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004178 const TemplateArgumentLoc &Arg) {
4179 TemplateName Name = Arg.getArgument().getAsTemplate();
4180 TemplateDecl *Template = Name.getAsTemplateDecl();
4181 if (!Template) {
4182 // Any dependent template name is fine.
4183 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4184 return false;
4185 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004186
Richard Smith3e4c6c42011-05-05 21:57:07 +00004187 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004188 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004189 // the name of a class template or an alias template, expressed as an
4190 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004191 // primary class templates are considered when matching the
4192 // template template argument with the corresponding parameter;
4193 // partial specializations are not considered even if their
4194 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004195 //
4196 // Note that we also allow template template parameters here, which
4197 // will happen when we are dealing with, e.g., class template
4198 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004199 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004200 !isa<TemplateTemplateParmDecl>(Template) &&
4201 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004202 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004203 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004204 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004205 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004206 << Template;
4207 }
4208
4209 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4210 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004211 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004212 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004213 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004214}
4215
Douglas Gregor02024a92010-03-28 02:42:43 +00004216/// \brief Given a non-type template argument that refers to a
4217/// declaration and the type of its corresponding non-type template
4218/// parameter, produce an expression that properly refers to that
4219/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004220ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004221Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4222 QualType ParamType,
4223 SourceLocation Loc) {
4224 assert(Arg.getKind() == TemplateArgument::Declaration &&
4225 "Only declaration template arguments permitted here");
4226 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4227
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004228 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004229 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4230 // If the value is a class member, we might have a pointer-to-member.
4231 // Determine whether the non-type template template parameter is of
4232 // pointer-to-member type. If so, we need to build an appropriate
4233 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4234 // would refer to the member itself.
4235 if (ParamType->isMemberPointerType()) {
4236 QualType ClassType
4237 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4238 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004239 = NestedNameSpecifier::Create(Context, 0, false,
4240 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004241 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004242 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004243
4244 // The actual value-ness of this is unimportant, but for
4245 // internal consistency's sake, references to instance methods
4246 // are r-values.
4247 ExprValueKind VK = VK_LValue;
4248 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4249 VK = VK_RValue;
4250
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004251 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004252 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004253 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004254 Loc,
4255 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004256 if (RefExpr.isInvalid())
4257 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004258
John McCall2de56d12010-08-25 11:45:40 +00004259 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004260
Douglas Gregorc0c83002010-04-30 21:46:38 +00004261 // We might need to perform a trailing qualification conversion, since
4262 // the element type on the parameter could be more qualified than the
4263 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004264 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004265 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004266 ParamType.getUnqualifiedType(), false,
4267 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004268 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004269
Douglas Gregor02024a92010-03-28 02:42:43 +00004270 assert(!RefExpr.isInvalid() &&
4271 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004272 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004273 return move(RefExpr);
4274 }
4275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004276
Douglas Gregor02024a92010-03-28 02:42:43 +00004277 QualType T = VD->getType().getNonReferenceType();
4278 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004279 // When the non-type template parameter is a pointer, take the
4280 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004281 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004282 if (RefExpr.isInvalid())
4283 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004284
4285 if (T->isFunctionType() || T->isArrayType()) {
4286 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004287 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4288 if (RefExpr.isInvalid())
4289 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004290
4291 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004292 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004293
Douglas Gregorb7a09262010-04-01 18:32:35 +00004294 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004295 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004296 }
4297
John McCallf89e55a2010-11-18 06:31:45 +00004298 ExprValueKind VK = VK_RValue;
4299
Douglas Gregor02024a92010-03-28 02:42:43 +00004300 // If the non-type template parameter has reference type, qualify the
4301 // resulting declaration reference with the extra qualifiers on the
4302 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004303 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4304 VK = VK_LValue;
4305 T = Context.getQualifiedType(T,
4306 TargetRef->getPointeeType().getQualifiers());
4307 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004308
John McCallf89e55a2010-11-18 06:31:45 +00004309 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004310}
4311
4312/// \brief Construct a new expression that refers to the given
4313/// integral template argument with the given source-location
4314/// information.
4315///
4316/// This routine takes care of the mapping from an integral template
4317/// argument (which may have any integral type) to the appropriate
4318/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004319ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004320Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4321 SourceLocation Loc) {
4322 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004323 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004324 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004325 if (T->isAnyCharacterType()) {
4326 CharacterLiteral::CharacterKind Kind;
4327 if (T->isWideCharType())
4328 Kind = CharacterLiteral::Wide;
4329 else if (T->isChar16Type())
4330 Kind = CharacterLiteral::UTF16;
4331 else if (T->isChar32Type())
4332 Kind = CharacterLiteral::UTF32;
4333 else
4334 Kind = CharacterLiteral::Ascii;
4335
Douglas Gregor02024a92010-03-28 02:42:43 +00004336 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004337 Arg.getAsIntegral()->getZExtValue(),
4338 Kind, T, Loc));
4339 }
4340
Douglas Gregor02024a92010-03-28 02:42:43 +00004341 if (T->isBooleanType())
4342 return Owned(new (Context) CXXBoolLiteralExpr(
4343 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004344 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004345
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004346 if (T->isNullPtrType())
4347 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4348
Chris Lattner223de242011-04-25 20:37:58 +00004349 // If this is an enum type that we're instantiating, we need to use an integer
4350 // type the same size as the enumerator. We don't want to build an
4351 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004352 QualType BT;
4353 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004354 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004355 else
4356 BT = T;
4357
John McCall4e9272d2011-07-15 07:47:58 +00004358 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4359 if (T->isEnumeralType()) {
4360 // FIXME: This is a hack. We need a better way to handle substituted
4361 // non-type template parameters.
4362 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4363 Context.getTrivialTypeSourceInfo(T, Loc),
4364 Loc, Loc);
4365 }
4366
4367 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004368}
4369
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004370/// \brief Match two template parameters within template parameter lists.
4371static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4372 bool Complain,
4373 Sema::TemplateParameterListEqualKind Kind,
4374 SourceLocation TemplateArgLoc) {
4375 // Check the actual kind (type, non-type, template).
4376 if (Old->getKind() != New->getKind()) {
4377 if (Complain) {
4378 unsigned NextDiag = diag::err_template_param_different_kind;
4379 if (TemplateArgLoc.isValid()) {
4380 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4381 NextDiag = diag::note_template_param_different_kind;
4382 }
4383 S.Diag(New->getLocation(), NextDiag)
4384 << (Kind != Sema::TPL_TemplateMatch);
4385 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4386 << (Kind != Sema::TPL_TemplateMatch);
4387 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004388
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004389 return false;
4390 }
4391
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004392 // Check that both are parameter packs are neither are parameter packs.
4393 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004394 // template template parameter, the template template parameter can have
4395 // a parameter pack where the template template argument does not.
4396 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4397 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4398 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004399 if (Complain) {
4400 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4401 if (TemplateArgLoc.isValid()) {
4402 S.Diag(TemplateArgLoc,
4403 diag::err_template_arg_template_params_mismatch);
4404 NextDiag = diag::note_template_parameter_pack_non_pack;
4405 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004406
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004407 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4408 : isa<NonTypeTemplateParmDecl>(New)? 1
4409 : 2;
4410 S.Diag(New->getLocation(), NextDiag)
4411 << ParamKind << New->isParameterPack();
4412 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4413 << ParamKind << Old->isParameterPack();
4414 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004415
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004416 return false;
4417 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004418
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004419 // For non-type template parameters, check the type of the parameter.
4420 if (NonTypeTemplateParmDecl *OldNTTP
4421 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4422 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004423
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004424 // If we are matching a template template argument to a template
4425 // template parameter and one of the non-type template parameter types
4426 // is dependent, then we must wait until template instantiation time
4427 // to actually compare the arguments.
4428 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4429 (OldNTTP->getType()->isDependentType() ||
4430 NewNTTP->getType()->isDependentType()))
4431 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004432
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004433 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4434 if (Complain) {
4435 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4436 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004437 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004438 diag::err_template_arg_template_params_mismatch);
4439 NextDiag = diag::note_template_nontype_parm_different_type;
4440 }
4441 S.Diag(NewNTTP->getLocation(), NextDiag)
4442 << NewNTTP->getType()
4443 << (Kind != Sema::TPL_TemplateMatch);
4444 S.Diag(OldNTTP->getLocation(),
4445 diag::note_template_nontype_parm_prev_declaration)
4446 << OldNTTP->getType();
4447 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004448
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004449 return false;
4450 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004451
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004452 return true;
4453 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004454
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004455 // For template template parameters, check the template parameter types.
4456 // The template parameter lists of template template
4457 // parameters must agree.
4458 if (TemplateTemplateParmDecl *OldTTP
4459 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004460 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004461 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4462 OldTTP->getTemplateParameters(),
4463 Complain,
4464 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004465 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004466 : Kind),
4467 TemplateArgLoc);
4468 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004469
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004470 return true;
4471}
Douglas Gregor02024a92010-03-28 02:42:43 +00004472
Douglas Gregora0347822011-01-13 00:08:50 +00004473/// \brief Diagnose a known arity mismatch when comparing template argument
4474/// lists.
4475static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004476void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004477 TemplateParameterList *New,
4478 TemplateParameterList *Old,
4479 Sema::TemplateParameterListEqualKind Kind,
4480 SourceLocation TemplateArgLoc) {
4481 unsigned NextDiag = diag::err_template_param_list_different_arity;
4482 if (TemplateArgLoc.isValid()) {
4483 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4484 NextDiag = diag::note_template_param_list_different_arity;
4485 }
4486 S.Diag(New->getTemplateLoc(), NextDiag)
4487 << (New->size() > Old->size())
4488 << (Kind != Sema::TPL_TemplateMatch)
4489 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4490 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4491 << (Kind != Sema::TPL_TemplateMatch)
4492 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4493}
4494
Douglas Gregorddc29e12009-02-06 22:42:48 +00004495/// \brief Determine whether the given template parameter lists are
4496/// equivalent.
4497///
Mike Stump1eb44332009-09-09 15:08:12 +00004498/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004499/// source code as part of a new template declaration.
4500///
4501/// \param Old The old template parameter list, typically found via
4502/// name lookup of the template declared with this template parameter
4503/// list.
4504///
4505/// \param Complain If true, this routine will produce a diagnostic if
4506/// the template parameter lists are not equivalent.
4507///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004508/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004509///
4510/// \param TemplateArgLoc If this source location is valid, then we
4511/// are actually checking the template parameter list of a template
4512/// argument (New) against the template parameter list of its
4513/// corresponding template template parameter (Old). We produce
4514/// slightly different diagnostics in this scenario.
4515///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004516/// \returns True if the template parameter lists are equal, false
4517/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004518bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004519Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4520 TemplateParameterList *Old,
4521 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004522 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004523 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004524 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4525 if (Complain)
4526 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4527 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004528
4529 return false;
4530 }
4531
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004532 // C++0x [temp.arg.template]p3:
4533 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004534 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004535 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004536 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004537 // template-parameter-list of P. [...]
4538 TemplateParameterList::iterator NewParm = New->begin();
4539 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004540 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004541 OldParmEnd = Old->end();
4542 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004543 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4544 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004545 if (NewParm == NewParmEnd) {
4546 if (Complain)
4547 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4548 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004549
Douglas Gregora0347822011-01-13 00:08:50 +00004550 return false;
4551 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004552
Douglas Gregora0347822011-01-13 00:08:50 +00004553 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4554 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004555 return false;
4556
Douglas Gregora0347822011-01-13 00:08:50 +00004557 ++NewParm;
4558 continue;
4559 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004560
Douglas Gregora0347822011-01-13 00:08:50 +00004561 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004562 // [...] When P's template- parameter-list contains a template parameter
4563 // pack (14.5.3), the template parameter pack will match zero or more
4564 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004565 // template-parameter-list of A with the same type and form as the
4566 // template parameter pack in P (ignoring whether those template
4567 // parameters are template parameter packs).
4568 for (; NewParm != NewParmEnd; ++NewParm) {
4569 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4570 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004571 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004572 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004573 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004574
Douglas Gregora0347822011-01-13 00:08:50 +00004575 // Make sure we exhausted all of the arguments.
4576 if (NewParm != NewParmEnd) {
4577 if (Complain)
4578 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4579 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004580
Douglas Gregora0347822011-01-13 00:08:50 +00004581 return false;
4582 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004583
Douglas Gregorddc29e12009-02-06 22:42:48 +00004584 return true;
4585}
4586
4587/// \brief Check whether a template can be declared within this scope.
4588///
4589/// If the template declaration is valid in this scope, returns
4590/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004591bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004592Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004593 if (!S)
4594 return false;
4595
Douglas Gregorddc29e12009-02-06 22:42:48 +00004596 // Find the nearest enclosing declaration scope.
4597 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4598 (S->getFlags() & Scope::TemplateParamScope) != 0)
4599 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004600
Douglas Gregorddc29e12009-02-06 22:42:48 +00004601 // C++ [temp]p2:
4602 // A template-declaration can appear only as a namespace scope or
4603 // class scope declaration.
4604 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004605 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4606 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004607 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004608 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004609
Eli Friedman1503f772009-07-31 01:43:05 +00004610 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004611 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004612
4613 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4614 return false;
4615
Mike Stump1eb44332009-09-09 15:08:12 +00004616 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004617 diag::err_template_outside_namespace_or_class_scope)
4618 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004619}
Douglas Gregorcc636682009-02-17 23:15:12 +00004620
Douglas Gregord5cb8762009-10-07 00:13:32 +00004621/// \brief Determine what kind of template specialization the given declaration
4622/// is.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00004623static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004624 if (!D)
4625 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004626
Douglas Gregorf6b11852009-10-08 15:14:33 +00004627 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4628 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004629 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4630 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004631 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4632 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004633
Douglas Gregord5cb8762009-10-07 00:13:32 +00004634 return TSK_Undeclared;
4635}
4636
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004637/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004638/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004639///
Douglas Gregor9302da62009-10-14 23:50:59 +00004640/// This routine determines whether a template specialization can be declared
4641/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004642///
4643/// \param S the semantic analysis object for which this check is being
4644/// performed.
4645///
4646/// \param Specialized the entity being specialized or instantiated, which
4647/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004648/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004649/// member class).
4650///
4651/// \param PrevDecl the previous declaration of this entity, if any.
4652///
4653/// \param Loc the location of the explicit specialization or instantiation of
4654/// this entity.
4655///
4656/// \param IsPartialSpecialization whether this is a partial specialization of
4657/// a class template.
4658///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004659/// \returns true if there was an error that we cannot recover from, false
4660/// otherwise.
4661static bool CheckTemplateSpecializationScope(Sema &S,
4662 NamedDecl *Specialized,
4663 NamedDecl *PrevDecl,
4664 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004665 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004666 // Keep these "kind" numbers in sync with the %select statements in the
4667 // various diagnostics emitted by this routine.
4668 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004669 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004670 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004671 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004672 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004673 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004674 EntityKind = 3;
4675 else if (isa<VarDecl>(Specialized))
4676 EntityKind = 4;
4677 else if (isa<RecordDecl>(Specialized))
4678 EntityKind = 5;
4679 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004680 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4681 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004682 return true;
4683 }
4684
Douglas Gregor88b70942009-02-25 22:02:03 +00004685 // C++ [temp.expl.spec]p2:
4686 // An explicit specialization shall be declared in the namespace
4687 // of which the template is a member, or, for member templates, in
4688 // the namespace of which the enclosing class or enclosing class
4689 // template is a member. An explicit specialization of a member
4690 // function, member class or static data member of a class
4691 // template shall be declared in the namespace of which the class
4692 // template is a member. Such a declaration may also be a
4693 // definition. If the declaration is not a definition, the
4694 // specialization may be defined later in the name- space in which
4695 // the explicit specialization was declared, or in a namespace
4696 // that encloses the one in which the explicit specialization was
4697 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004698 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004699 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004700 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004701 return true;
4702 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004703
Douglas Gregor0a407472009-10-07 17:30:37 +00004704 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004705 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004706 // Do not warn for class scope explicit specialization during
4707 // instantiation, warning was already emitted during pattern
4708 // semantic analysis.
4709 if (!S.ActiveTemplateInstantiations.size())
4710 S.Diag(Loc, diag::ext_function_specialization_in_class)
4711 << Specialized;
4712 } else {
4713 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4714 << Specialized;
4715 return true;
4716 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004717 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004718
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004719 if (S.CurContext->isRecord() &&
4720 !S.CurContext->Equals(Specialized->getDeclContext())) {
4721 // Make sure that we're specializing in the right record context.
4722 // Otherwise, things can go horribly wrong.
4723 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4724 << Specialized;
4725 return true;
4726 }
4727
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004728 // C++ [temp.class.spec]p6:
4729 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004730 // in any namespace scope in which its definition may be defined (14.5.1
4731 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004732 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004733 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004734 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004735 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004736 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004737 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4738 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004739 // C++ [temp.exp.spec]p2:
4740 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004741 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004742 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004743 // An explicit specialization of a member function, member class or
4744 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004745 // namespace of which the class template is a member.
4746 //
4747 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004748 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004749 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004750 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4751 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4752 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4753 assert(!IsCPlusPlus0xExtension &&
4754 "DC encloses TU but isn't in enclosing namespace set");
4755 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004756 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004757 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4758 int Diag;
4759 if (!IsCPlusPlus0xExtension)
4760 Diag = diag::err_template_spec_decl_out_of_scope;
4761 else if (!S.getLangOptions().CPlusPlus0x)
4762 Diag = diag::ext_template_spec_decl_out_of_scope;
4763 else
4764 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4765 S.Diag(Loc, Diag)
4766 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004768
Douglas Gregor9302da62009-10-14 23:50:59 +00004769 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004770 ComplainedAboutScope =
4771 !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004772 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004773 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004774
4775 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004776 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004777 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004778 // specializations of function templates, static data members, and member
4779 // functions, so we skip the check here for those kinds of entities.
4780 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004781 // Should we refactor that check, so that it occurs later?
4782 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004783 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4784 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004785 if (isa<TranslationUnitDecl>(SpecializedContext))
4786 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4787 << EntityKind << Specialized;
4788 else if (isa<NamespaceDecl>(SpecializedContext))
4789 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4790 << EntityKind << Specialized
4791 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004792
Douglas Gregor9302da62009-10-14 23:50:59 +00004793 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004794 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004795
Douglas Gregord5cb8762009-10-07 00:13:32 +00004796 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004797
Douglas Gregor88b70942009-02-25 22:02:03 +00004798 return false;
4799}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004800
Douglas Gregorbacb9492011-01-03 21:13:47 +00004801/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4802/// that checks non-type template partial specialization arguments.
4803static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4804 NonTypeTemplateParmDecl *Param,
4805 const TemplateArgument *Args,
4806 unsigned NumArgs) {
4807 for (unsigned I = 0; I != NumArgs; ++I) {
4808 if (Args[I].getKind() == TemplateArgument::Pack) {
4809 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004810 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004811 Args[I].pack_size()))
4812 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004813
Douglas Gregore94866f2009-06-12 21:21:02 +00004814 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004815 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004816
Douglas Gregorbacb9492011-01-03 21:13:47 +00004817 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004818 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004819 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004820 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004821
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004822 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004823 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4824 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004825
4826 // Strip off any implicit casts we added as part of type checking.
4827 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4828 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004829
Douglas Gregore94866f2009-06-12 21:21:02 +00004830 // C++ [temp.class.spec]p8:
4831 // A non-type argument is non-specialized if it is the name of a
4832 // non-type parameter. All other non-type arguments are
4833 // specialized.
4834 //
4835 // Below, we check the two conditions that only apply to
4836 // specialized non-type arguments, so skip any non-specialized
4837 // arguments.
4838 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004839 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004840 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004841
Douglas Gregore94866f2009-06-12 21:21:02 +00004842 // C++ [temp.class.spec]p9:
4843 // Within the argument list of a class template partial
4844 // specialization, the following restrictions apply:
4845 // -- A partially specialized non-type argument expression
4846 // shall not involve a template parameter of the partial
4847 // specialization except when the argument expression is a
4848 // simple identifier.
4849 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004850 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004851 diag::err_dependent_non_type_arg_in_partial_spec)
4852 << ArgExpr->getSourceRange();
4853 return true;
4854 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004855
Douglas Gregore94866f2009-06-12 21:21:02 +00004856 // -- The type of a template parameter corresponding to a
4857 // specialized non-type argument shall not be dependent on a
4858 // parameter of the specialization.
4859 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004860 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004861 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4862 << Param->getType()
4863 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004864 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004865 return true;
4866 }
4867 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004868
Douglas Gregorbacb9492011-01-03 21:13:47 +00004869 return false;
4870}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004871
Douglas Gregorbacb9492011-01-03 21:13:47 +00004872/// \brief Check the non-type template arguments of a class template
4873/// partial specialization according to C++ [temp.class.spec]p9.
4874///
4875/// \param TemplateParams the template parameters of the primary class
4876/// template.
4877///
4878/// \param TemplateArg the template arguments of the class template
4879/// partial specialization.
4880///
4881/// \returns true if there was an error, false otherwise.
4882static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4883 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004884 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004885 const TemplateArgument *ArgList = TemplateArgs.data();
4886
4887 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4888 NonTypeTemplateParmDecl *Param
4889 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4890 if (!Param)
4891 continue;
4892
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004893 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004894 &ArgList[I], 1))
4895 return true;
4896 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004897
4898 return false;
4899}
4900
John McCalld226f652010-08-21 09:40:31 +00004901DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004902Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4903 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004904 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004905 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004906 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004907 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004908 SourceLocation TemplateNameLoc,
4909 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004910 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004911 SourceLocation RAngleLoc,
4912 AttributeList *Attr,
4913 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004914 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004915
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004916 // NOTE: KWLoc is the location of the tag keyword. This will instead
4917 // store the location of the outermost template keyword in the declaration.
4918 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4919 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4920
Douglas Gregorcc636682009-02-17 23:15:12 +00004921 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004922 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004923 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004924 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4925
4926 if (!ClassTemplate) {
4927 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004928 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004929 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4930 return true;
4931 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004932
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004933 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004934 bool isPartialSpecialization = false;
4935
Douglas Gregor88b70942009-02-25 22:02:03 +00004936 // Check the validity of the template headers that introduce this
4937 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004938 // FIXME: We probably shouldn't complain about these headers for
4939 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004940 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004941 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004942 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4943 TemplateNameLoc,
4944 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004945 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004946 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004947 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004948 isExplicitSpecialization,
4949 Invalid);
4950 if (Invalid)
4951 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004952
Douglas Gregor05396e22009-08-25 17:23:04 +00004953 if (TemplateParams && TemplateParams->size() > 0) {
4954 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004955
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004956 if (TUK == TUK_Friend) {
4957 Diag(KWLoc, diag::err_partial_specialization_friend)
4958 << SourceRange(LAngleLoc, RAngleLoc);
4959 return true;
4960 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004961
Douglas Gregor05396e22009-08-25 17:23:04 +00004962 // C++ [temp.class.spec]p10:
4963 // The template parameter list of a specialization shall not
4964 // contain default template argument values.
4965 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4966 Decl *Param = TemplateParams->getParam(I);
4967 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4968 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004969 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004970 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004971 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004972 }
4973 } else if (NonTypeTemplateParmDecl *NTTP
4974 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4975 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004976 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004977 diag::err_default_arg_in_partial_spec)
4978 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004979 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004980 }
4981 } else {
4982 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004983 if (TTP->hasDefaultArgument()) {
4984 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004985 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004986 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004987 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004988 }
4989 }
4990 }
Douglas Gregora735b202009-10-13 14:39:41 +00004991 } else if (TemplateParams) {
4992 if (TUK == TUK_Friend)
4993 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004994 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004995 SourceRange(TemplateParams->getTemplateLoc(),
4996 TemplateParams->getRAngleLoc()))
4997 << SourceRange(LAngleLoc, RAngleLoc);
4998 else
4999 isExplicitSpecialization = true;
5000 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00005001 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00005002 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005003 isExplicitSpecialization = true;
5004 }
Douglas Gregor88b70942009-02-25 22:02:03 +00005005
Douglas Gregorcc636682009-02-17 23:15:12 +00005006 // Check that the specialization uses the same tag kind as the
5007 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005008 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5009 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005010 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005011 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005012 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005013 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00005014 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005015 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00005016 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005017 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005018 diag::note_previous_use);
5019 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5020 }
5021
Douglas Gregor40808ce2009-03-09 23:48:35 +00005022 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005023 TemplateArgumentListInfo TemplateArgs;
5024 TemplateArgs.setLAngleLoc(LAngleLoc);
5025 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005026 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00005027
Douglas Gregor925910d2011-01-03 20:35:03 +00005028 // Check for unexpanded parameter packs in any of the template arguments.
5029 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005030 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00005031 UPPC_PartialSpecialization))
5032 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005033
Douglas Gregorcc636682009-02-17 23:15:12 +00005034 // Check that the template argument list is well-formed for this
5035 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005036 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005037 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5038 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005039 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005040
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005041 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00005042 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00005043 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00005044 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00005045 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00005046 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00005047 return true;
5048
Douglas Gregor561f8122011-07-01 01:22:09 +00005049 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005050 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00005051 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005052 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00005053 TemplateArgs.size(),
5054 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00005055 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
5056 << ClassTemplate->getDeclName();
5057 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00005058 }
5059 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005060
Douglas Gregorcc636682009-02-17 23:15:12 +00005061 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005062 ClassTemplateSpecializationDecl *PrevDecl = 0;
5063
5064 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005065 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005066 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005067 = ClassTemplate->findPartialSpecialization(Converted.data(),
5068 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005069 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005070 else
5071 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005072 = ClassTemplate->findSpecialization(Converted.data(),
5073 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00005074
5075 ClassTemplateSpecializationDecl *Specialization = 0;
5076
Douglas Gregor88b70942009-02-25 22:02:03 +00005077 // Check whether we can declare a class template specialization in
5078 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005079 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005080 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
5081 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00005082 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005083 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005084
Douglas Gregorb88e8882009-07-30 17:40:51 +00005085 // The canonical type
5086 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005087 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005088 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00005089 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005090 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005091 // arguments was referenced but not declared, or we're only
5092 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005093 // declaration node as our own, updating its source location and
5094 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005095 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005096 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005097 if (TemplateParameterLists.size() > 0) {
5098 Specialization->setTemplateParameterListsInfo(Context,
5099 TemplateParameterLists.size(),
5100 (TemplateParameterList**) TemplateParameterLists.release());
5101 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005102 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00005103 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005104 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00005105 // Build the canonical type that describes the converted template
5106 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00005107 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
5108 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005109 Converted.data(),
5110 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005111
5112 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005113 ClassTemplate->getInjectedClassNameSpecialization())) {
5114 // C++ [temp.class.spec]p9b3:
5115 //
5116 // -- The argument list of the specialization shall not be identical
5117 // to the implicit argument list of the primary template.
5118 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00005119 << (TUK == TUK_Definition)
5120 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00005121 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5122 ClassTemplate->getIdentifier(),
5123 TemplateNameLoc,
5124 Attr,
5125 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005126 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005127 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005128 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005129 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005130
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005131 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005132 ClassTemplatePartialSpecializationDecl *PrevPartial
5133 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005134 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005135 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005136 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005137 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005138 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005139 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005140 TemplateParams,
5141 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005142 Converted.data(),
5143 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005144 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005145 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005146 PrevPartial,
5147 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005148 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005149 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005150 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005151 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005152 (TemplateParameterList**) TemplateParameterLists.release());
5153 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005154
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005155 if (!PrevPartial)
5156 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005157 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005158
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005159 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005160 // template specialization, make a note of that.
5161 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5162 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005163
Douglas Gregor031a5882009-06-13 00:26:55 +00005164 // Check that all of the template parameters of the class template
5165 // partial specialization are deducible from the template
5166 // arguments. If not, this class template partial specialization
5167 // will never be used.
Benjamin Kramer013b3662012-01-30 16:17:39 +00005168 llvm::SmallBitVector DeducibleParams(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005169 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005170 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005171 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005172
Benjamin Kramer013b3662012-01-30 16:17:39 +00005173 if (!DeducibleParams.all()) {
5174 unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count();
Douglas Gregor031a5882009-06-13 00:26:55 +00005175 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5176 << (NumNonDeducible > 1)
5177 << SourceRange(TemplateNameLoc, RAngleLoc);
5178 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5179 if (!DeducibleParams[I]) {
5180 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5181 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005182 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005183 diag::note_partial_spec_unused_parameter)
5184 << Param->getDeclName();
5185 else
Mike Stump1eb44332009-09-09 15:08:12 +00005186 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005187 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005188 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005189 }
5190 }
5191 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005192 } else {
5193 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005194 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005195 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005196 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005197 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005198 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005199 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005200 Converted.data(),
5201 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005202 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005203 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005204 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005205 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005206 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005207 (TemplateParameterList**) TemplateParameterLists.release());
5208 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005209
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005210 if (!PrevDecl)
5211 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005212
5213 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005214 }
5215
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005216 // C++ [temp.expl.spec]p6:
5217 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005218 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005219 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005220 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005221 // use occurs; no diagnostic is required.
5222 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005223 bool Okay = false;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005224 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005225 // Is there any previous explicit specialization declaration?
5226 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5227 Okay = true;
5228 break;
5229 }
5230 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005231
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005232 if (!Okay) {
5233 SourceRange Range(TemplateNameLoc, RAngleLoc);
5234 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5235 << Context.getTypeDeclType(Specialization) << Range;
5236
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005237 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005238 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005239 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005240 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005241 return true;
5242 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005243 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005244
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005245 // If this is not a friend, note that this is an explicit specialization.
5246 if (TUK != TUK_Friend)
5247 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005248
5249 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005250 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005251 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005252 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005253 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005254 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005255 Diag(Def->getLocation(), diag::note_previous_definition);
5256 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005257 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005258 }
5259 }
5260
John McCall7f1b9872010-12-18 03:30:47 +00005261 if (Attr)
5262 ProcessDeclAttributeList(S, Specialization, Attr);
5263
Douglas Gregord023aec2011-09-09 20:53:38 +00005264 if (ModulePrivateLoc.isValid())
5265 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5266 << (isPartialSpecialization? 1 : 0)
5267 << FixItHint::CreateRemoval(ModulePrivateLoc);
5268
Douglas Gregorfc705b82009-02-26 22:19:44 +00005269 // Build the fully-sugared type for this class template
5270 // specialization as the user wrote in the specialization
5271 // itself. This means that we'll pretty-print the type retrieved
5272 // from the specialization's declaration the way that the user
5273 // actually wrote the specialization, rather than formatting the
5274 // name based on the "canonical" representation used to store the
5275 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005276 TypeSourceInfo *WrittenTy
5277 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5278 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005279 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005280 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005281 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005282 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005283 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005284
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005285 // C++ [temp.expl.spec]p9:
5286 // A template explicit specialization is in the scope of the
5287 // namespace in which the template was defined.
5288 //
5289 // We actually implement this paragraph where we set the semantic
5290 // context (in the creation of the ClassTemplateSpecializationDecl),
5291 // but we also maintain the lexical context where the actual
5292 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005293 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005294
Douglas Gregorcc636682009-02-17 23:15:12 +00005295 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005296 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005297 Specialization->startDefinition();
5298
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005299 if (TUK == TUK_Friend) {
5300 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5301 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005302 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005303 /*FIXME:*/KWLoc);
5304 Friend->setAccess(AS_public);
5305 CurContext->addDecl(Friend);
5306 } else {
5307 // Add the specialization into its lexical context, so that it can
5308 // be seen when iterating through the list of declarations in that
5309 // context. However, specializations are not found by name lookup.
5310 CurContext->addDecl(Specialization);
5311 }
John McCalld226f652010-08-21 09:40:31 +00005312 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005313}
Douglas Gregord57959a2009-03-27 23:10:48 +00005314
John McCalld226f652010-08-21 09:40:31 +00005315Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005316 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005317 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005318 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005319}
5320
John McCalld226f652010-08-21 09:40:31 +00005321Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005322 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005323 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005324 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005325 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005326
Douglas Gregor52591bf2009-06-24 00:54:41 +00005327 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005328 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005329 }
Mike Stump1eb44332009-09-09 15:08:12 +00005330
Douglas Gregor52591bf2009-06-24 00:54:41 +00005331 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005332
Douglas Gregor45fa5602011-11-07 20:56:01 +00005333 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005334 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005335 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005336 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005337 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005338 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005339 FunctionTemplate->getTemplatedDecl());
5340 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5341 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5342 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005343}
5344
John McCall75042392010-02-11 01:33:53 +00005345/// \brief Strips various properties off an implicit instantiation
5346/// that has just been explicitly specialized.
5347static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005348 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005349
5350 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5351 FD->setInlineSpecified(false);
5352 }
5353}
5354
Nico Weberd1d512a2012-01-09 19:52:25 +00005355/// \brief Compute the diagnostic location for an explicit instantiation
5356// declaration or definition.
5357static SourceLocation DiagLocForExplicitInstantiation(
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005358 NamedDecl* D, SourceLocation PointOfInstantiation) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005359 // Explicit instantiations following a specialization have no effect and
5360 // hence no PointOfInstantiation. In that case, walk decl backwards
5361 // until a valid name loc is found.
5362 SourceLocation PrevDiagLoc = PointOfInstantiation;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005363 for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
5364 Prev = Prev->getPreviousDecl()) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005365 PrevDiagLoc = Prev->getLocation();
5366 }
5367 assert(PrevDiagLoc.isValid() &&
5368 "Explicit instantiation without point of instantiation?");
5369 return PrevDiagLoc;
5370}
5371
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005372/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005373/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005374/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005375/// new specialization/instantiation will have any effect.
5376///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005377/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005378/// instantiation.
5379///
5380/// \param NewTSK the kind of the new explicit specialization or instantiation.
5381///
5382/// \param PrevDecl the previous declaration of the entity.
5383///
5384/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5385///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005386/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005387/// declaration was instantiated (either implicitly or explicitly).
5388///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005389/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005390/// specialization or instantiation has no effect and should be ignored.
5391///
5392/// \returns true if there was an error that should prevent the introduction of
5393/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005394bool
5395Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5396 TemplateSpecializationKind NewTSK,
5397 NamedDecl *PrevDecl,
5398 TemplateSpecializationKind PrevTSK,
5399 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005400 bool &HasNoEffect) {
5401 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005402
Douglas Gregor454885e2009-10-15 15:54:05 +00005403 switch (NewTSK) {
5404 case TSK_Undeclared:
5405 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005406 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005407
Douglas Gregor454885e2009-10-15 15:54:05 +00005408 case TSK_ExplicitSpecialization:
5409 switch (PrevTSK) {
5410 case TSK_Undeclared:
5411 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005412 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005413 // explicitly specialized or has merely been mentioned without any
5414 // instantiation.
5415 return false;
5416
5417 case TSK_ImplicitInstantiation:
5418 if (PrevPointOfInstantiation.isInvalid()) {
5419 // The declaration itself has not actually been instantiated, so it is
5420 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005421 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005422 return false;
5423 }
5424 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005425
Douglas Gregor454885e2009-10-15 15:54:05 +00005426 case TSK_ExplicitInstantiationDeclaration:
5427 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005428 assert((PrevTSK == TSK_ImplicitInstantiation ||
5429 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005430 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005431
Douglas Gregor454885e2009-10-15 15:54:05 +00005432 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005433 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005434 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005435 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005436 // implicit instantiation to take place, in every translation unit in
5437 // which such a use occurs; no diagnostic is required.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005438 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005439 // Is there any previous explicit specialization declaration?
5440 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5441 return false;
5442 }
5443
Douglas Gregor0d035142009-10-27 18:42:08 +00005444 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005445 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005446 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005447 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005448
Douglas Gregor454885e2009-10-15 15:54:05 +00005449 return true;
5450 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005451
Douglas Gregor454885e2009-10-15 15:54:05 +00005452 case TSK_ExplicitInstantiationDeclaration:
5453 switch (PrevTSK) {
5454 case TSK_ExplicitInstantiationDeclaration:
5455 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005456 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005457 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005458
Douglas Gregor454885e2009-10-15 15:54:05 +00005459 case TSK_Undeclared:
5460 case TSK_ImplicitInstantiation:
5461 // We're explicitly instantiating something that may have already been
5462 // implicitly instantiated; that's fine.
5463 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005464
Douglas Gregor454885e2009-10-15 15:54:05 +00005465 case TSK_ExplicitSpecialization:
5466 // C++0x [temp.explicit]p4:
5467 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005468 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005469 // specialization for that template, the explicit instantiation has no
5470 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005471 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005472 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005473
Douglas Gregor454885e2009-10-15 15:54:05 +00005474 case TSK_ExplicitInstantiationDefinition:
5475 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005476 // If an entity is the subject of both an explicit instantiation
5477 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005478 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005479 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005480 diag::err_explicit_instantiation_declaration_after_definition);
Nico Weberff91d242011-12-23 20:58:04 +00005481
5482 // Explicit instantiations following a specialization have no effect and
5483 // hence no PrevPointOfInstantiation. In that case, walk decl backwards
5484 // until a valid name loc is found.
Nico Weberd1d512a2012-01-09 19:52:25 +00005485 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
5486 diag::note_explicit_instantiation_definition_here);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005487 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005488 return false;
5489 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005490
Douglas Gregor454885e2009-10-15 15:54:05 +00005491 case TSK_ExplicitInstantiationDefinition:
5492 switch (PrevTSK) {
5493 case TSK_Undeclared:
5494 case TSK_ImplicitInstantiation:
5495 // We're explicitly instantiating something that may have already been
5496 // implicitly instantiated; that's fine.
5497 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005498
Douglas Gregor454885e2009-10-15 15:54:05 +00005499 case TSK_ExplicitSpecialization:
5500 // C++ DR 259, C++0x [temp.explicit]p4:
5501 // For a given set of template parameters, if an explicit
5502 // instantiation of a template appears after a declaration of
5503 // an explicit specialization for that template, the explicit
5504 // instantiation has no effect.
5505 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005506 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005507 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005508 // has been explicitly specialized.
Richard Smithebaf0e62011-10-18 20:49:44 +00005509 Diag(NewLoc, getLangOptions().CPlusPlus0x ?
5510 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5511 diag::ext_explicit_instantiation_after_specialization)
5512 << PrevDecl;
5513 Diag(PrevDecl->getLocation(),
5514 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005515 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005516 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005517
Douglas Gregor454885e2009-10-15 15:54:05 +00005518 case TSK_ExplicitInstantiationDeclaration:
5519 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005520 // were previously asked to suppress instantiations. That's fine.
Nico Weberff91d242011-12-23 20:58:04 +00005521
5522 // C++0x [temp.explicit]p4:
5523 // For a given set of template parameters, if an explicit instantiation
5524 // of a template appears after a declaration of an explicit
5525 // specialization for that template, the explicit instantiation has no
5526 // effect.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005527 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Nico Weberff91d242011-12-23 20:58:04 +00005528 // Is there any previous explicit specialization declaration?
5529 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5530 HasNoEffect = true;
5531 break;
5532 }
5533 }
5534
Douglas Gregor454885e2009-10-15 15:54:05 +00005535 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005536
Douglas Gregor454885e2009-10-15 15:54:05 +00005537 case TSK_ExplicitInstantiationDefinition:
5538 // C++0x [temp.spec]p5:
5539 // For a given template and a given set of template-arguments,
5540 // - an explicit instantiation definition shall appear at most once
5541 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005542 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005543 << PrevDecl;
Nico Weberd1d512a2012-01-09 19:52:25 +00005544 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
Douglas Gregor0d035142009-10-27 18:42:08 +00005545 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005546 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005547 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005548 }
Douglas Gregor454885e2009-10-15 15:54:05 +00005549 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005550
David Blaikieb219cfc2011-09-23 05:06:16 +00005551 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005552}
5553
John McCallaf2094e2010-04-08 09:05:18 +00005554/// \brief Perform semantic analysis for the given dependent function
5555/// template specialization. The only possible way to get a dependent
5556/// function template specialization is with a friend declaration,
5557/// like so:
5558///
5559/// template <class T> void foo(T);
5560/// template <class T> class A {
5561/// friend void foo<>(T);
5562/// };
5563///
5564/// There really isn't any useful analysis we can do here, so we
5565/// just store the information.
5566bool
5567Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5568 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5569 LookupResult &Previous) {
5570 // Remove anything from Previous that isn't a function template in
5571 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005572 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005573 LookupResult::Filter F = Previous.makeFilter();
5574 while (F.hasNext()) {
5575 NamedDecl *D = F.next()->getUnderlyingDecl();
5576 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005577 !FDLookupContext->InEnclosingNamespaceSetOf(
5578 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005579 F.erase();
5580 }
5581 F.done();
5582
5583 // Should this be diagnosed here?
5584 if (Previous.empty()) return true;
5585
5586 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5587 ExplicitTemplateArgs);
5588 return false;
5589}
5590
Abramo Bagnarae03db982010-05-20 15:32:11 +00005591/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005592/// specialization.
5593///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005594/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005595/// explicit function template specialization. On successful completion,
5596/// the function declaration \p FD will become a function template
5597/// specialization.
5598///
5599/// \param FD the function declaration, which will be updated to become a
5600/// function template specialization.
5601///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005602/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5603/// if any. Note that this may be valid info even when 0 arguments are
5604/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5605/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005606///
Francois Pichet59e7c562011-07-08 06:21:47 +00005607/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005608/// this function specialization.
5609bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005610Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005611 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005612 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005613 // The set of function template specializations that could match this
5614 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005615 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005616
Sebastian Redl7a126a42010-08-31 00:36:30 +00005617 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005618 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5619 I != E; ++I) {
5620 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5621 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005622 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005623 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005624 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5625 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005626 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005627
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005628 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005629 // A trailing template-argument can be left unspecified in the
5630 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005631 // provided it can be deduced from the function argument type.
5632 // Perform template argument deduction to determine whether we may be
5633 // specializing this template.
5634 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005635 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005636 FunctionDecl *Specialization = 0;
5637 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005638 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005639 FD->getType(),
5640 Specialization,
5641 Info)) {
5642 // FIXME: Template argument deduction failed; record why it failed, so
5643 // that we can provide nifty diagnostics.
5644 (void)TDK;
5645 continue;
5646 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005647
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005648 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005649 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005650 }
5651 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005652
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005653 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005654 UnresolvedSetIterator Result
5655 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005656 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005657 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005658 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005659 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005660 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005661 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005662 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005663 return true;
John McCallc373d482010-01-27 01:50:18 +00005664
5665 // Ignore access information; it doesn't figure into redeclaration checking.
5666 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005667
5668 FunctionTemplateSpecializationInfo *SpecInfo
5669 = Specialization->getTemplateSpecializationInfo();
5670 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005671
5672 // Note: do not overwrite location info if previous template
5673 // specialization kind was explicit.
5674 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5675 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5676 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005677
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005678 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005679 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005680
5681 // If this is a friend declaration, then we're not really declaring
5682 // an explicit specialization.
5683 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005684
Douglas Gregord5cb8762009-10-07 00:13:32 +00005685 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005686 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005687 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005688 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005689 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005690 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005691 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005692
5693 // C++ [temp.expl.spec]p6:
5694 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005695 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005696 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005697 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005698 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005699 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005700 if (!isFriend &&
5701 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005702 TSK_ExplicitSpecialization,
5703 Specialization,
5704 SpecInfo->getTemplateSpecializationKind(),
5705 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005706 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005707 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005708
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005709 // Mark the prior declaration as an explicit specialization, so that later
5710 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005711 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005712 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005713 MarkUnusedFileScopedDecl(Specialization);
5714 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005715
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005716 // Turn the given function declaration into a function template
5717 // specialization, with the template arguments from the previous
5718 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005719 // Take copies of (semantic and syntactic) template argument lists.
5720 const TemplateArgumentList* TemplArgs = new (Context)
5721 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005722 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005723 TemplArgs, /*InsertPos=*/0,
5724 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005725 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005726 FD->setStorageClass(Specialization->getStorageClass());
5727
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005728 // The "previous declaration" for this function template specialization is
5729 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005730 Previous.clear();
5731 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005732 return false;
5733}
5734
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005735/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005736/// specialization.
5737///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005738/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005739/// explicit member function specialization. On successful completion,
5740/// the function declaration \p FD will become a member function
5741/// specialization.
5742///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005743/// \param Member the member declaration, which will be updated to become a
5744/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005745///
John McCall68263142009-11-18 22:49:29 +00005746/// \param Previous the set of declarations, one of which may be specialized
5747/// by this function specialization; the set will be modified to contain the
5748/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005749bool
John McCall68263142009-11-18 22:49:29 +00005750Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005751 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005752
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005753 // Try to find the member we are instantiating.
5754 NamedDecl *Instantiation = 0;
5755 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005756 MemberSpecializationInfo *MSInfo = 0;
5757
John McCall68263142009-11-18 22:49:29 +00005758 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005759 // Nowhere to look anyway.
5760 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005761 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5762 I != E; ++I) {
5763 NamedDecl *D = (*I)->getUnderlyingDecl();
5764 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005765 if (Context.hasSameType(Function->getType(), Method->getType())) {
5766 Instantiation = Method;
5767 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005768 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005769 break;
5770 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005771 }
5772 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005773 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005774 VarDecl *PrevVar;
5775 if (Previous.isSingleResult() &&
5776 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005777 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005778 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005779 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005780 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005781 }
5782 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005783 CXXRecordDecl *PrevRecord;
5784 if (Previous.isSingleResult() &&
5785 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5786 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005787 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005788 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005789 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005790 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005791
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005792 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005793 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005794 // specializations are always out-of-line, the caller will complain about
5795 // this mismatch later.
5796 return false;
5797 }
John McCall77e8b112010-04-13 20:37:33 +00005798
5799 // If this is a friend, just bail out here before we start turning
5800 // things into explicit specializations.
5801 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5802 // Preserve instantiation information.
5803 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5804 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5805 cast<CXXMethodDecl>(InstantiatedFrom),
5806 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5807 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5808 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5809 cast<CXXRecordDecl>(InstantiatedFrom),
5810 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5811 }
5812
5813 Previous.clear();
5814 Previous.addDecl(Instantiation);
5815 return false;
5816 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005817
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005818 // Make sure that this is a specialization of a member.
5819 if (!InstantiatedFrom) {
5820 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5821 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005822 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5823 return true;
5824 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005825
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005826 // C++ [temp.expl.spec]p6:
5827 // If a template, a member template or the member of a class template is
Nico Weberff91d242011-12-23 20:58:04 +00005828 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005829 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005830 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005831 // use occurs; no diagnostic is required.
5832 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005833
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005834 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005835 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5836 TSK_ExplicitSpecialization,
5837 Instantiation,
5838 MSInfo->getTemplateSpecializationKind(),
5839 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005840 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005841 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005842
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005843 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005844 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005845 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005846 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005847 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005848 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005849
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005850 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005851 // the original declaration to note that it is an explicit specialization
5852 // (if it was previously an implicit instantiation). This latter step
5853 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005854 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005855 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5856 if (InstantiationFunction->getTemplateSpecializationKind() ==
5857 TSK_ImplicitInstantiation) {
5858 InstantiationFunction->setTemplateSpecializationKind(
5859 TSK_ExplicitSpecialization);
5860 InstantiationFunction->setLocation(Member->getLocation());
5861 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005862
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005863 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5864 cast<CXXMethodDecl>(InstantiatedFrom),
5865 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005866 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005867 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005868 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5869 if (InstantiationVar->getTemplateSpecializationKind() ==
5870 TSK_ImplicitInstantiation) {
5871 InstantiationVar->setTemplateSpecializationKind(
5872 TSK_ExplicitSpecialization);
5873 InstantiationVar->setLocation(Member->getLocation());
5874 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005875
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005876 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5877 cast<VarDecl>(InstantiatedFrom),
5878 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005879 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005880 } else {
5881 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005882 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5883 if (InstantiationClass->getTemplateSpecializationKind() ==
5884 TSK_ImplicitInstantiation) {
5885 InstantiationClass->setTemplateSpecializationKind(
5886 TSK_ExplicitSpecialization);
5887 InstantiationClass->setLocation(Member->getLocation());
5888 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005889
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005890 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005891 cast<CXXRecordDecl>(InstantiatedFrom),
5892 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005893 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005894
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005895 // Save the caller the trouble of having to figure out which declaration
5896 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005897 Previous.clear();
5898 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005899 return false;
5900}
5901
Douglas Gregor558c0322009-10-14 23:41:34 +00005902/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005903///
5904/// \returns true if a serious error occurs, false otherwise.
5905static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005906 SourceLocation InstLoc,
5907 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005908 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5909 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005910
Douglas Gregor669eed82010-07-13 00:10:04 +00005911 if (CurContext->isRecord()) {
5912 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5913 << D;
5914 return true;
5915 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005916
Richard Smith3e2e91e2011-10-18 02:28:33 +00005917 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005918 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005919 // template. If the name declared in the explicit instantiation is an
5920 // unqualified name, the explicit instantiation shall appear in the
5921 // namespace where its template is declared or, if that namespace is inline
5922 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005923 //
5924 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005925 if (WasQualifiedName) {
5926 if (CurContext->Encloses(OrigContext))
5927 return false;
5928 } else {
5929 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5930 return false;
5931 }
5932
5933 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5934 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005935 S.Diag(InstLoc,
5936 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005937 diag::err_explicit_instantiation_out_of_scope :
5938 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005939 << D << NS;
5940 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005941 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005942 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005943 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5944 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5945 << D << NS;
5946 } else
5947 S.Diag(InstLoc,
5948 S.getLangOptions().CPlusPlus0x?
5949 diag::err_explicit_instantiation_must_be_global :
5950 diag::warn_explicit_instantiation_must_be_global_0x)
5951 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005952 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005953 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005954}
5955
5956/// \brief Determine whether the given scope specifier has a template-id in it.
5957static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5958 if (!SS.isSet())
5959 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005960
Richard Smith3e2e91e2011-10-18 02:28:33 +00005961 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005962 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005963 // or a static data member of a class template specialization, the name of
5964 // the class template specialization in the qualified-id for the member
5965 // name shall be a simple-template-id.
5966 //
5967 // C++98 has the same restriction, just worded differently.
5968 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5969 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005970 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005971 if (isa<TemplateSpecializationType>(T))
5972 return true;
5973
5974 return false;
5975}
5976
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005977// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005978DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005979Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005980 SourceLocation ExternLoc,
5981 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005982 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005983 SourceLocation KWLoc,
5984 const CXXScopeSpec &SS,
5985 TemplateTy TemplateD,
5986 SourceLocation TemplateNameLoc,
5987 SourceLocation LAngleLoc,
5988 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005989 SourceLocation RAngleLoc,
5990 AttributeList *Attr) {
5991 // Find the class template we're specializing
5992 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005993 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005994 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5995
5996 // Check that the specialization uses the same tag kind as the
5997 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005998 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5999 assert(Kind != TTK_Enum &&
6000 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006001 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00006002 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006003 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00006004 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006005 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00006006 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006007 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00006008 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006009 diag::note_previous_use);
6010 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
6011 }
6012
Douglas Gregor558c0322009-10-14 23:41:34 +00006013 // C++0x [temp.explicit]p2:
6014 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006015 // definition and an explicit instantiation declaration. An explicit
6016 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00006017 TemplateSpecializationKind TSK
6018 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6019 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006020
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006021 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00006022 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00006023 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006024
6025 // Check that the template argument list is well-formed for this
6026 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00006027 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00006028 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
6029 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006030 return true;
6031
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006032 // Find the class template specialization declaration that
6033 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006034 void *InsertPos = 0;
6035 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00006036 = ClassTemplate->findSpecialization(Converted.data(),
6037 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006038
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006039 TemplateSpecializationKind PrevDecl_TSK
6040 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
6041
Douglas Gregord5cb8762009-10-07 00:13:32 +00006042 // C++0x [temp.explicit]p2:
6043 // [...] An explicit instantiation shall appear in an enclosing
6044 // namespace of its template. [...]
6045 //
6046 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00006047 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
6048 SS.isSet()))
6049 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006050
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006051 ClassTemplateSpecializationDecl *Specialization = 0;
6052
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006053 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006054 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00006055 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006056 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006057 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006058 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00006059 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006060
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006061 // Even though HasNoEffect == true means that this explicit instantiation
6062 // has no effect on semantics, we go on to put its syntax in the AST.
6063
6064 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
6065 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00006066 // Since the only prior class template specialization with these
6067 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006068 // declaration node as our own, updating the source location
6069 // for the template name to reflect our new declaration.
6070 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00006071 Specialization = PrevDecl;
6072 Specialization->setLocation(TemplateNameLoc);
6073 PrevDecl = 0;
6074 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006075 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006076
Douglas Gregor52604ab2009-09-11 21:19:12 +00006077 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006078 // Create a new class template specialization declaration node for
6079 // this explicit specialization.
6080 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00006081 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006082 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00006083 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006084 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00006085 Converted.data(),
6086 Converted.size(),
6087 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00006088 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006089
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006090 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006091 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006092 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006093 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006094 }
6095
6096 // Build the fully-sugared type for this explicit instantiation as
6097 // the user wrote in the explicit instantiation itself. This means
6098 // that we'll pretty-print the type retrieved from the
6099 // specialization's declaration the way that the user actually wrote
6100 // the explicit instantiation, rather than formatting the name based
6101 // on the "canonical" representation used to store the template
6102 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00006103 TypeSourceInfo *WrittenTy
6104 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
6105 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006106 Context.getTypeDeclType(Specialization));
6107 Specialization->setTypeAsWritten(WrittenTy);
6108 TemplateArgsIn.release();
6109
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006110 // Set source locations for keywords.
6111 Specialization->setExternLoc(ExternLoc);
6112 Specialization->setTemplateKeywordLoc(TemplateLoc);
6113
Rafael Espindola0257b7f2012-01-03 06:04:21 +00006114 if (Attr)
6115 ProcessDeclAttributeList(S, Specialization, Attr);
6116
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006117 // Add the explicit instantiation into its lexical context. However,
6118 // since explicit instantiations are never found by name lookup, we
6119 // just put it into the declaration context directly.
6120 Specialization->setLexicalDeclContext(CurContext);
6121 CurContext->addDecl(Specialization);
6122
6123 // Syntax is now OK, so return if it has no other effect on semantics.
6124 if (HasNoEffect) {
6125 // Set the template specialization kind.
6126 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006127 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00006128 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006129
6130 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006131 // A definition of a class template or class member template
6132 // shall be in scope at the point of the explicit instantiation of
6133 // the class template or class member template.
6134 //
6135 // This check comes when we actually try to perform the
6136 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006137 ClassTemplateSpecializationDecl *Def
6138 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006139 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006140 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006141 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006142 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006143 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006144 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6145 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006146
Douglas Gregor0d035142009-10-27 18:42:08 +00006147 // Instantiate the members of this class template specialization.
6148 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006149 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006150 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006151 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6152
6153 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6154 // TSK_ExplicitInstantiationDefinition
6155 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6156 TSK == TSK_ExplicitInstantiationDefinition)
6157 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006158
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006159 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006160 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006161
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006162 // Set the template specialization kind.
6163 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006164 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006165}
6166
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006167// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006168DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006169Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006170 SourceLocation ExternLoc,
6171 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006172 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006173 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006174 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006175 IdentifierInfo *Name,
6176 SourceLocation NameLoc,
6177 AttributeList *Attr) {
6178
Douglas Gregor402abb52009-05-28 23:31:59 +00006179 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006180 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006181 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006182 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006183 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006184 MultiTemplateParamsArg(*this, 0, 0),
Richard Smithbdad7a22012-01-10 01:33:14 +00006185 Owned, IsDependent, SourceLocation(), false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006186 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006187 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6188
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006189 if (!TagD)
6190 return true;
6191
John McCalld226f652010-08-21 09:40:31 +00006192 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006193 if (Tag->isEnum()) {
6194 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6195 << Context.getTypeDeclType(Tag);
6196 return true;
6197 }
6198
Douglas Gregord0c87372009-05-27 17:30:49 +00006199 if (Tag->isInvalidDecl())
6200 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006201
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006202 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6203 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6204 if (!Pattern) {
6205 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6206 << Context.getTypeDeclType(Record);
6207 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6208 return true;
6209 }
6210
Douglas Gregor558c0322009-10-14 23:41:34 +00006211 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006212 // If the explicit instantiation is for a class or member class, the
6213 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006214 // simple-template-id.
6215 //
6216 // C++98 has the same restriction, just worded differently.
6217 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006218 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006219 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006220
Douglas Gregor558c0322009-10-14 23:41:34 +00006221 // C++0x [temp.explicit]p2:
6222 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006223 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006224 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006225 TemplateSpecializationKind TSK
6226 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6227 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006228
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006229 // C++0x [temp.explicit]p2:
6230 // [...] An explicit instantiation shall appear in an enclosing
6231 // namespace of its template. [...]
6232 //
6233 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006234 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006235
Douglas Gregor454885e2009-10-15 15:54:05 +00006236 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006237 CXXRecordDecl *PrevDecl
Douglas Gregoref96ee02012-01-14 16:38:05 +00006238 = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
Douglas Gregor952b0172010-02-11 01:04:33 +00006239 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006240 PrevDecl = Record;
6241 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006242 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006243 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006244 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006245 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006246 PrevDecl,
6247 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006248 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006249 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006250 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006251 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006252 return TagD;
6253 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006254
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006255 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006256 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006257 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006258 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006259 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006260 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006261 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006262 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006263 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006264 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6265 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006266 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6267 << Pattern;
6268 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006269 } else {
6270 if (InstantiateClass(NameLoc, Record, Def,
6271 getTemplateInstantiationArgs(Record),
6272 TSK))
6273 return true;
6274
Douglas Gregor952b0172010-02-11 01:04:33 +00006275 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006276 if (!RecordDef)
6277 return true;
6278 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006279 }
6280
Douglas Gregor0d035142009-10-27 18:42:08 +00006281 // Instantiate all of the members of the class.
6282 InstantiateClassMembers(NameLoc, RecordDef,
6283 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006284
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006285 if (TSK == TSK_ExplicitInstantiationDefinition)
6286 MarkVTableUsed(NameLoc, RecordDef, true);
6287
Mike Stump390b4cc2009-05-16 07:39:55 +00006288 // FIXME: We don't have any representation for explicit instantiations of
6289 // member classes. Such a representation is not needed for compilation, but it
6290 // should be available for clients that want to see all of the declarations in
6291 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006292 return TagD;
6293}
6294
John McCallf312b1e2010-08-26 23:41:50 +00006295DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6296 SourceLocation ExternLoc,
6297 SourceLocation TemplateLoc,
6298 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006299 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006300 // TODO: check if/when DNInfo should replace Name.
6301 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6302 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006303 if (!Name) {
6304 if (!D.isInvalidType())
6305 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6306 diag::err_explicit_instantiation_requires_name)
6307 << D.getDeclSpec().getSourceRange()
6308 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006309
Douglas Gregord5a423b2009-09-25 18:43:00 +00006310 return true;
6311 }
6312
6313 // The scope passed in may not be a decl scope. Zip up the scope tree until
6314 // we find one that is.
6315 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6316 (S->getFlags() & Scope::TemplateParamScope) != 0)
6317 S = S->getParent();
6318
6319 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006320 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6321 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006322 if (R.isNull())
6323 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006324
Douglas Gregore885e182011-05-21 18:53:30 +00006325 // C++ [dcl.stc]p1:
6326 // A storage-class-specifier shall not be specified in [...] an explicit
6327 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006328 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006329 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6330 << Name;
6331 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006332 } else if (D.getDeclSpec().getStorageClassSpec()
6333 != DeclSpec::SCS_unspecified) {
6334 // Complain about then remove the storage class specifier.
6335 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6336 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6337
6338 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006339 }
6340
Douglas Gregor663b5a02009-10-14 20:14:33 +00006341 // C++0x [temp.explicit]p1:
6342 // [...] An explicit instantiation of a function template shall not use the
6343 // inline or constexpr specifiers.
6344 // Presumably, this also applies to member functions of class templates as
6345 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006346 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006347 Diag(D.getDeclSpec().getInlineSpecLoc(),
Richard Smith2dc7ece2011-10-18 03:44:03 +00006348 getLangOptions().CPlusPlus0x ?
6349 diag::err_explicit_instantiation_inline :
6350 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006351 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6352 if (D.getDeclSpec().isConstexprSpecified())
6353 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6354 // not already specified.
6355 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6356 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006357
Douglas Gregor558c0322009-10-14 23:41:34 +00006358 // C++0x [temp.explicit]p2:
6359 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006360 // definition and an explicit instantiation declaration. An explicit
6361 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006362 TemplateSpecializationKind TSK
6363 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6364 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006365
Abramo Bagnara25777432010-08-11 22:01:17 +00006366 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006367 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006368
6369 if (!R->isFunctionType()) {
6370 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006371 // A [...] static data member of a class template can be explicitly
6372 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006373 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006374 if (Previous.isAmbiguous())
6375 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006376
John McCall1bcee0a2009-12-02 08:25:40 +00006377 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006378 if (!Prev || !Prev->isStaticDataMember()) {
6379 // We expect to see a data data member here.
6380 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6381 << Name;
6382 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6383 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006384 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006385 return true;
6386 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006387
Douglas Gregord5a423b2009-09-25 18:43:00 +00006388 if (!Prev->getInstantiatedFromStaticDataMember()) {
6389 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006390 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006391 diag::err_explicit_instantiation_data_member_not_instantiated)
6392 << Prev;
6393 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6394 // FIXME: Can we provide a note showing where this was declared?
6395 return true;
6396 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006397
Douglas Gregor558c0322009-10-14 23:41:34 +00006398 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006399 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006400 // or a static data member of a class template specialization, the name of
6401 // the class template specialization in the qualified-id for the member
6402 // name shall be a simple-template-id.
6403 //
6404 // C++98 has the same restriction, just worded differently.
6405 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006406 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006407 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006408 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006409
Douglas Gregor558c0322009-10-14 23:41:34 +00006410 // Check the scope of this explicit instantiation.
6411 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006412
Douglas Gregor454885e2009-10-15 15:54:05 +00006413 // Verify that it is okay to explicitly instantiate here.
6414 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6415 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006416 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006417 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006418 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006419 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006420 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006421 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006422 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006423 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006424
Douglas Gregord5a423b2009-09-25 18:43:00 +00006425 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006426 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006427 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006428 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006429
Douglas Gregord5a423b2009-09-25 18:43:00 +00006430 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006431 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006432 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006433
6434 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006435 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006436 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006437 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006438 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6439 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006440 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6441 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006442 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6443 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006444 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006445 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006446 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006447 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006448 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006449
Douglas Gregord5a423b2009-09-25 18:43:00 +00006450 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006451 // A [...] function [...] can be explicitly instantiated from its template.
6452 // A member function [...] of a class template can be explicitly
6453 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006454 // template.
John McCallc373d482010-01-27 01:50:18 +00006455 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006456 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6457 P != PEnd; ++P) {
6458 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006459 if (!HasExplicitTemplateArgs) {
6460 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6461 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6462 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006463
John McCallc373d482010-01-27 01:50:18 +00006464 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006465 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6466 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006467 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006468 }
6469 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006470
Douglas Gregord5a423b2009-09-25 18:43:00 +00006471 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6472 if (!FunTmpl)
6473 continue;
6474
John McCall5769d612010-02-08 23:07:23 +00006475 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006476 FunctionDecl *Specialization = 0;
6477 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006478 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006479 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006480 R, Specialization, Info)) {
6481 // FIXME: Keep track of almost-matches?
6482 (void)TDK;
6483 continue;
6484 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006485
John McCallc373d482010-01-27 01:50:18 +00006486 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006487 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006488
Douglas Gregord5a423b2009-09-25 18:43:00 +00006489 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006490 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006491 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006492 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006493 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6494 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6495 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006496
John McCallc373d482010-01-27 01:50:18 +00006497 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006498 return true;
John McCallc373d482010-01-27 01:50:18 +00006499
6500 // Ignore access control bits, we don't need them for redeclaration checking.
6501 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006502
Douglas Gregor0a897e32009-10-15 17:21:20 +00006503 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006504 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006505 diag::err_explicit_instantiation_member_function_not_instantiated)
6506 << Specialization
6507 << (Specialization->getTemplateSpecializationKind() ==
6508 TSK_ExplicitSpecialization);
6509 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6510 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006511 }
6512
Douglas Gregoref96ee02012-01-14 16:38:05 +00006513 FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006514 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6515 PrevDecl = Specialization;
6516
Douglas Gregor0a897e32009-10-15 17:21:20 +00006517 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006518 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006519 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006520 PrevDecl,
6521 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006522 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006523 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006524 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006525
Douglas Gregor0a897e32009-10-15 17:21:20 +00006526 // FIXME: We may still want to build some representation of this
6527 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006528 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006529 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006530 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006531
6532 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Rafael Espindola256fc4d2012-01-04 05:40:59 +00006533 AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
6534 if (Attr)
6535 ProcessDeclAttributeList(S, Specialization, Attr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006536
Douglas Gregor0a897e32009-10-15 17:21:20 +00006537 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006538 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006539
Douglas Gregor558c0322009-10-14 23:41:34 +00006540 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006541 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006542 // or a static data member of a class template specialization, the name of
6543 // the class template specialization in the qualified-id for the member
6544 // name shall be a simple-template-id.
6545 //
6546 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006547 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006548 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006549 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006550 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006551 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006552 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006553 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006554
Douglas Gregor558c0322009-10-14 23:41:34 +00006555 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006556 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006557 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006558 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006559 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006560
Douglas Gregord5a423b2009-09-25 18:43:00 +00006561 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006562 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006563}
6564
John McCallf312b1e2010-08-26 23:41:50 +00006565TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006566Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6567 const CXXScopeSpec &SS, IdentifierInfo *Name,
6568 SourceLocation TagLoc, SourceLocation NameLoc) {
6569 // This has to hold, because SS is expected to be defined.
6570 assert(Name && "Expected a name in a dependent tag");
6571
6572 NestedNameSpecifier *NNS
6573 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6574 if (!NNS)
6575 return true;
6576
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006577 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006578
Douglas Gregor48c89f42010-04-24 16:38:41 +00006579 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6580 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006581 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006582 return true;
6583 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006584
Douglas Gregor059101f2011-03-02 00:47:37 +00006585 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006586 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006587 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6588
6589 // Create type-source location information for this type.
6590 TypeLocBuilder TLB;
6591 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006592 TL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00006593 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6594 TL.setNameLoc(NameLoc);
6595 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006596}
6597
John McCallf312b1e2010-08-26 23:41:50 +00006598TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006599Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6600 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006601 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006602 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006603 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006604
Richard Smithebaf0e62011-10-18 20:49:44 +00006605 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6606 Diag(TypenameLoc,
6607 getLangOptions().CPlusPlus0x ?
6608 diag::warn_cxx98_compat_typename_outside_of_template :
6609 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006610 << FixItHint::CreateRemoval(TypenameLoc);
6611
Douglas Gregor2494dd02011-03-01 01:34:45 +00006612 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006613 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6614 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006615 if (T.isNull())
6616 return true;
John McCall63b43852010-04-29 23:50:39 +00006617
6618 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6619 if (isa<DependentNameType>(T)) {
6620 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006621 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006622 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006623 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006624 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006625 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006626 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006627 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006628 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006629 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006630
John McCallb3d87482010-08-24 05:47:05 +00006631 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006632}
6633
John McCallf312b1e2010-08-26 23:41:50 +00006634TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006635Sema::ActOnTypenameType(Scope *S,
6636 SourceLocation TypenameLoc,
6637 const CXXScopeSpec &SS,
6638 SourceLocation TemplateKWLoc,
Douglas Gregora02411e2011-02-27 22:46:49 +00006639 TemplateTy TemplateIn,
6640 SourceLocation TemplateNameLoc,
6641 SourceLocation LAngleLoc,
6642 ASTTemplateArgsPtr TemplateArgsIn,
6643 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006644 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6645 Diag(TypenameLoc,
6646 getLangOptions().CPlusPlus0x ?
6647 diag::warn_cxx98_compat_typename_outside_of_template :
6648 diag::ext_typename_outside_of_template)
6649 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006650
6651 // Translate the parser's template argument list in our AST format.
6652 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6653 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6654
6655 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006656 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6657 // Construct a dependent template specialization type.
6658 assert(DTN && "dependent template has non-dependent name?");
6659 assert(DTN->getQualifier()
6660 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6661 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6662 DTN->getQualifier(),
6663 DTN->getIdentifier(),
6664 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006665
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006666 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006667 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006668 DependentTemplateSpecializationTypeLoc SpecTL
6669 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006670 SpecTL.setElaboratedKeywordLoc(TypenameLoc);
6671 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00006672 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006673 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006674 SpecTL.setLAngleLoc(LAngleLoc);
6675 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006676 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6677 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006678 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006679 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006680
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006681 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6682 if (T.isNull())
6683 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006684
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006685 // Provide source-location information for the template specialization type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006686 TypeLocBuilder Builder;
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006687 TemplateSpecializationTypeLoc SpecTL
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006688 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006689 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
6690 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006691 SpecTL.setLAngleLoc(LAngleLoc);
6692 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006693 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6694 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6695
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006696 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6697 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006698 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006699 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6700
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006701 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6702 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006703}
6704
Douglas Gregora02411e2011-02-27 22:46:49 +00006705
Douglas Gregord57959a2009-03-27 23:10:48 +00006706/// \brief Build the type that describes a C++ typename specifier,
6707/// e.g., "typename T::type".
6708QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006709Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6710 SourceLocation KeywordLoc,
6711 NestedNameSpecifierLoc QualifierLoc,
6712 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006713 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006714 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006715 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006716
John McCall77bb1aa2010-05-01 00:40:08 +00006717 DeclContext *Ctx = computeDeclContext(SS);
6718 if (!Ctx) {
6719 // If the nested-name-specifier is dependent and couldn't be
6720 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006721 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6722 return Context.getDependentNameType(Keyword,
6723 QualifierLoc.getNestedNameSpecifier(),
6724 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006725 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006726
John McCall77bb1aa2010-05-01 00:40:08 +00006727 // If the nested-name-specifier refers to the current instantiation,
6728 // the "typename" keyword itself is superfluous. In C++03, the
6729 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6730 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006731 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006732
John McCall77bb1aa2010-05-01 00:40:08 +00006733 if (RequireCompleteDeclContext(SS, Ctx))
6734 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006735
6736 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006737 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006738 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006739 unsigned DiagID = 0;
6740 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006741 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006742 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006743 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006744 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006745
6746 case LookupResult::FoundUnresolvedValue: {
6747 // We found a using declaration that is a value. Most likely, the using
6748 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006749 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006750 IILoc);
6751 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6752 << Name << Ctx << FullRange;
6753 if (UnresolvedUsingValueDecl *Using
6754 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006755 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006756 Diag(Loc, diag::note_using_value_decl_missing_typename)
6757 << FixItHint::CreateInsertion(Loc, "typename ");
6758 }
6759 }
6760 // Fall through to create a dependent typename type, from which we can recover
6761 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006762
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006763 case LookupResult::NotFoundInCurrentInstantiation:
6764 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006765 return Context.getDependentNameType(Keyword,
6766 QualifierLoc.getNestedNameSpecifier(),
6767 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006768
6769 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006770 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006771 // We found a type. Build an ElaboratedType, since the
6772 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006773 return Context.getElaboratedType(ETK_Typename,
6774 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006775 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006776 }
6777
6778 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006779 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006780 break;
6781
6782 case LookupResult::FoundOverloaded:
6783 DiagID = diag::err_typename_nested_not_type;
6784 Referenced = *Result.begin();
6785 break;
6786
John McCall6e247262009-10-10 05:48:19 +00006787 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006788 return QualType();
6789 }
6790
6791 // If we get here, it's because name lookup did not find a
6792 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006793 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006794 IILoc);
6795 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006796 if (Referenced)
6797 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6798 << Name;
6799 return QualType();
6800}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006801
6802namespace {
6803 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006804 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006805 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006806 SourceLocation Loc;
6807 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006808
Douglas Gregor4a959d82009-08-06 16:20:37 +00006809 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006810 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006811
Mike Stump1eb44332009-09-09 15:08:12 +00006812 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006813 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006814 DeclarationName Entity)
6815 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006816 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006817
6818 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006819 /// transformed.
6820 ///
6821 /// For the purposes of type reconstruction, a type has already been
6822 /// transformed if it is NULL or if it is not dependent.
6823 bool AlreadyTransformed(QualType T) {
6824 return T.isNull() || !T->isDependentType();
6825 }
Mike Stump1eb44332009-09-09 15:08:12 +00006826
6827 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006828 /// rebuilt.
6829 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006830
Douglas Gregor4a959d82009-08-06 16:20:37 +00006831 /// \brief Returns the name of the entity whose type is being rebuilt.
6832 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006833
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006834 /// \brief Sets the "base" location and entity when that
6835 /// information is known based on another transformation.
6836 void setBase(SourceLocation Loc, DeclarationName Entity) {
6837 this->Loc = Loc;
6838 this->Entity = Entity;
6839 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006840 };
6841}
6842
Douglas Gregor4a959d82009-08-06 16:20:37 +00006843/// \brief Rebuilds a type within the context of the current instantiation.
6844///
Mike Stump1eb44332009-09-09 15:08:12 +00006845/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006846/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006847/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006848/// partial specialization thereof). This routine will rebuild that type now
6849/// that we have entered the declarator's scope, which may produce different
6850/// canonical types, e.g.,
6851///
6852/// \code
6853/// template<typename T>
6854/// struct X {
6855/// typedef T* pointer;
6856/// pointer data();
6857/// };
6858///
6859/// template<typename T>
6860/// typename X<T>::pointer X<T>::data() { ... }
6861/// \endcode
6862///
Douglas Gregor4714c122010-03-31 17:34:00 +00006863/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006864/// since we do not know that we can look into X<T> when we parsed the type.
6865/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006866/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006867/// as the canonical type of T*, allowing the return types of the out-of-line
6868/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006869TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6870 SourceLocation Loc,
6871 DeclarationName Name) {
6872 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006873 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006874
Douglas Gregor4a959d82009-08-06 16:20:37 +00006875 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6876 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006877}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006878
John McCall60d7b3a2010-08-24 06:29:42 +00006879ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006880 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6881 DeclarationName());
6882 return Rebuilder.TransformExpr(E);
6883}
6884
John McCall63b43852010-04-29 23:50:39 +00006885bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006886 if (SS.isInvalid())
6887 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006888
Douglas Gregor7e384942011-02-25 16:07:42 +00006889 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006890 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6891 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006892 NestedNameSpecifierLoc Rebuilt
6893 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6894 if (!Rebuilt)
6895 return true;
John McCall63b43852010-04-29 23:50:39 +00006896
Douglas Gregor7e384942011-02-25 16:07:42 +00006897 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006898 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006899}
6900
Douglas Gregor20606502011-10-14 15:31:12 +00006901/// \brief Rebuild the template parameters now that we know we're in a current
6902/// instantiation.
6903bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6904 TemplateParameterList *Params) {
6905 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6906 Decl *Param = Params->getParam(I);
6907
6908 // There is nothing to rebuild in a type parameter.
6909 if (isa<TemplateTypeParmDecl>(Param))
6910 continue;
6911
6912 // Rebuild the template parameter list of a template template parameter.
6913 if (TemplateTemplateParmDecl *TTP
6914 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6915 if (RebuildTemplateParamsInCurrentInstantiation(
6916 TTP->getTemplateParameters()))
6917 return true;
6918
6919 continue;
6920 }
6921
6922 // Rebuild the type of a non-type template parameter.
6923 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6924 TypeSourceInfo *NewTSI
6925 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6926 NTTP->getLocation(),
6927 NTTP->getDeclName());
6928 if (!NewTSI)
6929 return true;
6930
6931 if (NewTSI != NTTP->getTypeSourceInfo()) {
6932 NTTP->setTypeSourceInfo(NewTSI);
6933 NTTP->setType(NewTSI->getType());
6934 }
6935 }
6936
6937 return false;
6938}
6939
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006940/// \brief Produces a formatted string that describes the binding of
6941/// template parameters to template arguments.
6942std::string
6943Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6944 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006945 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006946}
6947
6948std::string
6949Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6950 const TemplateArgument *Args,
6951 unsigned NumArgs) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00006952 SmallString<128> Str;
Douglas Gregor87dd6972010-12-20 16:52:59 +00006953 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006954
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006955 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006956 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006957
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006958 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006959 if (I >= NumArgs)
6960 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006961
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006962 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006963 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006964 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006965 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006966
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006967 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006968 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006969 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006970 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006971 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006972
Douglas Gregor87dd6972010-12-20 16:52:59 +00006973 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006974 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006975 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006976
6977 Out << ']';
6978 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006979}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006980
6981void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6982 if (!FD)
6983 return;
6984 FD->setLateTemplateParsed(Flag);
6985}
6986
6987bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6988 DeclContext *DC = CurContext;
6989
6990 while (DC) {
6991 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6992 const FunctionDecl *FD = RD->isLocalClass();
6993 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6994 } else if (DC->isTranslationUnit() || DC->isNamespace())
6995 return false;
6996
6997 DC = DC->getParent();
6998 }
6999 return false;
7000}