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 | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/Basic/IdentifierTable.h" |
| 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // TemplateParameterList Implementation |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 27 | TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc, |
| 28 | SourceLocation LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 29 | NamedDecl **Params, unsigned NumParams, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 30 | SourceLocation RAngleLoc) |
| 31 | : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc), |
| 32 | NumParams(NumParams) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 33 | for (unsigned Idx = 0; Idx < NumParams; ++Idx) |
| 34 | begin()[Idx] = Params[Idx]; |
| 35 | } |
| 36 | |
| 37 | TemplateParameterList * |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 38 | TemplateParameterList::Create(ASTContext &C, SourceLocation TemplateLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 39 | SourceLocation LAngleLoc, NamedDecl **Params, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 40 | unsigned NumParams, SourceLocation RAngleLoc) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 41 | unsigned Size = sizeof(TemplateParameterList) |
| 42 | + sizeof(NamedDecl *) * NumParams; |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 43 | unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment; |
| 44 | void *Mem = C.Allocate(Size, Align); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 46 | NumParams, RAngleLoc); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 49 | unsigned TemplateParameterList::getMinRequiredArguments() const { |
| 50 | unsigned NumRequiredArgs = size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | iterator Param = const_cast<TemplateParameterList *>(this)->end(), |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 52 | ParamBegin = const_cast<TemplateParameterList *>(this)->begin(); |
| 53 | while (Param != ParamBegin) { |
| 54 | --Param; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 56 | if (!(*Param)->isTemplateParameterPack() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | !(isa<TemplateTypeParmDecl>(*Param) && |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 58 | cast<TemplateTypeParmDecl>(*Param)->hasDefaultArgument()) && |
| 59 | !(isa<NonTypeTemplateParmDecl>(*Param) && |
| 60 | cast<NonTypeTemplateParmDecl>(*Param)->hasDefaultArgument()) && |
| 61 | !(isa<TemplateTemplateParmDecl>(*Param) && |
| 62 | cast<TemplateTemplateParmDecl>(*Param)->hasDefaultArgument())) |
| 63 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 65 | --NumRequiredArgs; |
| 66 | } |
| 67 | |
| 68 | return NumRequiredArgs; |
| 69 | } |
| 70 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 71 | unsigned TemplateParameterList::getDepth() const { |
| 72 | if (size() == 0) |
| 73 | return 0; |
| 74 | |
| 75 | const NamedDecl *FirstParm = getParam(0); |
| 76 | if (const TemplateTypeParmDecl *TTP |
| 77 | = dyn_cast<TemplateTypeParmDecl>(FirstParm)) |
| 78 | return TTP->getDepth(); |
| 79 | else if (const NonTypeTemplateParmDecl *NTTP |
| 80 | = dyn_cast<NonTypeTemplateParmDecl>(FirstParm)) |
| 81 | return NTTP->getDepth(); |
| 82 | else |
| 83 | return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth(); |
| 84 | } |
| 85 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 86 | //===----------------------------------------------------------------------===// |
| 87 | // TemplateDecl Implementation |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | |
| 90 | TemplateDecl::~TemplateDecl() { |
| 91 | } |
| 92 | |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | // FunctionTemplateDecl Implementation |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 97 | void FunctionTemplateDecl::DeallocateCommon(void *Ptr) { |
| 98 | static_cast<Common *>(Ptr)->~Common(); |
| 99 | } |
| 100 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 101 | FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C, |
| 102 | DeclContext *DC, |
| 103 | SourceLocation L, |
| 104 | DeclarationName Name, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 105 | TemplateParameterList *Params, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 106 | NamedDecl *Decl) { |
| 107 | return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl); |
| 108 | } |
| 109 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 110 | void FunctionTemplateDecl::Destroy(ASTContext &C) { |
| 111 | if (Common *CommonPtr = CommonOrPrev.dyn_cast<Common*>()) { |
| 112 | for (llvm::FoldingSet<FunctionTemplateSpecializationInfo>::iterator |
| 113 | Spec = CommonPtr->Specializations.begin(), |
| 114 | SpecEnd = CommonPtr->Specializations.end(); |
| 115 | Spec != SpecEnd; ++Spec) |
| 116 | C.Deallocate(&*Spec); |
| 117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 119 | Decl::Destroy(C); |
| 120 | } |
| 121 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 122 | FunctionTemplateDecl *FunctionTemplateDecl::getCanonicalDecl() { |
| 123 | FunctionTemplateDecl *FunTmpl = this; |
| 124 | while (FunTmpl->getPreviousDeclaration()) |
| 125 | FunTmpl = FunTmpl->getPreviousDeclaration(); |
| 126 | return FunTmpl; |
| 127 | } |
| 128 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 129 | FunctionTemplateDecl::Common *FunctionTemplateDecl::getCommonPtr() { |
| 130 | // Find the first declaration of this function template. |
| 131 | FunctionTemplateDecl *First = this; |
| 132 | while (First->getPreviousDeclaration()) |
| 133 | First = First->getPreviousDeclaration(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 135 | if (First->CommonOrPrev.isNull()) { |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 136 | Common *CommonPtr = new (getASTContext()) Common; |
| 137 | getASTContext().AddDeallocation(DeallocateCommon, CommonPtr); |
| 138 | First->CommonOrPrev = CommonPtr; |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 139 | } |
| 140 | return First->CommonOrPrev.get<Common*>(); |
| 141 | } |
| 142 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 143 | //===----------------------------------------------------------------------===// |
| 144 | // ClassTemplateDecl Implementation |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 147 | void ClassTemplateDecl::DeallocateCommon(void *Ptr) { |
| 148 | static_cast<Common *>(Ptr)->~Common(); |
| 149 | } |
| 150 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 151 | ClassTemplateDecl *ClassTemplateDecl::getCanonicalDecl() { |
| 152 | ClassTemplateDecl *Template = this; |
| 153 | while (Template->getPreviousDeclaration()) |
| 154 | Template = Template->getPreviousDeclaration(); |
| 155 | return Template; |
| 156 | } |
| 157 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 158 | ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, |
| 159 | DeclContext *DC, |
| 160 | SourceLocation L, |
| 161 | DeclarationName Name, |
| 162 | TemplateParameterList *Params, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 163 | NamedDecl *Decl, |
| 164 | ClassTemplateDecl *PrevDecl) { |
| 165 | Common *CommonPtr; |
| 166 | if (PrevDecl) |
| 167 | CommonPtr = PrevDecl->CommonPtr; |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 168 | else { |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 169 | CommonPtr = new (C) Common; |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 170 | C.AddDeallocation(DeallocateCommon, CommonPtr); |
| 171 | } |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 172 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl, PrevDecl, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 174 | CommonPtr); |
| 175 | } |
| 176 | |
| 177 | ClassTemplateDecl::~ClassTemplateDecl() { |
| 178 | assert(CommonPtr == 0 && "ClassTemplateDecl must be explicitly destroyed"); |
| 179 | } |
| 180 | |
| 181 | void ClassTemplateDecl::Destroy(ASTContext& C) { |
| 182 | if (!PreviousDeclaration) { |
| 183 | CommonPtr->~Common(); |
| 184 | C.Deallocate((void*)CommonPtr); |
| 185 | } |
| 186 | CommonPtr = 0; |
| 187 | |
| 188 | this->~ClassTemplateDecl(); |
| 189 | C.Deallocate((void*)this); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 192 | void ClassTemplateDecl::getPartialSpecializations( |
| 193 | llvm::SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) { |
| 194 | llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs |
| 195 | = CommonPtr->PartialSpecializations; |
| 196 | PS.clear(); |
| 197 | PS.resize(PartialSpecs.size()); |
| 198 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 199 | P = PartialSpecs.begin(), PEnd = PartialSpecs.end(); |
| 200 | P != PEnd; ++P) { |
| 201 | assert(!PS[P->getSequenceNumber()]); |
| 202 | PS[P->getSequenceNumber()] = &*P; |
| 203 | } |
| 204 | } |
| 205 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 206 | ClassTemplatePartialSpecializationDecl * |
| 207 | ClassTemplateDecl::findPartialSpecialization(QualType T) { |
| 208 | ASTContext &Context = getASTContext(); |
| 209 | typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 210 | partial_spec_iterator; |
| 211 | for (partial_spec_iterator P = getPartialSpecializations().begin(), |
| 212 | PEnd = getPartialSpecializations().end(); |
| 213 | P != PEnd; ++P) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 214 | if (Context.hasSameType(P->getInjectedSpecializationType(), T)) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 215 | return &*P; |
| 216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 221 | QualType |
| 222 | ClassTemplateDecl::getInjectedClassNameSpecialization(ASTContext &Context) { |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 223 | if (!CommonPtr->InjectedClassNameType.isNull()) |
| 224 | return CommonPtr->InjectedClassNameType; |
| 225 | |
| 226 | // FIXME: n2800 14.6.1p1 should say how the template arguments |
| 227 | // corresponding to template parameter packs should be pack |
| 228 | // expansions. We already say that in 14.6.2.1p2, so it would be |
| 229 | // better to fix that redundancy. |
| 230 | |
| 231 | TemplateParameterList *Params = getTemplateParameters(); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 232 | llvm::SmallVector<TemplateArgument, 16> TemplateArgs; |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 233 | TemplateArgs.reserve(Params->size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 235 | ParamEnd = Params->end(); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 236 | Param != ParamEnd; ++Param) { |
| 237 | if (isa<TemplateTypeParmDecl>(*Param)) { |
| 238 | QualType ParamType = Context.getTypeDeclType(cast<TypeDecl>(*Param)); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 239 | TemplateArgs.push_back(TemplateArgument(ParamType)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | } else if (NonTypeTemplateParmDecl *NTTP = |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 241 | dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Chandler Carruth | b7de181 | 2010-01-31 07:24:03 +0000 | [diff] [blame] | 242 | Expr *E = new (Context) DeclRefExpr(NTTP, |
| 243 | NTTP->getType().getNonReferenceType(), |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 244 | NTTP->getLocation()); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 245 | TemplateArgs.push_back(TemplateArgument(E)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | } else { |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 247 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 248 | TemplateArgs.push_back(TemplateArgument(TemplateName(TTP))); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 252 | CommonPtr->InjectedClassNameType |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 253 | = Context.getTemplateSpecializationType(TemplateName(this), |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 254 | &TemplateArgs[0], |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 255 | TemplateArgs.size()); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 256 | return CommonPtr->InjectedClassNameType; |
| 257 | } |
| 258 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 259 | //===----------------------------------------------------------------------===// |
| 260 | // TemplateTypeParm Allocation/Deallocation Method Implementations |
| 261 | //===----------------------------------------------------------------------===// |
| 262 | |
| 263 | TemplateTypeParmDecl * |
| 264 | TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 265 | SourceLocation L, unsigned D, unsigned P, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 266 | IdentifierInfo *Id, bool Typename, |
| 267 | bool ParameterPack) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 268 | QualType Type = C.getTemplateTypeParmType(D, P, ParameterPack, Id); |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 269 | return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type, ParameterPack); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 270 | } |
| 271 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 272 | SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const { |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 273 | return DefaultArgument->getTypeLoc().getSourceRange().getBegin(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 276 | unsigned TemplateTypeParmDecl::getDepth() const { |
| 277 | return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth(); |
| 278 | } |
| 279 | |
| 280 | unsigned TemplateTypeParmDecl::getIndex() const { |
| 281 | return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex(); |
| 282 | } |
| 283 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 284 | //===----------------------------------------------------------------------===// |
| 285 | // NonTypeTemplateParmDecl Method Implementations |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | |
| 288 | NonTypeTemplateParmDecl * |
| 289 | NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 290 | SourceLocation L, unsigned D, unsigned P, |
| 291 | IdentifierInfo *Id, QualType T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 292 | TypeSourceInfo *TInfo) { |
| 293 | return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, TInfo); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 296 | SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const { |
| 297 | return DefaultArgument? DefaultArgument->getSourceRange().getBegin() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | : SourceLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 301 | //===----------------------------------------------------------------------===// |
| 302 | // TemplateTemplateParmDecl Method Implementations |
| 303 | //===----------------------------------------------------------------------===// |
| 304 | |
| 305 | TemplateTemplateParmDecl * |
| 306 | TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 307 | SourceLocation L, unsigned D, unsigned P, |
| 308 | IdentifierInfo *Id, |
| 309 | TemplateParameterList *Params) { |
| 310 | return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params); |
| 311 | } |
| 312 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 313 | //===----------------------------------------------------------------------===// |
Anders Carlsson | 9ba4164 | 2009-06-05 05:31:27 +0000 | [diff] [blame] | 314 | // TemplateArgumentListBuilder Implementation |
| 315 | //===----------------------------------------------------------------------===// |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 98d279b | 2010-05-20 00:25:36 +0000 | [diff] [blame] | 317 | void TemplateArgumentListBuilder::Append(const TemplateArgument &Arg) { |
| 318 | assert((Arg.getKind() != TemplateArgument::Type || |
| 319 | Arg.getAsType().isCanonical()) && "Type must be canonical!"); |
| 320 | assert(FlatArgs.size() < MaxFlatArgs && "Argument list builder is full!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | assert(!StructuredArgs && |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 322 | "Can't append arguments when an argument pack has been added!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 98d279b | 2010-05-20 00:25:36 +0000 | [diff] [blame] | 324 | FlatArgs.push_back(Arg); |
Anders Carlsson | 9ba4164 | 2009-06-05 05:31:27 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 327 | void TemplateArgumentListBuilder::BeginPack() { |
| 328 | assert(!AddingToPack && "Already adding to pack!"); |
| 329 | assert(!StructuredArgs && "Argument list already contains a pack!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 331 | AddingToPack = true; |
Chris Lattner | 98d279b | 2010-05-20 00:25:36 +0000 | [diff] [blame] | 332 | PackBeginIndex = FlatArgs.size(); |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 335 | void TemplateArgumentListBuilder::EndPack() { |
| 336 | assert(AddingToPack && "Not adding to pack!"); |
| 337 | assert(!StructuredArgs && "Argument list already contains a pack!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 339 | AddingToPack = false; |
Anders Carlsson | 3b36b66 | 2009-06-15 17:56:45 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 304d0fa | 2010-05-20 00:26:28 +0000 | [diff] [blame] | 341 | // FIXME: This is a memory leak! |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 342 | StructuredArgs = new TemplateArgument[MaxStructuredArgs]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 344 | // First copy the flat entries over to the list (if any) |
| 345 | for (unsigned I = 0; I != PackBeginIndex; ++I) { |
| 346 | NumStructuredArgs++; |
| 347 | StructuredArgs[I] = FlatArgs[I]; |
| 348 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 350 | // Next, set the pack. |
| 351 | TemplateArgument *PackArgs = 0; |
| 352 | unsigned NumPackArgs = NumFlatArgs - PackBeginIndex; |
Chris Lattner | 98d279b | 2010-05-20 00:25:36 +0000 | [diff] [blame] | 353 | // FIXME: NumPackArgs shouldn't be negative here??? |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 354 | if (NumPackArgs) |
Chris Lattner | 98d279b | 2010-05-20 00:25:36 +0000 | [diff] [blame] | 355 | PackArgs = FlatArgs.data()+PackBeginIndex; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
| 357 | StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 358 | /*CopyArgs=*/false); |
| 359 | } |
| 360 | |
Anders Carlsson | 9ba4164 | 2009-06-05 05:31:27 +0000 | [diff] [blame] | 361 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 362 | // TemplateArgumentList Implementation |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | TemplateArgumentList::TemplateArgumentList(ASTContext &Context, |
Anders Carlsson | e9c904b | 2009-06-05 04:47:51 +0000 | [diff] [blame] | 365 | TemplateArgumentListBuilder &Builder, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 366 | bool TakeArgs) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | : FlatArguments(Builder.getFlatArguments(), TakeArgs), |
| 368 | NumFlatArguments(Builder.flatSize()), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 369 | StructuredArguments(Builder.getStructuredArguments(), TakeArgs), |
| 370 | NumStructuredArguments(Builder.structuredSize()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 372 | if (!TakeArgs) |
| 373 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 56ef550 | 2010-05-20 00:11:47 +0000 | [diff] [blame] | 375 | // If this does take ownership of the arguments, then we have to new them |
| 376 | // and copy over. |
Ted Kremenek | 3458d43 | 2010-05-25 20:43:29 +0000 | [diff] [blame^] | 377 | TemplateArgument *NewArgs = |
| 378 | new (Context) TemplateArgument[Builder.flatSize()]; |
Chris Lattner | 56ef550 | 2010-05-20 00:11:47 +0000 | [diff] [blame] | 379 | std::copy(Builder.getFlatArguments(), |
| 380 | Builder.getFlatArguments()+Builder.flatSize(), NewArgs); |
| 381 | FlatArguments.setPointer(NewArgs); |
| 382 | |
| 383 | // Just reuse the structured and flat arguments array if possible. |
| 384 | if (Builder.getStructuredArguments() == Builder.getFlatArguments()) { |
| 385 | StructuredArguments.setPointer(NewArgs); |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 386 | StructuredArguments.setInt(0); |
Chris Lattner | 56ef550 | 2010-05-20 00:11:47 +0000 | [diff] [blame] | 387 | } else { |
Ted Kremenek | 3458d43 | 2010-05-25 20:43:29 +0000 | [diff] [blame^] | 388 | TemplateArgument *NewSArgs = |
| 389 | new (Context) TemplateArgument[Builder.flatSize()]; |
Chris Lattner | 56ef550 | 2010-05-20 00:11:47 +0000 | [diff] [blame] | 390 | std::copy(Builder.getFlatArguments(), |
| 391 | Builder.getFlatArguments()+Builder.flatSize(), NewSArgs); |
| 392 | StructuredArguments.setPointer(NewSArgs); |
| 393 | } |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Chris Lattner | 8859891 | 2010-05-20 00:19:09 +0000 | [diff] [blame] | 396 | /// Produces a shallow copy of the given template argument list. This |
| 397 | /// assumes that the input argument list outlives it. This takes the list as |
| 398 | /// a pointer to avoid looking like a copy constructor, since this really |
| 399 | /// really isn't safe to use that way. |
| 400 | TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList *Other) |
| 401 | : FlatArguments(Other->FlatArguments.getPointer(), false), |
| 402 | NumFlatArguments(Other->flat_size()), |
| 403 | StructuredArguments(Other->StructuredArguments.getPointer(), false), |
| 404 | NumStructuredArguments(Other->NumStructuredArguments) { } |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | 3458d43 | 2010-05-25 20:43:29 +0000 | [diff] [blame^] | 406 | void TemplateArgumentList::Destroy(ASTContext &C) { |
Chris Lattner | 56ef550 | 2010-05-20 00:11:47 +0000 | [diff] [blame] | 407 | if (FlatArguments.getInt()) |
Ted Kremenek | 3458d43 | 2010-05-25 20:43:29 +0000 | [diff] [blame^] | 408 | C.Deallocate((void*)FlatArguments.getPointer()); |
Chris Lattner | 56ef550 | 2010-05-20 00:11:47 +0000 | [diff] [blame] | 409 | if (StructuredArguments.getInt()) |
Ted Kremenek | 3458d43 | 2010-05-25 20:43:29 +0000 | [diff] [blame^] | 410 | C.Deallocate((void*)StructuredArguments.getPointer()); |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Ted Kremenek | 3458d43 | 2010-05-25 20:43:29 +0000 | [diff] [blame^] | 413 | TemplateArgumentList::~TemplateArgumentList() {} |
| 414 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 415 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 416 | // ClassTemplateSpecializationDecl Implementation |
| 417 | //===----------------------------------------------------------------------===// |
| 418 | ClassTemplateSpecializationDecl:: |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 419 | ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 420 | DeclContext *DC, SourceLocation L, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 421 | ClassTemplateDecl *SpecializedTemplate, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 422 | TemplateArgumentListBuilder &Builder, |
| 423 | ClassTemplateSpecializationDecl *PrevDecl) |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 424 | : CXXRecordDecl(DK, TK, DC, L, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 425 | SpecializedTemplate->getIdentifier(), |
| 426 | PrevDecl), |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 427 | SpecializedTemplate(SpecializedTemplate), |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 428 | TypeAsWritten(0), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 429 | TemplateArgs(Context, Builder, /*TakeArgs=*/true), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 430 | SpecializationKind(TSK_Undeclared) { |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 433 | ClassTemplateSpecializationDecl * |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 434 | ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 435 | DeclContext *DC, SourceLocation L, |
| 436 | ClassTemplateDecl *SpecializedTemplate, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 437 | TemplateArgumentListBuilder &Builder, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 438 | ClassTemplateSpecializationDecl *PrevDecl) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 439 | ClassTemplateSpecializationDecl *Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | = new (Context)ClassTemplateSpecializationDecl(Context, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 441 | ClassTemplateSpecialization, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 442 | TK, DC, L, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 443 | SpecializedTemplate, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 444 | Builder, |
| 445 | PrevDecl); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 446 | Context.getTypeDeclType(Result, PrevDecl); |
| 447 | return Result; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 448 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 449 | |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 450 | void ClassTemplateSpecializationDecl::Destroy(ASTContext &C) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | if (SpecializedPartialSpecialization *PartialSpec |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 452 | = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) |
| 453 | C.Deallocate(PartialSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 455 | CXXRecordDecl::Destroy(C); |
| 456 | } |
| 457 | |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 458 | void |
| 459 | ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S, |
| 460 | const PrintingPolicy &Policy, |
| 461 | bool Qualified) const { |
| 462 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 463 | |
| 464 | const TemplateArgumentList &TemplateArgs = getTemplateArgs(); |
| 465 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 466 | TemplateArgs.getFlatArgumentList(), |
| 467 | TemplateArgs.flat_size(), |
| 468 | Policy); |
| 469 | } |
| 470 | |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 471 | ClassTemplateDecl * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | ClassTemplateSpecializationDecl::getSpecializedTemplate() const { |
| 473 | if (SpecializedPartialSpecialization *PartialSpec |
Douglas Gregor | 37d93e9 | 2009-08-02 23:24:31 +0000 | [diff] [blame] | 474 | = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) |
| 475 | return PartialSpec->PartialSpecialization->getSpecializedTemplate(); |
| 476 | return SpecializedTemplate.get<ClassTemplateDecl*>(); |
| 477 | } |
| 478 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 479 | //===----------------------------------------------------------------------===// |
| 480 | // ClassTemplatePartialSpecializationDecl Implementation |
| 481 | //===----------------------------------------------------------------------===// |
| 482 | ClassTemplatePartialSpecializationDecl * |
| 483 | ClassTemplatePartialSpecializationDecl:: |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 484 | Create(ASTContext &Context, TagKind TK,DeclContext *DC, SourceLocation L, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 485 | TemplateParameterList *Params, |
| 486 | ClassTemplateDecl *SpecializedTemplate, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 487 | TemplateArgumentListBuilder &Builder, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 488 | const TemplateArgumentListInfo &ArgInfos, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 489 | QualType CanonInjectedType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 490 | ClassTemplatePartialSpecializationDecl *PrevDecl, |
| 491 | unsigned SequenceNumber) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 492 | unsigned N = ArgInfos.size(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 493 | TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N]; |
| 494 | for (unsigned I = 0; I != N; ++I) |
| 495 | ClonedArgs[I] = ArgInfos[I]; |
| 496 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 497 | ClassTemplatePartialSpecializationDecl *Result |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 498 | = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 499 | DC, L, Params, |
| 500 | SpecializedTemplate, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 501 | Builder, |
| 502 | ClonedArgs, N, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 503 | PrevDecl, |
| 504 | SequenceNumber); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 505 | Result->setSpecializationKind(TSK_ExplicitSpecialization); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 506 | |
| 507 | Context.getInjectedClassNameType(Result, CanonInjectedType); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 508 | return Result; |
| 509 | } |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 510 | |
| 511 | //===----------------------------------------------------------------------===// |
| 512 | // FriendTemplateDecl Implementation |
| 513 | //===----------------------------------------------------------------------===// |
| 514 | |
| 515 | FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context, |
| 516 | DeclContext *DC, |
| 517 | SourceLocation L, |
| 518 | unsigned NParams, |
| 519 | TemplateParameterList **Params, |
| 520 | FriendUnion Friend, |
| 521 | SourceLocation FLoc) { |
| 522 | FriendTemplateDecl *Result |
| 523 | = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc); |
| 524 | return Result; |
| 525 | } |