Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1 | //===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===// |
| 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" |
| 18 | #include "clang/Basic/IdentifierTable.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | using namespace clang; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // TemplateParameterList Implementation |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 26 | TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc, |
| 27 | SourceLocation LAngleLoc, |
| 28 | Decl **Params, unsigned NumParams, |
| 29 | SourceLocation RAngleLoc) |
| 30 | : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc), |
| 31 | NumParams(NumParams) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 32 | for (unsigned Idx = 0; Idx < NumParams; ++Idx) |
| 33 | begin()[Idx] = Params[Idx]; |
| 34 | } |
| 35 | |
| 36 | TemplateParameterList * |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 37 | TemplateParameterList::Create(ASTContext &C, SourceLocation TemplateLoc, |
| 38 | SourceLocation LAngleLoc, Decl **Params, |
| 39 | unsigned NumParams, SourceLocation RAngleLoc) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 40 | unsigned Size = sizeof(TemplateParameterList) + sizeof(Decl *) * NumParams; |
| 41 | unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment; |
| 42 | void *Mem = C.Allocate(Size, Align); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 43 | return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params, |
| 44 | NumParams, RAngleLoc); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 47 | unsigned TemplateParameterList::getMinRequiredArguments() const { |
| 48 | unsigned NumRequiredArgs = size(); |
| 49 | iterator Param = const_cast<TemplateParameterList *>(this)->end(), |
| 50 | ParamBegin = const_cast<TemplateParameterList *>(this)->begin(); |
| 51 | while (Param != ParamBegin) { |
| 52 | --Param; |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 53 | |
| 54 | if (!(*Param)->isTemplateParameterPack() && |
| 55 | !(isa<TemplateTypeParmDecl>(*Param) && |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 56 | cast<TemplateTypeParmDecl>(*Param)->hasDefaultArgument()) && |
| 57 | !(isa<NonTypeTemplateParmDecl>(*Param) && |
| 58 | cast<NonTypeTemplateParmDecl>(*Param)->hasDefaultArgument()) && |
| 59 | !(isa<TemplateTemplateParmDecl>(*Param) && |
| 60 | cast<TemplateTemplateParmDecl>(*Param)->hasDefaultArgument())) |
| 61 | break; |
| 62 | |
| 63 | --NumRequiredArgs; |
| 64 | } |
| 65 | |
| 66 | return NumRequiredArgs; |
| 67 | } |
| 68 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 69 | //===----------------------------------------------------------------------===// |
| 70 | // TemplateDecl Implementation |
| 71 | //===----------------------------------------------------------------------===// |
| 72 | |
| 73 | TemplateDecl::~TemplateDecl() { |
| 74 | } |
| 75 | |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | // FunctionTemplateDecl Implementation |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
| 80 | FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C, |
| 81 | DeclContext *DC, |
| 82 | SourceLocation L, |
| 83 | DeclarationName Name, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 84 | TemplateParameterList *Params, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 85 | NamedDecl *Decl) { |
| 86 | return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl); |
| 87 | } |
| 88 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 89 | void FunctionTemplateDecl::Destroy(ASTContext &C) { |
| 90 | if (Common *CommonPtr = CommonOrPrev.dyn_cast<Common*>()) { |
| 91 | for (llvm::FoldingSet<FunctionTemplateSpecializationInfo>::iterator |
| 92 | Spec = CommonPtr->Specializations.begin(), |
| 93 | SpecEnd = CommonPtr->Specializations.end(); |
| 94 | Spec != SpecEnd; ++Spec) |
| 95 | C.Deallocate(&*Spec); |
| 96 | } |
| 97 | |
| 98 | Decl::Destroy(C); |
| 99 | } |
| 100 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 101 | FunctionTemplateDecl *FunctionTemplateDecl::getCanonicalDecl() { |
| 102 | FunctionTemplateDecl *FunTmpl = this; |
| 103 | while (FunTmpl->getPreviousDeclaration()) |
| 104 | FunTmpl = FunTmpl->getPreviousDeclaration(); |
| 105 | return FunTmpl; |
| 106 | } |
| 107 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 108 | FunctionTemplateDecl::Common *FunctionTemplateDecl::getCommonPtr() { |
| 109 | // Find the first declaration of this function template. |
| 110 | FunctionTemplateDecl *First = this; |
| 111 | while (First->getPreviousDeclaration()) |
| 112 | First = First->getPreviousDeclaration(); |
| 113 | |
| 114 | if (First->CommonOrPrev.isNull()) { |
| 115 | // FIXME: Allocate with the ASTContext |
| 116 | First->CommonOrPrev = new Common; |
| 117 | } |
| 118 | return First->CommonOrPrev.get<Common*>(); |
| 119 | } |
| 120 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 121 | //===----------------------------------------------------------------------===// |
| 122 | // ClassTemplateDecl Implementation |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 125 | ClassTemplateDecl *ClassTemplateDecl::getCanonicalDecl() { |
| 126 | ClassTemplateDecl *Template = this; |
| 127 | while (Template->getPreviousDeclaration()) |
| 128 | Template = Template->getPreviousDeclaration(); |
| 129 | return Template; |
| 130 | } |
| 131 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 132 | ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, |
| 133 | DeclContext *DC, |
| 134 | SourceLocation L, |
| 135 | DeclarationName Name, |
| 136 | TemplateParameterList *Params, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 137 | NamedDecl *Decl, |
| 138 | ClassTemplateDecl *PrevDecl) { |
| 139 | Common *CommonPtr; |
| 140 | if (PrevDecl) |
| 141 | CommonPtr = PrevDecl->CommonPtr; |
| 142 | else |
| 143 | CommonPtr = new (C) Common; |
| 144 | |
| 145 | return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl, PrevDecl, |
| 146 | CommonPtr); |
| 147 | } |
| 148 | |
| 149 | ClassTemplateDecl::~ClassTemplateDecl() { |
| 150 | assert(CommonPtr == 0 && "ClassTemplateDecl must be explicitly destroyed"); |
| 151 | } |
| 152 | |
| 153 | void ClassTemplateDecl::Destroy(ASTContext& C) { |
| 154 | if (!PreviousDeclaration) { |
| 155 | CommonPtr->~Common(); |
| 156 | C.Deallocate((void*)CommonPtr); |
| 157 | } |
| 158 | CommonPtr = 0; |
| 159 | |
| 160 | this->~ClassTemplateDecl(); |
| 161 | C.Deallocate((void*)this); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame^] | 164 | ClassTemplatePartialSpecializationDecl * |
| 165 | ClassTemplateDecl::findPartialSpecialization(QualType T) { |
| 166 | ASTContext &Context = getASTContext(); |
| 167 | typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 168 | partial_spec_iterator; |
| 169 | for (partial_spec_iterator P = getPartialSpecializations().begin(), |
| 170 | PEnd = getPartialSpecializations().end(); |
| 171 | P != PEnd; ++P) { |
| 172 | if (Context.hasSameType(Context.getTypeDeclType(&*P), T)) |
| 173 | return &*P; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 179 | QualType ClassTemplateDecl::getInjectedClassNameType(ASTContext &Context) { |
| 180 | if (!CommonPtr->InjectedClassNameType.isNull()) |
| 181 | return CommonPtr->InjectedClassNameType; |
| 182 | |
| 183 | // FIXME: n2800 14.6.1p1 should say how the template arguments |
| 184 | // corresponding to template parameter packs should be pack |
| 185 | // expansions. We already say that in 14.6.2.1p2, so it would be |
| 186 | // better to fix that redundancy. |
| 187 | |
| 188 | TemplateParameterList *Params = getTemplateParameters(); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 189 | llvm::SmallVector<TemplateArgument, 16> TemplateArgs; |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 190 | TemplateArgs.reserve(Params->size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 191 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 192 | ParamEnd = Params->end(); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 193 | Param != ParamEnd; ++Param) { |
| 194 | if (isa<TemplateTypeParmDecl>(*Param)) { |
| 195 | QualType ParamType = Context.getTypeDeclType(cast<TypeDecl>(*Param)); |
| 196 | TemplateArgs.push_back(TemplateArgument((*Param)->getLocation(), |
| 197 | ParamType)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 198 | } else if (NonTypeTemplateParmDecl *NTTP = |
| 199 | dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 200 | Expr *E = new (Context) DeclRefExpr(NTTP, NTTP->getType(), |
| 201 | NTTP->getLocation(), |
| 202 | NTTP->getType()->isDependentType(), |
| 203 | /*Value-dependent=*/true); |
| 204 | TemplateArgs.push_back(TemplateArgument(E)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 205 | } else { |
| 206 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param); |
| 207 | TemplateArgs.push_back(TemplateArgument(TTP->getLocation(), TTP)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 211 | CommonPtr->InjectedClassNameType |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 212 | = Context.getTemplateSpecializationType(TemplateName(this), |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 213 | &TemplateArgs[0], |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 214 | TemplateArgs.size()); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 215 | return CommonPtr->InjectedClassNameType; |
| 216 | } |
| 217 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 218 | //===----------------------------------------------------------------------===// |
| 219 | // TemplateTypeParm Allocation/Deallocation Method Implementations |
| 220 | //===----------------------------------------------------------------------===// |
| 221 | |
| 222 | TemplateTypeParmDecl * |
| 223 | TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 224 | SourceLocation L, unsigned D, unsigned P, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 225 | IdentifierInfo *Id, bool Typename, |
| 226 | bool ParameterPack) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 227 | QualType Type = C.getTemplateTypeParmType(D, P, ParameterPack, Id); |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 228 | return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type, ParameterPack); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | //===----------------------------------------------------------------------===// |
| 232 | // NonTypeTemplateParmDecl Method Implementations |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | |
| 235 | NonTypeTemplateParmDecl * |
| 236 | NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 237 | SourceLocation L, unsigned D, unsigned P, |
| 238 | IdentifierInfo *Id, QualType T, |
| 239 | SourceLocation TypeSpecStartLoc) { |
| 240 | return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, |
| 241 | TypeSpecStartLoc); |
| 242 | } |
| 243 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 244 | SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const { |
| 245 | return DefaultArgument? DefaultArgument->getSourceRange().getBegin() |
| 246 | : SourceLocation(); |
| 247 | } |
| 248 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 249 | //===----------------------------------------------------------------------===// |
| 250 | // TemplateTemplateParmDecl Method Implementations |
| 251 | //===----------------------------------------------------------------------===// |
| 252 | |
| 253 | TemplateTemplateParmDecl * |
| 254 | TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 255 | SourceLocation L, unsigned D, unsigned P, |
| 256 | IdentifierInfo *Id, |
| 257 | TemplateParameterList *Params) { |
| 258 | return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params); |
| 259 | } |
| 260 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 261 | SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const { |
| 262 | return DefaultArgument? DefaultArgument->getSourceRange().getBegin() |
| 263 | : SourceLocation(); |
| 264 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 265 | |
| 266 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 267 | // TemplateArgument Implementation |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | |
| 270 | TemplateArgument::TemplateArgument(Expr *E) : Kind(Expression) { |
| 271 | TypeOrValue = reinterpret_cast<uintptr_t>(E); |
| 272 | StartLoc = E->getSourceRange().getBegin(); |
| 273 | } |
| 274 | |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 275 | /// \brief Construct a template argument pack. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 276 | void TemplateArgument::setArgumentPack(TemplateArgument *args, unsigned NumArgs, |
| 277 | bool CopyArgs) { |
| 278 | assert(isNull() && "Must call setArgumentPack on a null argument"); |
| 279 | |
| 280 | Kind = Pack; |
| 281 | Args.NumArgs = NumArgs; |
| 282 | Args.CopyArgs = CopyArgs; |
| 283 | if (!Args.CopyArgs) { |
| 284 | Args.Args = args; |
| 285 | return; |
| 286 | } |
| 287 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 288 | // FIXME: Allocate in ASTContext |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 289 | Args.Args = new TemplateArgument[NumArgs]; |
| 290 | for (unsigned I = 0; I != Args.NumArgs; ++I) |
| 291 | Args.Args[I] = args[I]; |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 294 | //===----------------------------------------------------------------------===// |
Anders Carlsson | 9ba4164 | 2009-06-05 05:31:27 +0000 | [diff] [blame] | 295 | // TemplateArgumentListBuilder Implementation |
| 296 | //===----------------------------------------------------------------------===// |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 297 | |
| 298 | void TemplateArgumentListBuilder::Append(const TemplateArgument& Arg) { |
Anders Carlsson | 9ba4164 | 2009-06-05 05:31:27 +0000 | [diff] [blame] | 299 | switch (Arg.getKind()) { |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 300 | default: break; |
| 301 | case TemplateArgument::Type: |
| 302 | assert(Arg.getAsType()->isCanonical() && "Type must be canonical!"); |
| 303 | break; |
Anders Carlsson | 9ba4164 | 2009-06-05 05:31:27 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 306 | assert(NumFlatArgs < MaxFlatArgs && "Argument list builder is full!"); |
| 307 | assert(!StructuredArgs && |
| 308 | "Can't append arguments when an argument pack has been added!"); |
Anders Carlsson | 3b36b66 | 2009-06-15 17:56:45 +0000 | [diff] [blame] | 309 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 310 | if (!FlatArgs) |
| 311 | FlatArgs = new TemplateArgument[MaxFlatArgs]; |
| 312 | |
| 313 | FlatArgs[NumFlatArgs++] = Arg; |
Anders Carlsson | 9ba4164 | 2009-06-05 05:31:27 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 316 | void TemplateArgumentListBuilder::BeginPack() { |
| 317 | assert(!AddingToPack && "Already adding to pack!"); |
| 318 | assert(!StructuredArgs && "Argument list already contains a pack!"); |
| 319 | |
| 320 | AddingToPack = true; |
| 321 | PackBeginIndex = NumFlatArgs; |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 324 | void TemplateArgumentListBuilder::EndPack() { |
| 325 | assert(AddingToPack && "Not adding to pack!"); |
| 326 | assert(!StructuredArgs && "Argument list already contains a pack!"); |
| 327 | |
| 328 | AddingToPack = false; |
Anders Carlsson | 3b36b66 | 2009-06-15 17:56:45 +0000 | [diff] [blame] | 329 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 330 | StructuredArgs = new TemplateArgument[MaxStructuredArgs]; |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 331 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 332 | // First copy the flat entries over to the list (if any) |
| 333 | for (unsigned I = 0; I != PackBeginIndex; ++I) { |
| 334 | NumStructuredArgs++; |
| 335 | StructuredArgs[I] = FlatArgs[I]; |
| 336 | } |
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 | // Next, set the pack. |
| 339 | TemplateArgument *PackArgs = 0; |
| 340 | unsigned NumPackArgs = NumFlatArgs - PackBeginIndex; |
| 341 | if (NumPackArgs) |
| 342 | PackArgs = &FlatArgs[PackBeginIndex]; |
| 343 | |
| 344 | StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs, |
| 345 | /*CopyArgs=*/false); |
| 346 | } |
| 347 | |
| 348 | void TemplateArgumentListBuilder::ReleaseArgs() { |
| 349 | FlatArgs = 0; |
| 350 | NumFlatArgs = 0; |
| 351 | MaxFlatArgs = 0; |
| 352 | StructuredArgs = 0; |
| 353 | NumStructuredArgs = 0; |
| 354 | MaxStructuredArgs = 0; |
| 355 | } |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 356 | |
Anders Carlsson | 9ba4164 | 2009-06-05 05:31:27 +0000 | [diff] [blame] | 357 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 358 | // TemplateArgumentList Implementation |
| 359 | //===----------------------------------------------------------------------===// |
| 360 | TemplateArgumentList::TemplateArgumentList(ASTContext &Context, |
Anders Carlsson | e9c904b | 2009-06-05 04:47:51 +0000 | [diff] [blame] | 361 | TemplateArgumentListBuilder &Builder, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 362 | bool TakeArgs) |
| 363 | : FlatArguments(Builder.getFlatArguments(), TakeArgs), |
| 364 | NumFlatArguments(Builder.flatSize()), |
| 365 | StructuredArguments(Builder.getStructuredArguments(), TakeArgs), |
| 366 | NumStructuredArguments(Builder.structuredSize()) { |
Anders Carlsson | e9c904b | 2009-06-05 04:47:51 +0000 | [diff] [blame] | 367 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 368 | if (!TakeArgs) |
| 369 | return; |
| 370 | |
| 371 | if (Builder.getStructuredArguments() == Builder.getFlatArguments()) |
| 372 | StructuredArguments.setInt(0); |
| 373 | Builder.ReleaseArgs(); |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | TemplateArgumentList::~TemplateArgumentList() { |
| 377 | // FIXME: Deallocate template arguments |
| 378 | } |
| 379 | |
| 380 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 381 | // ClassTemplateSpecializationDecl Implementation |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | ClassTemplateSpecializationDecl:: |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 384 | ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 385 | DeclContext *DC, SourceLocation L, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 386 | ClassTemplateDecl *SpecializedTemplate, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 387 | TemplateArgumentListBuilder &Builder, |
| 388 | ClassTemplateSpecializationDecl *PrevDecl) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 389 | : CXXRecordDecl(DK, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 390 | SpecializedTemplate->getTemplatedDecl()->getTagKind(), |
| 391 | DC, L, |
| 392 | // FIXME: Should we use DeclarationName for the name of |
| 393 | // class template specializations? |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 394 | SpecializedTemplate->getIdentifier(), |
| 395 | PrevDecl), |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 396 | SpecializedTemplate(SpecializedTemplate), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 397 | TemplateArgs(Context, Builder, /*TakeArgs=*/true), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 398 | SpecializationKind(TSK_Undeclared) { |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | ClassTemplateSpecializationDecl * |
| 402 | ClassTemplateSpecializationDecl::Create(ASTContext &Context, |
| 403 | DeclContext *DC, SourceLocation L, |
| 404 | ClassTemplateDecl *SpecializedTemplate, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 405 | TemplateArgumentListBuilder &Builder, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 406 | ClassTemplateSpecializationDecl *PrevDecl) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 407 | ClassTemplateSpecializationDecl *Result |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 408 | = new (Context)ClassTemplateSpecializationDecl(Context, |
| 409 | ClassTemplateSpecialization, |
| 410 | DC, L, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 411 | SpecializedTemplate, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 412 | Builder, |
| 413 | PrevDecl); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 414 | Context.getTypeDeclType(Result, PrevDecl); |
| 415 | return Result; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 416 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 417 | |
| 418 | //===----------------------------------------------------------------------===// |
| 419 | // ClassTemplatePartialSpecializationDecl Implementation |
| 420 | //===----------------------------------------------------------------------===// |
| 421 | ClassTemplatePartialSpecializationDecl * |
| 422 | ClassTemplatePartialSpecializationDecl:: |
| 423 | Create(ASTContext &Context, DeclContext *DC, SourceLocation L, |
| 424 | TemplateParameterList *Params, |
| 425 | ClassTemplateDecl *SpecializedTemplate, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 426 | TemplateArgumentListBuilder &Builder, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 427 | ClassTemplatePartialSpecializationDecl *PrevDecl) { |
| 428 | ClassTemplatePartialSpecializationDecl *Result |
| 429 | = new (Context)ClassTemplatePartialSpecializationDecl(Context, |
| 430 | DC, L, Params, |
| 431 | SpecializedTemplate, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 432 | Builder, PrevDecl); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 433 | Result->setSpecializationKind(TSK_ExplicitSpecialization); |
| 434 | Context.getTypeDeclType(Result, PrevDecl); |
| 435 | return Result; |
| 436 | } |