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