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