blob: 3cc12c9a86f2402f43345c7c2ac73dd480430e0b [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Argiris Kirtzidise7dfca12008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnere4650482008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
17#include "clang/Basic/IdentifierTable.h"
Ted Kremenekafdf8112008-05-20 00:43:19 +000018
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattnera8344c32008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000022// Decl Allocation/Deallocation Method Implementations
23//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000024
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000025TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
26 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
27 return new (Mem) TranslationUnitDecl();
28}
29
Argiris Kirtzidis03e6aaf2008-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 Kremenek5be49242008-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 Kremenek0f433842008-05-24 15:09:56 +000040 this->~NamespaceDecl();
Ted Kremenek5be49242008-05-20 04:49:55 +000041 C.getAllocator().Deallocate((void *)this);
42}
43
44
Chris Lattner8c7c6a12008-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 Lattneref87a202008-04-22 18:39:57 +000051VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff72a6ebc2008-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 Lattneref87a202008-04-22 18:39:57 +000056 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +000057}
58
Chris Lattneref87a202008-04-22 18:39:57 +000059ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000060 SourceLocation L, IdentifierInfo *Id,
61 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +000062 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +000063 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +000064 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +000065}
66
Chris Lattneref87a202008-04-22 18:39:57 +000067FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000068 SourceLocation L,
Chris Lattner4c7802b2008-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>();
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +000073 return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattner4c7802b2008-03-15 21:24:04 +000074}
75
Chris Lattnerf3874bc2008-04-06 04:47:34 +000076FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +000077 IdentifierInfo *Id, QualType T, Expr *BW) {
78 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +000079 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner81db64a2008-03-16 00:16:02 +000080}
81
Chris Lattner4c7802b2008-03-15 21:24:04 +000082
Chris Lattnereee57c02008-04-04 06:12:32 +000083EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
84 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +000085 IdentifierInfo *Id, QualType T,
86 Expr *E, const llvm::APSInt &V,
87 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +000088 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000089 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +000090}
91
Ted Kremenek5be49242008-05-20 04:49:55 +000092void EnumConstantDecl::Destroy(ASTContext& C) {
93 if (Init) Init->Destroy(C);
94 Decl::Destroy(C);
95}
96
Chris Lattneref87a202008-04-22 18:39:57 +000097TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000098 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +000099 IdentifierInfo *Id, QualType T,
100 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000101 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000102 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000103}
104
Chris Lattneref87a202008-04-22 18:39:57 +0000105EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000106 IdentifierInfo *Id,
Chris Lattner58114f02008-03-15 21:32:50 +0000107 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000108 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000109 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000110}
111
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000112RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000113 SourceLocation L, IdentifierInfo *Id,
114 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000115 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000116 Kind DK;
117 switch (TK) {
Ted Kremenek9ddb74e2008-06-16 23:45:12 +0000118 default: assert(0 && "Invalid TagKind!");
119 case TK_enum: assert(0 && "Enum TagKind passed for Record!");
120 case TK_struct: DK = Struct; break;
121 case TK_union: DK = Union; break;
122 case TK_class: DK = Class; break;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000123 }
Chris Lattneref87a202008-04-22 18:39:57 +0000124 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000125}
126
Ted Kremenek5be49242008-05-20 04:49:55 +0000127void EnumDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisa5c14b22008-06-10 01:32:09 +0000128 if (getEnumConstantList()) getEnumConstantList()->Destroy(C);
Ted Kremenek5be49242008-05-20 04:49:55 +0000129 Decl::Destroy(C);
130}
131
132
Chris Lattnereee57c02008-04-04 06:12:32 +0000133FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
134 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000135 StringLiteral *Str) {
136 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
137 return new (Mem) FileScopeAsmDecl(L, Str);
138}
139
Chris Lattnereee57c02008-04-04 06:12:32 +0000140LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
141 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000142 LanguageIDs Lang, Decl *D) {
143 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
144 return new (Mem) LinkageSpecDecl(L, Lang, D);
145}
Chris Lattnere4650482008-03-15 06:12:44 +0000146
147//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000148// NamedDecl Implementation
149//===----------------------------------------------------------------------===//
150
Chris Lattner910435b2007-10-06 22:53:46 +0000151const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000152 if (const IdentifierInfo *II = getIdentifier())
153 return II->getName();
154 return "";
155}
156
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000157//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000158// FunctionDecl Implementation
159//===----------------------------------------------------------------------===//
160
Chris Lattner4b009652007-07-25 00:24:17 +0000161FunctionDecl::~FunctionDecl() {
162 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000163}
164
Ted Kremenekafdf8112008-05-20 00:43:19 +0000165void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000166 if (Body)
167 Body->Destroy(C);
168
169 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
170 (*I)->Destroy(C);
171
Ted Kremenekafdf8112008-05-20 00:43:19 +0000172 Decl::Destroy(C);
173}
174
175
Douglas Gregor42214c52008-04-21 02:02:58 +0000176Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
177 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
178 if (FD->Body) {
179 Definition = FD;
180 return FD->Body;
181 }
182 }
183
184 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000185}
186
187unsigned FunctionDecl::getNumParams() const {
Chris Lattnere8733592008-04-06 23:09:52 +0000188 const FunctionType *FT = getType()->getAsFunctionType();
189 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000190 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000191 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000192}
193
194void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
195 assert(ParamInfo == 0 && "Already has param info!");
196 assert(NumParams == getNumParams() && "Parameter count mismatch!");
197
198 // Zero params -> null pointer.
199 if (NumParams) {
200 ParamInfo = new ParmVarDecl*[NumParams];
201 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
202 }
203}
204
Chris Lattner97316c02008-04-10 02:22:51 +0000205/// getMinRequiredArguments - Returns the minimum number of arguments
206/// needed to call this function. This may be fewer than the number of
207/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000208/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000209unsigned FunctionDecl::getMinRequiredArguments() const {
210 unsigned NumRequiredArgs = getNumParams();
211 while (NumRequiredArgs > 0
212 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
213 --NumRequiredArgs;
214
215 return NumRequiredArgs;
216}
217
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000218//===----------------------------------------------------------------------===//
219// RecordDecl Implementation
220//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000221
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000222RecordDecl::~RecordDecl() {
223 delete[] Members;
224}
225
226void RecordDecl::Destroy(ASTContext& C) {
227 if (isDefinition())
228 for (field_iterator I=field_begin(), E=field_end(); I!=E; ++I)
229 (*I)->Destroy(C);
230
231 TagDecl::Destroy(C);
232}
233
234
Chris Lattner4b009652007-07-25 00:24:17 +0000235/// defineBody - When created, RecordDecl's correspond to a forward declared
236/// record. This method is used to mark the decl as being defined, with the
237/// specified contents.
238void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
239 assert(!isDefinition() && "Cannot redefine record!");
240 setDefinition(true);
241 NumMembers = numMembers;
242 if (numMembers) {
243 Members = new FieldDecl*[numMembers];
244 memcpy(Members, members, numMembers*sizeof(Decl*));
245 }
246}
247
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000248FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Chris Lattner4b009652007-07-25 00:24:17 +0000249 if (Members == 0 || NumMembers < 0)
250 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000251
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000252 // Linear search. When C++ classes come along, will likely need to revisit.
253 for (int i = 0; i != NumMembers; ++i)
254 if (Members[i]->getIdentifier() == II)
Chris Lattner4b009652007-07-25 00:24:17 +0000255 return Members[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000256 return 0;
257}