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