blob: 7389b83d20d2c9f4e607fdbbd3a41645deef03e8 [file] [log] [blame]
Douglas Gregoraaba5e32009-02-04 19:02:06 +00001//===--- 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 Gregor55f6b142009-02-09 18:46:07 +000016#include "clang/AST/Expr.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/ASTContext.h"
18#include "clang/Basic/IdentifierTable.h"
19#include "llvm/ADT/STLExtras.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// TemplateParameterList Implementation
24//===----------------------------------------------------------------------===//
25
Douglas Gregorddc29e12009-02-06 22:42:48 +000026TemplateParameterList::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 Gregoraaba5e32009-02-04 19:02:06 +000032 for (unsigned Idx = 0; Idx < NumParams; ++Idx)
33 begin()[Idx] = Params[Idx];
34}
35
36TemplateParameterList *
Douglas Gregorddc29e12009-02-06 22:42:48 +000037TemplateParameterList::Create(ASTContext &C, SourceLocation TemplateLoc,
38 SourceLocation LAngleLoc, Decl **Params,
39 unsigned NumParams, SourceLocation RAngleLoc) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +000040 unsigned Size = sizeof(TemplateParameterList) + sizeof(Decl *) * NumParams;
41 unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment;
42 void *Mem = C.Allocate(Size, Align);
Douglas Gregorddc29e12009-02-06 22:42:48 +000043 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
44 NumParams, RAngleLoc);
Douglas Gregoraaba5e32009-02-04 19:02:06 +000045}
46
Douglas Gregor62cb18d2009-02-11 18:16:40 +000047unsigned 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 Gregoraaba5e32009-02-04 19:02:06 +000067//===----------------------------------------------------------------------===//
68// TemplateDecl Implementation
69//===----------------------------------------------------------------------===//
70
71TemplateDecl::~TemplateDecl() {
72}
73
74//===----------------------------------------------------------------------===//
75// FunctionTemplateDecl Implementation
76//===----------------------------------------------------------------------===//
77
78FunctionTemplateDecl *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
91ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
92 DeclContext *DC,
93 SourceLocation L,
94 DeclarationName Name,
95 TemplateParameterList *Params,
Douglas Gregor5953d8b2009-03-19 17:26:29 +000096 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
108ClassTemplateDecl::~ClassTemplateDecl() {
109 assert(CommonPtr == 0 && "ClassTemplateDecl must be explicitly destroyed");
110}
111
112void 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 Gregoraaba5e32009-02-04 19:02:06 +0000121}
122
123//===----------------------------------------------------------------------===//
124// TemplateTypeParm Allocation/Deallocation Method Implementations
125//===----------------------------------------------------------------------===//
126
127TemplateTypeParmDecl *
128TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
129 SourceLocation L, unsigned D, unsigned P,
130 IdentifierInfo *Id, bool Typename) {
Douglas Gregorfab9d672009-02-05 23:33:38 +0000131 QualType Type = C.getTemplateTypeParmType(D, P, Id);
132 return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000133}
134
135//===----------------------------------------------------------------------===//
136// NonTypeTemplateParmDecl Method Implementations
137//===----------------------------------------------------------------------===//
138
139NonTypeTemplateParmDecl *
140NonTypeTemplateParmDecl::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 Gregord684b002009-02-10 19:49:53 +0000148SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
149 return DefaultArgument? DefaultArgument->getSourceRange().getBegin()
150 : SourceLocation();
151}
152
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000153//===----------------------------------------------------------------------===//
154// TemplateTemplateParmDecl Method Implementations
155//===----------------------------------------------------------------------===//
156
157TemplateTemplateParmDecl *
158TemplateTemplateParmDecl::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 Gregord684b002009-02-10 19:49:53 +0000165SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
166 return DefaultArgument? DefaultArgument->getSourceRange().getBegin()
167 : SourceLocation();
168}
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000169
170//===----------------------------------------------------------------------===//
Douglas Gregor40808ce2009-03-09 23:48:35 +0000171// TemplateArgument Implementation
172//===----------------------------------------------------------------------===//
173
174TemplateArgument::TemplateArgument(Expr *E) : Kind(Expression) {
175 TypeOrValue = reinterpret_cast<uintptr_t>(E);
176 StartLoc = E->getSourceRange().getBegin();
177}
178
179//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000180// ClassTemplateSpecializationDecl Implementation
181//===----------------------------------------------------------------------===//
182ClassTemplateSpecializationDecl::
183ClassTemplateSpecializationDecl(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 Gregorcc636682009-02-17 23:15:12 +0000194 NumTemplateArgs(NumTemplateArgs), SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000195 TemplateArgument *Arg = reinterpret_cast<TemplateArgument *>(this + 1);
196 for (unsigned ArgIdx = 0; ArgIdx < NumTemplateArgs; ++ArgIdx, ++Arg)
Douglas Gregor39a8de12009-02-25 19:37:18 +0000197 new (Arg) TemplateArgument(TemplateArgs[ArgIdx]);
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000198}
199
200ClassTemplateSpecializationDecl *
201ClassTemplateSpecializationDecl::Create(ASTContext &Context,
202 DeclContext *DC, SourceLocation L,
203 ClassTemplateDecl *SpecializedTemplate,
204 TemplateArgument *TemplateArgs,
Douglas Gregorcc636682009-02-17 23:15:12 +0000205 unsigned NumTemplateArgs,
206 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000207 unsigned Size = sizeof(ClassTemplateSpecializationDecl) +
208 sizeof(TemplateArgument) * NumTemplateArgs;
209 unsigned Align = llvm::AlignOf<ClassTemplateSpecializationDecl>::Alignment;
210 void *Mem = Context.Allocate(Size, Align);
Douglas Gregorcc636682009-02-17 23:15:12 +0000211 ClassTemplateSpecializationDecl *Result
212 = new (Mem) ClassTemplateSpecializationDecl(DC, L, SpecializedTemplate,
213 TemplateArgs, NumTemplateArgs);
Douglas Gregorcc636682009-02-17 23:15:12 +0000214 Context.getTypeDeclType(Result, PrevDecl);
215 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000216}