blob: be8cbe7344dc657652f4b8ae2ffe9f96029ea863 [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"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000017#include "clang/AST/Expr.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/Basic/IdentifierTable.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000019
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Chris Lattnerd3b90652008-03-15 05:43:15 +000022//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000023// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000025
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000026TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
27 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
28 return new (Mem) TranslationUnitDecl();
29}
30
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000031NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
32 SourceLocation L, IdentifierInfo *Id) {
33 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
34 return new (Mem) NamespaceDecl(DC, L, Id);
35}
36
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000037void NamespaceDecl::Destroy(ASTContext& C) {
38 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
39 // together. They are all top-level Decls.
40
Ted Kremenekebf27b12008-05-24 15:09:56 +000041 this->~NamespaceDecl();
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000042 C.getAllocator().Deallocate((void *)this);
43}
44
45
Chris Lattner41110242008-06-17 18:05:57 +000046ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
47 SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) {
48 void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>();
49 return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl);
50}
51
Chris Lattner9fdf9c62008-04-22 18:39:57 +000052ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000053 SourceLocation L, IdentifierInfo *Id,
54 QualType T, StorageClass S,
Chris Lattner04421082008-04-08 04:40:51 +000055 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +000056 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +000057 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +000058}
59
Chris Lattner9fdf9c62008-04-22 18:39:57 +000060FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000061 SourceLocation L,
Douglas Gregor10bd3682008-11-17 22:58:34 +000062 DeclarationName N, QualType T,
Chris Lattnera98e58d2008-03-15 21:24:04 +000063 StorageClass S, bool isInline,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000064 ScopedDecl *PrevDecl,
65 SourceLocation TypeSpecStartLoc) {
Chris Lattnera98e58d2008-03-15 21:24:04 +000066 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Douglas Gregor10bd3682008-11-17 22:58:34 +000067 return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline, PrevDecl,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000068 TypeSpecStartLoc);
Chris Lattnera98e58d2008-03-15 21:24:04 +000069}
70
Steve Naroff090276f2008-10-10 01:28:17 +000071BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff56ee6892008-10-08 17:01:13 +000072 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff090276f2008-10-10 01:28:17 +000073 return new (Mem) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +000074}
75
Douglas Gregor44b43212008-12-11 16:49:14 +000076FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
77 IdentifierInfo *Id, QualType T, Expr *BW,
78 bool Mutable, ScopedDecl *PrevDecl) {
Chris Lattner8e25d862008-03-16 00:16:02 +000079 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregor44b43212008-12-11 16:49:14 +000080 return new (Mem) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, PrevDecl);
Chris Lattner8e25d862008-03-16 00:16:02 +000081}
82
Chris Lattnera98e58d2008-03-15 21:24:04 +000083
Chris Lattner0ed844b2008-04-04 06:12:32 +000084EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
85 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +000086 IdentifierInfo *Id, QualType T,
87 Expr *E, const llvm::APSInt &V,
88 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000089 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +000090 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000091}
92
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000093void EnumConstantDecl::Destroy(ASTContext& C) {
94 if (Init) Init->Destroy(C);
95 Decl::Destroy(C);
96}
97
Chris Lattner9fdf9c62008-04-22 18:39:57 +000098TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000099 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000100 IdentifierInfo *Id, QualType T,
101 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000102 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000103 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000104}
105
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000106EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000107 IdentifierInfo *Id,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000108 EnumDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000109 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000110 EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id, 0);
111 C.getTypeDeclType(Enum, PrevDecl);
112 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000113}
114
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000115void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000116 Decl::Destroy(C);
117}
118
Douglas Gregor44b43212008-12-11 16:49:14 +0000119void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
120 assert(!isDefinition() && "Cannot redefine enums!");
121 setDefinition(true);
122
123 IntegerType = NewType;
124
125 // Let ASTContext know that this is the defining EnumDecl for this
126 // type.
127 C.setTagDefinition(this);
128}
129
Chris Lattner0ed844b2008-04-04 06:12:32 +0000130FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
131 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000132 StringLiteral *Str) {
133 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
134 return new (Mem) FileScopeAsmDecl(L, Str);
135}
136
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000137//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000138// ScopedDecl Implementation
139//===----------------------------------------------------------------------===//
140
141void ScopedDecl::setLexicalDeclContext(DeclContext *DC) {
142 if (DC == getLexicalDeclContext())
143 return;
144
145 if (isInSemaDC()) {
146 MultipleDC *MDC = new MultipleDC();
147 MDC->SemanticDC = getDeclContext();
148 MDC->LexicalDC = DC;
149 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
150 } else {
151 getMultipleDC()->LexicalDC = DC;
152 }
153}
154
155ScopedDecl::~ScopedDecl() {
156 if (isOutOfSemaDC())
157 delete getMultipleDC();
158}
159
160//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000161// VarDecl Implementation
162//===----------------------------------------------------------------------===//
163
164VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
165 SourceLocation L,
166 IdentifierInfo *Id, QualType T,
167 StorageClass S, ScopedDecl *PrevDecl,
168 SourceLocation TypeSpecStartLoc) {
169 void *Mem = C.getAllocator().Allocate<VarDecl>();
170 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc);
171}
172
173void VarDecl::Destroy(ASTContext& C) {
174 this->~VarDecl();
175 C.getAllocator().Deallocate((void *)this);
176}
177
178VarDecl::~VarDecl() {
179 delete getInit();
180}
181
182//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000183// FunctionDecl Implementation
184//===----------------------------------------------------------------------===//
185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186FunctionDecl::~FunctionDecl() {
187 delete[] ParamInfo;
Douglas Gregorf0097952008-04-21 02:02:58 +0000188}
189
Ted Kremenek27f8a282008-05-20 00:43:19 +0000190void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000191 if (Body)
192 Body->Destroy(C);
193
194 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
195 (*I)->Destroy(C);
196
Ted Kremenek27f8a282008-05-20 00:43:19 +0000197 Decl::Destroy(C);
198}
199
200
Douglas Gregorf0097952008-04-21 02:02:58 +0000201Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
202 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
203 if (FD->Body) {
204 Definition = FD;
205 return FD->Body;
206 }
207 }
208
209 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000212// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
213static unsigned getNumTypeParams(QualType T) {
214 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000215 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000216 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000217 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000218}
219
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000220unsigned FunctionDecl::getNumParams() const {
221 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
222 if (!ParamInfo)
223 return 0;
224
225 return getNumTypeParams(getType());
226}
227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
229 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000230 assert(NumParams == getNumTypeParams(getType()) &&
231 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000232
233 // Zero params -> null pointer.
234 if (NumParams) {
235 ParamInfo = new ParmVarDecl*[NumParams];
236 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
237 }
238}
239
Chris Lattner8123a952008-04-10 02:22:51 +0000240/// getMinRequiredArguments - Returns the minimum number of arguments
241/// needed to call this function. This may be fewer than the number of
242/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000243/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000244unsigned FunctionDecl::getMinRequiredArguments() const {
245 unsigned NumRequiredArgs = getNumParams();
246 while (NumRequiredArgs > 0
247 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
248 --NumRequiredArgs;
249
250 return NumRequiredArgs;
251}
252
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000253/// getOverloadedOperator - Which C++ overloaded operator this
254/// function represents, if any.
255OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000256 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
257 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000258 else
259 return OO_None;
260}
261
Chris Lattner8a934232008-03-31 00:36:02 +0000262//===----------------------------------------------------------------------===//
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000263// TagdDecl Implementation
264//===----------------------------------------------------------------------===//
265
266TagDecl* TagDecl::getDefinition(ASTContext& C) const {
267 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
268 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
269 return D->isDefinition() ? D : 0;
270}
271
272//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000273// RecordDecl Implementation
274//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000275
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000276RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000277 IdentifierInfo *Id)
Douglas Gregor44b43212008-12-11 16:49:14 +0000278 : TagDecl(DK, TK, DC, L, Id, 0), DeclContext(DK) {
Ted Kremenek63597922008-09-02 21:12:32 +0000279
280 HasFlexibleArrayMember = false;
281 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000282}
283
284RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000285 SourceLocation L, IdentifierInfo *Id,
286 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000287
Ted Kremenek63597922008-09-02 21:12:32 +0000288 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000289 RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000290 C.getTypeDeclType(R, PrevDecl);
291 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000292}
293
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000294RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000295}
296
297void RecordDecl::Destroy(ASTContext& C) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000298 DeclContext::DestroyDecls(C);
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000299 TagDecl::Destroy(C);
300}
301
Douglas Gregor44b43212008-12-11 16:49:14 +0000302/// completeDefinition - Notes that the definition of this type is now
303/// complete.
304void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 setDefinition(true);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000308
Douglas Gregor44b43212008-12-11 16:49:14 +0000309 // Let ASTContext know that this is the defining RecordDecl for this
310 // type.
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000311 C.setTagDefinition(this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000312}
313
Steve Naroff56ee6892008-10-08 17:01:13 +0000314//===----------------------------------------------------------------------===//
315// BlockDecl Implementation
316//===----------------------------------------------------------------------===//
317
318BlockDecl::~BlockDecl() {
319}
320
321void BlockDecl::Destroy(ASTContext& C) {
322 if (Body)
323 Body->Destroy(C);
324
325 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
326 (*I)->Destroy(C);
327
328 Decl::Destroy(C);
329}