blob: 04cec6f66bb3382eac3a5e3ed598f71e73ef8246 [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
Chris Lattner0ed844b2008-04-04 06:12:32 +0000117FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
118 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000119 StringLiteral *Str) {
120 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
121 return new (Mem) FileScopeAsmDecl(L, Str);
122}
123
Chris Lattner0ed844b2008-04-04 06:12:32 +0000124LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
125 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000126 LanguageIDs Lang, Decl *D) {
127 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
128 return new (Mem) LinkageSpecDecl(L, Lang, D);
129}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000130
131//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000132// NamedDecl Implementation
133//===----------------------------------------------------------------------===//
134
Chris Lattnerfd5de472007-10-06 22:53:46 +0000135const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 if (const IdentifierInfo *II = getIdentifier())
137 return II->getName();
138 return "";
139}
140
Chris Lattner8a934232008-03-31 00:36:02 +0000141//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000142// FunctionDecl Implementation
143//===----------------------------------------------------------------------===//
144
Reid Spencer5f016e22007-07-11 17:01:13 +0000145FunctionDecl::~FunctionDecl() {
146 delete[] ParamInfo;
Douglas Gregorf0097952008-04-21 02:02:58 +0000147}
148
Ted Kremenek27f8a282008-05-20 00:43:19 +0000149void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000150 if (Body)
151 Body->Destroy(C);
152
153 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
154 (*I)->Destroy(C);
155
Ted Kremenek27f8a282008-05-20 00:43:19 +0000156 Decl::Destroy(C);
157}
158
159
Douglas Gregorf0097952008-04-21 02:02:58 +0000160Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
161 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
162 if (FD->Body) {
163 Definition = FD;
164 return FD->Body;
165 }
166 }
167
168 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000169}
170
171unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000172 const FunctionType *FT = getType()->getAsFunctionType();
173 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000174 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000175 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
178void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
179 assert(ParamInfo == 0 && "Already has param info!");
180 assert(NumParams == getNumParams() && "Parameter count mismatch!");
181
182 // Zero params -> null pointer.
183 if (NumParams) {
184 ParamInfo = new ParmVarDecl*[NumParams];
185 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
186 }
187}
188
Chris Lattner8123a952008-04-10 02:22:51 +0000189/// getMinRequiredArguments - Returns the minimum number of arguments
190/// needed to call this function. This may be fewer than the number of
191/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000192/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000193unsigned FunctionDecl::getMinRequiredArguments() const {
194 unsigned NumRequiredArgs = getNumParams();
195 while (NumRequiredArgs > 0
196 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
197 --NumRequiredArgs;
198
199 return NumRequiredArgs;
200}
201
Chris Lattner8a934232008-03-31 00:36:02 +0000202//===----------------------------------------------------------------------===//
203// RecordDecl Implementation
204//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000205
Ted Kremenek63597922008-09-02 21:12:32 +0000206RecordDecl::RecordDecl(Kind DK, DeclContext *DC, SourceLocation L,
207 IdentifierInfo *Id, RecordDecl *PrevDecl)
208: TagDecl(DK, DC, L, Id, 0), NextDecl(0) {
209
210 HasFlexibleArrayMember = false;
211 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
212 Members = 0;
213 NumMembers = -1;
214
215 // Hook up the RecordDecl chain.
216 if (PrevDecl) {
217 RecordDecl* Tmp = PrevDecl->NextDecl;
218 // 'Tmp' might be non-NULL if it is the RecordDecl that provides the
219 // definition of the struct/union. By construction, the last RecordDecl
220 // in the chain is the 'defining' RecordDecl.
221 if (Tmp) {
222 assert (Tmp->NextDecl == 0);
223 assert (Tmp->isDefinition()
224 && "Previous RecordDecl has a NextDecl that is "
225 "not the 'defining' RecordDecl");
226
227 NextDecl = Tmp;
228 }
229
230 PrevDecl->NextDecl = this;
231 }
232}
233
234RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
235 SourceLocation L, IdentifierInfo *Id,
236 RecordDecl *PrevDecl) {
237 void *Mem = C.getAllocator().Allocate<RecordDecl>();
238 Kind DK;
239 switch (TK) {
240 default: assert(0 && "Invalid TagKind!");
241 case TK_enum: assert(0 && "Enum TagKind passed for Record!");
242 case TK_struct: DK = Struct; break;
243 case TK_union: DK = Union; break;
244 case TK_class: DK = Class; break;
245 }
246 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
247}
248
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000249RecordDecl::~RecordDecl() {
250 delete[] Members;
251}
252
253void RecordDecl::Destroy(ASTContext& C) {
254 if (isDefinition())
255 for (field_iterator I=field_begin(), E=field_end(); I!=E; ++I)
256 (*I)->Destroy(C);
257
258 TagDecl::Destroy(C);
259}
260
Reid Spencer5f016e22007-07-11 17:01:13 +0000261/// defineBody - When created, RecordDecl's correspond to a forward declared
262/// record. This method is used to mark the decl as being defined, with the
263/// specified contents.
264void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
265 assert(!isDefinition() && "Cannot redefine record!");
266 setDefinition(true);
267 NumMembers = numMembers;
268 if (numMembers) {
269 Members = new FieldDecl*[numMembers];
270 memcpy(Members, members, numMembers*sizeof(Decl*));
271 }
272}
273
Chris Lattner8a934232008-03-31 00:36:02 +0000274FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 if (Members == 0 || NumMembers < 0)
276 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000277
Chris Lattner8a934232008-03-31 00:36:02 +0000278 // Linear search. When C++ classes come along, will likely need to revisit.
279 for (int i = 0; i != NumMembers; ++i)
280 if (Members[i]->getIdentifier() == II)
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 return Members[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000283}
Ted Kremenek63597922008-09-02 21:12:32 +0000284
285/// getDefinitionDecl - Returns the RecordDecl for the struct/union that
286/// represents the actual definition (i.e., not a forward declaration).
287/// This method returns NULL if no such RecordDecl exists.
288const RecordDecl* RecordDecl::getDefinitionDecl() const {
289 const RecordDecl* R = this;
290
291 for (RecordDecl* N = R->NextDecl; N; N = R->NextDecl)
292 R = N;
293
294 return R->Members ? R : 0;
295}
296