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), |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 464 | &TemplateArgs[0], |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 465 | TemplateArgs.size()); |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 466 | return CommonPtr->InjectedClassNameType; |
| 467 | } |
| 468 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 469 | //===----------------------------------------------------------------------===// |
| 470 | // TemplateTypeParm Allocation/Deallocation Method Implementations |
| 471 | //===----------------------------------------------------------------------===// |
| 472 | |
| 473 | TemplateTypeParmDecl * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 474 | TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 475 | SourceLocation KeyLoc, SourceLocation NameLoc, |
| 476 | unsigned D, unsigned P, IdentifierInfo *Id, |
| 477 | bool Typename, bool ParameterPack) { |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 478 | TemplateTypeParmDecl *TTPDecl = |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 479 | new (C, DC) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 480 | QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl); |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 481 | TTPDecl->setTypeForDecl(TTPType.getTypePtr()); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 482 | return TTPDecl; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 485 | TemplateTypeParmDecl * |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 486 | TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 487 | return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(), |
| 488 | SourceLocation(), nullptr, false); |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 489 | } |
| 490 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 491 | SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const { |
Abramo Bagnara | 23485e0 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 492 | return hasDefaultArgument() |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 493 | ? getDefaultArgumentInfo()->getTypeLoc().getBeginLoc() |
| 494 | : SourceLocation(); |
Abramo Bagnara | 23485e0 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | SourceRange TemplateTypeParmDecl::getSourceRange() const { |
| 498 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 499 | return SourceRange(getLocStart(), |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 500 | getDefaultArgumentInfo()->getTypeLoc().getEndLoc()); |
Abramo Bagnara | 23485e0 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 501 | else |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 502 | return TypeDecl::getSourceRange(); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 505 | unsigned TemplateTypeParmDecl::getDepth() const { |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 506 | return getTypeForDecl()->getAs<TemplateTypeParmType>()->getDepth(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | unsigned TemplateTypeParmDecl::getIndex() const { |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 510 | return getTypeForDecl()->getAs<TemplateTypeParmType>()->getIndex(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 513 | bool TemplateTypeParmDecl::isParameterPack() const { |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 514 | return getTypeForDecl()->getAs<TemplateTypeParmType>()->isParameterPack(); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 517 | //===----------------------------------------------------------------------===// |
| 518 | // NonTypeTemplateParmDecl Method Implementations |
| 519 | //===----------------------------------------------------------------------===// |
| 520 | |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 521 | NonTypeTemplateParmDecl::NonTypeTemplateParmDecl( |
| 522 | DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D, |
| 523 | unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| 524 | ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos) |
| 525 | : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc), |
| 526 | TemplateParmPosition(D, P), ParameterPack(true), |
| 527 | ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) { |
| 528 | if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 529 | auto TypesAndInfos = |
| 530 | getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 531 | for (unsigned I = 0; I != NumExpandedTypes; ++I) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 532 | new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]); |
| 533 | TypesAndInfos[I].second = ExpandedTInfos[I]; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 538 | NonTypeTemplateParmDecl * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 539 | NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 540 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 541 | unsigned D, unsigned P, IdentifierInfo *Id, |
| 542 | QualType T, bool ParameterPack, |
| 543 | TypeSourceInfo *TInfo) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 544 | return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, |
| 545 | T, ParameterPack, TInfo); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 546 | } |
| 547 | |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 548 | NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create( |
| 549 | const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 550 | SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id, |
| 551 | QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes, |
| 552 | ArrayRef<TypeSourceInfo *> ExpandedTInfos) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 553 | return new (C, DC, |
| 554 | additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>( |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 555 | ExpandedTypes.size())) |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 556 | NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 557 | ExpandedTypes, ExpandedTInfos); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 560 | NonTypeTemplateParmDecl * |
| 561 | NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 562 | return new (C, ID) NonTypeTemplateParmDecl(nullptr, SourceLocation(), |
| 563 | SourceLocation(), 0, 0, nullptr, |
| 564 | QualType(), false, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | NonTypeTemplateParmDecl * |
| 568 | NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 569 | unsigned NumExpandedTypes) { |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 570 | auto *NTTP = |
| 571 | new (C, ID, additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>( |
| 572 | NumExpandedTypes)) |
| 573 | NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(), |
| 574 | 0, 0, nullptr, QualType(), nullptr, None, |
| 575 | None); |
| 576 | NTTP->NumExpandedTypes = NumExpandedTypes; |
| 577 | return NTTP; |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 578 | } |
| 579 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 580 | SourceRange NonTypeTemplateParmDecl::getSourceRange() const { |
Abramo Bagnara | e15d553 | 2011-03-04 11:03:48 +0000 | [diff] [blame] | 581 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 582 | return SourceRange(getOuterLocStart(), |
| 583 | getDefaultArgument()->getSourceRange().getEnd()); |
| 584 | return DeclaratorDecl::getSourceRange(); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 587 | SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 588 | return hasDefaultArgument() |
| 589 | ? getDefaultArgument()->getSourceRange().getBegin() |
| 590 | : SourceLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 593 | //===----------------------------------------------------------------------===// |
| 594 | // TemplateTemplateParmDecl Method Implementations |
| 595 | //===----------------------------------------------------------------------===// |
| 596 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 597 | void TemplateTemplateParmDecl::anchor() { } |
| 598 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 599 | TemplateTemplateParmDecl::TemplateTemplateParmDecl( |
| 600 | DeclContext *DC, SourceLocation L, unsigned D, unsigned P, |
| 601 | IdentifierInfo *Id, TemplateParameterList *Params, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 602 | ArrayRef<TemplateParameterList *> Expansions) |
| 603 | : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params), |
| 604 | TemplateParmPosition(D, P), ParameterPack(true), |
| 605 | ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) { |
| 606 | if (!Expansions.empty()) |
| 607 | std::uninitialized_copy(Expansions.begin(), Expansions.end(), |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 608 | getTrailingObjects<TemplateParameterList *>()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 611 | TemplateTemplateParmDecl * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 612 | TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 613 | SourceLocation L, unsigned D, unsigned P, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 614 | bool ParameterPack, IdentifierInfo *Id, |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 615 | TemplateParameterList *Params) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 616 | return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id, |
| 617 | Params); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 620 | TemplateTemplateParmDecl * |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 621 | TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
| 622 | SourceLocation L, unsigned D, unsigned P, |
| 623 | IdentifierInfo *Id, |
| 624 | TemplateParameterList *Params, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 625 | ArrayRef<TemplateParameterList *> Expansions) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 626 | return new (C, DC, |
| 627 | additionalSizeToAlloc<TemplateParameterList *>(Expansions.size())) |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 628 | TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | TemplateTemplateParmDecl * |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 632 | TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 633 | return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, |
| 634 | false, nullptr, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 637 | TemplateTemplateParmDecl * |
| 638 | TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 639 | unsigned NumExpansions) { |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 640 | auto *TTP = |
| 641 | new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions)) |
| 642 | TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr, |
| 643 | nullptr, None); |
| 644 | TTP->NumExpandedParams = NumExpansions; |
| 645 | return TTP; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 648 | SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const { |
| 649 | return hasDefaultArgument() ? getDefaultArgument().getLocation() |
| 650 | : SourceLocation(); |
| 651 | } |
| 652 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 653 | void TemplateTemplateParmDecl::setDefaultArgument( |
| 654 | const ASTContext &C, const TemplateArgumentLoc &DefArg) { |
| 655 | if (DefArg.getArgument().isNull()) |
| 656 | DefaultArgument.set(nullptr); |
| 657 | else |
| 658 | DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg)); |
| 659 | } |
| 660 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 661 | //===----------------------------------------------------------------------===// |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 662 | // TemplateArgumentList Implementation |
| 663 | //===----------------------------------------------------------------------===// |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 664 | TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args) |
| 665 | : Arguments(getTrailingObjects<TemplateArgument>()), |
| 666 | NumArguments(Args.size()) { |
| 667 | std::uninitialized_copy(Args.begin(), Args.end(), |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 668 | getTrailingObjects<TemplateArgument>()); |
| 669 | } |
| 670 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 671 | TemplateArgumentList * |
| 672 | TemplateArgumentList::CreateCopy(ASTContext &Context, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 673 | ArrayRef<TemplateArgument> Args) { |
| 674 | void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size())); |
| 675 | return new (Mem) TemplateArgumentList(Args); |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 678 | FunctionTemplateSpecializationInfo * |
| 679 | FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD, |
| 680 | FunctionTemplateDecl *Template, |
| 681 | TemplateSpecializationKind TSK, |
| 682 | const TemplateArgumentList *TemplateArgs, |
| 683 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 684 | SourceLocation POI) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 685 | const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr; |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 686 | if (TemplateArgsAsWritten) |
| 687 | ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C, |
| 688 | *TemplateArgsAsWritten); |
| 689 | |
| 690 | return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK, |
| 691 | TemplateArgs, |
| 692 | ArgsAsWritten, |
| 693 | POI); |
| 694 | } |
| 695 | |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 696 | //===----------------------------------------------------------------------===// |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 697 | // TemplateDecl Implementation |
| 698 | //===----------------------------------------------------------------------===// |
| 699 | |
| 700 | void TemplateDecl::anchor() { } |
| 701 | |
| 702 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 703 | // ClassTemplateSpecializationDecl Implementation |
| 704 | //===----------------------------------------------------------------------===// |
| 705 | ClassTemplateSpecializationDecl:: |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 706 | ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 707 | DeclContext *DC, SourceLocation StartLoc, |
| 708 | SourceLocation IdLoc, |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 709 | ClassTemplateDecl *SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 710 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 711 | ClassTemplateSpecializationDecl *PrevDecl) |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 712 | : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc, |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 713 | SpecializedTemplate->getIdentifier(), |
| 714 | PrevDecl), |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 715 | SpecializedTemplate(SpecializedTemplate), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 716 | ExplicitInfo(nullptr), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 717 | TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)), |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 718 | SpecializationKind(TSK_Undeclared) { |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 719 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 721 | ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C, |
| 722 | Kind DK) |
| 723 | : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(), |
| 724 | SourceLocation(), nullptr, nullptr), |
| 725 | ExplicitInfo(nullptr), SpecializationKind(TSK_Undeclared) {} |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 727 | ClassTemplateSpecializationDecl * |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 728 | ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 729 | DeclContext *DC, |
| 730 | SourceLocation StartLoc, |
| 731 | SourceLocation IdLoc, |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 732 | ClassTemplateDecl *SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 733 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 734 | ClassTemplateSpecializationDecl *PrevDecl) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 735 | ClassTemplateSpecializationDecl *Result = |
| 736 | new (Context, DC) ClassTemplateSpecializationDecl( |
| 737 | Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 738 | SpecializedTemplate, Args, PrevDecl); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 739 | Result->MayHaveOutOfDateDef = false; |
| 740 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 741 | Context.getTypeDeclType(Result, PrevDecl); |
| 742 | return Result; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 743 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 744 | |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 745 | ClassTemplateSpecializationDecl * |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 746 | ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 747 | unsigned ID) { |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 748 | ClassTemplateSpecializationDecl *Result = |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 749 | new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 750 | Result->MayHaveOutOfDateDef = false; |
| 751 | return Result; |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 754 | void ClassTemplateSpecializationDecl::getNameForDiagnostic( |
| 755 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 756 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 757 | |
| 758 | const TemplateArgumentList &TemplateArgs = getTemplateArgs(); |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 759 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 760 | OS, TemplateArgs.data(), TemplateArgs.size(), Policy); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Douglas Gregor | 9dc8bd3 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 763 | ClassTemplateDecl * |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | ClassTemplateSpecializationDecl::getSpecializedTemplate() const { |
| 765 | if (SpecializedPartialSpecialization *PartialSpec |
Douglas Gregor | 9dc8bd3 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 766 | = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) |
| 767 | return PartialSpec->PartialSpecialization->getSpecializedTemplate(); |
| 768 | return SpecializedTemplate.get<ClassTemplateDecl*>(); |
| 769 | } |
| 770 | |
Abramo Bagnara | a093526 | 2011-03-04 14:20:30 +0000 | [diff] [blame] | 771 | SourceRange |
| 772 | ClassTemplateSpecializationDecl::getSourceRange() const { |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 773 | if (ExplicitInfo) { |
Abramo Bagnara | c76dcbd | 2012-10-15 21:06:42 +0000 | [diff] [blame] | 774 | SourceLocation Begin = getTemplateKeywordLoc(); |
| 775 | if (Begin.isValid()) { |
| 776 | // Here we have an explicit (partial) specialization or instantiation. |
| 777 | assert(getSpecializationKind() == TSK_ExplicitSpecialization || |
| 778 | getSpecializationKind() == TSK_ExplicitInstantiationDeclaration || |
| 779 | getSpecializationKind() == TSK_ExplicitInstantiationDefinition); |
| 780 | if (getExternLoc().isValid()) |
| 781 | Begin = getExternLoc(); |
| 782 | SourceLocation End = getRBraceLoc(); |
| 783 | if (End.isInvalid()) |
| 784 | End = getTypeAsWritten()->getTypeLoc().getEndLoc(); |
| 785 | return SourceRange(Begin, End); |
| 786 | } |
| 787 | // An implicit instantiation of a class template partial specialization |
| 788 | // uses ExplicitInfo to record the TypeAsWritten, but the source |
| 789 | // locations should be retrieved from the instantiation pattern. |
| 790 | typedef ClassTemplatePartialSpecializationDecl CTPSDecl; |
| 791 | CTPSDecl *ctpsd = const_cast<CTPSDecl*>(cast<CTPSDecl>(this)); |
| 792 | CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 793 | assert(inst_from != nullptr); |
Abramo Bagnara | c76dcbd | 2012-10-15 21:06:42 +0000 | [diff] [blame] | 794 | return inst_from->getSourceRange(); |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 795 | } |
| 796 | else { |
| 797 | // No explicit info available. |
| 798 | llvm::PointerUnion<ClassTemplateDecl *, |
| 799 | ClassTemplatePartialSpecializationDecl *> |
| 800 | inst_from = getInstantiatedFrom(); |
| 801 | if (inst_from.isNull()) |
| 802 | return getSpecializedTemplate()->getSourceRange(); |
| 803 | if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>()) |
| 804 | return ctd->getSourceRange(); |
| 805 | return inst_from.get<ClassTemplatePartialSpecializationDecl*>() |
| 806 | ->getSourceRange(); |
| 807 | } |
Abramo Bagnara | a093526 | 2011-03-04 14:20:30 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 810 | //===----------------------------------------------------------------------===// |
| 811 | // ClassTemplatePartialSpecializationDecl Implementation |
| 812 | //===----------------------------------------------------------------------===// |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 813 | void ClassTemplatePartialSpecializationDecl::anchor() { } |
| 814 | |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 815 | ClassTemplatePartialSpecializationDecl:: |
| 816 | ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 817 | DeclContext *DC, |
| 818 | SourceLocation StartLoc, |
| 819 | SourceLocation IdLoc, |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 820 | TemplateParameterList *Params, |
| 821 | ClassTemplateDecl *SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 822 | ArrayRef<TemplateArgument> Args, |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 823 | const ASTTemplateArgumentListInfo *ArgInfos, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 824 | ClassTemplatePartialSpecializationDecl *PrevDecl) |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 825 | : ClassTemplateSpecializationDecl(Context, |
| 826 | ClassTemplatePartialSpecialization, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 827 | TK, DC, StartLoc, IdLoc, |
| 828 | SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 829 | Args, PrevDecl), |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 830 | TemplateParams(Params), ArgsAsWritten(ArgInfos), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 831 | InstantiatedFromMember(nullptr, false) |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 832 | { |
Douglas Gregor | 3c41bf7 | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 833 | AdoptTemplateParameterList(Params, this); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 836 | ClassTemplatePartialSpecializationDecl * |
| 837 | ClassTemplatePartialSpecializationDecl:: |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 838 | Create(ASTContext &Context, TagKind TK,DeclContext *DC, |
| 839 | SourceLocation StartLoc, SourceLocation IdLoc, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 840 | TemplateParameterList *Params, |
| 841 | ClassTemplateDecl *SpecializedTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 842 | ArrayRef<TemplateArgument> Args, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 843 | const TemplateArgumentListInfo &ArgInfos, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 844 | QualType CanonInjectedType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 845 | ClassTemplatePartialSpecializationDecl *PrevDecl) { |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 846 | const ASTTemplateArgumentListInfo *ASTArgInfos = |
| 847 | ASTTemplateArgumentListInfo::Create(Context, ArgInfos); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 848 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 849 | ClassTemplatePartialSpecializationDecl *Result = new (Context, DC) |
| 850 | ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc, |
| 851 | Params, SpecializedTemplate, Args, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 852 | ASTArgInfos, PrevDecl); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 853 | Result->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 854 | Result->MayHaveOutOfDateDef = false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 855 | |
| 856 | Context.getInjectedClassNameType(Result, CanonInjectedType); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 857 | return Result; |
| 858 | } |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 859 | |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 860 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 861 | ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C, |
| 862 | unsigned ID) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 863 | ClassTemplatePartialSpecializationDecl *Result = |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 864 | new (C, ID) ClassTemplatePartialSpecializationDecl(C); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 865 | Result->MayHaveOutOfDateDef = false; |
| 866 | return Result; |
Argyrios Kyrtzidis | fe6ba88 | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 867 | } |
| 868 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 869 | //===----------------------------------------------------------------------===// |
| 870 | // FriendTemplateDecl Implementation |
| 871 | //===----------------------------------------------------------------------===// |
| 872 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 873 | void FriendTemplateDecl::anchor() { } |
| 874 | |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 875 | FriendTemplateDecl * |
| 876 | FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC, |
| 877 | SourceLocation L, |
| 878 | MutableArrayRef<TemplateParameterList *> Params, |
| 879 | FriendUnion Friend, SourceLocation FLoc) { |
| 880 | return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc); |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 881 | } |
Argyrios Kyrtzidis | 165b581 | 2010-07-22 16:04:10 +0000 | [diff] [blame] | 882 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 883 | FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C, |
| 884 | unsigned ID) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 885 | return new (C, ID) FriendTemplateDecl(EmptyShell()); |
Argyrios Kyrtzidis | 165b581 | 2010-07-22 16:04:10 +0000 | [diff] [blame] | 886 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 887 | |
| 888 | //===----------------------------------------------------------------------===// |
| 889 | // TypeAliasTemplateDecl Implementation |
| 890 | //===----------------------------------------------------------------------===// |
| 891 | |
| 892 | TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C, |
| 893 | DeclContext *DC, |
| 894 | SourceLocation L, |
| 895 | DeclarationName Name, |
| 896 | TemplateParameterList *Params, |
| 897 | NamedDecl *Decl) { |
| 898 | AdoptTemplateParameterList(Params, DC); |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 899 | return new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 902 | TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C, |
| 903 | unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 904 | return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 905 | DeclarationName(), nullptr, nullptr); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) { |
| 909 | static_cast<Common *>(Ptr)->~Common(); |
| 910 | } |
| 911 | RedeclarableTemplateDecl::CommonBase * |
Dmitri Gribenko | b53f37c | 2013-01-23 16:52:57 +0000 | [diff] [blame] | 912 | TypeAliasTemplateDecl::newCommon(ASTContext &C) const { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 913 | Common *CommonPtr = new (C) Common; |
| 914 | C.AddDeallocation(DeallocateCommon, CommonPtr); |
| 915 | return CommonPtr; |
| 916 | } |
| 917 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 918 | //===----------------------------------------------------------------------===// |
| 919 | // ClassScopeFunctionSpecializationDecl Implementation |
| 920 | //===----------------------------------------------------------------------===// |
| 921 | |
| 922 | void ClassScopeFunctionSpecializationDecl::anchor() { } |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 923 | |
| 924 | ClassScopeFunctionSpecializationDecl * |
| 925 | ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C, |
| 926 | unsigned ID) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 927 | return new (C, ID) ClassScopeFunctionSpecializationDecl( |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 928 | nullptr, SourceLocation(), nullptr, false, TemplateArgumentListInfo()); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 929 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 930 | |
| 931 | //===----------------------------------------------------------------------===// |
| 932 | // VarTemplateDecl Implementation |
| 933 | //===----------------------------------------------------------------------===// |
| 934 | |
| 935 | void VarTemplateDecl::DeallocateCommon(void *Ptr) { |
| 936 | static_cast<Common *>(Ptr)->~Common(); |
| 937 | } |
| 938 | |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 939 | VarTemplateDecl *VarTemplateDecl::getDefinition() { |
| 940 | VarTemplateDecl *CurD = this; |
| 941 | while (CurD) { |
| 942 | if (CurD->isThisDeclarationADefinition()) |
| 943 | return CurD; |
| 944 | CurD = CurD->getPreviousDecl(); |
| 945 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 946 | return nullptr; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 949 | VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC, |
| 950 | SourceLocation L, DeclarationName Name, |
| 951 | TemplateParameterList *Params, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 952 | VarDecl *Decl) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 953 | return new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C, |
| 957 | unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 958 | return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(), |
| 959 | DeclarationName(), nullptr, nullptr); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 962 | // TODO: Unify across class, function and variable templates? |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 963 | // May require moving this and Common to RedeclarableTemplateDecl. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 964 | void VarTemplateDecl::LoadLazySpecializations() const { |
Richard Smith | e3536dd | 2015-02-24 02:44:23 +0000 | [diff] [blame] | 965 | // Grab the most recent declaration to ensure we've loaded any lazy |
| 966 | // redeclarations of this template. |
| 967 | // |
| 968 | // FIXME: Avoid walking the entire redeclaration chain here. |
| 969 | Common *CommonPtr = getMostRecentDecl()->getCommonPtr(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 970 | if (CommonPtr->LazySpecializations) { |
| 971 | ASTContext &Context = getASTContext(); |
| 972 | uint32_t *Specs = CommonPtr->LazySpecializations; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 973 | CommonPtr->LazySpecializations = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 974 | for (uint32_t I = 0, N = *Specs++; I != N; ++I) |
| 975 | (void)Context.getExternalSource()->GetExternalDecl(Specs[I]); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | llvm::FoldingSetVector<VarTemplateSpecializationDecl> & |
| 980 | VarTemplateDecl::getSpecializations() const { |
| 981 | LoadLazySpecializations(); |
| 982 | return getCommonPtr()->Specializations; |
| 983 | } |
| 984 | |
| 985 | llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & |
| 986 | VarTemplateDecl::getPartialSpecializations() { |
| 987 | LoadLazySpecializations(); |
| 988 | return getCommonPtr()->PartialSpecializations; |
| 989 | } |
| 990 | |
| 991 | RedeclarableTemplateDecl::CommonBase * |
| 992 | VarTemplateDecl::newCommon(ASTContext &C) const { |
| 993 | Common *CommonPtr = new (C) Common; |
| 994 | C.AddDeallocation(DeallocateCommon, CommonPtr); |
| 995 | return CommonPtr; |
| 996 | } |
| 997 | |
| 998 | VarTemplateSpecializationDecl * |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 999 | VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, |
| 1000 | void *&InsertPos) { |
| 1001 | return findSpecializationImpl(getSpecializations(), Args, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D, |
| 1005 | void *InsertPos) { |
Richard Smith | e977e51 | 2015-02-24 01:23:23 +0000 | [diff] [blame] | 1006 | addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | VarTemplatePartialSpecializationDecl * |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1010 | VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args, |
| 1011 | void *&InsertPos) { |
| 1012 | return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | void VarTemplateDecl::AddPartialSpecialization( |
| 1016 | VarTemplatePartialSpecializationDecl *D, void *InsertPos) { |
| 1017 | if (InsertPos) |
| 1018 | getPartialSpecializations().InsertNode(D, InsertPos); |
| 1019 | else { |
| 1020 | VarTemplatePartialSpecializationDecl *Existing = |
| 1021 | getPartialSpecializations().GetOrInsertNode(D); |
| 1022 | (void)Existing; |
| 1023 | assert(Existing->isCanonicalDecl() && "Non-canonical specialization?"); |
| 1024 | } |
| 1025 | |
| 1026 | if (ASTMutationListener *L = getASTMutationListener()) |
| 1027 | L->AddedCXXTemplateSpecialization(this, D); |
| 1028 | } |
| 1029 | |
| 1030 | void VarTemplateDecl::getPartialSpecializations( |
| 1031 | SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) { |
| 1032 | llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs = |
| 1033 | getPartialSpecializations(); |
| 1034 | PS.clear(); |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 1035 | PS.reserve(PartialSpecs.size()); |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 1036 | for (VarTemplatePartialSpecializationDecl &P : PartialSpecs) |
| 1037 | PS.push_back(P.getMostRecentDecl()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | VarTemplatePartialSpecializationDecl * |
| 1041 | VarTemplateDecl::findPartialSpecInstantiatedFromMember( |
| 1042 | VarTemplatePartialSpecializationDecl *D) { |
| 1043 | Decl *DCanon = D->getCanonicalDecl(); |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 1044 | for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) { |
| 1045 | if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon) |
| 1046 | return P.getMostRecentDecl(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1049 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | //===----------------------------------------------------------------------===// |
| 1053 | // VarTemplateSpecializationDecl Implementation |
| 1054 | //===----------------------------------------------------------------------===// |
| 1055 | VarTemplateSpecializationDecl::VarTemplateSpecializationDecl( |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1056 | Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1057 | SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1058 | TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1059 | : VarDecl(DK, Context, DC, StartLoc, IdLoc, |
| 1060 | SpecializedTemplate->getIdentifier(), T, TInfo, S), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1061 | SpecializedTemplate(SpecializedTemplate), ExplicitInfo(nullptr), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1062 | TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1063 | SpecializationKind(TSK_Undeclared) {} |
| 1064 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1065 | VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK, |
| 1066 | ASTContext &C) |
| 1067 | : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1068 | QualType(), nullptr, SC_None), |
| 1069 | ExplicitInfo(nullptr), SpecializationKind(TSK_Undeclared) {} |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1070 | |
| 1071 | VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create( |
| 1072 | ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
| 1073 | SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1074 | TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 1075 | return new (Context, DC) VarTemplateSpecializationDecl( |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1076 | VarTemplateSpecialization, Context, DC, StartLoc, IdLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1077 | SpecializedTemplate, T, TInfo, S, Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | VarTemplateSpecializationDecl * |
| 1081 | VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1082 | return new (C, ID) |
| 1083 | VarTemplateSpecializationDecl(VarTemplateSpecialization, C); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | void VarTemplateSpecializationDecl::getNameForDiagnostic( |
| 1087 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 1088 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
| 1089 | |
| 1090 | const TemplateArgumentList &TemplateArgs = getTemplateArgs(); |
| 1091 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 1092 | OS, TemplateArgs.data(), TemplateArgs.size(), Policy); |
| 1093 | } |
| 1094 | |
| 1095 | VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const { |
| 1096 | if (SpecializedPartialSpecialization *PartialSpec = |
| 1097 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
| 1098 | return PartialSpec->PartialSpecialization->getSpecializedTemplate(); |
| 1099 | return SpecializedTemplate.get<VarTemplateDecl *>(); |
| 1100 | } |
| 1101 | |
| 1102 | void VarTemplateSpecializationDecl::setTemplateArgsInfo( |
| 1103 | const TemplateArgumentListInfo &ArgsInfo) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1104 | TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc()); |
| 1105 | TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc()); |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 1106 | for (const TemplateArgumentLoc &Loc : ArgsInfo.arguments()) |
| 1107 | TemplateArgsInfo.addArgument(Loc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | //===----------------------------------------------------------------------===// |
| 1111 | // VarTemplatePartialSpecializationDecl Implementation |
| 1112 | //===----------------------------------------------------------------------===// |
| 1113 | void VarTemplatePartialSpecializationDecl::anchor() {} |
| 1114 | |
| 1115 | VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl( |
| 1116 | ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
| 1117 | SourceLocation IdLoc, TemplateParameterList *Params, |
| 1118 | VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1119 | StorageClass S, ArrayRef<TemplateArgument> Args, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 1120 | const ASTTemplateArgumentListInfo *ArgInfos) |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1121 | : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1122 | DC, StartLoc, IdLoc, SpecializedTemplate, T, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1123 | TInfo, S, Args), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1124 | TemplateParams(Params), ArgsAsWritten(ArgInfos), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1125 | InstantiatedFromMember(nullptr, false) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1126 | // TODO: The template parameters should be in DC by now. Verify. |
| 1127 | // AdoptTemplateParameterList(Params, DC); |
| 1128 | } |
| 1129 | |
| 1130 | VarTemplatePartialSpecializationDecl * |
| 1131 | VarTemplatePartialSpecializationDecl::Create( |
| 1132 | ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
| 1133 | SourceLocation IdLoc, TemplateParameterList *Params, |
| 1134 | VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1135 | StorageClass S, ArrayRef<TemplateArgument> Args, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 1136 | const TemplateArgumentListInfo &ArgInfos) { |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 1137 | const ASTTemplateArgumentListInfo *ASTArgInfos |
| 1138 | = ASTTemplateArgumentListInfo::Create(Context, ArgInfos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1139 | |
| 1140 | VarTemplatePartialSpecializationDecl *Result = |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 1141 | new (Context, DC) VarTemplatePartialSpecializationDecl( |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1142 | Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1143 | S, Args, ASTArgInfos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1144 | Result->setSpecializationKind(TSK_ExplicitSpecialization); |
| 1145 | return Result; |
| 1146 | } |
| 1147 | |
| 1148 | VarTemplatePartialSpecializationDecl * |
| 1149 | VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C, |
| 1150 | unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1151 | return new (C, ID) VarTemplatePartialSpecializationDecl(C); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1152 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1153 | |
| 1154 | static TemplateParameterList * |
| 1155 | createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) { |
| 1156 | // typename T |
| 1157 | auto *T = TemplateTypeParmDecl::Create( |
| 1158 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0, |
| 1159 | /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false); |
| 1160 | T->setImplicit(true); |
| 1161 | |
| 1162 | // T ...Ints |
| 1163 | TypeSourceInfo *TI = |
| 1164 | C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0)); |
| 1165 | auto *N = NonTypeTemplateParmDecl::Create( |
| 1166 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1, |
| 1167 | /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI); |
| 1168 | N->setImplicit(true); |
| 1169 | |
| 1170 | // <typename T, T ...Ints> |
| 1171 | NamedDecl *P[2] = {T, N}; |
| 1172 | auto *TPL = TemplateParameterList::Create( |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1173 | C, SourceLocation(), SourceLocation(), P, SourceLocation()); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1174 | |
| 1175 | // template <typename T, ...Ints> class IntSeq |
| 1176 | auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create( |
| 1177 | C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0, |
| 1178 | /*ParameterPack=*/false, /*Id=*/nullptr, TPL); |
| 1179 | TemplateTemplateParm->setImplicit(true); |
| 1180 | |
| 1181 | // typename T |
| 1182 | auto *TemplateTypeParm = TemplateTypeParmDecl::Create( |
| 1183 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1, |
| 1184 | /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false); |
| 1185 | TemplateTypeParm->setImplicit(true); |
| 1186 | |
| 1187 | // T N |
| 1188 | TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo( |
| 1189 | QualType(TemplateTypeParm->getTypeForDecl(), 0)); |
| 1190 | auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create( |
| 1191 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2, |
| 1192 | /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo); |
| 1193 | NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm, |
| 1194 | NonTypeTemplateParm}; |
| 1195 | |
| 1196 | // template <template <typename T, T ...Ints> class IntSeq, typename T, T N> |
| 1197 | return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1198 | Params, SourceLocation()); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 1201 | static TemplateParameterList * |
| 1202 | createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) { |
| 1203 | // std::size_t Index |
| 1204 | TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType()); |
| 1205 | auto *Index = NonTypeTemplateParmDecl::Create( |
| 1206 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0, |
| 1207 | /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo); |
| 1208 | |
| 1209 | // typename ...T |
| 1210 | auto *Ts = TemplateTypeParmDecl::Create( |
| 1211 | C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1, |
| 1212 | /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true); |
| 1213 | Ts->setImplicit(true); |
| 1214 | |
| 1215 | // template <std::size_t Index, typename ...T> |
| 1216 | NamedDecl *Params[] = {Index, Ts}; |
| 1217 | return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(), |
| 1218 | llvm::makeArrayRef(Params), |
| 1219 | SourceLocation()); |
| 1220 | } |
| 1221 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1222 | static TemplateParameterList *createBuiltinTemplateParameterList( |
| 1223 | const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) { |
| 1224 | switch (BTK) { |
| 1225 | case BTK__make_integer_seq: |
| 1226 | return createMakeIntegerSeqParameterList(C, DC); |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 1227 | case BTK__type_pack_element: |
| 1228 | return createTypePackElementParameterList(C, DC); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | llvm_unreachable("unhandled BuiltinTemplateKind!"); |
| 1232 | } |
| 1233 | |
| 1234 | void BuiltinTemplateDecl::anchor() {} |
| 1235 | |
| 1236 | BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC, |
| 1237 | DeclarationName Name, |
| 1238 | BuiltinTemplateKind BTK) |
| 1239 | : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name, |
| 1240 | createBuiltinTemplateParameterList(C, DC, BTK)), |
| 1241 | BTK(BTK) {} |