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