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