blob: dcefaa915b7b964891e33a7d6ef5bae5d4fc8dd4 [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,
Douglas Gregor6704b312008-11-17 22:58:34 +000070 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +000071 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>();
Douglas Gregor6704b312008-11-17 22:58:34 +000075 return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline, PrevDecl,
Steve Naroff71cd7762008-10-03 00:02:03 +000076 TypeSpecStartLoc);
Chris Lattner4c7802b2008-03-15 21:24:04 +000077}
78
Steve Naroff52059382008-10-10 01:28:17 +000079BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff9ac456d2008-10-08 17:01:13 +000080 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff52059382008-10-10 01:28:17 +000081 return new (Mem) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +000082}
83
Douglas Gregor8acb7272008-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 Lattner81db64a2008-03-16 00:16:02 +000087 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregor8acb7272008-12-11 16:49:14 +000088 return new (Mem) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, PrevDecl);
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,
Douglas Gregorae644892008-12-15 16:32:14 +0000116 EnumDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000117 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Douglas Gregorae644892008-12-15 16:32:14 +0000118 EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id, 0);
119 C.getTypeDeclType(Enum, PrevDecl);
120 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000121}
122
Ted Kremenek25d8be12008-09-02 20:13:32 +0000123void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000124 Decl::Destroy(C);
125}
126
Douglas Gregor8acb7272008-12-11 16:49:14 +0000127void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
128 assert(!isDefinition() && "Cannot redefine enums!");
129 setDefinition(true);
130
131 IntegerType = NewType;
132
133 // Let ASTContext know that this is the defining EnumDecl for this
134 // type.
135 C.setTagDefinition(this);
136}
137
Chris Lattnereee57c02008-04-04 06:12:32 +0000138FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
139 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000140 StringLiteral *Str) {
141 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
142 return new (Mem) FileScopeAsmDecl(L, Str);
143}
144
Chris Lattnere4650482008-03-15 06:12:44 +0000145//===----------------------------------------------------------------------===//
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000146// ScopedDecl Implementation
147//===----------------------------------------------------------------------===//
148
149void ScopedDecl::setLexicalDeclContext(DeclContext *DC) {
150 if (DC == getLexicalDeclContext())
151 return;
152
153 if (isInSemaDC()) {
154 MultipleDC *MDC = new MultipleDC();
155 MDC->SemanticDC = getDeclContext();
156 MDC->LexicalDC = DC;
157 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
158 } else {
159 getMultipleDC()->LexicalDC = DC;
160 }
161}
162
163ScopedDecl::~ScopedDecl() {
164 if (isOutOfSemaDC())
165 delete getMultipleDC();
166}
167
168//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000169// FunctionDecl Implementation
170//===----------------------------------------------------------------------===//
171
Chris Lattner4b009652007-07-25 00:24:17 +0000172FunctionDecl::~FunctionDecl() {
173 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000174}
175
Ted Kremenekafdf8112008-05-20 00:43:19 +0000176void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000177 if (Body)
178 Body->Destroy(C);
179
180 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
181 (*I)->Destroy(C);
182
Ted Kremenekafdf8112008-05-20 00:43:19 +0000183 Decl::Destroy(C);
184}
185
186
Douglas Gregor42214c52008-04-21 02:02:58 +0000187Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
188 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
189 if (FD->Body) {
190 Definition = FD;
191 return FD->Body;
192 }
193 }
194
195 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000196}
197
Ted Kremeneka6ded352008-10-29 18:41:34 +0000198// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
199static unsigned getNumTypeParams(QualType T) {
200 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnere8733592008-04-06 23:09:52 +0000201 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000202 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000203 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000204}
205
Ted Kremeneka6ded352008-10-29 18:41:34 +0000206unsigned FunctionDecl::getNumParams() const {
207 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
208 if (!ParamInfo)
209 return 0;
210
211 return getNumTypeParams(getType());
212}
213
Chris Lattner4b009652007-07-25 00:24:17 +0000214void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
215 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremeneka6ded352008-10-29 18:41:34 +0000216 assert(NumParams == getNumTypeParams(getType()) &&
217 "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000218
219 // Zero params -> null pointer.
220 if (NumParams) {
221 ParamInfo = new ParmVarDecl*[NumParams];
222 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
223 }
224}
225
Chris Lattner97316c02008-04-10 02:22:51 +0000226/// getMinRequiredArguments - Returns the minimum number of arguments
227/// needed to call this function. This may be fewer than the number of
228/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000229/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000230unsigned FunctionDecl::getMinRequiredArguments() const {
231 unsigned NumRequiredArgs = getNumParams();
232 while (NumRequiredArgs > 0
233 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
234 --NumRequiredArgs;
235
236 return NumRequiredArgs;
237}
238
Douglas Gregore60e5d32008-11-06 22:13:31 +0000239/// getOverloadedOperator - Which C++ overloaded operator this
240/// function represents, if any.
241OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000242 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
243 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000244 else
245 return OO_None;
246}
247
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000248//===----------------------------------------------------------------------===//
Ted Kremenek46a837c2008-09-05 17:16:31 +0000249// TagdDecl Implementation
250//===----------------------------------------------------------------------===//
251
252TagDecl* TagDecl::getDefinition(ASTContext& C) const {
253 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
254 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
255 return D->isDefinition() ? D : 0;
256}
257
258//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000259// RecordDecl Implementation
260//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000261
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000262RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000263 IdentifierInfo *Id)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000264 : TagDecl(DK, TK, DC, L, Id, 0), DeclContext(DK) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000265
266 HasFlexibleArrayMember = false;
267 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000268}
269
270RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000271 SourceLocation L, IdentifierInfo *Id,
272 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000273
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000274 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000275 RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000276 C.getTypeDeclType(R, PrevDecl);
277 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000278}
279
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000280RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000281}
282
283void RecordDecl::Destroy(ASTContext& C) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000284 DeclContext::DestroyDecls(C);
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000285 TagDecl::Destroy(C);
286}
287
Douglas Gregor8acb7272008-12-11 16:49:14 +0000288/// completeDefinition - Notes that the definition of this type is now
289/// complete.
290void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000291 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000292
Chris Lattner4b009652007-07-25 00:24:17 +0000293 setDefinition(true);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000294
Douglas Gregor8acb7272008-12-11 16:49:14 +0000295 // Let ASTContext know that this is the defining RecordDecl for this
296 // type.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000297 C.setTagDefinition(this);
Chris Lattner4b009652007-07-25 00:24:17 +0000298}
299
Steve Naroff9ac456d2008-10-08 17:01:13 +0000300//===----------------------------------------------------------------------===//
301// BlockDecl Implementation
302//===----------------------------------------------------------------------===//
303
304BlockDecl::~BlockDecl() {
305}
306
307void BlockDecl::Destroy(ASTContext& C) {
308 if (Body)
309 Body->Destroy(C);
310
311 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
312 (*I)->Destroy(C);
313
314 Decl::Destroy(C);
315}