blob: c2ae5d91ab69b4f4fd4ee33788011bd69245ceff [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
17#include "clang/Basic/IdentifierTable.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000018
Chris Lattner6d9a6852006-10-25 05:11:20 +000019using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000020
Chris Lattner88f70d62008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
Chris Lattnera7b32872008-03-15 06:12:44 +000022// Decl Allocation/Deallocation Method Implementations
23//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000024
Argyrios Kyrtzidisc3b69ae2008-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 Kyrtzidis08114892008-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 Kremenek78aa98f2008-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 Kremeneka08154d2008-05-24 15:09:56 +000040 this->~NamespaceDecl();
Ted Kremenek78aa98f2008-05-20 04:49:55 +000041 C.getAllocator().Deallocate((void *)this);
42}
43
44
Chris Lattner5696e7b2008-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 Lattnerbec41342008-04-22 18:39:57 +000051VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff08899ff2008-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 Lattnerbec41342008-04-22 18:39:57 +000056 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +000057}
58
Chris Lattnerbec41342008-04-22 18:39:57 +000059ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000060 SourceLocation L, IdentifierInfo *Id,
61 QualType T, StorageClass S,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000062 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner4b08ca82008-03-15 21:10:16 +000063 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +000064 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +000065}
66
Chris Lattnerbec41342008-04-22 18:39:57 +000067FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000068 SourceLocation L,
Chris Lattner5072bae2008-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 Kyrtzidis2951e142008-06-09 21:05:31 +000073 return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattner5072bae2008-03-15 21:24:04 +000074}
75
Chris Lattner0a5ff0d2008-04-06 04:47:34 +000076FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +000077 IdentifierInfo *Id, QualType T, Expr *BW) {
78 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattner0a5ff0d2008-04-06 04:47:34 +000079 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattneree1284a2008-03-16 00:16:02 +000080}
81
Chris Lattner5072bae2008-03-15 21:24:04 +000082
Chris Lattnerc5ffed42008-04-04 06:12:32 +000083EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
84 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +000085 IdentifierInfo *Id, QualType T,
86 Expr *E, const llvm::APSInt &V,
87 ScopedDecl *PrevDecl){
Chris Lattnera7b32872008-03-15 06:12:44 +000088 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +000089 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +000090}
91
Ted Kremenek78aa98f2008-05-20 04:49:55 +000092void EnumConstantDecl::Destroy(ASTContext& C) {
93 if (Init) Init->Destroy(C);
94 Decl::Destroy(C);
95}
96
Chris Lattnerbec41342008-04-22 18:39:57 +000097TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000098 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +000099 IdentifierInfo *Id, QualType T,
100 ScopedDecl *PD) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000101 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000102 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnera7b32872008-03-15 06:12:44 +0000103}
104
Chris Lattnerbec41342008-04-22 18:39:57 +0000105EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000106 IdentifierInfo *Id,
Chris Lattner96c460d2008-03-15 21:32:50 +0000107 ScopedDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000108 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000109 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000110}
111
Ted Kremenek123f0252008-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 Kyrtzidis554a07b2008-06-09 23:19:58 +0000148RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000149 SourceLocation L, IdentifierInfo *Id,
Ted Kremenek123f0252008-09-02 20:13:32 +0000150 RecordDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000151 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +0000152 Kind DK;
153 switch (TK) {
Ted Kremenekbba87242008-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 Kyrtzidis554a07b2008-06-09 23:19:58 +0000159 }
Chris Lattnerbec41342008-04-22 18:39:57 +0000160 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000161}
162
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000163
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000164FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
165 SourceLocation L,
Chris Lattneree1284a2008-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 Lattnerc5ffed42008-04-04 06:12:32 +0000171LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
172 SourceLocation L,
Chris Lattneree1284a2008-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 Lattnera7b32872008-03-15 06:12:44 +0000177
178//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000179// NamedDecl Implementation
180//===----------------------------------------------------------------------===//
181
Chris Lattnera4016552007-10-06 22:53:46 +0000182const char *NamedDecl::getName() const {
Chris Lattnerec040b12007-01-21 23:09:50 +0000183 if (const IdentifierInfo *II = getIdentifier())
184 return II->getName();
185 return "";
Chris Lattner17ed4872006-11-20 04:58:19 +0000186}
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000187
Chris Lattner59a25942008-03-31 00:36:02 +0000188//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000189// FunctionDecl Implementation
190//===----------------------------------------------------------------------===//
191
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000192FunctionDecl::~FunctionDecl() {
193 delete[] ParamInfo;
Douglas Gregor89f238c2008-04-21 02:02:58 +0000194}
195
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000196void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekfa8a09e2008-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 Kremenekce20e8f2008-05-20 00:43:19 +0000203 Decl::Destroy(C);
204}
205
206
Douglas Gregor89f238c2008-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;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000216}
217
218unsigned FunctionDecl::getNumParams() const {
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000219 const FunctionType *FT = getType()->getAsFunctionType();
220 if (isa<FunctionTypeNoProto>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000221 return 0;
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000222 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000223}
224
Chris Lattner53621a52007-06-13 20:44:40 +0000225void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000226 assert(ParamInfo == 0 && "Already has param info!");
227 assert(NumParams == getNumParams() && "Parameter count mismatch!");
228
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000229 // Zero params -> null pointer.
230 if (NumParams) {
Chris Lattner53621a52007-06-13 20:44:40 +0000231 ParamInfo = new ParmVarDecl*[NumParams];
232 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000233 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000234}
Chris Lattner41943152007-01-25 04:52:46 +0000235
Chris Lattner58258242008-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 Lattnerb0d38442008-04-12 23:52:44 +0000239/// arguments (in C++).
Chris Lattner58258242008-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 Lattner59a25942008-03-31 00:36:02 +0000249//===----------------------------------------------------------------------===//
250// RecordDecl Implementation
251//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000252
Argyrios Kyrtzidis6bd44af2008-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
Chris Lattner41943152007-01-25 04:52:46 +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.
Steve Naroffcc321422007-03-26 23:09:51 +0000269void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
Chris Lattner41943152007-01-25 04:52:46 +0000270 assert(!isDefinition() && "Cannot redefine record!");
271 setDefinition(true);
Chris Lattner5f521502007-01-25 06:27:24 +0000272 NumMembers = numMembers;
273 if (numMembers) {
Steve Naroffcc321422007-03-26 23:09:51 +0000274 Members = new FieldDecl*[numMembers];
Chris Lattner5f521502007-01-25 06:27:24 +0000275 memcpy(Members, members, numMembers*sizeof(Decl*));
Chris Lattner41943152007-01-25 04:52:46 +0000276 }
277}
Steve Naroffcc321422007-03-26 23:09:51 +0000278
Chris Lattner59a25942008-03-31 00:36:02 +0000279FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Steve Naroffcc321422007-03-26 23:09:51 +0000280 if (Members == 0 || NumMembers < 0)
281 return 0;
Fariborz Jahanian67341402007-10-04 00:45:27 +0000282
Chris Lattner59a25942008-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)
Steve Naroffcc321422007-03-26 23:09:51 +0000286 return Members[i];
Steve Naroffcc321422007-03-26 23:09:51 +0000287 return 0;
Chris Lattnerbd4de5df2007-07-12 15:43:07 +0000288}