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