blob: 7643e4eda8746fb7387bd4faae9c9914f63e6d63 [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,
Steve Naroff22315692008-10-03 00:02:03 +000054 StorageClass S, ScopedDecl *PrevDecl,
55 SourceLocation TypeSpecStartLoc) {
Steve Naroff08899ff2008-04-15 22:42:06 +000056 void *Mem = C.getAllocator().Allocate<VarDecl>();
Steve Naroff22315692008-10-03 00:02:03 +000057 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc);
Chris Lattner4b08ca82008-03-15 21:10:16 +000058}
59
Chris Lattnerbec41342008-04-22 18:39:57 +000060ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000061 SourceLocation L, IdentifierInfo *Id,
62 QualType T, StorageClass S,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000063 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner4b08ca82008-03-15 21:10:16 +000064 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +000065 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +000066}
67
Chris Lattnerbec41342008-04-22 18:39:57 +000068FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000069 SourceLocation L,
Douglas Gregor92751d42008-11-17 22:58:34 +000070 DeclarationName N, QualType T,
Chris Lattner5072bae2008-03-15 21:24:04 +000071 StorageClass S, bool isInline,
Steve Naroff22315692008-10-03 00:02:03 +000072 ScopedDecl *PrevDecl,
73 SourceLocation TypeSpecStartLoc) {
Chris Lattner5072bae2008-03-15 21:24:04 +000074 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Douglas Gregor92751d42008-11-17 22:58:34 +000075 return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline, PrevDecl,
Steve Naroff22315692008-10-03 00:02:03 +000076 TypeSpecStartLoc);
Chris Lattner5072bae2008-03-15 21:24:04 +000077}
78
Steve Naroff1d95e5a2008-10-10 01:28:17 +000079BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff415d3d52008-10-08 17:01:13 +000080 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff1d95e5a2008-10-10 01:28:17 +000081 return new (Mem) BlockDecl(DC, L);
Steve Naroff415d3d52008-10-08 17:01:13 +000082}
83
Douglas Gregor91f84212008-12-11 16:49:14 +000084FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
85 IdentifierInfo *Id, QualType T, Expr *BW,
86 bool Mutable, ScopedDecl *PrevDecl) {
Chris Lattneree1284a2008-03-16 00:16:02 +000087 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregor91f84212008-12-11 16:49:14 +000088 return new (Mem) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, PrevDecl);
Chris Lattneree1284a2008-03-16 00:16:02 +000089}
90
Chris Lattner5072bae2008-03-15 21:24:04 +000091
Chris Lattnerc5ffed42008-04-04 06:12:32 +000092EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
93 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +000094 IdentifierInfo *Id, QualType T,
95 Expr *E, const llvm::APSInt &V,
96 ScopedDecl *PrevDecl){
Chris Lattnera7b32872008-03-15 06:12:44 +000097 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +000098 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +000099}
100
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000101void EnumConstantDecl::Destroy(ASTContext& C) {
102 if (Init) Init->Destroy(C);
103 Decl::Destroy(C);
104}
105
Chris Lattnerbec41342008-04-22 18:39:57 +0000106TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000107 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000108 IdentifierInfo *Id, QualType T,
109 ScopedDecl *PD) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000110 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000111 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnera7b32872008-03-15 06:12:44 +0000112}
113
Chris Lattnerbec41342008-04-22 18:39:57 +0000114EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000115 IdentifierInfo *Id,
Chris Lattner96c460d2008-03-15 21:32:50 +0000116 ScopedDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000117 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000118 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000119}
120
Ted Kremenek123f0252008-09-02 20:13:32 +0000121void EnumDecl::Destroy(ASTContext& C) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000122 DeclContext::DestroyDecls(C);
Ted Kremenek123f0252008-09-02 20:13:32 +0000123 Decl::Destroy(C);
124}
125
Douglas Gregor91f84212008-12-11 16:49:14 +0000126void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
127 assert(!isDefinition() && "Cannot redefine enums!");
128 setDefinition(true);
129
130 IntegerType = NewType;
131
132 // Let ASTContext know that this is the defining EnumDecl for this
133 // type.
134 C.setTagDefinition(this);
135}
136
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000137FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
138 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000139 StringLiteral *Str) {
140 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
141 return new (Mem) FileScopeAsmDecl(L, Str);
142}
143
Chris Lattnera7b32872008-03-15 06:12:44 +0000144//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000145// ScopedDecl Implementation
146//===----------------------------------------------------------------------===//
147
148void ScopedDecl::setLexicalDeclContext(DeclContext *DC) {
149 if (DC == getLexicalDeclContext())
150 return;
151
152 if (isInSemaDC()) {
153 MultipleDC *MDC = new MultipleDC();
154 MDC->SemanticDC = getDeclContext();
155 MDC->LexicalDC = DC;
156 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
157 } else {
158 getMultipleDC()->LexicalDC = DC;
159 }
160}
161
162ScopedDecl::~ScopedDecl() {
163 if (isOutOfSemaDC())
164 delete getMultipleDC();
165}
166
167//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000168// FunctionDecl Implementation
169//===----------------------------------------------------------------------===//
170
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000171FunctionDecl::~FunctionDecl() {
172 delete[] ParamInfo;
Douglas Gregor89f238c2008-04-21 02:02:58 +0000173}
174
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000175void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000176 if (Body)
177 Body->Destroy(C);
178
179 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
180 (*I)->Destroy(C);
181
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000182 Decl::Destroy(C);
183}
184
185
Douglas Gregor89f238c2008-04-21 02:02:58 +0000186Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
187 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
188 if (FD->Body) {
189 Definition = FD;
190 return FD->Body;
191 }
192 }
193
194 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000195}
196
Ted Kremenek82c1cc32008-10-29 18:41:34 +0000197// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
198static unsigned getNumTypeParams(QualType T) {
199 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000200 if (isa<FunctionTypeNoProto>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000201 return 0;
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000202 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000203}
204
Ted Kremenek82c1cc32008-10-29 18:41:34 +0000205unsigned FunctionDecl::getNumParams() const {
206 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
207 if (!ParamInfo)
208 return 0;
209
210 return getNumTypeParams(getType());
211}
212
Chris Lattner53621a52007-06-13 20:44:40 +0000213void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000214 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek82c1cc32008-10-29 18:41:34 +0000215 assert(NumParams == getNumTypeParams(getType()) &&
216 "Parameter count mismatch!");
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000217
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000218 // Zero params -> null pointer.
219 if (NumParams) {
Chris Lattner53621a52007-06-13 20:44:40 +0000220 ParamInfo = new ParmVarDecl*[NumParams];
221 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000222 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000223}
Chris Lattner41943152007-01-25 04:52:46 +0000224
Chris Lattner58258242008-04-10 02:22:51 +0000225/// getMinRequiredArguments - Returns the minimum number of arguments
226/// needed to call this function. This may be fewer than the number of
227/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000228/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000229unsigned FunctionDecl::getMinRequiredArguments() const {
230 unsigned NumRequiredArgs = getNumParams();
231 while (NumRequiredArgs > 0
232 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
233 --NumRequiredArgs;
234
235 return NumRequiredArgs;
236}
237
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000238/// getOverloadedOperator - Which C++ overloaded operator this
239/// function represents, if any.
240OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000241 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
242 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000243 else
244 return OO_None;
245}
246
Chris Lattner59a25942008-03-31 00:36:02 +0000247//===----------------------------------------------------------------------===//
Ted Kremenek21475702008-09-05 17:16:31 +0000248// TagdDecl Implementation
249//===----------------------------------------------------------------------===//
250
251TagDecl* TagDecl::getDefinition(ASTContext& C) const {
252 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
253 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
254 return D->isDefinition() ? D : 0;
255}
256
257//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000258// RecordDecl Implementation
259//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000260
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000261RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek47923c72008-09-05 01:34:33 +0000262 IdentifierInfo *Id)
Douglas Gregor91f84212008-12-11 16:49:14 +0000263 : TagDecl(DK, TK, DC, L, Id, 0), DeclContext(DK) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000264
265 HasFlexibleArrayMember = false;
266 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +0000267}
268
269RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +0000270 SourceLocation L, IdentifierInfo *Id,
271 RecordDecl* PrevDecl) {
Ted Kremenek47923c72008-09-05 01:34:33 +0000272
Ted Kremenek52baf502008-09-02 21:12:32 +0000273 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000274 RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek21475702008-09-05 17:16:31 +0000275 C.getTypeDeclType(R, PrevDecl);
276 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +0000277}
278
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000279RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000280}
281
282void RecordDecl::Destroy(ASTContext& C) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000283 DeclContext::DestroyDecls(C);
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000284 TagDecl::Destroy(C);
285}
286
Douglas Gregor91f84212008-12-11 16:49:14 +0000287/// completeDefinition - Notes that the definition of this type is now
288/// complete.
289void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +0000290 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor91f84212008-12-11 16:49:14 +0000291
Chris Lattner41943152007-01-25 04:52:46 +0000292 setDefinition(true);
Ted Kremenek21475702008-09-05 17:16:31 +0000293
Douglas Gregor91f84212008-12-11 16:49:14 +0000294 // Let ASTContext know that this is the defining RecordDecl for this
295 // type.
Ted Kremenek21475702008-09-05 17:16:31 +0000296 C.setTagDefinition(this);
Chris Lattner41943152007-01-25 04:52:46 +0000297}
Steve Naroffcc321422007-03-26 23:09:51 +0000298
Steve Naroff415d3d52008-10-08 17:01:13 +0000299//===----------------------------------------------------------------------===//
300// BlockDecl Implementation
301//===----------------------------------------------------------------------===//
302
303BlockDecl::~BlockDecl() {
304}
305
306void BlockDecl::Destroy(ASTContext& C) {
307 if (Body)
308 Body->Destroy(C);
309
310 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
311 (*I)->Destroy(C);
312
313 Decl::Destroy(C);
314}