Sebastian Redl | 06a59bb | 2009-10-23 22:13:42 +0000 | [diff] [blame] | 1 | //===--- DeclTemplate.cpp - Template Declaration AST Node Implementation --===// |
Douglas Gregor | aaba5e3 | 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 | |
| 14 | #include "clang/AST/DeclCXX.h" |
| 15 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Douglas Gregor | b95cc97 | 2011-01-04 02:33:52 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Argyrios Kyrtzidis | bef1a7b | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 23 | #include <memory> |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // TemplateParameterList Implementation |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 30 | TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc, |
| 31 | SourceLocation LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 32 | NamedDecl **Params, unsigned NumParams, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 33 | SourceLocation RAngleLoc) |
| 34 | : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc), |
| 35 | NumParams(NumParams) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 36 | for (unsigned Idx = 0; Idx < NumParams; ++Idx) |
| 37 | begin()[Idx] = Params[Idx]; |
| 38 | } |
| 39 | |
| 40 | TemplateParameterList * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 41 | TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 42 | SourceLocation LAngleLoc, NamedDecl **Params, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 43 | unsigned NumParams, SourceLocation RAngleLoc) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 44 | unsigned Size = sizeof(TemplateParameterList) |
| 45 | + sizeof(NamedDecl *) * NumParams; |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 46 | unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment; |
| 47 | void *Mem = C.Allocate(Size, Align); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 49 | NumParams, RAngleLoc); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 52 | unsigned TemplateParameterList::getMinRequiredArguments() const { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 53 | unsigned NumRequiredArgs = 0; |
| 54 | for (iterator P = const_cast<TemplateParameterList *>(this)->begin(), |
| 55 | PEnd = const_cast<TemplateParameterList *>(this)->end(); |
| 56 | P != PEnd; ++P) { |
| 57 | if ((*P)->isTemplateParameterPack()) { |
| 58 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) |
| 59 | if (NTTP->isExpandedParameterPack()) { |
| 60 | NumRequiredArgs += NTTP->getNumExpansionTypes(); |
| 61 | continue; |
| 62 | } |
| 63 | |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 64 | break; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 68 | if (TTP->hasDefaultArgument()) |
| 69 | break; |
| 70 | } else if (NonTypeTemplateParmDecl *NTTP |
| 71 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 72 | if (NTTP->hasDefaultArgument()) |
| 73 | break; |
| 74 | } else if (cast<TemplateTemplateParmDecl>(*P)->hasDefaultArgument()) |
| 75 | break; |
| 76 | |
| 77 | ++NumRequiredArgs; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 78 | } |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 80 | return NumRequiredArgs; |
| 81 | } |
| 82 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 83 | unsigned TemplateParameterList::getDepth() const { |
| 84 | if (size() == 0) |
| 85 | return 0; |
| 86 | |
| 87 | const NamedDecl *FirstParm = getParam(0); |
| 88 | if (const TemplateTypeParmDecl *TTP |
| 89 | = dyn_cast<TemplateTypeParmDecl>(FirstParm)) |
| 90 | return TTP->getDepth(); |
| 91 | else if (const NonTypeTemplateParmDecl *NTTP |
| 92 | = dyn_cast<NonTypeTemplateParmDecl>(FirstParm)) |
| 93 | return NTTP->getDepth(); |
| 94 | else |
| 95 | return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth(); |
| 96 | } |
| 97 | |
Douglas Gregor | 787a40d | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 98 | static void AdoptTemplateParameterList(TemplateParameterList *Params, |
| 99 | DeclContext *Owner) { |
| 100 | for (TemplateParameterList::iterator P = Params->begin(), |
| 101 | PEnd = Params->end(); |
| 102 | P != PEnd; ++P) { |
| 103 | (*P)->setDeclContext(Owner); |
| 104 | |
| 105 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*P)) |
| 106 | AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner); |
| 107 | } |
| 108 | } |
| 109 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 110 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 9eabeba | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 111 | // RedeclarableTemplateDecl Implementation |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | |
| 114 | RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() { |
| 115 | // Find the first declaration of this function template. |
| 116 | RedeclarableTemplateDecl *First = getCanonicalDecl(); |
| 117 | |
| 118 | if (First->CommonOrPrev.isNull()) { |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 119 | CommonBase *CommonPtr = First->newCommon(getASTContext()); |
Peter Collingbourne | 9eabeba | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 120 | First->CommonOrPrev = CommonPtr; |
Peter Collingbourne | 8a798a7 | 2010-07-29 16:12:01 +0000 | [diff] [blame] | 121 | CommonPtr->Latest = First; |
Peter Collingbourne | 9eabeba | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 122 | } |
| 123 | return First->CommonOrPrev.get<CommonBase*>(); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | RedeclarableTemplateDecl *RedeclarableTemplateDecl::getCanonicalDeclImpl() { |
| 128 | RedeclarableTemplateDecl *Tmpl = this; |
| 129 | while (Tmpl->getPreviousDeclaration()) |
| 130 | Tmpl = Tmpl->getPreviousDeclaration(); |
| 131 | return Tmpl; |
| 132 | } |
| 133 | |
Peter Collingbourne | 8a798a7 | 2010-07-29 16:12:01 +0000 | [diff] [blame] | 134 | void RedeclarableTemplateDecl::setPreviousDeclarationImpl( |
| 135 | RedeclarableTemplateDecl *Prev) { |
| 136 | if (Prev) { |
| 137 | CommonBase *Common = Prev->getCommonPtr(); |
| 138 | Prev = Common->Latest; |
| 139 | Common->Latest = this; |
| 140 | CommonOrPrev = Prev; |
| 141 | } else { |
| 142 | assert(CommonOrPrev.is<CommonBase*>() && "Cannot reset TemplateDecl Prev"); |
| 143 | } |
| 144 | } |
| 145 | |
Peter Collingbourne | f88718e | 2010-07-29 16:12:09 +0000 | [diff] [blame] | 146 | RedeclarableTemplateDecl *RedeclarableTemplateDecl::getNextRedeclaration() { |
| 147 | if (CommonOrPrev.is<RedeclarableTemplateDecl*>()) |
| 148 | return CommonOrPrev.get<RedeclarableTemplateDecl*>(); |
| 149 | CommonBase *Common = CommonOrPrev.get<CommonBase*>(); |
| 150 | return Common ? Common->Latest : this; |
| 151 | } |
| 152 | |
Peter Collingbourne | 4048590 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 153 | template <class EntryType> |
| 154 | typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType* |
| 155 | RedeclarableTemplateDecl::findSpecializationImpl( |
| 156 | llvm::FoldingSet<EntryType> &Specs, |
| 157 | const TemplateArgument *Args, unsigned NumArgs, |
| 158 | void *&InsertPos) { |
| 159 | typedef SpecEntryTraits<EntryType> SETraits; |
| 160 | llvm::FoldingSetNodeID ID; |
| 161 | EntryType::Profile(ID,Args,NumArgs, getASTContext()); |
| 162 | EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos); |
| 163 | return Entry ? SETraits::getMostRecentDeclaration(Entry) : 0; |
| 164 | } |
| 165 | |
Douglas Gregor | c494f77 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 166 | /// \brief Generate the injected template arguments for the given template |
| 167 | /// parameter list, e.g., for the injected-class-name of a class template. |
| 168 | static void GenerateInjectedTemplateArgs(ASTContext &Context, |
| 169 | TemplateParameterList *Params, |
| 170 | TemplateArgument *Args) { |
| 171 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 172 | ParamEnd = Params->end(); |
| 173 | Param != ParamEnd; ++Param) { |
| 174 | TemplateArgument Arg; |
| 175 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 176 | QualType ArgType = Context.getTypeDeclType(TTP); |
| 177 | if (TTP->isParameterPack()) |
| 178 | ArgType = Context.getPackExpansionType(ArgType, |
| 179 | llvm::Optional<unsigned>()); |
| 180 | |
| 181 | Arg = TemplateArgument(ArgType); |
| 182 | } else if (NonTypeTemplateParmDecl *NTTP = |
| 183 | dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 184 | Expr *E = new (Context) DeclRefExpr(NTTP, |
| 185 | NTTP->getType().getNonLValueExprType(Context), |
| 186 | Expr::getValueKindForType(NTTP->getType()), |
| 187 | NTTP->getLocation()); |
| 188 | |
| 189 | if (NTTP->isParameterPack()) |
| 190 | E = new (Context) PackExpansionExpr(Context.DependentTy, E, |
| 191 | NTTP->getLocation(), |
| 192 | llvm::Optional<unsigned>()); |
| 193 | Arg = TemplateArgument(E); |
| 194 | } else { |
| 195 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param); |
| 196 | if (TTP->isParameterPack()) |
| 197 | Arg = TemplateArgument(TemplateName(TTP), llvm::Optional<unsigned>()); |
| 198 | else |
| 199 | Arg = TemplateArgument(TemplateName(TTP)); |
| 200 | } |
| 201 | |
| 202 | if ((*Param)->isTemplateParameterPack()) |
| 203 | Arg = TemplateArgument::CreatePackCopy(Context, &Arg, 1); |
| 204 | |
| 205 | *Args++ = Arg; |
| 206 | } |
| 207 | } |
| 208 | |
Peter Collingbourne | 9eabeba | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 209 | //===----------------------------------------------------------------------===// |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 210 | // FunctionTemplateDecl Implementation |
| 211 | //===----------------------------------------------------------------------===// |
| 212 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 213 | void FunctionTemplateDecl::DeallocateCommon(void *Ptr) { |
| 214 | static_cast<Common *>(Ptr)->~Common(); |
| 215 | } |
| 216 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 217 | FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C, |
| 218 | DeclContext *DC, |
| 219 | SourceLocation L, |
| 220 | DeclarationName Name, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 221 | TemplateParameterList *Params, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 222 | NamedDecl *Decl) { |
Douglas Gregor | 787a40d | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 223 | AdoptTemplateParameterList(Params, cast<DeclContext>(Decl)); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 224 | return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl); |
| 225 | } |
| 226 | |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 227 | FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C, EmptyShell) { |
| 228 | return new (C) FunctionTemplateDecl(0, SourceLocation(), DeclarationName(), |
| 229 | 0, 0); |
| 230 | } |
| 231 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 232 | RedeclarableTemplateDecl::CommonBase * |
| 233 | FunctionTemplateDecl::newCommon(ASTContext &C) { |
| 234 | Common *CommonPtr = new (C) Common; |
| 235 | C.AddDeallocation(DeallocateCommon, CommonPtr); |
Peter Collingbourne | 9eabeba | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 236 | return CommonPtr; |
| 237 | } |
| 238 | |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 239 | FunctionDecl * |
| 240 | FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args, |
| 241 | unsigned NumArgs, void *&InsertPos) { |
Peter Collingbourne | 4048590 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 242 | return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos); |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Sebastian Redl | 5bbcdbf | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 245 | void FunctionTemplateDecl::addSpecialization( |
| 246 | FunctionTemplateSpecializationInfo *Info, void *InsertPos) { |
| 247 | getSpecializations().InsertNode(Info, InsertPos); |
| 248 | if (ASTMutationListener *L = getASTMutationListener()) |
| 249 | L->AddedCXXTemplateSpecialization(this, Info->Function); |
| 250 | } |
| 251 | |
Douglas Gregor | c494f77 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 252 | std::pair<const TemplateArgument *, unsigned> |
| 253 | FunctionTemplateDecl::getInjectedTemplateArgs() { |
| 254 | TemplateParameterList *Params = getTemplateParameters(); |
| 255 | Common *CommonPtr = getCommonPtr(); |
| 256 | if (!CommonPtr->InjectedArgs) { |
| 257 | CommonPtr->InjectedArgs |
| 258 | = new (getASTContext()) TemplateArgument [Params->size()]; |
| 259 | GenerateInjectedTemplateArgs(getASTContext(), Params, |
| 260 | CommonPtr->InjectedArgs); |
| 261 | } |
| 262 | |
| 263 | return std::make_pair(CommonPtr->InjectedArgs, Params->size()); |
| 264 | } |
| 265 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 266 | //===----------------------------------------------------------------------===// |
| 267 | // ClassTemplateDecl Implementation |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 270 | void ClassTemplateDecl::DeallocateCommon(void *Ptr) { |
| 271 | static_cast<Common *>(Ptr)->~Common(); |
| 272 | } |
| 273 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 274 | ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, |
| 275 | DeclContext *DC, |
| 276 | SourceLocation L, |
| 277 | DeclarationName Name, |
| 278 | TemplateParameterList *Params, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 279 | NamedDecl *Decl, |
| 280 | ClassTemplateDecl *PrevDecl) { |
Douglas Gregor | 787a40d | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 281 | AdoptTemplateParameterList(Params, cast<DeclContext>(Decl)); |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 282 | ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl); |
Argyrios Kyrtzidis | 5bf1bdc | 2010-06-21 10:57:41 +0000 | [diff] [blame] | 283 | New->setPreviousDeclaration(PrevDecl); |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 284 | return New; |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 287 | ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, EmptyShell Empty) { |
| 288 | return new (C) ClassTemplateDecl(Empty); |
| 289 | } |
| 290 | |
Douglas Gregor | c8e5cf8 | 2010-10-27 22:21:36 +0000 | [diff] [blame] | 291 | void ClassTemplateDecl::LoadLazySpecializations() { |
| 292 | Common *CommonPtr = getCommonPtr(); |
| 293 | if (CommonPtr->LazySpecializations) { |
| 294 | ASTContext &Context = getASTContext(); |
| 295 | uint32_t *Specs = CommonPtr->LazySpecializations; |
| 296 | CommonPtr->LazySpecializations = 0; |
| 297 | for (uint32_t I = 0, N = *Specs++; I != N; ++I) |
| 298 | (void)Context.getExternalSource()->GetExternalDecl(Specs[I]); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | llvm::FoldingSet<ClassTemplateSpecializationDecl> & |
| 303 | ClassTemplateDecl::getSpecializations() { |
| 304 | LoadLazySpecializations(); |
| 305 | return getCommonPtr()->Specializations; |
| 306 | } |
| 307 | |
| 308 | llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> & |
| 309 | ClassTemplateDecl::getPartialSpecializations() { |
| 310 | LoadLazySpecializations(); |
| 311 | return getCommonPtr()->PartialSpecializations; |
| 312 | } |
| 313 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 314 | RedeclarableTemplateDecl::CommonBase * |
| 315 | ClassTemplateDecl::newCommon(ASTContext &C) { |
| 316 | Common *CommonPtr = new (C) Common; |
| 317 | C.AddDeallocation(DeallocateCommon, CommonPtr); |
Peter Collingbourne | 9eabeba | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 318 | return CommonPtr; |
| 319 | } |
| 320 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 321 | ClassTemplateSpecializationDecl * |
| 322 | ClassTemplateDecl::findSpecialization(const TemplateArgument *Args, |
| 323 | unsigned NumArgs, void *&InsertPos) { |
Peter Collingbourne | 4048590 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 324 | return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Argyrios Kyrtzidis | bef1a7b | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 327 | void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D, |
| 328 | void *InsertPos) { |
| 329 | getSpecializations().InsertNode(D, InsertPos); |
| 330 | if (ASTMutationListener *L = getASTMutationListener()) |
| 331 | L->AddedCXXTemplateSpecialization(this, D); |
| 332 | } |
| 333 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 334 | ClassTemplatePartialSpecializationDecl * |
| 335 | ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args, |
| 336 | unsigned NumArgs, |
| 337 | void *&InsertPos) { |
Peter Collingbourne | 4048590 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 338 | return findSpecializationImpl(getPartialSpecializations(), Args, NumArgs, |
| 339 | InsertPos); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Argyrios Kyrtzidis | bef1a7b | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 342 | void ClassTemplateDecl::AddPartialSpecialization( |
| 343 | ClassTemplatePartialSpecializationDecl *D, |
| 344 | void *InsertPos) { |
| 345 | getPartialSpecializations().InsertNode(D, InsertPos); |
| 346 | if (ASTMutationListener *L = getASTMutationListener()) |
| 347 | L->AddedCXXTemplateSpecialization(this, D); |
| 348 | } |
| 349 | |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 350 | void ClassTemplateDecl::getPartialSpecializations( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 351 | SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) { |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 352 | llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs |
Argyrios Kyrtzidis | 5bf1bdc | 2010-06-21 10:57:41 +0000 | [diff] [blame] | 353 | = getPartialSpecializations(); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 354 | PS.clear(); |
| 355 | PS.resize(PartialSpecs.size()); |
| 356 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 357 | P = PartialSpecs.begin(), PEnd = PartialSpecs.end(); |
| 358 | P != PEnd; ++P) { |
| 359 | assert(!PS[P->getSequenceNumber()]); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 360 | PS[P->getSequenceNumber()] = P->getMostRecentDeclaration(); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 364 | ClassTemplatePartialSpecializationDecl * |
| 365 | ClassTemplateDecl::findPartialSpecialization(QualType T) { |
| 366 | ASTContext &Context = getASTContext(); |
| 367 | typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 368 | partial_spec_iterator; |
| 369 | for (partial_spec_iterator P = getPartialSpecializations().begin(), |
| 370 | PEnd = getPartialSpecializations().end(); |
| 371 | P != PEnd; ++P) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 372 | if (Context.hasSameType(P->getInjectedSpecializationType(), T)) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 373 | return P->getMostRecentDeclaration(); |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | ClassTemplatePartialSpecializationDecl * |
| 380 | ClassTemplateDecl::findPartialSpecInstantiatedFromMember( |
| 381 | ClassTemplatePartialSpecializationDecl *D) { |
| 382 | Decl *DCanon = D->getCanonicalDecl(); |
| 383 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 384 | P = getPartialSpecializations().begin(), |
| 385 | PEnd = getPartialSpecializations().end(); |
| 386 | P != PEnd; ++P) { |
| 387 | if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon) |
| 388 | return P->getMostRecentDeclaration(); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 389 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 394 | QualType |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 395 | ClassTemplateDecl::getInjectedClassNameSpecialization() { |
Argyrios Kyrtzidis | 5bf1bdc | 2010-06-21 10:57:41 +0000 | [diff] [blame] | 396 | Common *CommonPtr = getCommonPtr(); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 397 | if (!CommonPtr->InjectedClassNameType.isNull()) |
| 398 | return CommonPtr->InjectedClassNameType; |
| 399 | |
Douglas Gregor | b7d09d6 | 2010-12-23 16:00:30 +0000 | [diff] [blame] | 400 | // C++0x [temp.dep.type]p2: |
| 401 | // The template argument list of a primary template is a template argument |
| 402 | // list in which the nth template argument has the value of the nth template |
| 403 | // parameter of the class template. If the nth template parameter is a |
| 404 | // template parameter pack (14.5.3), the nth template argument is a pack |
| 405 | // expansion (14.5.3) whose pattern is the name of the template parameter |
| 406 | // pack. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 407 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 408 | TemplateParameterList *Params = getTemplateParameters(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 409 | SmallVector<TemplateArgument, 16> TemplateArgs; |
Douglas Gregor | c494f77 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 410 | TemplateArgs.resize(Params->size()); |
| 411 | GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data()); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 412 | CommonPtr->InjectedClassNameType |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 413 | = Context.getTemplateSpecializationType(TemplateName(this), |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 414 | &TemplateArgs[0], |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 415 | TemplateArgs.size()); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 416 | return CommonPtr->InjectedClassNameType; |
| 417 | } |
| 418 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 419 | //===----------------------------------------------------------------------===// |
| 420 | // TemplateTypeParm Allocation/Deallocation Method Implementations |
| 421 | //===----------------------------------------------------------------------===// |
| 422 | |
| 423 | TemplateTypeParmDecl * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 424 | TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 425 | SourceLocation KeyLoc, SourceLocation NameLoc, |
| 426 | unsigned D, unsigned P, IdentifierInfo *Id, |
| 427 | bool Typename, bool ParameterPack) { |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 428 | TemplateTypeParmDecl *TTPDecl = |
| 429 | new (C) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename); |
| 430 | QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl); |
| 431 | TTPDecl->TypeForDecl = TTPType.getTypePtr(); |
| 432 | return TTPDecl; |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 435 | TemplateTypeParmDecl * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 436 | TemplateTypeParmDecl::Create(const ASTContext &C, EmptyShell Empty) { |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 437 | return new (C) TemplateTypeParmDecl(0, SourceLocation(), SourceLocation(), |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 438 | 0, false); |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 439 | } |
| 440 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 441 | SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const { |
Abramo Bagnara | 77d4ee2 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 442 | return hasDefaultArgument() |
| 443 | ? DefaultArgument->getTypeLoc().getBeginLoc() |
| 444 | : SourceLocation(); |
| 445 | } |
| 446 | |
| 447 | SourceRange TemplateTypeParmDecl::getSourceRange() const { |
| 448 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 449 | return SourceRange(getLocStart(), |
Abramo Bagnara | 77d4ee2 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 450 | DefaultArgument->getTypeLoc().getEndLoc()); |
| 451 | else |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 452 | return TypeDecl::getSourceRange(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 455 | unsigned TemplateTypeParmDecl::getDepth() const { |
| 456 | return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth(); |
| 457 | } |
| 458 | |
| 459 | unsigned TemplateTypeParmDecl::getIndex() const { |
| 460 | return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex(); |
| 461 | } |
| 462 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 463 | bool TemplateTypeParmDecl::isParameterPack() const { |
| 464 | return TypeForDecl->getAs<TemplateTypeParmType>()->isParameterPack(); |
| 465 | } |
| 466 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 467 | //===----------------------------------------------------------------------===// |
| 468 | // NonTypeTemplateParmDecl Method Implementations |
| 469 | //===----------------------------------------------------------------------===// |
| 470 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 471 | NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 472 | SourceLocation StartLoc, |
| 473 | SourceLocation IdLoc, |
| 474 | unsigned D, unsigned P, |
| 475 | IdentifierInfo *Id, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 476 | QualType T, |
| 477 | TypeSourceInfo *TInfo, |
| 478 | const QualType *ExpandedTypes, |
| 479 | unsigned NumExpandedTypes, |
| 480 | TypeSourceInfo **ExpandedTInfos) |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 481 | : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 482 | TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false), |
| 483 | ParameterPack(true), ExpandedParameterPack(true), |
| 484 | NumExpandedTypes(NumExpandedTypes) |
| 485 | { |
| 486 | if (ExpandedTypes && ExpandedTInfos) { |
| 487 | void **TypesAndInfos = reinterpret_cast<void **>(this + 1); |
| 488 | for (unsigned I = 0; I != NumExpandedTypes; ++I) { |
| 489 | TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr(); |
| 490 | TypesAndInfos[2*I + 1] = ExpandedTInfos[I]; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 495 | NonTypeTemplateParmDecl * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 496 | NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 497 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 498 | unsigned D, unsigned P, IdentifierInfo *Id, |
| 499 | QualType T, bool ParameterPack, |
| 500 | TypeSourceInfo *TInfo) { |
| 501 | return new (C) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, |
| 502 | T, ParameterPack, TInfo); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 505 | NonTypeTemplateParmDecl * |
| 506 | NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 507 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 508 | unsigned D, unsigned P, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 509 | IdentifierInfo *Id, QualType T, |
| 510 | TypeSourceInfo *TInfo, |
| 511 | const QualType *ExpandedTypes, |
| 512 | unsigned NumExpandedTypes, |
| 513 | TypeSourceInfo **ExpandedTInfos) { |
| 514 | unsigned Size = sizeof(NonTypeTemplateParmDecl) |
| 515 | + NumExpandedTypes * 2 * sizeof(void*); |
| 516 | void *Mem = C.Allocate(Size); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 517 | return new (Mem) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, |
| 518 | D, P, Id, T, TInfo, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 519 | ExpandedTypes, NumExpandedTypes, |
| 520 | ExpandedTInfos); |
| 521 | } |
| 522 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 523 | SourceRange NonTypeTemplateParmDecl::getSourceRange() const { |
Abramo Bagnara | ee4bfd4 | 2011-03-04 11:03:48 +0000 | [diff] [blame] | 524 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 525 | return SourceRange(getOuterLocStart(), |
| 526 | getDefaultArgument()->getSourceRange().getEnd()); |
| 527 | return DeclaratorDecl::getSourceRange(); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 530 | SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 531 | return hasDefaultArgument() |
| 532 | ? getDefaultArgument()->getSourceRange().getBegin() |
| 533 | : SourceLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 536 | //===----------------------------------------------------------------------===// |
| 537 | // TemplateTemplateParmDecl Method Implementations |
| 538 | //===----------------------------------------------------------------------===// |
| 539 | |
| 540 | TemplateTemplateParmDecl * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 541 | TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 542 | SourceLocation L, unsigned D, unsigned P, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 543 | bool ParameterPack, IdentifierInfo *Id, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 544 | TemplateParameterList *Params) { |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 545 | return new (C) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id, |
| 546 | Params); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 549 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 550 | // TemplateArgumentList Implementation |
| 551 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 552 | TemplateArgumentList * |
| 553 | TemplateArgumentList::CreateCopy(ASTContext &Context, |
| 554 | const TemplateArgument *Args, |
| 555 | unsigned NumArgs) { |
| 556 | std::size_t Size = sizeof(TemplateArgumentList) |
| 557 | + NumArgs * sizeof(TemplateArgument); |
| 558 | void *Mem = Context.Allocate(Size); |
| 559 | TemplateArgument *StoredArgs |
| 560 | = reinterpret_cast<TemplateArgument *>( |
| 561 | static_cast<TemplateArgumentList *>(Mem) + 1); |
| 562 | std::uninitialized_copy(Args, Args + NumArgs, StoredArgs); |
| 563 | return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true); |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 566 | FunctionTemplateSpecializationInfo * |
| 567 | FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD, |
| 568 | FunctionTemplateDecl *Template, |
| 569 | TemplateSpecializationKind TSK, |
| 570 | const TemplateArgumentList *TemplateArgs, |
| 571 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 572 | SourceLocation POI) { |
| 573 | const ASTTemplateArgumentListInfo *ArgsAsWritten = 0; |
| 574 | if (TemplateArgsAsWritten) |
| 575 | ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C, |
| 576 | *TemplateArgsAsWritten); |
| 577 | |
| 578 | return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK, |
| 579 | TemplateArgs, |
| 580 | ArgsAsWritten, |
| 581 | POI); |
| 582 | } |
| 583 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 584 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 585 | // ClassTemplateSpecializationDecl Implementation |
| 586 | //===----------------------------------------------------------------------===// |
| 587 | ClassTemplateSpecializationDecl:: |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 588 | ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 589 | DeclContext *DC, SourceLocation StartLoc, |
| 590 | SourceLocation IdLoc, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 591 | ClassTemplateDecl *SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 592 | const TemplateArgument *Args, |
| 593 | unsigned NumArgs, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 594 | ClassTemplateSpecializationDecl *PrevDecl) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 595 | : CXXRecordDecl(DK, TK, DC, StartLoc, IdLoc, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 596 | SpecializedTemplate->getIdentifier(), |
| 597 | PrevDecl), |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 598 | SpecializedTemplate(SpecializedTemplate), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 599 | ExplicitInfo(0), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 600 | TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 601 | SpecializationKind(TSK_Undeclared) { |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 602 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 604 | ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 605 | : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0), |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 606 | ExplicitInfo(0), |
| 607 | SpecializationKind(TSK_Undeclared) { |
| 608 | } |
| 609 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 610 | ClassTemplateSpecializationDecl * |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 611 | ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 612 | DeclContext *DC, |
| 613 | SourceLocation StartLoc, |
| 614 | SourceLocation IdLoc, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 615 | ClassTemplateDecl *SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 616 | const TemplateArgument *Args, |
| 617 | unsigned NumArgs, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 618 | ClassTemplateSpecializationDecl *PrevDecl) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 619 | ClassTemplateSpecializationDecl *Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | = new (Context)ClassTemplateSpecializationDecl(Context, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 621 | ClassTemplateSpecialization, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 622 | TK, DC, StartLoc, IdLoc, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 623 | SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 624 | Args, NumArgs, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 625 | PrevDecl); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 626 | Context.getTypeDeclType(Result, PrevDecl); |
| 627 | return Result; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 628 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 629 | |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 630 | ClassTemplateSpecializationDecl * |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 631 | ClassTemplateSpecializationDecl::Create(ASTContext &Context, EmptyShell Empty) { |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 632 | return |
| 633 | new (Context)ClassTemplateSpecializationDecl(ClassTemplateSpecialization); |
| 634 | } |
| 635 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 636 | void |
| 637 | ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S, |
| 638 | const PrintingPolicy &Policy, |
| 639 | bool Qualified) const { |
| 640 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 641 | |
| 642 | const TemplateArgumentList &TemplateArgs = getTemplateArgs(); |
| 643 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 644 | TemplateArgs.data(), |
| 645 | TemplateArgs.size(), |
| 646 | Policy); |
| 647 | } |
| 648 | |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 649 | ClassTemplateDecl * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | ClassTemplateSpecializationDecl::getSpecializedTemplate() const { |
| 651 | if (SpecializedPartialSpecialization *PartialSpec |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 652 | = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) |
| 653 | return PartialSpec->PartialSpecialization->getSpecializedTemplate(); |
| 654 | return SpecializedTemplate.get<ClassTemplateDecl*>(); |
| 655 | } |
| 656 | |
Abramo Bagnara | 4a85a73 | 2011-03-04 14:20:30 +0000 | [diff] [blame] | 657 | SourceRange |
| 658 | ClassTemplateSpecializationDecl::getSourceRange() const { |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 659 | if (ExplicitInfo) { |
| 660 | SourceLocation Begin = getExternLoc(); |
| 661 | if (Begin.isInvalid()) |
| 662 | Begin = getTemplateKeywordLoc(); |
| 663 | SourceLocation End = getRBraceLoc(); |
| 664 | if (End.isInvalid()) |
| 665 | End = getTypeAsWritten()->getTypeLoc().getEndLoc(); |
| 666 | return SourceRange(Begin, End); |
| 667 | } |
| 668 | else { |
| 669 | // No explicit info available. |
| 670 | llvm::PointerUnion<ClassTemplateDecl *, |
| 671 | ClassTemplatePartialSpecializationDecl *> |
| 672 | inst_from = getInstantiatedFrom(); |
| 673 | if (inst_from.isNull()) |
| 674 | return getSpecializedTemplate()->getSourceRange(); |
| 675 | if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>()) |
| 676 | return ctd->getSourceRange(); |
| 677 | return inst_from.get<ClassTemplatePartialSpecializationDecl*>() |
| 678 | ->getSourceRange(); |
| 679 | } |
Abramo Bagnara | 4a85a73 | 2011-03-04 14:20:30 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 682 | //===----------------------------------------------------------------------===// |
| 683 | // ClassTemplatePartialSpecializationDecl Implementation |
| 684 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 685 | ClassTemplatePartialSpecializationDecl:: |
| 686 | ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 687 | DeclContext *DC, |
| 688 | SourceLocation StartLoc, |
| 689 | SourceLocation IdLoc, |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 690 | TemplateParameterList *Params, |
| 691 | ClassTemplateDecl *SpecializedTemplate, |
| 692 | const TemplateArgument *Args, |
| 693 | unsigned NumArgs, |
| 694 | TemplateArgumentLoc *ArgInfos, |
| 695 | unsigned NumArgInfos, |
| 696 | ClassTemplatePartialSpecializationDecl *PrevDecl, |
| 697 | unsigned SequenceNumber) |
| 698 | : ClassTemplateSpecializationDecl(Context, |
| 699 | ClassTemplatePartialSpecialization, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 700 | TK, DC, StartLoc, IdLoc, |
| 701 | SpecializedTemplate, |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 702 | Args, NumArgs, PrevDecl), |
| 703 | TemplateParams(Params), ArgsAsWritten(ArgInfos), |
| 704 | NumArgsAsWritten(NumArgInfos), SequenceNumber(SequenceNumber), |
| 705 | InstantiatedFromMember(0, false) |
| 706 | { |
Douglas Gregor | 787a40d | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 707 | AdoptTemplateParameterList(Params, this); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 710 | ClassTemplatePartialSpecializationDecl * |
| 711 | ClassTemplatePartialSpecializationDecl:: |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 712 | Create(ASTContext &Context, TagKind TK,DeclContext *DC, |
| 713 | SourceLocation StartLoc, SourceLocation IdLoc, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 714 | TemplateParameterList *Params, |
| 715 | ClassTemplateDecl *SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 716 | const TemplateArgument *Args, |
| 717 | unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 718 | const TemplateArgumentListInfo &ArgInfos, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 719 | QualType CanonInjectedType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 720 | ClassTemplatePartialSpecializationDecl *PrevDecl, |
| 721 | unsigned SequenceNumber) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 722 | unsigned N = ArgInfos.size(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 723 | TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N]; |
| 724 | for (unsigned I = 0; I != N; ++I) |
| 725 | ClonedArgs[I] = ArgInfos[I]; |
| 726 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 727 | ClassTemplatePartialSpecializationDecl *Result |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 728 | = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK, DC, |
| 729 | StartLoc, IdLoc, |
| 730 | Params, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 731 | SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 732 | Args, NumArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 733 | ClonedArgs, N, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 734 | PrevDecl, |
| 735 | SequenceNumber); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 736 | Result->setSpecializationKind(TSK_ExplicitSpecialization); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 737 | |
| 738 | Context.getInjectedClassNameType(Result, CanonInjectedType); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 739 | return Result; |
| 740 | } |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 741 | |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 742 | ClassTemplatePartialSpecializationDecl * |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 743 | ClassTemplatePartialSpecializationDecl::Create(ASTContext &Context, |
| 744 | EmptyShell Empty) { |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 745 | return new (Context)ClassTemplatePartialSpecializationDecl(); |
| 746 | } |
| 747 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 748 | //===----------------------------------------------------------------------===// |
| 749 | // FriendTemplateDecl Implementation |
| 750 | //===----------------------------------------------------------------------===// |
| 751 | |
| 752 | FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context, |
| 753 | DeclContext *DC, |
| 754 | SourceLocation L, |
| 755 | unsigned NParams, |
| 756 | TemplateParameterList **Params, |
| 757 | FriendUnion Friend, |
| 758 | SourceLocation FLoc) { |
| 759 | FriendTemplateDecl *Result |
| 760 | = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc); |
| 761 | return Result; |
| 762 | } |
Argyrios Kyrtzidis | 554e6aa | 2010-07-22 16:04:10 +0000 | [diff] [blame] | 763 | |
| 764 | FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context, |
| 765 | EmptyShell Empty) { |
| 766 | return new (Context) FriendTemplateDecl(Empty); |
| 767 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 768 | |
| 769 | //===----------------------------------------------------------------------===// |
| 770 | // TypeAliasTemplateDecl Implementation |
| 771 | //===----------------------------------------------------------------------===// |
| 772 | |
| 773 | TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C, |
| 774 | DeclContext *DC, |
| 775 | SourceLocation L, |
| 776 | DeclarationName Name, |
| 777 | TemplateParameterList *Params, |
| 778 | NamedDecl *Decl) { |
| 779 | AdoptTemplateParameterList(Params, DC); |
| 780 | return new (C) TypeAliasTemplateDecl(DC, L, Name, Params, Decl); |
| 781 | } |
| 782 | |
| 783 | TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C, |
| 784 | EmptyShell) { |
| 785 | return new (C) TypeAliasTemplateDecl(0, SourceLocation(), DeclarationName(), |
| 786 | 0, 0); |
| 787 | } |
| 788 | |
| 789 | void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) { |
| 790 | static_cast<Common *>(Ptr)->~Common(); |
| 791 | } |
| 792 | RedeclarableTemplateDecl::CommonBase * |
| 793 | TypeAliasTemplateDecl::newCommon(ASTContext &C) { |
| 794 | Common *CommonPtr = new (C) Common; |
| 795 | C.AddDeallocation(DeallocateCommon, CommonPtr); |
| 796 | return CommonPtr; |
| 797 | } |
| 798 | |