Sebastian Redl | e2530ec | 2009-10-23 22:13:42 +0000 | [diff] [blame] | 1 | //===--- DeclTemplate.cpp - Template Declaration AST Node Implementation --===// |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the C++ related Decl classes for templates. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/ASTMutationListener.h" |
| 17 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Douglas Gregor | 8575911 | 2011-01-04 02:33:52 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeLoc.h" |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Builtins.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 22 | #include "clang/Basic/IdentifierTable.h" |
| 23 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 24 | #include <memory> |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // TemplateParameterList Implementation |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 31 | TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc, |
| 32 | SourceLocation LAngleLoc, |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 33 | ArrayRef<NamedDecl *> Params, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 34 | SourceLocation RAngleLoc, |
| 35 | Expr *RequiresClause) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 36 | : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 37 | NumParams(Params.size()), ContainsUnexpandedParameterPack(false), |
| 38 | HasRequiresClause(static_cast<bool>(RequiresClause)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 39 | for (unsigned Idx = 0; Idx < NumParams; ++Idx) { |
| 40 | NamedDecl *P = Params[Idx]; |
| 41 | begin()[Idx] = P; |
| 42 | |
| 43 | if (!P->isTemplateParameterPack()) { |
| 44 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) |
| 45 | if (NTTP->getType()->containsUnexpandedParameterPack()) |
| 46 | ContainsUnexpandedParameterPack = true; |
| 47 | |
| 48 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 49 | if (TTP->getTemplateParameters()->containsUnexpandedParameterPack()) |
| 50 | ContainsUnexpandedParameterPack = true; |
| 51 | |
| 52 | // FIXME: If a default argument contains an unexpanded parameter pack, the |
| 53 | // template parameter list does too. |
| 54 | } |
| 55 | } |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 56 | if (RequiresClause) { |
| 57 | *getTrailingObjects<Expr *>() = RequiresClause; |
| 58 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 61 | TemplateParameterList * |
| 62 | TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc, |
| 63 | SourceLocation LAngleLoc, |
| 64 | ArrayRef<NamedDecl *> Params, |
| 65 | SourceLocation RAngleLoc, Expr *RequiresClause) { |
| 66 | void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *, Expr *>( |
| 67 | Params.size(), RequiresClause ? 1u : 0u), |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 68 | alignof(TemplateParameterList)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 70 | RAngleLoc, RequiresClause); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Douglas Gregor | f8f8683 | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 73 | unsigned TemplateParameterList::getMinRequiredArguments() const { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 74 | unsigned NumRequiredArgs = 0; |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 75 | for (const NamedDecl *P : asArray()) { |
| 76 | if (P->isTemplateParameterPack()) { |
| 77 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 78 | if (NTTP->isExpandedParameterPack()) { |
| 79 | NumRequiredArgs += NTTP->getNumExpansionTypes(); |
| 80 | continue; |
| 81 | } |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | f8f8683 | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 83 | break; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 84 | } |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 85 | |
| 86 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 87 | if (TTP->hasDefaultArgument()) |
| 88 | break; |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 89 | } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 90 | if (NTTP->hasDefaultArgument()) |
| 91 | break; |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 92 | } else if (cast<TemplateTemplateParmDecl>(P)->hasDefaultArgument()) |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 93 | break; |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 95 | ++NumRequiredArgs; |
Douglas Gregor | f8f8683 | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 96 | } |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 97 | |
Douglas Gregor | f8f8683 | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 98 | return NumRequiredArgs; |
| 99 | } |
| 100 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 101 | unsigned TemplateParameterList::getDepth() const { |
| 102 | if (size() == 0) |
| 103 | return 0; |
| 104 | |
| 105 | const NamedDecl *FirstParm = getParam(0); |
| 106 | if (const TemplateTypeParmDecl *TTP |
| 107 | = dyn_cast<TemplateTypeParmDecl>(FirstParm)) |
| 108 | return TTP->getDepth(); |
| 109 | else if (const NonTypeTemplateParmDecl *NTTP |
| 110 | = dyn_cast<NonTypeTemplateParmDecl>(FirstParm)) |
| 111 | return NTTP->getDepth(); |
| 112 | else |
| 113 | return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth(); |
| 114 | } |
| 115 | |
Douglas Gregor | 3c41bf7 | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 116 | static void AdoptTemplateParameterList(TemplateParameterList *Params, |
| 117 | DeclContext *Owner) { |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 118 | for (NamedDecl *P : *Params) { |
| 119 | P->setDeclContext(Owner); |
| 120 | |
| 121 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) |
Douglas Gregor | 3c41bf7 | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 122 | AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner); |
| 123 | } |
| 124 | } |
| 125 | |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 126 | namespace clang { |
| 127 | void *allocateDefaultArgStorageChain(const ASTContext &C) { |
| 128 | return new (C) char[sizeof(void*) * 2]; |
| 129 | } |
| 130 | } |
| 131 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 132 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 91b25b7 | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 133 | // RedeclarableTemplateDecl Implementation |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | |
Dmitri Gribenko | b53f37c | 2013-01-23 16:52:57 +0000 | [diff] [blame] | 136 | RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const { |
Rafael Espindola | cb444fe | 2013-10-19 02:28:17 +0000 | [diff] [blame] | 137 | if (Common) |
| 138 | return Common; |
| 139 | |
| 140 | // Walk the previous-declaration chain until we either find a declaration |
| 141 | // with a common pointer or we run out of previous declarations. |
| 142 | SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls; |
| 143 | for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev; |
| 144 | Prev = Prev->getPreviousDecl()) { |
| 145 | if (Prev->Common) { |
| 146 | Common = Prev->Common; |
| 147 | break; |
Douglas Gregor | 68444de | 2012-01-14 15:13:49 +0000 | [diff] [blame] | 148 | } |
Peter Collingbourne | 91b25b7 | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 149 | |
Rafael Espindola | cb444fe | 2013-10-19 02:28:17 +0000 | [diff] [blame] | 150 | PrevDecls.push_back(Prev); |
Peter Collingbourne | 91b25b7 | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 151 | } |
Peter Collingbourne | 91b25b7 | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 152 | |
Rafael Espindola | cb444fe | 2013-10-19 02:28:17 +0000 | [diff] [blame] | 153 | // If we never found a common pointer, allocate one now. |
| 154 | if (!Common) { |
| 155 | // FIXME: If any of the declarations is from an AST file, we probably |
| 156 | // need an update record to add the common data. |
| 157 | |
| 158 | Common = newCommon(getASTContext()); |
| 159 | } |
| 160 | |
| 161 | // Update any previous declarations we saw with the common pointer. |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 162 | for (const RedeclarableTemplateDecl *Prev : PrevDecls) |
| 163 | Prev->Common = Common; |
Rafael Espindola | cb444fe | 2013-10-19 02:28:17 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | 68444de | 2012-01-14 15:13:49 +0000 | [diff] [blame] | 165 | return Common; |
Peter Collingbourne | 029fd69 | 2010-07-29 16:12:09 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Richard Smith | e977e51 | 2015-02-24 01:23:23 +0000 | [diff] [blame] | 168 | template<class EntryType> |
| 169 | typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType * |
Peter Collingbourne | b498ed6 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 170 | RedeclarableTemplateDecl::findSpecializationImpl( |
Richard Smith | e977e51 | 2015-02-24 01:23:23 +0000 | [diff] [blame] | 171 | llvm::FoldingSetVector<EntryType> &Specs, ArrayRef<TemplateArgument> Args, |
| 172 | void *&InsertPos) { |
Peter Collingbourne | b498ed6 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 173 | typedef SpecEntryTraits<EntryType> SETraits; |
| 174 | llvm::FoldingSetNodeID ID; |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 175 | EntryType::Profile(ID,Args, getASTContext()); |
Peter Collingbourne | b498ed6 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 176 | EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos); |
Richard Smith | e977e51 | 2015-02-24 01:23:23 +0000 | [diff] [blame] | 177 | return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr; |
| 178 | } |
| 179 | |
| 180 | template<class Derived, class EntryType> |
| 181 | void RedeclarableTemplateDecl::addSpecializationImpl( |
| 182 | llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry, |
| 183 | void *InsertPos) { |
| 184 | typedef SpecEntryTraits<EntryType> SETraits; |
| 185 | if (InsertPos) { |
| 186 | #ifndef NDEBUG |
| 187 | void *CorrectInsertPos; |
| 188 | assert(!findSpecializationImpl(Specializations, |
| 189 | SETraits::getTemplateArgs(Entry), |
| 190 | CorrectInsertPos) && |
| 191 | InsertPos == CorrectInsertPos && |
| 192 | "given incorrect InsertPos for specialization"); |
| 193 | #endif |
| 194 | Specializations.InsertNode(Entry, InsertPos); |
| 195 | } else { |
| 196 | EntryType *Existing = Specializations.GetOrInsertNode(Entry); |
| 197 | (void)Existing; |
| 198 | assert(SETraits::getDecl(Existing)->isCanonicalDecl() && |
| 199 | "non-canonical specialization?"); |
| 200 | } |
| 201 | |
| 202 | if (ASTMutationListener *L = getASTMutationListener()) |
| 203 | L->AddedCXXTemplateSpecialization(cast<Derived>(this), |
| 204 | SETraits::getDecl(Entry)); |
Peter Collingbourne | b498ed6 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Peter Collingbourne | 91b25b7 | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 207 | //===----------------------------------------------------------------------===// |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 208 | // FunctionTemplateDecl Implementation |
| 209 | //===----------------------------------------------------------------------===// |
| 210 | |
| 211 | FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C, |
| 212 | DeclContext *DC, |
| 213 | SourceLocation L, |
| 214 | DeclarationName Name, |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 215 | TemplateParameterList *Params, |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 216 | NamedDecl *Decl) { |
Douglas Gregor | 3c41bf7 | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 217 | AdoptTemplateParameterList(Params, cast<DeclContext>(Decl)); |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 218 | return new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 221 | FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C, |
| 222 | unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 223 | return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 224 | DeclarationName(), nullptr, nullptr); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 227 | RedeclarableTemplateDecl::CommonBase * |
Dmitri Gribenko | b53f37c | 2013-01-23 16:52:57 +0000 | [diff] [blame] | 228 | FunctionTemplateDecl::newCommon(ASTContext &C) const { |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 229 | Common *CommonPtr = new (C) Common; |
George Burgess IV | b61bfbd | 2017-02-14 05:37:36 +0000 | [diff] [blame^] | 230 | C.addDestruction(CommonPtr); |
Peter Collingbourne | 91b25b7 | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 231 | return CommonPtr; |
| 232 | } |
| 233 | |
Richard Smith | feb3e1a | 2013-06-28 04:37:53 +0000 | [diff] [blame] | 234 | void FunctionTemplateDecl::LoadLazySpecializations() const { |
Richard Smith | e3536dd | 2015-02-24 02:44:23 +0000 | [diff] [blame] | 235 | // Grab the most recent declaration to ensure we've loaded any lazy |
| 236 | // redeclarations of this template. |
| 237 | // |
| 238 | // FIXME: Avoid walking the entire redeclaration chain here. |
| 239 | Common *CommonPtr = getMostRecentDecl()->getCommonPtr(); |
Richard Smith | feb3e1a | 2013-06-28 04:37:53 +0000 | [diff] [blame] | 240 | if (CommonPtr->LazySpecializations) { |
| 241 | ASTContext &Context = getASTContext(); |
| 242 | uint32_t *Specs = CommonPtr->LazySpecializations; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 243 | CommonPtr->LazySpecializations = nullptr; |
Richard Smith | feb3e1a | 2013-06-28 04:37:53 +0000 | [diff] [blame] | 244 | for (uint32_t I = 0, N = *Specs++; I != N; ++I) |
| 245 | (void)Context.getExternalSource()->GetExternalDecl(Specs[I]); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> & |
| 250 | FunctionTemplateDecl::getSpecializations() const { |
| 251 | LoadLazySpecializations(); |
| 252 | return getCommonPtr()->Specializations; |
| 253 | } |
| 254 | |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 255 | FunctionDecl * |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 256 | FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, |
| 257 | void *&InsertPos) { |
| 258 | return findSpecializationImpl(getSpecializations(), Args, InsertPos); |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Sebastian Redl | 9ab988f | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 261 | void FunctionTemplateDecl::addSpecialization( |
| 262 | FunctionTemplateSpecializationInfo *Info, void *InsertPos) { |
Richard Smith | e977e51 | 2015-02-24 01:23:23 +0000 | [diff] [blame] | 263 | addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info, |
| 264 | InsertPos); |
Sebastian Redl | 9ab988f | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 267 | ArrayRef<TemplateArgument> FunctionTemplateDecl::getInjectedTemplateArgs() { |
Douglas Gregor | 43669f8 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 268 | TemplateParameterList *Params = getTemplateParameters(); |
| 269 | Common *CommonPtr = getCommonPtr(); |
| 270 | if (!CommonPtr->InjectedArgs) { |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 271 | auto &Context = getASTContext(); |
| 272 | SmallVector<TemplateArgument, 16> TemplateArgs; |
| 273 | Context.getInjectedTemplateArgs(Params, TemplateArgs); |
| 274 | CommonPtr->InjectedArgs = |
| 275 | new (Context) TemplateArgument[TemplateArgs.size()]; |
| 276 | std::copy(TemplateArgs.begin(), TemplateArgs.end(), |
| 277 | CommonPtr->InjectedArgs); |
Douglas Gregor | 43669f8 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 278 | } |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 279 | |
| 280 | return llvm::makeArrayRef(CommonPtr->InjectedArgs, Params->size()); |
Douglas Gregor | 43669f8 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 283 | //===----------------------------------------------------------------------===// |
| 284 | // ClassTemplateDecl Implementation |
| 285 | //===----------------------------------------------------------------------===// |
| 286 | |
| 287 | ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, |
| 288 | DeclContext *DC, |
| 289 | SourceLocation L, |
| 290 | DeclarationName Name, |
| 291 | TemplateParameterList *Params, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 292 | NamedDecl *Decl, |
| 293 | Expr *AssociatedConstraints) { |
Douglas Gregor | 3c41bf7 | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 294 | AdoptTemplateParameterList(Params, cast<DeclContext>(Decl)); |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 295 | |
| 296 | if (!AssociatedConstraints) { |
| 297 | return new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl); |
| 298 | } |
| 299 | |
| 300 | ConstrainedTemplateDeclInfo *const CTDI = new (C) ConstrainedTemplateDeclInfo; |
| 301 | ClassTemplateDecl *const New = |
| 302 | new (C, DC) ClassTemplateDecl(CTDI, C, DC, L, Name, Params, Decl); |
| 303 | New->setAssociatedConstraints(AssociatedConstraints); |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 304 | return New; |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 307 | ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C, |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 308 | unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 309 | return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(), |
| 310 | DeclarationName(), nullptr, nullptr); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Dmitri Gribenko | 81f2575 | 2013-02-14 13:20:36 +0000 | [diff] [blame] | 313 | void ClassTemplateDecl::LoadLazySpecializations() const { |
Richard Smith | e3536dd | 2015-02-24 02:44:23 +0000 | [diff] [blame] | 314 | // Grab the most recent declaration to ensure we've loaded any lazy |
| 315 | // redeclarations of this template. |
| 316 | // |
| 317 | // FIXME: Avoid walking the entire redeclaration chain here. |
| 318 | Common *CommonPtr = getMostRecentDecl()->getCommonPtr(); |
Douglas Gregor | 7e8c4e0 | 2010-10-27 22:21:36 +0000 | [diff] [blame] | 319 | if (CommonPtr->LazySpecializations) { |
| 320 | ASTContext &Context = getASTContext(); |
| 321 | uint32_t *Specs = CommonPtr->LazySpecializations; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 322 | CommonPtr->LazySpecializations = nullptr; |
Douglas Gregor | 7e8c4e0 | 2010-10-27 22:21:36 +0000 | [diff] [blame] | 323 | for (uint32_t I = 0, N = *Specs++; I != N; ++I) |
| 324 | (void)Context.getExternalSource()->GetExternalDecl(Specs[I]); |
| 325 | } |
| 326 | } |
| 327 | |
Chandler Carruth | b41171b | 2012-05-03 23:49:05 +0000 | [diff] [blame] | 328 | llvm::FoldingSetVector<ClassTemplateSpecializationDecl> & |
Dmitri Gribenko | 81f2575 | 2013-02-14 13:20:36 +0000 | [diff] [blame] | 329 | ClassTemplateDecl::getSpecializations() const { |
Douglas Gregor | 7e8c4e0 | 2010-10-27 22:21:36 +0000 | [diff] [blame] | 330 | LoadLazySpecializations(); |
| 331 | return getCommonPtr()->Specializations; |
| 332 | } |
| 333 | |
Chandler Carruth | b41171b | 2012-05-03 23:49:05 +0000 | [diff] [blame] | 334 | llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> & |
Douglas Gregor | 7e8c4e0 | 2010-10-27 22:21:36 +0000 | [diff] [blame] | 335 | ClassTemplateDecl::getPartialSpecializations() { |
| 336 | LoadLazySpecializations(); |
| 337 | return getCommonPtr()->PartialSpecializations; |
| 338 | } |
| 339 | |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 340 | RedeclarableTemplateDecl::CommonBase * |
Dmitri Gribenko | b53f37c | 2013-01-23 16:52:57 +0000 | [diff] [blame] | 341 | ClassTemplateDecl::newCommon(ASTContext &C) const { |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 342 | Common *CommonPtr = new (C) Common; |
George Burgess IV | b61bfbd | 2017-02-14 05:37:36 +0000 | [diff] [blame^] | 343 | C.addDestruction(CommonPtr); |
Peter Collingbourne | 91b25b7 | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 344 | return CommonPtr; |
| 345 | } |
| 346 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 347 | ClassTemplateSpecializationDecl * |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 348 | ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, |
| 349 | void *&InsertPos) { |
| 350 | return findSpecializationImpl(getSpecializations(), Args, InsertPos); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Argyrios Kyrtzidis | 402dbbb | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 353 | void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D, |
| 354 | void *InsertPos) { |
Richard Smith | e977e51 | 2015-02-24 01:23:23 +0000 | [diff] [blame] | 355 | addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos); |
Argyrios Kyrtzidis | 402dbbb | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 358 | ClassTemplatePartialSpecializationDecl * |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 359 | ClassTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args, |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 360 | void *&InsertPos) { |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 361 | return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Argyrios Kyrtzidis | 402dbbb | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 364 | void ClassTemplateDecl::AddPartialSpecialization( |
| 365 | ClassTemplatePartialSpecializationDecl *D, |
| 366 | void *InsertPos) { |
Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 367 | if (InsertPos) |
| 368 | getPartialSpecializations().InsertNode(D, InsertPos); |
| 369 | else { |
| 370 | ClassTemplatePartialSpecializationDecl *Existing |
| 371 | = getPartialSpecializations().GetOrInsertNode(D); |
| 372 | (void)Existing; |
| 373 | assert(Existing->isCanonicalDecl() && "Non-canonical specialization?"); |
| 374 | } |
| 375 | |
Argyrios Kyrtzidis | 402dbbb | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 376 | if (ASTMutationListener *L = getASTMutationListener()) |
| 377 | L->AddedCXXTemplateSpecialization(this, D); |
| 378 | } |
| 379 | |
Douglas Gregor | 407e961 | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 380 | void ClassTemplateDecl::getPartialSpecializations( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 381 | SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) { |
Chandler Carruth | b41171b | 2012-05-03 23:49:05 +0000 | [diff] [blame] | 382 | llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs |
Argyrios Kyrtzidis | a35c8e4 | 2010-06-21 10:57:41 +0000 | [diff] [blame] | 383 | = getPartialSpecializations(); |
Douglas Gregor | 407e961 | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 384 | PS.clear(); |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 385 | PS.reserve(PartialSpecs.size()); |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 386 | for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs) |
| 387 | PS.push_back(P.getMostRecentDecl()); |
Douglas Gregor | 407e961 | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 390 | ClassTemplatePartialSpecializationDecl * |
| 391 | ClassTemplateDecl::findPartialSpecialization(QualType T) { |
| 392 | ASTContext &Context = getASTContext(); |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 393 | for (ClassTemplatePartialSpecializationDecl &P : |
| 394 | getPartialSpecializations()) { |
| 395 | if (Context.hasSameType(P.getInjectedSpecializationType(), T)) |
| 396 | return P.getMostRecentDecl(); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 399 | return nullptr; |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | ClassTemplatePartialSpecializationDecl * |
| 403 | ClassTemplateDecl::findPartialSpecInstantiatedFromMember( |
| 404 | ClassTemplatePartialSpecializationDecl *D) { |
| 405 | Decl *DCanon = D->getCanonicalDecl(); |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 406 | for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) { |
| 407 | if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon) |
| 408 | return P.getMostRecentDecl(); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 409 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 411 | return nullptr; |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 412 | } |
| 413 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 414 | QualType |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 415 | ClassTemplateDecl::getInjectedClassNameSpecialization() { |
Argyrios Kyrtzidis | a35c8e4 | 2010-06-21 10:57:41 +0000 | [diff] [blame] | 416 | Common *CommonPtr = getCommonPtr(); |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 417 | if (!CommonPtr->InjectedClassNameType.isNull()) |
| 418 | return CommonPtr->InjectedClassNameType; |
| 419 | |
Douglas Gregor | 8092e80 | 2010-12-23 16:00:30 +0000 | [diff] [blame] | 420 | // C++0x [temp.dep.type]p2: |
| 421 | // The template argument list of a primary template is a template argument |
| 422 | // list in which the nth template argument has the value of the nth template |
| 423 | // parameter of the class template. If the nth template parameter is a |
| 424 | // template parameter pack (14.5.3), the nth template argument is a pack |
| 425 | // expansion (14.5.3) whose pattern is the name of the template parameter |
| 426 | // pack. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 427 | ASTContext &Context = getASTContext(); |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 428 | TemplateParameterList *Params = getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 429 | SmallVector<TemplateArgument, 16> TemplateArgs; |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 430 | Context.getInjectedTemplateArgs(Params, TemplateArgs); |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 431 | CommonPtr->InjectedClassNameType |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 432 | = Context.getTemplateSpecializationType(TemplateName(this), |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 433 | TemplateArgs); |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 434 | return CommonPtr->InjectedClassNameType; |
| 435 | } |
| 436 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 437 | //===----------------------------------------------------------------------===// |
| 438 | // TemplateTypeParm Allocation/Deallocation Method Implementations |
| 439 | //===----------------------------------------------------------------------===// |
| 440 | |
| 441 | TemplateTypeParmDecl * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 442 | TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 443 | SourceLocation KeyLoc, SourceLocation NameLoc, |
| 444 | unsigned D, unsigned P, IdentifierInfo *Id, |
| 445 | bool Typename, bool ParameterPack) { |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 446 | TemplateTypeParmDecl *TTPDecl = |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 447 | new (C, DC) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 448 | QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl); |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 449 | TTPDecl->setTypeForDecl(TTPType.getTypePtr()); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 450 | return TTPDecl; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 453 | TemplateTypeParmDecl * |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 454 | TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 455 | return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(), |
| 456 | SourceLocation(), nullptr, false); |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 457 | } |
| 458 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 459 | SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const { |
Abramo Bagnara | 23485e0 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 460 | return hasDefaultArgument() |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 461 | ? getDefaultArgumentInfo()->getTypeLoc().getBeginLoc() |
| 462 | : SourceLocation(); |
Abramo Bagnara | 23485e0 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | SourceRange TemplateTypeParmDecl::getSourceRange() const { |
| 466 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 467 | return SourceRange(getLocStart(), |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 468 | getDefaultArgumentInfo()->getTypeLoc().getEndLoc()); |
Abramo Bagnara | 23485e0 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 469 | else |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 470 | return TypeDecl::getSourceRange(); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 473 | unsigned TemplateTypeParmDecl::getDepth() const { |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 474 | return getTypeForDecl()->getAs<TemplateTypeParmType>()->getDepth(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | unsigned TemplateTypeParmDecl::getIndex() const { |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 478 | return getTypeForDecl()->getAs<TemplateTypeParmType>()->getIndex(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 481 | bool TemplateTypeParmDecl::isParameterPack() const { |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 482 | return getTypeForDecl()->getAs<TemplateTypeParmType>()->isParameterPack(); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 485 | //===----------------------------------------------------------------------===// |
| 486 | // NonTypeTemplateParmDecl Method Implementations |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 489 | NonTypeTemplateParmDecl::NonTypeTemplateParmDecl( |
| 490 | DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D, |
| 491 | unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| 492 | ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos) |
| 493 | : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc), |
| 494 | TemplateParmPosition(D, P), ParameterPack(true), |
| 495 | ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) { |
| 496 | if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 497 | auto TypesAndInfos = |
| 498 | getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 499 | for (unsigned I = 0; I != NumExpandedTypes; ++I) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 500 | new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]); |
| 501 | TypesAndInfos[I].second = ExpandedTInfos[I]; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 506 | NonTypeTemplateParmDecl * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 507 | NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 508 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 509 | unsigned D, unsigned P, IdentifierInfo *Id, |
| 510 | QualType T, bool ParameterPack, |
| 511 | TypeSourceInfo *TInfo) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 512 | return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, |
| 513 | T, ParameterPack, TInfo); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 514 | } |
| 515 | |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 516 | NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create( |
| 517 | const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 518 | SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id, |
| 519 | QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes, |
| 520 | ArrayRef<TypeSourceInfo *> ExpandedTInfos) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 521 | return new (C, DC, |
| 522 | additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>( |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 523 | ExpandedTypes.size())) |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 524 | NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 525 | ExpandedTypes, ExpandedTInfos); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 528 | NonTypeTemplateParmDecl * |
| 529 | NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 530 | return new (C, ID) NonTypeTemplateParmDecl(nullptr, SourceLocation(), |
| 531 | SourceLocation(), 0, 0, nullptr, |
| 532 | QualType(), false, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | NonTypeTemplateParmDecl * |
| 536 | NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 537 | unsigned NumExpandedTypes) { |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 538 | auto *NTTP = |
| 539 | new (C, ID, additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>( |
| 540 | NumExpandedTypes)) |
| 541 | NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(), |
| 542 | 0, 0, nullptr, QualType(), nullptr, None, |
| 543 | None); |
| 544 | NTTP->NumExpandedTypes = NumExpandedTypes; |
| 545 | return NTTP; |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 546 | } |
| 547 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 548 | SourceRange NonTypeTemplateParmDecl::getSourceRange() const { |
Abramo Bagnara | e15d553 | 2011-03-04 11:03:48 +0000 | [diff] [blame] | 549 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 550 | return SourceRange(getOuterLocStart(), |
| 551 | getDefaultArgument()->getSourceRange().getEnd()); |
| 552 | return DeclaratorDecl::getSourceRange(); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 555 | SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 556 | return hasDefaultArgument() |
| 557 | ? getDefaultArgument()->getSourceRange().getBegin() |
| 558 | : SourceLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 561 | //===----------------------------------------------------------------------===// |
| 562 | // TemplateTemplateParmDecl Method Implementations |
| 563 | //===----------------------------------------------------------------------===// |
| 564 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 565 | void TemplateTemplateParmDecl::anchor() { } |
| 566 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 567 | TemplateTemplateParmDecl::TemplateTemplateParmDecl( |
| 568 | DeclContext *DC, SourceLocation L, unsigned D, unsigned P, |
| 569 | IdentifierInfo *Id, TemplateParameterList *Params, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 570 | ArrayRef<TemplateParameterList *> Expansions) |
| 571 | : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params), |
| 572 | TemplateParmPosition(D, P), ParameterPack(true), |
| 573 | ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) { |
| 574 | if (!Expansions.empty()) |
| 575 | std::uninitialized_copy(Expansions.begin(), Expansions.end(), |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 576 | getTrailingObjects<TemplateParameterList *>()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 579 | TemplateTemplateParmDecl * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 580 | TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 581 | SourceLocation L, unsigned D, unsigned P, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 582 | bool ParameterPack, IdentifierInfo *Id, |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 583 | TemplateParameterList *Params) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 584 | return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id, |
| 585 | Params); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 588 | TemplateTemplateParmDecl * |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 589 | TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
| 590 | SourceLocation L, unsigned D, unsigned P, |
| 591 | IdentifierInfo *Id, |
| 592 | TemplateParameterList *Params, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 593 | ArrayRef<TemplateParameterList *> Expansions) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 594 | return new (C, DC, |
| 595 | additionalSizeToAlloc<TemplateParameterList *>(Expansions.size())) |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 596 | TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | TemplateTemplateParmDecl * |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 600 | TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 601 | return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, |
| 602 | false, nullptr, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 605 | TemplateTemplateParmDecl * |
| 606 | TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 607 | unsigned NumExpansions) { |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 608 | auto *TTP = |
| 609 | new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions)) |
| 610 | TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr, |
| 611 | nullptr, None); |
| 612 | TTP->NumExpandedParams = NumExpansions; |
| 613 | return TTP; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 616 | SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const { |
| 617 | return hasDefaultArgument() ? getDefaultArgument().getLocation() |
| 618 | : SourceLocation(); |
| 619 | } |
| 620 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 621 | void TemplateTemplateParmDecl::setDefaultArgument( |
| 622 | const ASTContext &C, const TemplateArgumentLoc &DefArg) { |
| 623 | if (DefArg.getArgument().isNull()) |
| 624 | DefaultArgument.set(nullptr); |
| 625 | else |
| 626 | DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg)); |
| 627 | } |
| 628 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 629 | //===----------------------------------------------------------------------===// |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 630 | // TemplateArgumentList Implementation |
| 631 | //===----------------------------------------------------------------------===// |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 632 | TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args) |
| 633 | : Arguments(getTrailingObjects<TemplateArgument>()), |
| 634 | NumArguments(Args.size()) { |
| 635 | std::uninitialized_copy(Args.begin(), Args.end(), |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 636 | getTrailingObjects<TemplateArgument>()); |
| 637 | } |
| 638 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 639 | TemplateArgumentList * |
| 640 | TemplateArgumentList::CreateCopy(ASTContext &Context, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 641 | ArrayRef<TemplateArgument> Args) { |
| 642 | void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size())); |
| 643 | return new (Mem) TemplateArgumentList(Args); |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 646 | FunctionTemplateSpecializationInfo * |
| 647 | FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD, |
| 648 | FunctionTemplateDecl *Template, |
| 649 | TemplateSpecializationKind TSK, |
| 650 | const TemplateArgumentList *TemplateArgs, |
| 651 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 652 | SourceLocation POI) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 653 | const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr; |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 654 | if (TemplateArgsAsWritten) |
| 655 | ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C, |
| 656 | *TemplateArgsAsWritten); |
| 657 | |
| 658 | return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK, |
| 659 | TemplateArgs, |
| 660 | ArgsAsWritten, |
| 661 | POI); |
| 662 | } |
| 663 | |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 664 | //===----------------------------------------------------------------------===// |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 665 | // TemplateDecl Implementation |
| 666 | //===----------------------------------------------------------------------===// |
| 667 | |
| 668 | void TemplateDecl::anchor() { } |
| 669 | |
| 670 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 671 | // ClassTemplateSpecializationDecl Implementation |
| 672 | //===----------------------------------------------------------------------===// |
| 673 | ClassTemplateSpecializationDecl:: |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 674 | ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 675 | DeclContext *DC, SourceLocation StartLoc, |
| 676 | SourceLocation IdLoc, |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 677 | ClassTemplateDecl *SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 678 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 679 | ClassTemplateSpecializationDecl *PrevDecl) |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 680 | : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc, |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 681 | SpecializedTemplate->getIdentifier(), |
| 682 | PrevDecl), |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 683 | SpecializedTemplate(SpecializedTemplate), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 684 | ExplicitInfo(nullptr), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 685 | TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)), |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 686 | SpecializationKind(TSK_Undeclared) { |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 687 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 689 | ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C, |
| 690 | Kind DK) |
| 691 | : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(), |
| 692 | SourceLocation(), nullptr, nullptr), |
| 693 | ExplicitInfo(nullptr), SpecializationKind(TSK_Undeclared) {} |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 695 | ClassTemplateSpecializationDecl * |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 696 | ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 697 | DeclContext *DC, |
| 698 | SourceLocation StartLoc, |
| 699 | SourceLocation IdLoc, |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 700 | ClassTemplateDecl *SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 701 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 702 | ClassTemplateSpecializationDecl *PrevDecl) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 703 | ClassTemplateSpecializationDecl *Result = |
| 704 | new (Context, DC) ClassTemplateSpecializationDecl( |
| 705 | Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 706 | SpecializedTemplate, Args, PrevDecl); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 707 | Result->MayHaveOutOfDateDef = false; |
| 708 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 709 | Context.getTypeDeclType(Result, PrevDecl); |
| 710 | return Result; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 711 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 712 | |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 713 | ClassTemplateSpecializationDecl * |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 714 | ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 715 | unsigned ID) { |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 716 | ClassTemplateSpecializationDecl *Result = |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 717 | new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 718 | Result->MayHaveOutOfDateDef = false; |
| 719 | return Result; |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 722 | void ClassTemplateSpecializationDecl::getNameForDiagnostic( |
| 723 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 724 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 725 | |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 726 | auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this); |
| 727 | if (const ASTTemplateArgumentListInfo *ArgsAsWritten = |
| 728 | PS ? PS->getTemplateArgsAsWritten() : nullptr) { |
| 729 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 730 | OS, ArgsAsWritten->arguments(), Policy); |
| 731 | } else { |
| 732 | const TemplateArgumentList &TemplateArgs = getTemplateArgs(); |
| 733 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 734 | OS, TemplateArgs.asArray(), Policy); |
| 735 | } |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Douglas Gregor | 9dc8bd3 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 738 | ClassTemplateDecl * |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | ClassTemplateSpecializationDecl::getSpecializedTemplate() const { |
| 740 | if (SpecializedPartialSpecialization *PartialSpec |
Douglas Gregor | 9dc8bd3 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 741 | = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) |
| 742 | return PartialSpec->PartialSpecialization->getSpecializedTemplate(); |
| 743 | return SpecializedTemplate.get<ClassTemplateDecl*>(); |
| 744 | } |
| 745 | |
Abramo Bagnara | a093526 | 2011-03-04 14:20:30 +0000 | [diff] [blame] | 746 | SourceRange |
| 747 | ClassTemplateSpecializationDecl::getSourceRange() const { |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 748 | if (ExplicitInfo) { |
Abramo Bagnara | c76dcbd | 2012-10-15 21:06:42 +0000 | [diff] [blame] | 749 | SourceLocation Begin = getTemplateKeywordLoc(); |
| 750 | if (Begin.isValid()) { |
| 751 | // Here we have an explicit (partial) specialization or instantiation. |
| 752 | assert(getSpecializationKind() == TSK_ExplicitSpecialization || |
| 753 | getSpecializationKind() == TSK_ExplicitInstantiationDeclaration || |
| 754 | getSpecializationKind() == TSK_ExplicitInstantiationDefinition); |
| 755 | if (getExternLoc().isValid()) |
| 756 | Begin = getExternLoc(); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 757 | SourceLocation End = getBraceRange().getEnd(); |
Abramo Bagnara | c76dcbd | 2012-10-15 21:06:42 +0000 | [diff] [blame] | 758 | if (End.isInvalid()) |
| 759 | End = getTypeAsWritten()->getTypeLoc().getEndLoc(); |
| 760 | return SourceRange(Begin, End); |
| 761 | } |
| 762 | // An implicit instantiation of a class template partial specialization |
| 763 | // uses ExplicitInfo to record the TypeAsWritten, but the source |
| 764 | // locations should be retrieved from the instantiation pattern. |
| 765 | typedef ClassTemplatePartialSpecializationDecl CTPSDecl; |
| 766 | CTPSDecl *ctpsd = const_cast<CTPSDecl*>(cast<CTPSDecl>(this)); |
| 767 | CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 768 | assert(inst_from != nullptr); |
Abramo Bagnara | c76dcbd | 2012-10-15 21:06:42 +0000 | [diff] [blame] | 769 | return inst_from->getSourceRange(); |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 770 | } |
| 771 | else { |
| 772 | // No explicit info available. |
| 773 | llvm::PointerUnion<ClassTemplateDecl *, |
| 774 | ClassTemplatePartialSpecializationDecl *> |
| 775 | inst_from = getInstantiatedFrom(); |
| 776 | if (inst_from.isNull()) |
| 777 | return getSpecializedTemplate()->getSourceRange(); |
| 778 | if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>()) |
| 779 | return ctd->getSourceRange(); |
| 780 | return inst_from.get<ClassTemplatePartialSpecializationDecl*>() |
| 781 | ->getSourceRange(); |
| 782 | } |
Abramo Bagnara | a093526 | 2011-03-04 14:20:30 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 785 | //===----------------------------------------------------------------------===// |
| 786 | // ClassTemplatePartialSpecializationDecl Implementation |
| 787 | //===----------------------------------------------------------------------===// |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 788 | void ClassTemplatePartialSpecializationDecl::anchor() { } |
| 789 | |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 790 | ClassTemplatePartialSpecializationDecl:: |
| 791 | ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 792 | DeclContext *DC, |
| 793 | SourceLocation StartLoc, |
| 794 | SourceLocation IdLoc, |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 795 | TemplateParameterList *Params, |
| 796 | ClassTemplateDecl *SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 797 | ArrayRef<TemplateArgument> Args, |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 798 | const ASTTemplateArgumentListInfo *ArgInfos, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 799 | ClassTemplatePartialSpecializationDecl *PrevDecl) |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 800 | : ClassTemplateSpecializationDecl(Context, |
| 801 | ClassTemplatePartialSpecialization, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 802 | TK, DC, StartLoc, IdLoc, |
| 803 | SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 804 | Args, PrevDecl), |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 805 | TemplateParams(Params), ArgsAsWritten(ArgInfos), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 806 | InstantiatedFromMember(nullptr, false) |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 807 | { |
Douglas Gregor | 3c41bf7 | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 808 | AdoptTemplateParameterList(Params, this); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 811 | ClassTemplatePartialSpecializationDecl * |
| 812 | ClassTemplatePartialSpecializationDecl:: |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 813 | Create(ASTContext &Context, TagKind TK,DeclContext *DC, |
| 814 | SourceLocation StartLoc, SourceLocation IdLoc, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 815 | TemplateParameterList *Params, |
| 816 | ClassTemplateDecl *SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 817 | ArrayRef<TemplateArgument> Args, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 818 | const TemplateArgumentListInfo &ArgInfos, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 819 | QualType CanonInjectedType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 820 | ClassTemplatePartialSpecializationDecl *PrevDecl) { |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 821 | const ASTTemplateArgumentListInfo *ASTArgInfos = |
| 822 | ASTTemplateArgumentListInfo::Create(Context, ArgInfos); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 823 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 824 | ClassTemplatePartialSpecializationDecl *Result = new (Context, DC) |
| 825 | ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc, |
| 826 | Params, SpecializedTemplate, Args, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 827 | ASTArgInfos, PrevDecl); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 828 | Result->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 829 | Result->MayHaveOutOfDateDef = false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 830 | |
| 831 | Context.getInjectedClassNameType(Result, CanonInjectedType); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 832 | return Result; |
| 833 | } |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 834 | |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 835 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 836 | ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C, |
| 837 | unsigned ID) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 838 | ClassTemplatePartialSpecializationDecl *Result = |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 839 | new (C, ID) ClassTemplatePartialSpecializationDecl(C); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 840 | Result->MayHaveOutOfDateDef = false; |
| 841 | return Result; |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 842 | } |
| 843 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 844 | //===----------------------------------------------------------------------===// |
| 845 | // FriendTemplateDecl Implementation |
| 846 | //===----------------------------------------------------------------------===// |
| 847 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 848 | void FriendTemplateDecl::anchor() { } |
| 849 | |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 850 | FriendTemplateDecl * |
| 851 | FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC, |
| 852 | SourceLocation L, |
| 853 | MutableArrayRef<TemplateParameterList *> Params, |
| 854 | FriendUnion Friend, SourceLocation FLoc) { |
| 855 | return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc); |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 856 | } |
Argyrios Kyrtzidis | 165b581 | 2010-07-22 16:04:10 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 858 | FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C, |
| 859 | unsigned ID) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 860 | return new (C, ID) FriendTemplateDecl(EmptyShell()); |
Argyrios Kyrtzidis | 165b581 | 2010-07-22 16:04:10 +0000 | [diff] [blame] | 861 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 862 | |
| 863 | //===----------------------------------------------------------------------===// |
| 864 | // TypeAliasTemplateDecl Implementation |
| 865 | //===----------------------------------------------------------------------===// |
| 866 | |
| 867 | TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C, |
| 868 | DeclContext *DC, |
| 869 | SourceLocation L, |
| 870 | DeclarationName Name, |
| 871 | TemplateParameterList *Params, |
| 872 | NamedDecl *Decl) { |
| 873 | AdoptTemplateParameterList(Params, DC); |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 874 | return new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 877 | TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C, |
| 878 | unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 879 | return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 880 | DeclarationName(), nullptr, nullptr); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 883 | RedeclarableTemplateDecl::CommonBase * |
Dmitri Gribenko | b53f37c | 2013-01-23 16:52:57 +0000 | [diff] [blame] | 884 | TypeAliasTemplateDecl::newCommon(ASTContext &C) const { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 885 | Common *CommonPtr = new (C) Common; |
George Burgess IV | b61bfbd | 2017-02-14 05:37:36 +0000 | [diff] [blame^] | 886 | C.addDestruction(CommonPtr); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 887 | return CommonPtr; |
| 888 | } |
| 889 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 890 | //===----------------------------------------------------------------------===// |
| 891 | // ClassScopeFunctionSpecializationDecl Implementation |
| 892 | //===----------------------------------------------------------------------===// |
| 893 | |
| 894 | void ClassScopeFunctionSpecializationDecl::anchor() { } |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 895 | |
| 896 | ClassScopeFunctionSpecializationDecl * |
| 897 | ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C, |
| 898 | unsigned ID) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 899 | return new (C, ID) ClassScopeFunctionSpecializationDecl( |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 900 | nullptr, SourceLocation(), nullptr, false, TemplateArgumentListInfo()); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 901 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 902 | |
| 903 | //===----------------------------------------------------------------------===// |
| 904 | // VarTemplateDecl Implementation |
| 905 | //===----------------------------------------------------------------------===// |
| 906 | |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 907 | VarTemplateDecl *VarTemplateDecl::getDefinition() { |
| 908 | VarTemplateDecl *CurD = this; |
| 909 | while (CurD) { |
| 910 | if (CurD->isThisDeclarationADefinition()) |
| 911 | return CurD; |
| 912 | CurD = CurD->getPreviousDecl(); |
| 913 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 914 | return nullptr; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 917 | VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC, |
| 918 | SourceLocation L, DeclarationName Name, |
| 919 | TemplateParameterList *Params, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 920 | VarDecl *Decl) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 921 | return new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C, |
| 925 | unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 926 | return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(), |
| 927 | DeclarationName(), nullptr, nullptr); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 930 | // TODO: Unify across class, function and variable templates? |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 931 | // May require moving this and Common to RedeclarableTemplateDecl. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 932 | void VarTemplateDecl::LoadLazySpecializations() const { |
Richard Smith | e3536dd | 2015-02-24 02:44:23 +0000 | [diff] [blame] | 933 | // Grab the most recent declaration to ensure we've loaded any lazy |
| 934 | // redeclarations of this template. |
| 935 | // |
| 936 | // FIXME: Avoid walking the entire redeclaration chain here. |
| 937 | Common *CommonPtr = getMostRecentDecl()->getCommonPtr(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 938 | if (CommonPtr->LazySpecializations) { |
| 939 | ASTContext &Context = getASTContext(); |
| 940 | uint32_t *Specs = CommonPtr->LazySpecializations; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 941 | CommonPtr->LazySpecializations = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 942 | for (uint32_t I = 0, N = *Specs++; I != N; ++I) |
| 943 | (void)Context.getExternalSource()->GetExternalDecl(Specs[I]); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | llvm::FoldingSetVector<VarTemplateSpecializationDecl> & |
| 948 | VarTemplateDecl::getSpecializations() const { |
| 949 | LoadLazySpecializations(); |
| 950 | return getCommonPtr()->Specializations; |
| 951 | } |
| 952 | |
| 953 | llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & |
| 954 | VarTemplateDecl::getPartialSpecializations() { |
| 955 | LoadLazySpecializations(); |
| 956 | return getCommonPtr()->PartialSpecializations; |
| 957 | } |
| 958 | |
| 959 | RedeclarableTemplateDecl::CommonBase * |
| 960 | VarTemplateDecl::newCommon(ASTContext &C) const { |
| 961 | Common *CommonPtr = new (C) Common; |
George Burgess IV | b61bfbd | 2017-02-14 05:37:36 +0000 | [diff] [blame^] | 962 | C.addDestruction(CommonPtr); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 963 | return CommonPtr; |
| 964 | } |
| 965 | |
| 966 | VarTemplateSpecializationDecl * |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 967 | VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, |
| 968 | void *&InsertPos) { |
| 969 | return findSpecializationImpl(getSpecializations(), Args, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D, |
| 973 | void *InsertPos) { |
Richard Smith | e977e51 | 2015-02-24 01:23:23 +0000 | [diff] [blame] | 974 | addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | VarTemplatePartialSpecializationDecl * |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 978 | VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args, |
| 979 | void *&InsertPos) { |
| 980 | return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | void VarTemplateDecl::AddPartialSpecialization( |
| 984 | VarTemplatePartialSpecializationDecl *D, void *InsertPos) { |
| 985 | if (InsertPos) |
| 986 | getPartialSpecializations().InsertNode(D, InsertPos); |
| 987 | else { |
| 988 | VarTemplatePartialSpecializationDecl *Existing = |
| 989 | getPartialSpecializations().GetOrInsertNode(D); |
| 990 | (void)Existing; |
| 991 | assert(Existing->isCanonicalDecl() && "Non-canonical specialization?"); |
| 992 | } |
| 993 | |
| 994 | if (ASTMutationListener *L = getASTMutationListener()) |
| 995 | L->AddedCXXTemplateSpecialization(this, D); |
| 996 | } |
| 997 | |
| 998 | void VarTemplateDecl::getPartialSpecializations( |
| 999 | SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) { |
| 1000 | llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs = |
| 1001 | getPartialSpecializations(); |
| 1002 | PS.clear(); |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 1003 | PS.reserve(PartialSpecs.size()); |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 1004 | for (VarTemplatePartialSpecializationDecl &P : PartialSpecs) |
| 1005 | PS.push_back(P.getMostRecentDecl()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | VarTemplatePartialSpecializationDecl * |
| 1009 | VarTemplateDecl::findPartialSpecInstantiatedFromMember( |
| 1010 | VarTemplatePartialSpecializationDecl *D) { |
| 1011 | Decl *DCanon = D->getCanonicalDecl(); |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 1012 | for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) { |
| 1013 | if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon) |
| 1014 | return P.getMostRecentDecl(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1017 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | //===----------------------------------------------------------------------===// |
| 1021 | // VarTemplateSpecializationDecl Implementation |
| 1022 | //===----------------------------------------------------------------------===// |
| 1023 | VarTemplateSpecializationDecl::VarTemplateSpecializationDecl( |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1024 | Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1025 | SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1026 | TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1027 | : VarDecl(DK, Context, DC, StartLoc, IdLoc, |
| 1028 | SpecializedTemplate->getIdentifier(), T, TInfo, S), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1029 | SpecializedTemplate(SpecializedTemplate), ExplicitInfo(nullptr), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1030 | TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1031 | SpecializationKind(TSK_Undeclared) {} |
| 1032 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1033 | VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK, |
| 1034 | ASTContext &C) |
| 1035 | : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1036 | QualType(), nullptr, SC_None), |
| 1037 | ExplicitInfo(nullptr), SpecializationKind(TSK_Undeclared) {} |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1038 | |
| 1039 | VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create( |
| 1040 | ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
| 1041 | SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1042 | TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 1043 | return new (Context, DC) VarTemplateSpecializationDecl( |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1044 | VarTemplateSpecialization, Context, DC, StartLoc, IdLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1045 | SpecializedTemplate, T, TInfo, S, Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | VarTemplateSpecializationDecl * |
| 1049 | VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1050 | return new (C, ID) |
| 1051 | VarTemplateSpecializationDecl(VarTemplateSpecialization, C); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | void VarTemplateSpecializationDecl::getNameForDiagnostic( |
| 1055 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 1056 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
| 1057 | |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 1058 | auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this); |
| 1059 | if (const ASTTemplateArgumentListInfo *ArgsAsWritten = |
| 1060 | PS ? PS->getTemplateArgsAsWritten() : nullptr) { |
| 1061 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 1062 | OS, ArgsAsWritten->arguments(), Policy); |
| 1063 | } else { |
| 1064 | const TemplateArgumentList &TemplateArgs = getTemplateArgs(); |
| 1065 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 1066 | OS, TemplateArgs.asArray(), Policy); |
| 1067 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const { |
| 1071 | if (SpecializedPartialSpecialization *PartialSpec = |
| 1072 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
| 1073 | return PartialSpec->PartialSpecialization->getSpecializedTemplate(); |
| 1074 | return SpecializedTemplate.get<VarTemplateDecl *>(); |
| 1075 | } |
| 1076 | |
| 1077 | void VarTemplateSpecializationDecl::setTemplateArgsInfo( |
| 1078 | const TemplateArgumentListInfo &ArgsInfo) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1079 | TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc()); |
| 1080 | TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc()); |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 1081 | for (const TemplateArgumentLoc &Loc : ArgsInfo.arguments()) |
| 1082 | TemplateArgsInfo.addArgument(Loc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | //===----------------------------------------------------------------------===// |
| 1086 | // VarTemplatePartialSpecializationDecl Implementation |
| 1087 | //===----------------------------------------------------------------------===// |
| 1088 | void VarTemplatePartialSpecializationDecl::anchor() {} |
| 1089 | |
| 1090 | VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl( |
| 1091 | ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
| 1092 | SourceLocation IdLoc, TemplateParameterList *Params, |
| 1093 | VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1094 | StorageClass S, ArrayRef<TemplateArgument> Args, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 1095 | const ASTTemplateArgumentListInfo *ArgInfos) |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1096 | : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1097 | DC, StartLoc, IdLoc, SpecializedTemplate, T, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1098 | TInfo, S, Args), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1099 | TemplateParams(Params), ArgsAsWritten(ArgInfos), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1100 | InstantiatedFromMember(nullptr, false) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1101 | // TODO: The template parameters should be in DC by now. Verify. |
| 1102 | // AdoptTemplateParameterList(Params, DC); |
| 1103 | } |
| 1104 | |
| 1105 | VarTemplatePartialSpecializationDecl * |
| 1106 | VarTemplatePartialSpecializationDecl::Create( |
| 1107 | ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
| 1108 | SourceLocation IdLoc, TemplateParameterList *Params, |
| 1109 | VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1110 | StorageClass S, ArrayRef<TemplateArgument> Args, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 1111 | const TemplateArgumentListInfo &ArgInfos) { |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 1112 | const ASTTemplateArgumentListInfo *ASTArgInfos |
| 1113 | = ASTTemplateArgumentListInfo::Create(Context, ArgInfos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1114 | |
| 1115 | VarTemplatePartialSpecializationDecl *Result = |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 1116 | new (Context, DC) VarTemplatePartialSpecializationDecl( |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1117 | Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1118 | S, Args, ASTArgInfos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1119 | Result->setSpecializationKind(TSK_ExplicitSpecialization); |
| 1120 | return Result; |
| 1121 | } |
| 1122 | |
| 1123 | VarTemplatePartialSpecializationDecl * |
| 1124 | VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C, |
| 1125 | unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1126 | return new (C, ID) VarTemplatePartialSpecializationDecl(C); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1127 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1128 | |
| 1129 | static TemplateParameterList * |
| 1130 | createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) { |
| 1131 | // typename T |
| 1132 | auto *T = TemplateTypeParmDecl::Create( |
| 1133 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0, |
| 1134 | /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false); |
| 1135 | T->setImplicit(true); |
| 1136 | |
| 1137 | // T ...Ints |
| 1138 | TypeSourceInfo *TI = |
| 1139 | C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0)); |
| 1140 | auto *N = NonTypeTemplateParmDecl::Create( |
| 1141 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1, |
| 1142 | /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI); |
| 1143 | N->setImplicit(true); |
| 1144 | |
| 1145 | // <typename T, T ...Ints> |
| 1146 | NamedDecl *P[2] = {T, N}; |
| 1147 | auto *TPL = TemplateParameterList::Create( |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 1148 | C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1149 | |
| 1150 | // template <typename T, ...Ints> class IntSeq |
| 1151 | auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create( |
| 1152 | C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0, |
| 1153 | /*ParameterPack=*/false, /*Id=*/nullptr, TPL); |
| 1154 | TemplateTemplateParm->setImplicit(true); |
| 1155 | |
| 1156 | // typename T |
| 1157 | auto *TemplateTypeParm = TemplateTypeParmDecl::Create( |
| 1158 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1, |
| 1159 | /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false); |
| 1160 | TemplateTypeParm->setImplicit(true); |
| 1161 | |
| 1162 | // T N |
| 1163 | TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo( |
| 1164 | QualType(TemplateTypeParm->getTypeForDecl(), 0)); |
| 1165 | auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create( |
| 1166 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2, |
| 1167 | /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo); |
| 1168 | NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm, |
| 1169 | NonTypeTemplateParm}; |
| 1170 | |
| 1171 | // template <template <typename T, T ...Ints> class IntSeq, typename T, T N> |
| 1172 | return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 1173 | Params, SourceLocation(), nullptr); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 1176 | static TemplateParameterList * |
| 1177 | createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) { |
| 1178 | // std::size_t Index |
| 1179 | TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType()); |
| 1180 | auto *Index = NonTypeTemplateParmDecl::Create( |
| 1181 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0, |
| 1182 | /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo); |
| 1183 | |
| 1184 | // typename ...T |
| 1185 | auto *Ts = TemplateTypeParmDecl::Create( |
| 1186 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1, |
| 1187 | /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true); |
| 1188 | Ts->setImplicit(true); |
| 1189 | |
| 1190 | // template <std::size_t Index, typename ...T> |
| 1191 | NamedDecl *Params[] = {Index, Ts}; |
| 1192 | return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(), |
| 1193 | llvm::makeArrayRef(Params), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 1194 | SourceLocation(), nullptr); |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1197 | static TemplateParameterList *createBuiltinTemplateParameterList( |
| 1198 | const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) { |
| 1199 | switch (BTK) { |
| 1200 | case BTK__make_integer_seq: |
| 1201 | return createMakeIntegerSeqParameterList(C, DC); |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 1202 | case BTK__type_pack_element: |
| 1203 | return createTypePackElementParameterList(C, DC); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | llvm_unreachable("unhandled BuiltinTemplateKind!"); |
| 1207 | } |
| 1208 | |
| 1209 | void BuiltinTemplateDecl::anchor() {} |
| 1210 | |
| 1211 | BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC, |
| 1212 | DeclarationName Name, |
| 1213 | BuiltinTemplateKind BTK) |
| 1214 | : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name, |
| 1215 | createBuiltinTemplateParameterList(C, DC, BTK)), |
| 1216 | BTK(BTK) {} |