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