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