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 | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 227 | FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C, |
| 228 | unsigned ID) { |
| 229 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionTemplateDecl)); |
| 230 | return new (Mem) FunctionTemplateDecl(0, SourceLocation(), DeclarationName(), |
| 231 | 0, 0); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 234 | RedeclarableTemplateDecl::CommonBase * |
| 235 | FunctionTemplateDecl::newCommon(ASTContext &C) { |
| 236 | Common *CommonPtr = new (C) Common; |
| 237 | C.AddDeallocation(DeallocateCommon, CommonPtr); |
Peter Collingbourne | 9eabeba | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 238 | return CommonPtr; |
| 239 | } |
| 240 | |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 241 | FunctionDecl * |
| 242 | FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args, |
| 243 | unsigned NumArgs, void *&InsertPos) { |
Peter Collingbourne | 4048590 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 244 | return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos); |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Sebastian Redl | 5bbcdbf | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 247 | void FunctionTemplateDecl::addSpecialization( |
| 248 | FunctionTemplateSpecializationInfo *Info, void *InsertPos) { |
| 249 | getSpecializations().InsertNode(Info, InsertPos); |
| 250 | if (ASTMutationListener *L = getASTMutationListener()) |
| 251 | L->AddedCXXTemplateSpecialization(this, Info->Function); |
| 252 | } |
| 253 | |
Douglas Gregor | c494f77 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 254 | std::pair<const TemplateArgument *, unsigned> |
| 255 | FunctionTemplateDecl::getInjectedTemplateArgs() { |
| 256 | TemplateParameterList *Params = getTemplateParameters(); |
| 257 | Common *CommonPtr = getCommonPtr(); |
| 258 | if (!CommonPtr->InjectedArgs) { |
| 259 | CommonPtr->InjectedArgs |
| 260 | = new (getASTContext()) TemplateArgument [Params->size()]; |
| 261 | GenerateInjectedTemplateArgs(getASTContext(), Params, |
| 262 | CommonPtr->InjectedArgs); |
| 263 | } |
| 264 | |
| 265 | return std::make_pair(CommonPtr->InjectedArgs, Params->size()); |
| 266 | } |
| 267 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
| 269 | // ClassTemplateDecl Implementation |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 272 | void ClassTemplateDecl::DeallocateCommon(void *Ptr) { |
| 273 | static_cast<Common *>(Ptr)->~Common(); |
| 274 | } |
| 275 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 276 | ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, |
| 277 | DeclContext *DC, |
| 278 | SourceLocation L, |
| 279 | DeclarationName Name, |
| 280 | TemplateParameterList *Params, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 281 | NamedDecl *Decl, |
| 282 | ClassTemplateDecl *PrevDecl) { |
Douglas Gregor | 787a40d | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 283 | AdoptTemplateParameterList(Params, cast<DeclContext>(Decl)); |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 284 | ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl); |
Argyrios Kyrtzidis | 5bf1bdc | 2010-06-21 10:57:41 +0000 | [diff] [blame] | 285 | New->setPreviousDeclaration(PrevDecl); |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 286 | return New; |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 289 | ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C, |
| 290 | unsigned ID) { |
| 291 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ClassTemplateDecl)); |
| 292 | return new (Mem) ClassTemplateDecl(EmptyShell()); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Douglas Gregor | c8e5cf8 | 2010-10-27 22:21:36 +0000 | [diff] [blame] | 295 | void ClassTemplateDecl::LoadLazySpecializations() { |
| 296 | Common *CommonPtr = getCommonPtr(); |
| 297 | if (CommonPtr->LazySpecializations) { |
| 298 | ASTContext &Context = getASTContext(); |
| 299 | uint32_t *Specs = CommonPtr->LazySpecializations; |
| 300 | CommonPtr->LazySpecializations = 0; |
| 301 | for (uint32_t I = 0, N = *Specs++; I != N; ++I) |
| 302 | (void)Context.getExternalSource()->GetExternalDecl(Specs[I]); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | llvm::FoldingSet<ClassTemplateSpecializationDecl> & |
| 307 | ClassTemplateDecl::getSpecializations() { |
| 308 | LoadLazySpecializations(); |
| 309 | return getCommonPtr()->Specializations; |
| 310 | } |
| 311 | |
| 312 | llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> & |
| 313 | ClassTemplateDecl::getPartialSpecializations() { |
| 314 | LoadLazySpecializations(); |
| 315 | return getCommonPtr()->PartialSpecializations; |
| 316 | } |
| 317 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 318 | RedeclarableTemplateDecl::CommonBase * |
| 319 | ClassTemplateDecl::newCommon(ASTContext &C) { |
| 320 | Common *CommonPtr = new (C) Common; |
| 321 | C.AddDeallocation(DeallocateCommon, CommonPtr); |
Peter Collingbourne | 9eabeba | 2010-07-29 16:11:51 +0000 | [diff] [blame] | 322 | return CommonPtr; |
| 323 | } |
| 324 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 325 | ClassTemplateSpecializationDecl * |
| 326 | ClassTemplateDecl::findSpecialization(const TemplateArgument *Args, |
| 327 | unsigned NumArgs, void *&InsertPos) { |
Peter Collingbourne | 4048590 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 328 | return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Argyrios Kyrtzidis | bef1a7b | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 331 | void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D, |
| 332 | void *InsertPos) { |
| 333 | getSpecializations().InsertNode(D, InsertPos); |
| 334 | if (ASTMutationListener *L = getASTMutationListener()) |
| 335 | L->AddedCXXTemplateSpecialization(this, D); |
| 336 | } |
| 337 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 338 | ClassTemplatePartialSpecializationDecl * |
| 339 | ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args, |
| 340 | unsigned NumArgs, |
| 341 | void *&InsertPos) { |
Peter Collingbourne | 4048590 | 2010-07-30 17:09:04 +0000 | [diff] [blame] | 342 | return findSpecializationImpl(getPartialSpecializations(), Args, NumArgs, |
| 343 | InsertPos); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Argyrios Kyrtzidis | bef1a7b | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 346 | void ClassTemplateDecl::AddPartialSpecialization( |
| 347 | ClassTemplatePartialSpecializationDecl *D, |
| 348 | void *InsertPos) { |
| 349 | getPartialSpecializations().InsertNode(D, InsertPos); |
| 350 | if (ASTMutationListener *L = getASTMutationListener()) |
| 351 | L->AddedCXXTemplateSpecialization(this, D); |
| 352 | } |
| 353 | |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 354 | void ClassTemplateDecl::getPartialSpecializations( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 355 | SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) { |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 356 | llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs |
Argyrios Kyrtzidis | 5bf1bdc | 2010-06-21 10:57:41 +0000 | [diff] [blame] | 357 | = getPartialSpecializations(); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 358 | PS.clear(); |
| 359 | PS.resize(PartialSpecs.size()); |
| 360 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 361 | P = PartialSpecs.begin(), PEnd = PartialSpecs.end(); |
| 362 | P != PEnd; ++P) { |
| 363 | assert(!PS[P->getSequenceNumber()]); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 364 | PS[P->getSequenceNumber()] = P->getMostRecentDeclaration(); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 368 | ClassTemplatePartialSpecializationDecl * |
| 369 | ClassTemplateDecl::findPartialSpecialization(QualType T) { |
| 370 | ASTContext &Context = getASTContext(); |
| 371 | typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 372 | partial_spec_iterator; |
| 373 | for (partial_spec_iterator P = getPartialSpecializations().begin(), |
| 374 | PEnd = getPartialSpecializations().end(); |
| 375 | P != PEnd; ++P) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 376 | if (Context.hasSameType(P->getInjectedSpecializationType(), T)) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 377 | return P->getMostRecentDeclaration(); |
| 378 | } |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | ClassTemplatePartialSpecializationDecl * |
| 384 | ClassTemplateDecl::findPartialSpecInstantiatedFromMember( |
| 385 | ClassTemplatePartialSpecializationDecl *D) { |
| 386 | Decl *DCanon = D->getCanonicalDecl(); |
| 387 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 388 | P = getPartialSpecializations().begin(), |
| 389 | PEnd = getPartialSpecializations().end(); |
| 390 | P != PEnd; ++P) { |
| 391 | if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon) |
| 392 | return P->getMostRecentDeclaration(); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 393 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 398 | QualType |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 399 | ClassTemplateDecl::getInjectedClassNameSpecialization() { |
Argyrios Kyrtzidis | 5bf1bdc | 2010-06-21 10:57:41 +0000 | [diff] [blame] | 400 | Common *CommonPtr = getCommonPtr(); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 401 | if (!CommonPtr->InjectedClassNameType.isNull()) |
| 402 | return CommonPtr->InjectedClassNameType; |
| 403 | |
Douglas Gregor | b7d09d6 | 2010-12-23 16:00:30 +0000 | [diff] [blame] | 404 | // C++0x [temp.dep.type]p2: |
| 405 | // The template argument list of a primary template is a template argument |
| 406 | // list in which the nth template argument has the value of the nth template |
| 407 | // parameter of the class template. If the nth template parameter is a |
| 408 | // template parameter pack (14.5.3), the nth template argument is a pack |
| 409 | // expansion (14.5.3) whose pattern is the name of the template parameter |
| 410 | // pack. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 411 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 412 | TemplateParameterList *Params = getTemplateParameters(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 413 | SmallVector<TemplateArgument, 16> TemplateArgs; |
Douglas Gregor | c494f77 | 2011-03-05 17:54:25 +0000 | [diff] [blame] | 414 | TemplateArgs.resize(Params->size()); |
| 415 | GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data()); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 416 | CommonPtr->InjectedClassNameType |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 417 | = Context.getTemplateSpecializationType(TemplateName(this), |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 418 | &TemplateArgs[0], |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 419 | TemplateArgs.size()); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 420 | return CommonPtr->InjectedClassNameType; |
| 421 | } |
| 422 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 423 | //===----------------------------------------------------------------------===// |
| 424 | // TemplateTypeParm Allocation/Deallocation Method Implementations |
| 425 | //===----------------------------------------------------------------------===// |
| 426 | |
| 427 | TemplateTypeParmDecl * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 428 | TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 429 | SourceLocation KeyLoc, SourceLocation NameLoc, |
| 430 | unsigned D, unsigned P, IdentifierInfo *Id, |
| 431 | bool Typename, bool ParameterPack) { |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 432 | TemplateTypeParmDecl *TTPDecl = |
| 433 | new (C) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename); |
| 434 | QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl); |
| 435 | TTPDecl->TypeForDecl = TTPType.getTypePtr(); |
| 436 | return TTPDecl; |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 439 | TemplateTypeParmDecl * |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 440 | TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
| 441 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTypeParmDecl)); |
| 442 | return new (Mem) TemplateTypeParmDecl(0, SourceLocation(), SourceLocation(), |
| 443 | 0, false); |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 444 | } |
| 445 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 446 | SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const { |
Abramo Bagnara | 77d4ee2 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 447 | return hasDefaultArgument() |
| 448 | ? DefaultArgument->getTypeLoc().getBeginLoc() |
| 449 | : SourceLocation(); |
| 450 | } |
| 451 | |
| 452 | SourceRange TemplateTypeParmDecl::getSourceRange() const { |
| 453 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 454 | return SourceRange(getLocStart(), |
Abramo Bagnara | 77d4ee2 | 2011-03-04 12:42:03 +0000 | [diff] [blame] | 455 | DefaultArgument->getTypeLoc().getEndLoc()); |
| 456 | else |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 457 | return TypeDecl::getSourceRange(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 460 | unsigned TemplateTypeParmDecl::getDepth() const { |
| 461 | return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth(); |
| 462 | } |
| 463 | |
| 464 | unsigned TemplateTypeParmDecl::getIndex() const { |
| 465 | return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex(); |
| 466 | } |
| 467 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 468 | bool TemplateTypeParmDecl::isParameterPack() const { |
| 469 | return TypeForDecl->getAs<TemplateTypeParmType>()->isParameterPack(); |
| 470 | } |
| 471 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 472 | //===----------------------------------------------------------------------===// |
| 473 | // NonTypeTemplateParmDecl Method Implementations |
| 474 | //===----------------------------------------------------------------------===// |
| 475 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 476 | NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 477 | SourceLocation StartLoc, |
| 478 | SourceLocation IdLoc, |
| 479 | unsigned D, unsigned P, |
| 480 | IdentifierInfo *Id, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 481 | QualType T, |
| 482 | TypeSourceInfo *TInfo, |
| 483 | const QualType *ExpandedTypes, |
| 484 | unsigned NumExpandedTypes, |
| 485 | TypeSourceInfo **ExpandedTInfos) |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 486 | : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 487 | TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false), |
| 488 | ParameterPack(true), ExpandedParameterPack(true), |
| 489 | NumExpandedTypes(NumExpandedTypes) |
| 490 | { |
| 491 | if (ExpandedTypes && ExpandedTInfos) { |
| 492 | void **TypesAndInfos = reinterpret_cast<void **>(this + 1); |
| 493 | for (unsigned I = 0; I != NumExpandedTypes; ++I) { |
| 494 | TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr(); |
| 495 | TypesAndInfos[2*I + 1] = ExpandedTInfos[I]; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 500 | NonTypeTemplateParmDecl * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 501 | NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 502 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 503 | unsigned D, unsigned P, IdentifierInfo *Id, |
| 504 | QualType T, bool ParameterPack, |
| 505 | TypeSourceInfo *TInfo) { |
| 506 | return new (C) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, |
| 507 | T, ParameterPack, TInfo); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 510 | NonTypeTemplateParmDecl * |
| 511 | NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 512 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 513 | unsigned D, unsigned P, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 514 | IdentifierInfo *Id, QualType T, |
| 515 | TypeSourceInfo *TInfo, |
| 516 | const QualType *ExpandedTypes, |
| 517 | unsigned NumExpandedTypes, |
| 518 | TypeSourceInfo **ExpandedTInfos) { |
| 519 | unsigned Size = sizeof(NonTypeTemplateParmDecl) |
| 520 | + NumExpandedTypes * 2 * sizeof(void*); |
| 521 | void *Mem = C.Allocate(Size); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 522 | return new (Mem) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, |
| 523 | D, P, Id, T, TInfo, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 524 | ExpandedTypes, NumExpandedTypes, |
| 525 | ExpandedTInfos); |
| 526 | } |
| 527 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 528 | NonTypeTemplateParmDecl * |
| 529 | NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 530 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NonTypeTemplateParmDecl)); |
| 531 | return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(), |
| 532 | SourceLocation(), 0, 0, 0, |
| 533 | QualType(), false, 0); |
| 534 | } |
| 535 | |
| 536 | NonTypeTemplateParmDecl * |
| 537 | NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 538 | unsigned NumExpandedTypes) { |
| 539 | unsigned Size = sizeof(NonTypeTemplateParmDecl) |
| 540 | + NumExpandedTypes * 2 * sizeof(void*); |
| 541 | |
| 542 | void *Mem = AllocateDeserializedDecl(C, ID, Size); |
| 543 | return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(), |
| 544 | SourceLocation(), 0, 0, 0, |
| 545 | QualType(), 0, 0, NumExpandedTypes, |
| 546 | 0); |
| 547 | } |
| 548 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 549 | SourceRange NonTypeTemplateParmDecl::getSourceRange() const { |
Abramo Bagnara | ee4bfd4 | 2011-03-04 11:03:48 +0000 | [diff] [blame] | 550 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 551 | return SourceRange(getOuterLocStart(), |
| 552 | getDefaultArgument()->getSourceRange().getEnd()); |
| 553 | return DeclaratorDecl::getSourceRange(); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 556 | SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 557 | return hasDefaultArgument() |
| 558 | ? getDefaultArgument()->getSourceRange().getBegin() |
| 559 | : SourceLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 562 | //===----------------------------------------------------------------------===// |
| 563 | // TemplateTemplateParmDecl Method Implementations |
| 564 | //===----------------------------------------------------------------------===// |
| 565 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 566 | void TemplateTemplateParmDecl::anchor() { } |
| 567 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 568 | TemplateTemplateParmDecl * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 569 | TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 570 | SourceLocation L, unsigned D, unsigned P, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 571 | bool ParameterPack, IdentifierInfo *Id, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 572 | TemplateParameterList *Params) { |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 573 | return new (C) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id, |
| 574 | Params); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 577 | TemplateTemplateParmDecl * |
| 578 | TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 579 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTemplateParmDecl)); |
| 580 | return new (Mem) TemplateTemplateParmDecl(0, SourceLocation(), 0, 0, false, |
| 581 | 0, 0); |
| 582 | } |
| 583 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 584 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 585 | // TemplateArgumentList Implementation |
| 586 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 587 | TemplateArgumentList * |
| 588 | TemplateArgumentList::CreateCopy(ASTContext &Context, |
| 589 | const TemplateArgument *Args, |
| 590 | unsigned NumArgs) { |
| 591 | std::size_t Size = sizeof(TemplateArgumentList) |
| 592 | + NumArgs * sizeof(TemplateArgument); |
| 593 | void *Mem = Context.Allocate(Size); |
| 594 | TemplateArgument *StoredArgs |
| 595 | = reinterpret_cast<TemplateArgument *>( |
| 596 | static_cast<TemplateArgumentList *>(Mem) + 1); |
| 597 | std::uninitialized_copy(Args, Args + NumArgs, StoredArgs); |
| 598 | return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true); |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 601 | FunctionTemplateSpecializationInfo * |
| 602 | FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD, |
| 603 | FunctionTemplateDecl *Template, |
| 604 | TemplateSpecializationKind TSK, |
| 605 | const TemplateArgumentList *TemplateArgs, |
| 606 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 607 | SourceLocation POI) { |
| 608 | const ASTTemplateArgumentListInfo *ArgsAsWritten = 0; |
| 609 | if (TemplateArgsAsWritten) |
| 610 | ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C, |
| 611 | *TemplateArgsAsWritten); |
| 612 | |
| 613 | return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK, |
| 614 | TemplateArgs, |
| 615 | ArgsAsWritten, |
| 616 | POI); |
| 617 | } |
| 618 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 619 | //===----------------------------------------------------------------------===// |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 620 | // TemplateDecl Implementation |
| 621 | //===----------------------------------------------------------------------===// |
| 622 | |
| 623 | void TemplateDecl::anchor() { } |
| 624 | |
| 625 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 626 | // ClassTemplateSpecializationDecl Implementation |
| 627 | //===----------------------------------------------------------------------===// |
| 628 | ClassTemplateSpecializationDecl:: |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 629 | ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 630 | DeclContext *DC, SourceLocation StartLoc, |
| 631 | SourceLocation IdLoc, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 632 | ClassTemplateDecl *SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 633 | const TemplateArgument *Args, |
| 634 | unsigned NumArgs, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 635 | ClassTemplateSpecializationDecl *PrevDecl) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 636 | : CXXRecordDecl(DK, TK, DC, StartLoc, IdLoc, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 637 | SpecializedTemplate->getIdentifier(), |
| 638 | PrevDecl), |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 639 | SpecializedTemplate(SpecializedTemplate), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 640 | ExplicitInfo(0), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 641 | TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 642 | SpecializationKind(TSK_Undeclared) { |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 643 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 645 | ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 646 | : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0), |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 647 | ExplicitInfo(0), |
| 648 | SpecializationKind(TSK_Undeclared) { |
| 649 | } |
| 650 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 651 | ClassTemplateSpecializationDecl * |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 652 | ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 653 | DeclContext *DC, |
| 654 | SourceLocation StartLoc, |
| 655 | SourceLocation IdLoc, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 656 | ClassTemplateDecl *SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 657 | const TemplateArgument *Args, |
| 658 | unsigned NumArgs, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 659 | ClassTemplateSpecializationDecl *PrevDecl) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 660 | ClassTemplateSpecializationDecl *Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | = new (Context)ClassTemplateSpecializationDecl(Context, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 662 | ClassTemplateSpecialization, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 663 | TK, DC, StartLoc, IdLoc, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 664 | SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 665 | Args, NumArgs, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 666 | PrevDecl); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 667 | Context.getTypeDeclType(Result, PrevDecl); |
| 668 | return Result; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 669 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 670 | |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 671 | ClassTemplateSpecializationDecl * |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 672 | ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, |
| 673 | unsigned ID) { |
| 674 | void *Mem = AllocateDeserializedDecl(C, ID, |
| 675 | sizeof(ClassTemplateSpecializationDecl)); |
| 676 | return new (Mem) ClassTemplateSpecializationDecl(ClassTemplateSpecialization); |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 679 | void |
| 680 | ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S, |
| 681 | const PrintingPolicy &Policy, |
| 682 | bool Qualified) const { |
| 683 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 684 | |
| 685 | const TemplateArgumentList &TemplateArgs = getTemplateArgs(); |
| 686 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 687 | TemplateArgs.data(), |
| 688 | TemplateArgs.size(), |
| 689 | Policy); |
| 690 | } |
| 691 | |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 692 | ClassTemplateDecl * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | ClassTemplateSpecializationDecl::getSpecializedTemplate() const { |
| 694 | if (SpecializedPartialSpecialization *PartialSpec |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 695 | = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) |
| 696 | return PartialSpec->PartialSpecialization->getSpecializedTemplate(); |
| 697 | return SpecializedTemplate.get<ClassTemplateDecl*>(); |
| 698 | } |
| 699 | |
Abramo Bagnara | 4a85a73 | 2011-03-04 14:20:30 +0000 | [diff] [blame] | 700 | SourceRange |
| 701 | ClassTemplateSpecializationDecl::getSourceRange() const { |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 702 | if (ExplicitInfo) { |
| 703 | SourceLocation Begin = getExternLoc(); |
| 704 | if (Begin.isInvalid()) |
| 705 | Begin = getTemplateKeywordLoc(); |
| 706 | SourceLocation End = getRBraceLoc(); |
| 707 | if (End.isInvalid()) |
| 708 | End = getTypeAsWritten()->getTypeLoc().getEndLoc(); |
| 709 | return SourceRange(Begin, End); |
| 710 | } |
| 711 | else { |
| 712 | // No explicit info available. |
| 713 | llvm::PointerUnion<ClassTemplateDecl *, |
| 714 | ClassTemplatePartialSpecializationDecl *> |
| 715 | inst_from = getInstantiatedFrom(); |
| 716 | if (inst_from.isNull()) |
| 717 | return getSpecializedTemplate()->getSourceRange(); |
| 718 | if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>()) |
| 719 | return ctd->getSourceRange(); |
| 720 | return inst_from.get<ClassTemplatePartialSpecializationDecl*>() |
| 721 | ->getSourceRange(); |
| 722 | } |
Abramo Bagnara | 4a85a73 | 2011-03-04 14:20:30 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 725 | //===----------------------------------------------------------------------===// |
| 726 | // ClassTemplatePartialSpecializationDecl Implementation |
| 727 | //===----------------------------------------------------------------------===// |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 728 | void ClassTemplatePartialSpecializationDecl::anchor() { } |
| 729 | |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 730 | ClassTemplatePartialSpecializationDecl:: |
| 731 | ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 732 | DeclContext *DC, |
| 733 | SourceLocation StartLoc, |
| 734 | SourceLocation IdLoc, |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 735 | TemplateParameterList *Params, |
| 736 | ClassTemplateDecl *SpecializedTemplate, |
| 737 | const TemplateArgument *Args, |
| 738 | unsigned NumArgs, |
| 739 | TemplateArgumentLoc *ArgInfos, |
| 740 | unsigned NumArgInfos, |
| 741 | ClassTemplatePartialSpecializationDecl *PrevDecl, |
| 742 | unsigned SequenceNumber) |
| 743 | : ClassTemplateSpecializationDecl(Context, |
| 744 | ClassTemplatePartialSpecialization, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 745 | TK, DC, StartLoc, IdLoc, |
| 746 | SpecializedTemplate, |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 747 | Args, NumArgs, PrevDecl), |
| 748 | TemplateParams(Params), ArgsAsWritten(ArgInfos), |
| 749 | NumArgsAsWritten(NumArgInfos), SequenceNumber(SequenceNumber), |
| 750 | InstantiatedFromMember(0, false) |
| 751 | { |
Douglas Gregor | 787a40d | 2011-03-04 18:32:38 +0000 | [diff] [blame] | 752 | AdoptTemplateParameterList(Params, this); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 755 | ClassTemplatePartialSpecializationDecl * |
| 756 | ClassTemplatePartialSpecializationDecl:: |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 757 | Create(ASTContext &Context, TagKind TK,DeclContext *DC, |
| 758 | SourceLocation StartLoc, SourceLocation IdLoc, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 759 | TemplateParameterList *Params, |
| 760 | ClassTemplateDecl *SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 761 | const TemplateArgument *Args, |
| 762 | unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 763 | const TemplateArgumentListInfo &ArgInfos, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 764 | QualType CanonInjectedType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 765 | ClassTemplatePartialSpecializationDecl *PrevDecl, |
| 766 | unsigned SequenceNumber) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 767 | unsigned N = ArgInfos.size(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 768 | TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N]; |
| 769 | for (unsigned I = 0; I != N; ++I) |
| 770 | ClonedArgs[I] = ArgInfos[I]; |
| 771 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 772 | ClassTemplatePartialSpecializationDecl *Result |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 773 | = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK, DC, |
| 774 | StartLoc, IdLoc, |
| 775 | Params, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 776 | SpecializedTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 777 | Args, NumArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 778 | ClonedArgs, N, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 779 | PrevDecl, |
| 780 | SequenceNumber); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 781 | Result->setSpecializationKind(TSK_ExplicitSpecialization); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 782 | |
| 783 | Context.getInjectedClassNameType(Result, CanonInjectedType); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 784 | return Result; |
| 785 | } |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 786 | |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 787 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 788 | ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C, |
| 789 | unsigned ID) { |
| 790 | void *Mem = AllocateDeserializedDecl(C, ID, |
| 791 | sizeof(ClassTemplatePartialSpecializationDecl)); |
| 792 | return new (Mem) ClassTemplatePartialSpecializationDecl(); |
Argyrios Kyrtzidis | 94d228d | 2010-06-23 13:48:23 +0000 | [diff] [blame] | 793 | } |
| 794 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 795 | //===----------------------------------------------------------------------===// |
| 796 | // FriendTemplateDecl Implementation |
| 797 | //===----------------------------------------------------------------------===// |
| 798 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 799 | void FriendTemplateDecl::anchor() { } |
| 800 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 801 | FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context, |
| 802 | DeclContext *DC, |
| 803 | SourceLocation L, |
| 804 | unsigned NParams, |
| 805 | TemplateParameterList **Params, |
| 806 | FriendUnion Friend, |
| 807 | SourceLocation FLoc) { |
| 808 | FriendTemplateDecl *Result |
| 809 | = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc); |
| 810 | return Result; |
| 811 | } |
Argyrios Kyrtzidis | 554e6aa | 2010-07-22 16:04:10 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 813 | FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C, |
| 814 | unsigned ID) { |
| 815 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FriendTemplateDecl)); |
| 816 | return new (Mem) FriendTemplateDecl(EmptyShell()); |
Argyrios Kyrtzidis | 554e6aa | 2010-07-22 16:04:10 +0000 | [diff] [blame] | 817 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 818 | |
| 819 | //===----------------------------------------------------------------------===// |
| 820 | // TypeAliasTemplateDecl Implementation |
| 821 | //===----------------------------------------------------------------------===// |
| 822 | |
| 823 | TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C, |
| 824 | DeclContext *DC, |
| 825 | SourceLocation L, |
| 826 | DeclarationName Name, |
| 827 | TemplateParameterList *Params, |
| 828 | NamedDecl *Decl) { |
| 829 | AdoptTemplateParameterList(Params, DC); |
| 830 | return new (C) TypeAliasTemplateDecl(DC, L, Name, Params, Decl); |
| 831 | } |
| 832 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 833 | TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C, |
| 834 | unsigned ID) { |
| 835 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasTemplateDecl)); |
| 836 | return new (Mem) TypeAliasTemplateDecl(0, SourceLocation(), DeclarationName(), |
| 837 | 0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) { |
| 841 | static_cast<Common *>(Ptr)->~Common(); |
| 842 | } |
| 843 | RedeclarableTemplateDecl::CommonBase * |
| 844 | TypeAliasTemplateDecl::newCommon(ASTContext &C) { |
| 845 | Common *CommonPtr = new (C) Common; |
| 846 | C.AddDeallocation(DeallocateCommon, CommonPtr); |
| 847 | return CommonPtr; |
| 848 | } |
| 849 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 850 | //===----------------------------------------------------------------------===// |
| 851 | // ClassScopeFunctionSpecializationDecl Implementation |
| 852 | //===----------------------------------------------------------------------===// |
| 853 | |
| 854 | void ClassScopeFunctionSpecializationDecl::anchor() { } |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame^] | 855 | |
| 856 | ClassScopeFunctionSpecializationDecl * |
| 857 | ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C, |
| 858 | unsigned ID) { |
| 859 | void *Mem = AllocateDeserializedDecl(C, ID, |
| 860 | sizeof(ClassScopeFunctionSpecializationDecl)); |
| 861 | return new (Mem) ClassScopeFunctionSpecializationDecl(0, SourceLocation(), 0); |
| 862 | } |