blob: a2b39c438d538edf2ede2f374d763c946625a047 [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"
Ted Kremenek27f8a282008-05-20 00:43:19 +000016
Reid Spencer5f016e22007-07-11 17:01:13 +000017using namespace clang;
18
Chris Lattnerd3b90652008-03-15 05:43:15 +000019//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000020// Decl Allocation/Deallocation Method Implementations
21//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000022
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000023TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
24 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
25 return new (Mem) TranslationUnitDecl();
26}
27
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000028NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
29 SourceLocation L, IdentifierInfo *Id) {
30 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
31 return new (Mem) NamespaceDecl(DC, L, Id);
32}
33
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000034void NamespaceDecl::Destroy(ASTContext& C) {
35 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
36 // together. They are all top-level Decls.
37
Ted Kremenekebf27b12008-05-24 15:09:56 +000038 this->~NamespaceDecl();
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000039 C.getAllocator().Deallocate((void *)this);
40}
41
42
Chris Lattner41110242008-06-17 18:05:57 +000043ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
44 SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) {
45 void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>();
46 return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl);
47}
48
Chris Lattner9fdf9c62008-04-22 18:39:57 +000049VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff248a7532008-04-15 22:42:06 +000050 SourceLocation L,
51 IdentifierInfo *Id, QualType T,
52 StorageClass S, ScopedDecl *PrevDecl) {
53 void *Mem = C.getAllocator().Allocate<VarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +000054 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +000055}
56
Chris Lattner9fdf9c62008-04-22 18:39:57 +000057ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000058 SourceLocation L, IdentifierInfo *Id,
59 QualType T, StorageClass S,
Chris Lattner04421082008-04-08 04:40:51 +000060 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +000061 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +000062 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +000063}
64
Chris Lattner9fdf9c62008-04-22 18:39:57 +000065FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000066 SourceLocation L,
Chris Lattnera98e58d2008-03-15 21:24:04 +000067 IdentifierInfo *Id, QualType T,
68 StorageClass S, bool isInline,
69 ScopedDecl *PrevDecl) {
70 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000071 return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattnera98e58d2008-03-15 21:24:04 +000072}
73
Chris Lattnerb048c982008-04-06 04:47:34 +000074FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +000075 IdentifierInfo *Id, QualType T, Expr *BW) {
76 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerb048c982008-04-06 04:47:34 +000077 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner8e25d862008-03-16 00:16:02 +000078}
79
Chris Lattnera98e58d2008-03-15 21:24:04 +000080
Chris Lattner0ed844b2008-04-04 06:12:32 +000081EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
82 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +000083 IdentifierInfo *Id, QualType T,
84 Expr *E, const llvm::APSInt &V,
85 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000086 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +000087 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000088}
89
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000090void EnumConstantDecl::Destroy(ASTContext& C) {
91 if (Init) Init->Destroy(C);
92 Decl::Destroy(C);
93}
94
Chris Lattner9fdf9c62008-04-22 18:39:57 +000095TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000096 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +000097 IdentifierInfo *Id, QualType T,
98 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000099 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000100 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000101}
102
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000103EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000104 IdentifierInfo *Id,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000105 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000106 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000107 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000108}
109
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000110RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000111 SourceLocation L, IdentifierInfo *Id,
112 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000113 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000114 Kind DK;
115 switch (TK) {
Ted Kremenek9bcf44a2008-06-16 23:45:12 +0000116 default: assert(0 && "Invalid TagKind!");
117 case TK_enum: assert(0 && "Enum TagKind passed for Record!");
118 case TK_struct: DK = Struct; break;
119 case TK_union: DK = Union; break;
120 case TK_class: DK = Class; break;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000121 }
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000122 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000123}
124
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000125void EnumDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000126 if (getEnumConstantList()) getEnumConstantList()->Destroy(C);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000127 Decl::Destroy(C);
128}
129
130
Chris Lattner0ed844b2008-04-04 06:12:32 +0000131FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
132 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000133 StringLiteral *Str) {
134 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
135 return new (Mem) FileScopeAsmDecl(L, Str);
136}
137
Chris Lattner0ed844b2008-04-04 06:12:32 +0000138LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
139 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000140 LanguageIDs Lang, Decl *D) {
141 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
142 return new (Mem) LinkageSpecDecl(L, Lang, D);
143}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000144
145//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000146// NamedDecl Implementation
147//===----------------------------------------------------------------------===//
148
Chris Lattnerfd5de472007-10-06 22:53:46 +0000149const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 if (const IdentifierInfo *II = getIdentifier())
151 return II->getName();
152 return "";
153}
154
Chris Lattner8a934232008-03-31 00:36:02 +0000155//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000156// FunctionDecl Implementation
157//===----------------------------------------------------------------------===//
158
Reid Spencer5f016e22007-07-11 17:01:13 +0000159FunctionDecl::~FunctionDecl() {
160 delete[] ParamInfo;
Douglas Gregorf0097952008-04-21 02:02:58 +0000161}
162
Ted Kremenek27f8a282008-05-20 00:43:19 +0000163void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000164 if (Body)
165 Body->Destroy(C);
166
167 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
168 (*I)->Destroy(C);
169
Ted Kremenek27f8a282008-05-20 00:43:19 +0000170 Decl::Destroy(C);
171}
172
173
Douglas Gregorf0097952008-04-21 02:02:58 +0000174Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
175 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
176 if (FD->Body) {
177 Definition = FD;
178 return FD->Body;
179 }
180 }
181
182 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000183}
184
185unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000186 const FunctionType *FT = getType()->getAsFunctionType();
187 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000188 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000189 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000190}
191
192void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
193 assert(ParamInfo == 0 && "Already has param info!");
194 assert(NumParams == getNumParams() && "Parameter count mismatch!");
195
196 // Zero params -> null pointer.
197 if (NumParams) {
198 ParamInfo = new ParmVarDecl*[NumParams];
199 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
200 }
201}
202
Chris Lattner8123a952008-04-10 02:22:51 +0000203/// getMinRequiredArguments - Returns the minimum number of arguments
204/// needed to call this function. This may be fewer than the number of
205/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000206/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000207unsigned FunctionDecl::getMinRequiredArguments() const {
208 unsigned NumRequiredArgs = getNumParams();
209 while (NumRequiredArgs > 0
210 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
211 --NumRequiredArgs;
212
213 return NumRequiredArgs;
214}
215
Chris Lattner8a934232008-03-31 00:36:02 +0000216//===----------------------------------------------------------------------===//
217// RecordDecl Implementation
218//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
220/// defineBody - When created, RecordDecl's correspond to a forward declared
221/// record. This method is used to mark the decl as being defined, with the
222/// specified contents.
223void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
224 assert(!isDefinition() && "Cannot redefine record!");
225 setDefinition(true);
226 NumMembers = numMembers;
227 if (numMembers) {
228 Members = new FieldDecl*[numMembers];
229 memcpy(Members, members, numMembers*sizeof(Decl*));
230 }
231}
232
Chris Lattner8a934232008-03-31 00:36:02 +0000233FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 if (Members == 0 || NumMembers < 0)
235 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000236
Chris Lattner8a934232008-03-31 00:36:02 +0000237 // Linear search. When C++ classes come along, will likely need to revisit.
238 for (int i = 0; i != NumMembers; ++i)
239 if (Members[i]->getIdentifier() == II)
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 return Members[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000242}