blob: fe2e95d157939354978d102b4406c0c2b540126d [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,
Steve Naroff71cd7762008-10-03 00:02:03 +000054 StorageClass S, ScopedDecl *PrevDecl,
55 SourceLocation TypeSpecStartLoc) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +000056 void *Mem = C.getAllocator().Allocate<VarDecl>();
Steve Naroff71cd7762008-10-03 00:02:03 +000057 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc);
Chris Lattner48d225c2008-03-15 21:10:16 +000058}
59
Chris Lattneref87a202008-04-22 18:39:57 +000060ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000061 SourceLocation L, IdentifierInfo *Id,
62 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +000063 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +000064 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +000065 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +000066}
67
Chris Lattneref87a202008-04-22 18:39:57 +000068FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000069 SourceLocation L,
Chris Lattner4c7802b2008-03-15 21:24:04 +000070 IdentifierInfo *Id, QualType T,
71 StorageClass S, bool isInline,
Steve Naroff71cd7762008-10-03 00:02:03 +000072 ScopedDecl *PrevDecl,
73 SourceLocation TypeSpecStartLoc) {
Chris Lattner4c7802b2008-03-15 21:24:04 +000074 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Steve Naroff71cd7762008-10-03 00:02:03 +000075 return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl,
76 TypeSpecStartLoc);
Chris Lattner4c7802b2008-03-15 21:24:04 +000077}
78
Steve Naroff9ac456d2008-10-08 17:01:13 +000079BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Steve Naroff95029d92008-10-08 18:44:00 +000080 ParmVarDecl **args, unsigned numargs) {
Steve Naroff9ac456d2008-10-08 17:01:13 +000081 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff95029d92008-10-08 18:44:00 +000082 return new (Mem) BlockDecl(DC, L, args, numargs);
Steve Naroff9ac456d2008-10-08 17:01:13 +000083}
84
Chris Lattnerf3874bc2008-04-06 04:47:34 +000085FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +000086 IdentifierInfo *Id, QualType T, Expr *BW) {
87 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +000088 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner81db64a2008-03-16 00:16:02 +000089}
90
Chris Lattner4c7802b2008-03-15 21:24:04 +000091
Chris Lattnereee57c02008-04-04 06:12:32 +000092EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
93 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +000094 IdentifierInfo *Id, QualType T,
95 Expr *E, const llvm::APSInt &V,
96 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +000097 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000098 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +000099}
100
Ted Kremenek5be49242008-05-20 04:49:55 +0000101void EnumConstantDecl::Destroy(ASTContext& C) {
102 if (Init) Init->Destroy(C);
103 Decl::Destroy(C);
104}
105
Chris Lattneref87a202008-04-22 18:39:57 +0000106TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000107 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000108 IdentifierInfo *Id, QualType T,
109 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000110 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000111 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000112}
113
Chris Lattneref87a202008-04-22 18:39:57 +0000114EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000115 IdentifierInfo *Id,
Chris Lattner58114f02008-03-15 21:32:50 +0000116 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000117 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000118 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000119}
120
Ted Kremenek25d8be12008-09-02 20:13:32 +0000121void EnumDecl::Destroy(ASTContext& C) {
122 if (getEnumConstantList()) getEnumConstantList()->Destroy(C);
123 Decl::Destroy(C);
124}
125
Chris Lattnereee57c02008-04-04 06:12:32 +0000126FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
127 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000128 StringLiteral *Str) {
129 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
130 return new (Mem) FileScopeAsmDecl(L, Str);
131}
132
Chris Lattnereee57c02008-04-04 06:12:32 +0000133LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
134 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000135 LanguageIDs Lang, Decl *D) {
136 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
137 return new (Mem) LinkageSpecDecl(L, Lang, D);
138}
Chris Lattnere4650482008-03-15 06:12:44 +0000139
140//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000141// NamedDecl Implementation
142//===----------------------------------------------------------------------===//
143
Chris Lattner910435b2007-10-06 22:53:46 +0000144const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 if (const IdentifierInfo *II = getIdentifier())
146 return II->getName();
147 return "";
148}
149
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000150//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000151// FunctionDecl Implementation
152//===----------------------------------------------------------------------===//
153
Chris Lattner4b009652007-07-25 00:24:17 +0000154FunctionDecl::~FunctionDecl() {
155 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000156}
157
Ted Kremenekafdf8112008-05-20 00:43:19 +0000158void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000159 if (Body)
160 Body->Destroy(C);
161
162 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
163 (*I)->Destroy(C);
164
Ted Kremenekafdf8112008-05-20 00:43:19 +0000165 Decl::Destroy(C);
166}
167
168
Douglas Gregor42214c52008-04-21 02:02:58 +0000169Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
170 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
171 if (FD->Body) {
172 Definition = FD;
173 return FD->Body;
174 }
175 }
176
177 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000178}
179
180unsigned FunctionDecl::getNumParams() const {
Chris Lattnere8733592008-04-06 23:09:52 +0000181 const FunctionType *FT = getType()->getAsFunctionType();
182 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000183 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000184 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000185}
186
187void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
188 assert(ParamInfo == 0 && "Already has param info!");
189 assert(NumParams == getNumParams() && "Parameter count mismatch!");
190
191 // Zero params -> null pointer.
192 if (NumParams) {
193 ParamInfo = new ParmVarDecl*[NumParams];
194 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
195 }
196}
197
Chris Lattner97316c02008-04-10 02:22:51 +0000198/// getMinRequiredArguments - Returns the minimum number of arguments
199/// needed to call this function. This may be fewer than the number of
200/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000201/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000202unsigned FunctionDecl::getMinRequiredArguments() const {
203 unsigned NumRequiredArgs = getNumParams();
204 while (NumRequiredArgs > 0
205 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
206 --NumRequiredArgs;
207
208 return NumRequiredArgs;
209}
210
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000211//===----------------------------------------------------------------------===//
Ted Kremenek46a837c2008-09-05 17:16:31 +0000212// TagdDecl Implementation
213//===----------------------------------------------------------------------===//
214
215TagDecl* TagDecl::getDefinition(ASTContext& C) const {
216 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
217 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
218 return D->isDefinition() ? D : 0;
219}
220
221//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000222// RecordDecl Implementation
223//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000224
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000225RecordDecl::RecordDecl(Kind DK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000226 IdentifierInfo *Id)
227: TagDecl(DK, DC, L, Id, 0) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000228
229 HasFlexibleArrayMember = false;
230 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
231 Members = 0;
Ted Kremenek2c984042008-09-05 01:34:33 +0000232 NumMembers = -1;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000233}
234
235RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000236 SourceLocation L, IdentifierInfo *Id,
237 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000238
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000239 void *Mem = C.getAllocator().Allocate<RecordDecl>();
240 Kind DK;
241 switch (TK) {
242 default: assert(0 && "Invalid TagKind!");
243 case TK_enum: assert(0 && "Enum TagKind passed for Record!");
244 case TK_struct: DK = Struct; break;
245 case TK_union: DK = Union; break;
246 case TK_class: DK = Class; break;
247 }
Ted Kremenek46a837c2008-09-05 17:16:31 +0000248
249 RecordDecl* R = new (Mem) RecordDecl(DK, DC, L, Id);
250 C.getTypeDeclType(R, PrevDecl);
251 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000252}
253
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000254RecordDecl::~RecordDecl() {
255 delete[] Members;
256}
257
258void RecordDecl::Destroy(ASTContext& C) {
259 if (isDefinition())
260 for (field_iterator I=field_begin(), E=field_end(); I!=E; ++I)
261 (*I)->Destroy(C);
262
263 TagDecl::Destroy(C);
264}
265
Chris Lattner4b009652007-07-25 00:24:17 +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.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000269void RecordDecl::defineBody(ASTContext& C, FieldDecl **members,
270 unsigned numMembers) {
Chris Lattner4b009652007-07-25 00:24:17 +0000271 assert(!isDefinition() && "Cannot redefine record!");
272 setDefinition(true);
273 NumMembers = numMembers;
274 if (numMembers) {
275 Members = new FieldDecl*[numMembers];
276 memcpy(Members, members, numMembers*sizeof(Decl*));
277 }
Ted Kremenek46a837c2008-09-05 17:16:31 +0000278
279 // Let ASTContext know that this is the defining RecordDecl this type.
280 C.setTagDefinition(this);
Chris Lattner4b009652007-07-25 00:24:17 +0000281}
282
Ted Kremenek46a837c2008-09-05 17:16:31 +0000283
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000284FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Chris Lattner4b009652007-07-25 00:24:17 +0000285 if (Members == 0 || NumMembers < 0)
286 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000287
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000288 // Linear search. When C++ classes come along, will likely need to revisit.
289 for (int i = 0; i != NumMembers; ++i)
290 if (Members[i]->getIdentifier() == II)
Chris Lattner4b009652007-07-25 00:24:17 +0000291 return Members[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000292 return 0;
293}
Steve Naroff9ac456d2008-10-08 17:01:13 +0000294
295//===----------------------------------------------------------------------===//
296// BlockDecl Implementation
297//===----------------------------------------------------------------------===//
298
299BlockDecl::~BlockDecl() {
300}
301
302void BlockDecl::Destroy(ASTContext& C) {
303 if (Body)
304 Body->Destroy(C);
305
306 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
307 (*I)->Destroy(C);
308
309 Decl::Destroy(C);
310}