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