blob: 021becc85a2ee6664cb49f26982f9a57e65d2097 [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"
Nuno Lopesc98406e2008-12-17 23:39:55 +000017#include "clang/AST/Expr.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/Basic/IdentifierTable.h"
Ted Kremenekafdf8112008-05-20 00:43:19 +000019
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattnera8344c32008-03-15 05:43:15 +000022//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000023// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000025
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000026TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
27 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
28 return new (Mem) TranslationUnitDecl();
29}
30
Argiris Kirtzidis03e6aaf2008-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 Kremenek5be49242008-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 Kremenek0f433842008-05-24 15:09:56 +000041 this->~NamespaceDecl();
Ted Kremenek5be49242008-05-20 04:49:55 +000042 C.getAllocator().Deallocate((void *)this);
43}
44
45
Chris Lattner8c7c6a12008-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 Lattneref87a202008-04-22 18:39:57 +000052ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000053 SourceLocation L, IdentifierInfo *Id,
54 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +000055 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +000056 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +000057 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +000058}
59
Fariborz Jahanian160e8812008-12-20 20:56:12 +000060ParmVarWithOriginalTypeDecl *ParmVarWithOriginalTypeDecl::Create(
61 ASTContext &C, DeclContext *DC,
62 SourceLocation L, IdentifierInfo *Id,
63 QualType T, QualType OT, StorageClass S,
64 Expr *DefArg, ScopedDecl *PrevDecl) {
65 void *Mem = C.getAllocator().Allocate<ParmVarWithOriginalTypeDecl>();
66 return new (Mem) ParmVarWithOriginalTypeDecl(DC, L, Id, T, OT, S,
67 DefArg, PrevDecl);
68}
69
Chris Lattneref87a202008-04-22 18:39:57 +000070FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000071 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +000072 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +000073 StorageClass S, bool isInline,
Steve Naroff71cd7762008-10-03 00:02:03 +000074 ScopedDecl *PrevDecl,
75 SourceLocation TypeSpecStartLoc) {
Chris Lattner4c7802b2008-03-15 21:24:04 +000076 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Douglas Gregor6704b312008-11-17 22:58:34 +000077 return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline, PrevDecl,
Steve Naroff71cd7762008-10-03 00:02:03 +000078 TypeSpecStartLoc);
Chris Lattner4c7802b2008-03-15 21:24:04 +000079}
80
Steve Naroff52059382008-10-10 01:28:17 +000081BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff9ac456d2008-10-08 17:01:13 +000082 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff52059382008-10-10 01:28:17 +000083 return new (Mem) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +000084}
85
Douglas Gregor8acb7272008-12-11 16:49:14 +000086FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
87 IdentifierInfo *Id, QualType T, Expr *BW,
88 bool Mutable, ScopedDecl *PrevDecl) {
Chris Lattner81db64a2008-03-16 00:16:02 +000089 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregor8acb7272008-12-11 16:49:14 +000090 return new (Mem) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, PrevDecl);
Chris Lattner81db64a2008-03-16 00:16:02 +000091}
92
Chris Lattner4c7802b2008-03-15 21:24:04 +000093
Chris Lattnereee57c02008-04-04 06:12:32 +000094EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
95 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +000096 IdentifierInfo *Id, QualType T,
97 Expr *E, const llvm::APSInt &V,
98 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +000099 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000100 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000101}
102
Ted Kremenek5be49242008-05-20 04:49:55 +0000103void EnumConstantDecl::Destroy(ASTContext& C) {
104 if (Init) Init->Destroy(C);
105 Decl::Destroy(C);
106}
107
Chris Lattneref87a202008-04-22 18:39:57 +0000108TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000109 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000110 IdentifierInfo *Id, QualType T,
111 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000112 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000113 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000114}
115
Chris Lattneref87a202008-04-22 18:39:57 +0000116EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000117 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000118 EnumDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000119 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Douglas Gregorae644892008-12-15 16:32:14 +0000120 EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id, 0);
121 C.getTypeDeclType(Enum, PrevDecl);
122 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000123}
124
Ted Kremenek25d8be12008-09-02 20:13:32 +0000125void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000126 Decl::Destroy(C);
127}
128
Douglas Gregor8acb7272008-12-11 16:49:14 +0000129void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
130 assert(!isDefinition() && "Cannot redefine enums!");
131 setDefinition(true);
132
133 IntegerType = NewType;
134
135 // Let ASTContext know that this is the defining EnumDecl for this
136 // type.
137 C.setTagDefinition(this);
138}
139
Chris Lattnereee57c02008-04-04 06:12:32 +0000140FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
141 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000142 StringLiteral *Str) {
143 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
144 return new (Mem) FileScopeAsmDecl(L, Str);
145}
146
Chris Lattnere4650482008-03-15 06:12:44 +0000147//===----------------------------------------------------------------------===//
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000148// ScopedDecl Implementation
149//===----------------------------------------------------------------------===//
150
151void ScopedDecl::setLexicalDeclContext(DeclContext *DC) {
152 if (DC == getLexicalDeclContext())
153 return;
154
155 if (isInSemaDC()) {
156 MultipleDC *MDC = new MultipleDC();
157 MDC->SemanticDC = getDeclContext();
158 MDC->LexicalDC = DC;
159 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
160 } else {
161 getMultipleDC()->LexicalDC = DC;
162 }
163}
164
165ScopedDecl::~ScopedDecl() {
166 if (isOutOfSemaDC())
167 delete getMultipleDC();
168}
169
170//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000171// VarDecl Implementation
172//===----------------------------------------------------------------------===//
173
174VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
175 SourceLocation L,
176 IdentifierInfo *Id, QualType T,
177 StorageClass S, ScopedDecl *PrevDecl,
178 SourceLocation TypeSpecStartLoc) {
179 void *Mem = C.getAllocator().Allocate<VarDecl>();
180 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc);
181}
182
183void VarDecl::Destroy(ASTContext& C) {
184 this->~VarDecl();
185 C.getAllocator().Deallocate((void *)this);
186}
187
188VarDecl::~VarDecl() {
189 delete getInit();
190}
191
192//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000193// FunctionDecl Implementation
194//===----------------------------------------------------------------------===//
195
Chris Lattner4b009652007-07-25 00:24:17 +0000196FunctionDecl::~FunctionDecl() {
197 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000198}
199
Ted Kremenekafdf8112008-05-20 00:43:19 +0000200void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000201 if (Body)
202 Body->Destroy(C);
203
204 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
205 (*I)->Destroy(C);
206
Ted Kremenekafdf8112008-05-20 00:43:19 +0000207 Decl::Destroy(C);
208}
209
210
Douglas Gregor42214c52008-04-21 02:02:58 +0000211Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
212 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
213 if (FD->Body) {
214 Definition = FD;
215 return FD->Body;
216 }
217 }
218
219 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000220}
221
Ted Kremeneka6ded352008-10-29 18:41:34 +0000222// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
223static unsigned getNumTypeParams(QualType T) {
224 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnere8733592008-04-06 23:09:52 +0000225 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000226 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000227 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000228}
229
Ted Kremeneka6ded352008-10-29 18:41:34 +0000230unsigned FunctionDecl::getNumParams() const {
231 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
232 if (!ParamInfo)
233 return 0;
234
235 return getNumTypeParams(getType());
236}
237
Chris Lattner4b009652007-07-25 00:24:17 +0000238void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
239 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremeneka6ded352008-10-29 18:41:34 +0000240 assert(NumParams == getNumTypeParams(getType()) &&
241 "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000242
243 // Zero params -> null pointer.
244 if (NumParams) {
245 ParamInfo = new ParmVarDecl*[NumParams];
246 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
247 }
248}
249
Chris Lattner97316c02008-04-10 02:22:51 +0000250/// getMinRequiredArguments - Returns the minimum number of arguments
251/// needed to call this function. This may be fewer than the number of
252/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000253/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000254unsigned FunctionDecl::getMinRequiredArguments() const {
255 unsigned NumRequiredArgs = getNumParams();
256 while (NumRequiredArgs > 0
257 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
258 --NumRequiredArgs;
259
260 return NumRequiredArgs;
261}
262
Douglas Gregore60e5d32008-11-06 22:13:31 +0000263/// getOverloadedOperator - Which C++ overloaded operator this
264/// function represents, if any.
265OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000266 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
267 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000268 else
269 return OO_None;
270}
271
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000272//===----------------------------------------------------------------------===//
Ted Kremenek46a837c2008-09-05 17:16:31 +0000273// TagdDecl Implementation
274//===----------------------------------------------------------------------===//
275
276TagDecl* TagDecl::getDefinition(ASTContext& C) const {
277 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
278 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
279 return D->isDefinition() ? D : 0;
280}
281
282//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000283// RecordDecl Implementation
284//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000285
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000286RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000287 IdentifierInfo *Id)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000288 : TagDecl(DK, TK, DC, L, Id, 0), DeclContext(DK) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000289
290 HasFlexibleArrayMember = false;
291 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000292}
293
294RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000295 SourceLocation L, IdentifierInfo *Id,
296 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000297
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000298 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000299 RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000300 C.getTypeDeclType(R, PrevDecl);
301 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000302}
303
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000304RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000305}
306
307void RecordDecl::Destroy(ASTContext& C) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000308 DeclContext::DestroyDecls(C);
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000309 TagDecl::Destroy(C);
310}
311
Douglas Gregor8acb7272008-12-11 16:49:14 +0000312/// completeDefinition - Notes that the definition of this type is now
313/// complete.
314void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000315 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000316
Chris Lattner4b009652007-07-25 00:24:17 +0000317 setDefinition(true);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000318
Douglas Gregor8acb7272008-12-11 16:49:14 +0000319 // Let ASTContext know that this is the defining RecordDecl for this
320 // type.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000321 C.setTagDefinition(this);
Chris Lattner4b009652007-07-25 00:24:17 +0000322}
323
Steve Naroff9ac456d2008-10-08 17:01:13 +0000324//===----------------------------------------------------------------------===//
325// BlockDecl Implementation
326//===----------------------------------------------------------------------===//
327
328BlockDecl::~BlockDecl() {
329}
330
331void BlockDecl::Destroy(ASTContext& C) {
332 if (Body)
333 Body->Destroy(C);
334
335 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
336 (*I)->Destroy(C);
337
338 Decl::Destroy(C);
339}