blob: c2ae5d91ab69b4f4fd4ee33788011bd69245ceff [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
17#include "clang/Basic/IdentifierTable.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000018
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Chris Lattnerd3b90652008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000022// Decl Allocation/Deallocation Method Implementations
23//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000024
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000025TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
26 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
27 return new (Mem) TranslationUnitDecl();
28}
29
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000030NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
31 SourceLocation L, IdentifierInfo *Id) {
32 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
33 return new (Mem) NamespaceDecl(DC, L, Id);
34}
35
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000036void NamespaceDecl::Destroy(ASTContext& C) {
37 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
38 // together. They are all top-level Decls.
39
Ted Kremenekebf27b12008-05-24 15:09:56 +000040 this->~NamespaceDecl();
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000041 C.getAllocator().Deallocate((void *)this);
42}
43
44
Chris Lattner41110242008-06-17 18:05:57 +000045ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
46 SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) {
47 void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>();
48 return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl);
49}
50
Chris Lattner9fdf9c62008-04-22 18:39:57 +000051VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff248a7532008-04-15 22:42:06 +000052 SourceLocation L,
53 IdentifierInfo *Id, QualType T,
54 StorageClass S, ScopedDecl *PrevDecl) {
55 void *Mem = C.getAllocator().Allocate<VarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +000056 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +000057}
58
Chris Lattner9fdf9c62008-04-22 18:39:57 +000059ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000060 SourceLocation L, IdentifierInfo *Id,
61 QualType T, StorageClass S,
Chris Lattner04421082008-04-08 04:40:51 +000062 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +000063 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +000064 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +000065}
66
Chris Lattner9fdf9c62008-04-22 18:39:57 +000067FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000068 SourceLocation L,
Chris Lattnera98e58d2008-03-15 21:24:04 +000069 IdentifierInfo *Id, QualType T,
70 StorageClass S, bool isInline,
71 ScopedDecl *PrevDecl) {
72 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000073 return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattnera98e58d2008-03-15 21:24:04 +000074}
75
Chris Lattnerb048c982008-04-06 04:47:34 +000076FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +000077 IdentifierInfo *Id, QualType T, Expr *BW) {
78 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerb048c982008-04-06 04:47:34 +000079 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner8e25d862008-03-16 00:16:02 +000080}
81
Chris Lattnera98e58d2008-03-15 21:24:04 +000082
Chris Lattner0ed844b2008-04-04 06:12:32 +000083EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
84 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +000085 IdentifierInfo *Id, QualType T,
86 Expr *E, const llvm::APSInt &V,
87 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000088 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +000089 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000090}
91
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000092void EnumConstantDecl::Destroy(ASTContext& C) {
93 if (Init) Init->Destroy(C);
94 Decl::Destroy(C);
95}
96
Chris Lattner9fdf9c62008-04-22 18:39:57 +000097TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000098 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +000099 IdentifierInfo *Id, QualType T,
100 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000101 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000102 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000103}
104
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000105EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000106 IdentifierInfo *Id,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000107 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000108 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000109 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000110}
111
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000112void EnumDecl::Destroy(ASTContext& C) {
113 if (getEnumConstantList()) getEnumConstantList()->Destroy(C);
114 Decl::Destroy(C);
115}
116
117//==------------------------------------------------------------------------==//
118// RecordDecl methods.
119//==------------------------------------------------------------------------==//
120
121RecordDecl::RecordDecl(Kind DK, DeclContext *DC, SourceLocation L,
122 IdentifierInfo *Id, RecordDecl *PrevDecl)
123 : TagDecl(DK, DC, L, Id, 0), NextDecl(0) {
124
125 HasFlexibleArrayMember = false;
126 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
127 Members = 0;
128 NumMembers = -1;
129
130 // Hook up the RecordDecl chain.
131 if (PrevDecl) {
132 RecordDecl* Tmp = PrevDecl->NextDecl;
133 // 'Tmp' might be non-NULL if it is the RecordDecl that provides the
134 // definition of the struct/union. By construction, the last RecordDecl
135 // in the chain is the 'defining' RecordDecl.
136 if (Tmp) {
137 assert (Tmp->NextDecl == 0);
138 assert (Tmp->Members && "Previous RecordDecl has a NextDecl that is "
139 "not the 'defining' RecordDecl");
140
141 NextDecl = Tmp;
142 }
143
144 PrevDecl->NextDecl = this;
145 }
146}
147
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000148RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000149 SourceLocation L, IdentifierInfo *Id,
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000150 RecordDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000151 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000152 Kind DK;
153 switch (TK) {
Ted Kremenek9bcf44a2008-06-16 23:45:12 +0000154 default: assert(0 && "Invalid TagKind!");
155 case TK_enum: assert(0 && "Enum TagKind passed for Record!");
156 case TK_struct: DK = Struct; break;
157 case TK_union: DK = Union; break;
158 case TK_class: DK = Class; break;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000159 }
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000160 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000161}
162
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000163
Chris Lattner0ed844b2008-04-04 06:12:32 +0000164FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
165 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000166 StringLiteral *Str) {
167 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
168 return new (Mem) FileScopeAsmDecl(L, Str);
169}
170
Chris Lattner0ed844b2008-04-04 06:12:32 +0000171LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
172 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000173 LanguageIDs Lang, Decl *D) {
174 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
175 return new (Mem) LinkageSpecDecl(L, Lang, D);
176}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000177
178//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000179// NamedDecl Implementation
180//===----------------------------------------------------------------------===//
181
Chris Lattnerfd5de472007-10-06 22:53:46 +0000182const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 if (const IdentifierInfo *II = getIdentifier())
184 return II->getName();
185 return "";
186}
187
Chris Lattner8a934232008-03-31 00:36:02 +0000188//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000189// FunctionDecl Implementation
190//===----------------------------------------------------------------------===//
191
Reid Spencer5f016e22007-07-11 17:01:13 +0000192FunctionDecl::~FunctionDecl() {
193 delete[] ParamInfo;
Douglas Gregorf0097952008-04-21 02:02:58 +0000194}
195
Ted Kremenek27f8a282008-05-20 00:43:19 +0000196void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000197 if (Body)
198 Body->Destroy(C);
199
200 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
201 (*I)->Destroy(C);
202
Ted Kremenek27f8a282008-05-20 00:43:19 +0000203 Decl::Destroy(C);
204}
205
206
Douglas Gregorf0097952008-04-21 02:02:58 +0000207Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
208 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
209 if (FD->Body) {
210 Definition = FD;
211 return FD->Body;
212 }
213 }
214
215 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000216}
217
218unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000219 const FunctionType *FT = getType()->getAsFunctionType();
220 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000221 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000222 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000223}
224
225void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
226 assert(ParamInfo == 0 && "Already has param info!");
227 assert(NumParams == getNumParams() && "Parameter count mismatch!");
228
229 // Zero params -> null pointer.
230 if (NumParams) {
231 ParamInfo = new ParmVarDecl*[NumParams];
232 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
233 }
234}
235
Chris Lattner8123a952008-04-10 02:22:51 +0000236/// getMinRequiredArguments - Returns the minimum number of arguments
237/// needed to call this function. This may be fewer than the number of
238/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000239/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000240unsigned FunctionDecl::getMinRequiredArguments() const {
241 unsigned NumRequiredArgs = getNumParams();
242 while (NumRequiredArgs > 0
243 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
244 --NumRequiredArgs;
245
246 return NumRequiredArgs;
247}
248
Chris Lattner8a934232008-03-31 00:36:02 +0000249//===----------------------------------------------------------------------===//
250// RecordDecl Implementation
251//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000252
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000253RecordDecl::~RecordDecl() {
254 delete[] Members;
255}
256
257void RecordDecl::Destroy(ASTContext& C) {
258 if (isDefinition())
259 for (field_iterator I=field_begin(), E=field_end(); I!=E; ++I)
260 (*I)->Destroy(C);
261
262 TagDecl::Destroy(C);
263}
264
265
Reid Spencer5f016e22007-07-11 17:01:13 +0000266/// defineBody - When created, RecordDecl's correspond to a forward declared
267/// record. This method is used to mark the decl as being defined, with the
268/// specified contents.
269void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
270 assert(!isDefinition() && "Cannot redefine record!");
271 setDefinition(true);
272 NumMembers = numMembers;
273 if (numMembers) {
274 Members = new FieldDecl*[numMembers];
275 memcpy(Members, members, numMembers*sizeof(Decl*));
276 }
277}
278
Chris Lattner8a934232008-03-31 00:36:02 +0000279FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 if (Members == 0 || NumMembers < 0)
281 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000282
Chris Lattner8a934232008-03-31 00:36:02 +0000283 // Linear search. When C++ classes come along, will likely need to revisit.
284 for (int i = 0; i != NumMembers; ++i)
285 if (Members[i]->getIdentifier() == II)
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 return Members[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000288}