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, |
| 96 | NamedDecl *Decl) { |
| 97 | return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl); |
| 98 | } |
| 99 | |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | // TemplateTypeParm Allocation/Deallocation Method Implementations |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
| 104 | TemplateTypeParmDecl * |
| 105 | TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 106 | SourceLocation L, unsigned D, unsigned P, |
| 107 | IdentifierInfo *Id, bool Typename) { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 108 | QualType Type = C.getTemplateTypeParmType(D, P, Id); |
| 109 | return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | // NonTypeTemplateParmDecl Method Implementations |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | |
| 116 | NonTypeTemplateParmDecl * |
| 117 | NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 118 | SourceLocation L, unsigned D, unsigned P, |
| 119 | IdentifierInfo *Id, QualType T, |
| 120 | SourceLocation TypeSpecStartLoc) { |
| 121 | return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, |
| 122 | TypeSpecStartLoc); |
| 123 | } |
| 124 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 125 | SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const { |
| 126 | return DefaultArgument? DefaultArgument->getSourceRange().getBegin() |
| 127 | : SourceLocation(); |
| 128 | } |
| 129 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 130 | //===----------------------------------------------------------------------===// |
| 131 | // TemplateTemplateParmDecl Method Implementations |
| 132 | //===----------------------------------------------------------------------===// |
| 133 | |
| 134 | TemplateTemplateParmDecl * |
| 135 | TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, |
| 136 | SourceLocation L, unsigned D, unsigned P, |
| 137 | IdentifierInfo *Id, |
| 138 | TemplateParameterList *Params) { |
| 139 | return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params); |
| 140 | } |
| 141 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 142 | SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const { |
| 143 | return DefaultArgument? DefaultArgument->getSourceRange().getBegin() |
| 144 | : SourceLocation(); |
| 145 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame^] | 146 | |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | // ClassTemplateSpecializationDecl Implementation |
| 149 | //===----------------------------------------------------------------------===// |
| 150 | ClassTemplateSpecializationDecl:: |
| 151 | ClassTemplateSpecializationDecl(DeclContext *DC, SourceLocation L, |
| 152 | ClassTemplateDecl *SpecializedTemplate, |
| 153 | TemplateArgument *TemplateArgs, |
| 154 | unsigned NumTemplateArgs) |
| 155 | : CXXRecordDecl(ClassTemplateSpecialization, |
| 156 | SpecializedTemplate->getTemplatedDecl()->getTagKind(), |
| 157 | DC, L, |
| 158 | // FIXME: Should we use DeclarationName for the name of |
| 159 | // class template specializations? |
| 160 | SpecializedTemplate->getIdentifier()), |
| 161 | SpecializedTemplate(SpecializedTemplate), |
| 162 | NumTemplateArgs(NumTemplateArgs) { |
| 163 | TemplateArgument *Arg = reinterpret_cast<TemplateArgument *>(this + 1); |
| 164 | for (unsigned ArgIdx = 0; ArgIdx < NumTemplateArgs; ++ArgIdx, ++Arg) |
| 165 | *Arg = TemplateArgs[ArgIdx]; |
| 166 | } |
| 167 | |
| 168 | ClassTemplateSpecializationDecl * |
| 169 | ClassTemplateSpecializationDecl::Create(ASTContext &Context, |
| 170 | DeclContext *DC, SourceLocation L, |
| 171 | ClassTemplateDecl *SpecializedTemplate, |
| 172 | TemplateArgument *TemplateArgs, |
| 173 | unsigned NumTemplateArgs) { |
| 174 | unsigned Size = sizeof(ClassTemplateSpecializationDecl) + |
| 175 | sizeof(TemplateArgument) * NumTemplateArgs; |
| 176 | unsigned Align = llvm::AlignOf<ClassTemplateSpecializationDecl>::Alignment; |
| 177 | void *Mem = Context.Allocate(Size, Align); |
| 178 | return new (Mem) ClassTemplateSpecializationDecl(DC, L, SpecializedTemplate, |
| 179 | TemplateArgs, |
| 180 | NumTemplateArgs); |
| 181 | } |