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; |
| 53 | if (!(isa<TemplateTypeParmDecl>(*Param) && |
| 54 | cast<TemplateTypeParmDecl>(*Param)->hasDefaultArgument()) && |
| 55 | !(isa<NonTypeTemplateParmDecl>(*Param) && |
| 56 | cast<NonTypeTemplateParmDecl>(*Param)->hasDefaultArgument()) && |
| 57 | !(isa<TemplateTemplateParmDecl>(*Param) && |
| 58 | cast<TemplateTemplateParmDecl>(*Param)->hasDefaultArgument())) |
| 59 | break; |
| 60 | |
| 61 | --NumRequiredArgs; |
| 62 | } |
| 63 | |
| 64 | return NumRequiredArgs; |
| 65 | } |
| 66 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // TemplateDecl Implementation |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | TemplateDecl::~TemplateDecl() { |
| 72 | } |
| 73 | |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | // FunctionTemplateDecl Implementation |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | |
| 78 | FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C, |
| 79 | DeclContext *DC, |
| 80 | SourceLocation L, |
| 81 | DeclarationName Name, |
| 82 | TemplateParameterList *Params, |
| 83 | NamedDecl *Decl) { |
| 84 | return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl); |
| 85 | } |
| 86 | |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | // ClassTemplateDecl Implementation |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | |
| 91 | ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, |
| 92 | DeclContext *DC, |
| 93 | SourceLocation L, |
| 94 | DeclarationName Name, |
| 95 | TemplateParameterList *Params, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame^] | 96 | NamedDecl *Decl, |
| 97 | ClassTemplateDecl *PrevDecl) { |
| 98 | Common *CommonPtr; |
| 99 | if (PrevDecl) |
| 100 | CommonPtr = PrevDecl->CommonPtr; |
| 101 | else |
| 102 | CommonPtr = new (C) Common; |
| 103 | |
| 104 | return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl, PrevDecl, |
| 105 | CommonPtr); |
| 106 | } |
| 107 | |
| 108 | ClassTemplateDecl::~ClassTemplateDecl() { |
| 109 | assert(CommonPtr == 0 && "ClassTemplateDecl must be explicitly destroyed"); |
| 110 | } |
| 111 | |
| 112 | void ClassTemplateDecl::Destroy(ASTContext& C) { |
| 113 | if (!PreviousDeclaration) { |
| 114 | CommonPtr->~Common(); |
| 115 | C.Deallocate((void*)CommonPtr); |
| 116 | } |
| 117 | CommonPtr = 0; |
| 118 | |
| 119 | this->~ClassTemplateDecl(); |
| 120 | C.Deallocate((void*)this); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | // TemplateTypeParm Allocation/Deallocation Method Implementations |
| 125 | //===----------------------------------------------------------------------===// |
| 126 | |
| 127 | TemplateTypeParmDecl * |
| 128 | TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 129 | SourceLocation L, unsigned D, unsigned P, |
| 130 | IdentifierInfo *Id, bool Typename) { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 131 | QualType Type = C.getTemplateTypeParmType(D, P, Id); |
| 132 | return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | // NonTypeTemplateParmDecl Method Implementations |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | |
| 139 | NonTypeTemplateParmDecl * |
| 140 | NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 141 | SourceLocation L, unsigned D, unsigned P, |
| 142 | IdentifierInfo *Id, QualType T, |
| 143 | SourceLocation TypeSpecStartLoc) { |
| 144 | return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, |
| 145 | TypeSpecStartLoc); |
| 146 | } |
| 147 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 148 | SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const { |
| 149 | return DefaultArgument? DefaultArgument->getSourceRange().getBegin() |
| 150 | : SourceLocation(); |
| 151 | } |
| 152 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 153 | //===----------------------------------------------------------------------===// |
| 154 | // TemplateTemplateParmDecl Method Implementations |
| 155 | //===----------------------------------------------------------------------===// |
| 156 | |
| 157 | TemplateTemplateParmDecl * |
| 158 | TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 159 | SourceLocation L, unsigned D, unsigned P, |
| 160 | IdentifierInfo *Id, |
| 161 | TemplateParameterList *Params) { |
| 162 | return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params); |
| 163 | } |
| 164 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 165 | SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const { |
| 166 | return DefaultArgument? DefaultArgument->getSourceRange().getBegin() |
| 167 | : SourceLocation(); |
| 168 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 169 | |
| 170 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 171 | // TemplateArgument Implementation |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
| 174 | TemplateArgument::TemplateArgument(Expr *E) : Kind(Expression) { |
| 175 | TypeOrValue = reinterpret_cast<uintptr_t>(E); |
| 176 | StartLoc = E->getSourceRange().getBegin(); |
| 177 | } |
| 178 | |
| 179 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 180 | // ClassTemplateSpecializationDecl Implementation |
| 181 | //===----------------------------------------------------------------------===// |
| 182 | ClassTemplateSpecializationDecl:: |
| 183 | ClassTemplateSpecializationDecl(DeclContext *DC, SourceLocation L, |
| 184 | ClassTemplateDecl *SpecializedTemplate, |
| 185 | TemplateArgument *TemplateArgs, |
| 186 | unsigned NumTemplateArgs) |
| 187 | : CXXRecordDecl(ClassTemplateSpecialization, |
| 188 | SpecializedTemplate->getTemplatedDecl()->getTagKind(), |
| 189 | DC, L, |
| 190 | // FIXME: Should we use DeclarationName for the name of |
| 191 | // class template specializations? |
| 192 | SpecializedTemplate->getIdentifier()), |
| 193 | SpecializedTemplate(SpecializedTemplate), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 194 | NumTemplateArgs(NumTemplateArgs), SpecializationKind(TSK_Undeclared) { |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 195 | TemplateArgument *Arg = reinterpret_cast<TemplateArgument *>(this + 1); |
| 196 | for (unsigned ArgIdx = 0; ArgIdx < NumTemplateArgs; ++ArgIdx, ++Arg) |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 197 | new (Arg) TemplateArgument(TemplateArgs[ArgIdx]); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | ClassTemplateSpecializationDecl * |
| 201 | ClassTemplateSpecializationDecl::Create(ASTContext &Context, |
| 202 | DeclContext *DC, SourceLocation L, |
| 203 | ClassTemplateDecl *SpecializedTemplate, |
| 204 | TemplateArgument *TemplateArgs, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 205 | unsigned NumTemplateArgs, |
| 206 | ClassTemplateSpecializationDecl *PrevDecl) { |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 207 | unsigned Size = sizeof(ClassTemplateSpecializationDecl) + |
| 208 | sizeof(TemplateArgument) * NumTemplateArgs; |
| 209 | unsigned Align = llvm::AlignOf<ClassTemplateSpecializationDecl>::Alignment; |
| 210 | void *Mem = Context.Allocate(Size, Align); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 211 | ClassTemplateSpecializationDecl *Result |
| 212 | = new (Mem) ClassTemplateSpecializationDecl(DC, L, SpecializedTemplate, |
| 213 | TemplateArgs, NumTemplateArgs); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 214 | Context.getTypeDeclType(Result, PrevDecl); |
| 215 | return Result; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 216 | } |