blob: 3ddf81974b682ed9b7bc422e2c9d0d254eb45ae0 [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,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000054 StorageClass S, ScopedDecl *PrevDecl,
55 SourceLocation TypeSpecStartLoc) {
Steve Naroff248a7532008-04-15 22:42:06 +000056 void *Mem = C.getAllocator().Allocate<VarDecl>();
Steve Naroff0eb07bf2008-10-03 00:02:03 +000057 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc);
Chris Lattner9e151e12008-03-15 21:10:16 +000058}
59
Chris Lattner9fdf9c62008-04-22 18:39:57 +000060ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000061 SourceLocation L, IdentifierInfo *Id,
62 QualType T, StorageClass S,
Chris Lattner04421082008-04-08 04:40:51 +000063 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +000064 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +000065 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +000066}
67
Chris Lattner9fdf9c62008-04-22 18:39:57 +000068FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000069 SourceLocation L,
Chris Lattnera98e58d2008-03-15 21:24:04 +000070 IdentifierInfo *Id, QualType T,
71 StorageClass S, bool isInline,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000072 ScopedDecl *PrevDecl,
73 SourceLocation TypeSpecStartLoc) {
Chris Lattnera98e58d2008-03-15 21:24:04 +000074 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Steve Naroff0eb07bf2008-10-03 00:02:03 +000075 return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl,
76 TypeSpecStartLoc);
Chris Lattnera98e58d2008-03-15 21:24:04 +000077}
78
Chris Lattnerb048c982008-04-06 04:47:34 +000079FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +000080 IdentifierInfo *Id, QualType T, Expr *BW) {
81 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerb048c982008-04-06 04:47:34 +000082 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner8e25d862008-03-16 00:16:02 +000083}
84
Chris Lattnera98e58d2008-03-15 21:24:04 +000085
Chris Lattner0ed844b2008-04-04 06:12:32 +000086EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
87 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +000088 IdentifierInfo *Id, QualType T,
89 Expr *E, const llvm::APSInt &V,
90 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000091 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +000092 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000093}
94
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000095void EnumConstantDecl::Destroy(ASTContext& C) {
96 if (Init) Init->Destroy(C);
97 Decl::Destroy(C);
98}
99
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000100TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000101 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000102 IdentifierInfo *Id, QualType T,
103 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000104 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000105 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000106}
107
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000108EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000109 IdentifierInfo *Id,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000110 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000111 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000112 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000113}
114
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000115void EnumDecl::Destroy(ASTContext& C) {
116 if (getEnumConstantList()) getEnumConstantList()->Destroy(C);
117 Decl::Destroy(C);
118}
119
Chris Lattner0ed844b2008-04-04 06:12:32 +0000120FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
121 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000122 StringLiteral *Str) {
123 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
124 return new (Mem) FileScopeAsmDecl(L, Str);
125}
126
Chris Lattner0ed844b2008-04-04 06:12:32 +0000127LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
128 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000129 LanguageIDs Lang, Decl *D) {
130 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
131 return new (Mem) LinkageSpecDecl(L, Lang, D);
132}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000133
134//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000135// NamedDecl Implementation
136//===----------------------------------------------------------------------===//
137
Chris Lattnerfd5de472007-10-06 22:53:46 +0000138const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 if (const IdentifierInfo *II = getIdentifier())
140 return II->getName();
141 return "";
142}
143
Chris Lattner8a934232008-03-31 00:36:02 +0000144//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000145// FunctionDecl Implementation
146//===----------------------------------------------------------------------===//
147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148FunctionDecl::~FunctionDecl() {
149 delete[] ParamInfo;
Douglas Gregorf0097952008-04-21 02:02:58 +0000150}
151
Ted Kremenek27f8a282008-05-20 00:43:19 +0000152void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000153 if (Body)
154 Body->Destroy(C);
155
156 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
157 (*I)->Destroy(C);
158
Ted Kremenek27f8a282008-05-20 00:43:19 +0000159 Decl::Destroy(C);
160}
161
162
Douglas Gregorf0097952008-04-21 02:02:58 +0000163Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
164 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
165 if (FD->Body) {
166 Definition = FD;
167 return FD->Body;
168 }
169 }
170
171 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000172}
173
174unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000175 const FunctionType *FT = getType()->getAsFunctionType();
176 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000177 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000178 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
181void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
182 assert(ParamInfo == 0 && "Already has param info!");
183 assert(NumParams == getNumParams() && "Parameter count mismatch!");
184
185 // Zero params -> null pointer.
186 if (NumParams) {
187 ParamInfo = new ParmVarDecl*[NumParams];
188 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
189 }
190}
191
Chris Lattner8123a952008-04-10 02:22:51 +0000192/// getMinRequiredArguments - Returns the minimum number of arguments
193/// needed to call this function. This may be fewer than the number of
194/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000195/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000196unsigned FunctionDecl::getMinRequiredArguments() const {
197 unsigned NumRequiredArgs = getNumParams();
198 while (NumRequiredArgs > 0
199 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
200 --NumRequiredArgs;
201
202 return NumRequiredArgs;
203}
204
Chris Lattner8a934232008-03-31 00:36:02 +0000205//===----------------------------------------------------------------------===//
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000206// TagdDecl Implementation
207//===----------------------------------------------------------------------===//
208
209TagDecl* TagDecl::getDefinition(ASTContext& C) const {
210 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
211 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
212 return D->isDefinition() ? D : 0;
213}
214
215//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000216// RecordDecl Implementation
217//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000218
Ted Kremenek63597922008-09-02 21:12:32 +0000219RecordDecl::RecordDecl(Kind DK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000220 IdentifierInfo *Id)
221: TagDecl(DK, DC, L, Id, 0) {
Ted Kremenek63597922008-09-02 21:12:32 +0000222
223 HasFlexibleArrayMember = false;
224 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
225 Members = 0;
Ted Kremenekdf042e62008-09-05 01:34:33 +0000226 NumMembers = -1;
Ted Kremenek63597922008-09-02 21:12:32 +0000227}
228
229RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000230 SourceLocation L, IdentifierInfo *Id,
231 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000232
Ted Kremenek63597922008-09-02 21:12:32 +0000233 void *Mem = C.getAllocator().Allocate<RecordDecl>();
234 Kind DK;
235 switch (TK) {
236 default: assert(0 && "Invalid TagKind!");
237 case TK_enum: assert(0 && "Enum TagKind passed for Record!");
238 case TK_struct: DK = Struct; break;
239 case TK_union: DK = Union; break;
240 case TK_class: DK = Class; break;
241 }
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000242
243 RecordDecl* R = new (Mem) RecordDecl(DK, DC, L, Id);
244 C.getTypeDeclType(R, PrevDecl);
245 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000246}
247
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000248RecordDecl::~RecordDecl() {
249 delete[] Members;
250}
251
252void RecordDecl::Destroy(ASTContext& C) {
253 if (isDefinition())
254 for (field_iterator I=field_begin(), E=field_end(); I!=E; ++I)
255 (*I)->Destroy(C);
256
257 TagDecl::Destroy(C);
258}
259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260/// defineBody - When created, RecordDecl's correspond to a forward declared
261/// record. This method is used to mark the decl as being defined, with the
262/// specified contents.
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000263void RecordDecl::defineBody(ASTContext& C, FieldDecl **members,
264 unsigned numMembers) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 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 }
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000272
273 // Let ASTContext know that this is the defining RecordDecl this type.
274 C.setTagDefinition(this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000275}
276
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000277
Chris Lattner8a934232008-03-31 00:36:02 +0000278FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 if (Members == 0 || NumMembers < 0)
280 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000281
Chris Lattner8a934232008-03-31 00:36:02 +0000282 // Linear search. When C++ classes come along, will likely need to revisit.
283 for (int i = 0; i != NumMembers; ++i)
284 if (Members[i]->getIdentifier() == II)
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 return Members[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000287}