blob: 9ddc62a51e7a29c7cf7544d57930ca3aaef6e693 [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,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000047 SourceLocation L, IdentifierInfo *Id, QualType T) {
Chris Lattner8c7c6a12008-06-17 18:05:57 +000048 void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000049 return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000050}
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,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000055 Expr *DefArg) {
Chris Lattner48d225c2008-03-15 21:10:16 +000056 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000057 return new (Mem) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000058}
59
60QualType ParmVarDecl::getOriginalType() const {
61 if (const ParmVarWithOriginalTypeDecl *PVD =
62 dyn_cast<ParmVarWithOriginalTypeDecl>(this))
63 return PVD->OriginalType;
64 return getType();
Chris Lattner48d225c2008-03-15 21:10:16 +000065}
66
Fariborz Jahanian160e8812008-12-20 20:56:12 +000067ParmVarWithOriginalTypeDecl *ParmVarWithOriginalTypeDecl::Create(
68 ASTContext &C, DeclContext *DC,
69 SourceLocation L, IdentifierInfo *Id,
70 QualType T, QualType OT, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000071 Expr *DefArg) {
Fariborz Jahanian160e8812008-12-20 20:56:12 +000072 void *Mem = C.getAllocator().Allocate<ParmVarWithOriginalTypeDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000073 return new (Mem) ParmVarWithOriginalTypeDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +000074}
75
Chris Lattneref87a202008-04-22 18:39:57 +000076FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000077 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +000078 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +000079 StorageClass S, bool isInline,
Steve Naroff71cd7762008-10-03 00:02:03 +000080 SourceLocation TypeSpecStartLoc) {
Chris Lattner4c7802b2008-03-15 21:24:04 +000081 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000082 return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline,
Steve Naroff71cd7762008-10-03 00:02:03 +000083 TypeSpecStartLoc);
Chris Lattner4c7802b2008-03-15 21:24:04 +000084}
85
Steve Naroff52059382008-10-10 01:28:17 +000086BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff9ac456d2008-10-08 17:01:13 +000087 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff52059382008-10-10 01:28:17 +000088 return new (Mem) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +000089}
90
Douglas Gregor8acb7272008-12-11 16:49:14 +000091FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
92 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000093 bool Mutable) {
Chris Lattner81db64a2008-03-16 00:16:02 +000094 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000095 return new (Mem) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +000096}
97
Douglas Gregorc7f01612009-01-07 19:46:03 +000098bool FieldDecl::isAnonymousStructOrUnion() const {
99 if (!isImplicit() || getDeclName())
100 return false;
101
102 if (const RecordType *Record = getType()->getAsRecordType())
103 return Record->getDecl()->isAnonymousStructOrUnion();
104
105 return false;
106}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000107
Chris Lattnereee57c02008-04-04 06:12:32 +0000108EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
109 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000110 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000111 Expr *E, const llvm::APSInt &V) {
Chris Lattnere4650482008-03-15 06:12:44 +0000112 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000113 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000114}
115
Ted Kremenek5be49242008-05-20 04:49:55 +0000116void EnumConstantDecl::Destroy(ASTContext& C) {
117 if (Init) Init->Destroy(C);
118 Decl::Destroy(C);
119}
120
Chris Lattneref87a202008-04-22 18:39:57 +0000121TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000122 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000123 IdentifierInfo *Id, QualType T) {
Chris Lattnere4650482008-03-15 06:12:44 +0000124 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000125 return new (Mem) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000126}
127
Chris Lattneref87a202008-04-22 18:39:57 +0000128EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000129 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000130 EnumDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000131 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000132 EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id);
Douglas Gregorae644892008-12-15 16:32:14 +0000133 C.getTypeDeclType(Enum, PrevDecl);
134 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000135}
136
Ted Kremenek25d8be12008-09-02 20:13:32 +0000137void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000138 Decl::Destroy(C);
139}
140
Douglas Gregor8acb7272008-12-11 16:49:14 +0000141void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
142 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000143 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000144 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000145}
146
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000147FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000148 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000149 StringLiteral *Str) {
150 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000151 return new (Mem) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000152}
153
Chris Lattnere4650482008-03-15 06:12:44 +0000154//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000155// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000156//===----------------------------------------------------------------------===//
157
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000158bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000159 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
160
161 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
162 // For function declarations, we keep track of redeclarations.
163 return FD->getPreviousDeclaration() == OldD;
164
165 // For non-function declarations, if the declarations are of the
166 // same kind then this must be a redeclaration, or semantic analysis
167 // would not have given us the new declaration.
168 return this->getKind() == OldD->getKind();
169}
170
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000171
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000172//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000173// VarDecl Implementation
174//===----------------------------------------------------------------------===//
175
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000176VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
177 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopesc98406e2008-12-17 23:39:55 +0000178 SourceLocation TypeSpecStartLoc) {
179 void *Mem = C.getAllocator().Allocate<VarDecl>();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000180 return new (Mem) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000181}
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
Ted Kremenekafdf8112008-05-20 00:43:19 +0000196void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000197 if (Body)
198 Body->Destroy(C);
199
200 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
201 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000202
203 C.getAllocator().Deallocate(ParamInfo);
204
Ted Kremenekafdf8112008-05-20 00:43:19 +0000205 Decl::Destroy(C);
206}
207
208
Douglas Gregor42214c52008-04-21 02:02:58 +0000209Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
210 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
211 if (FD->Body) {
212 Definition = FD;
213 return FD->Body;
214 }
215 }
216
217 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000218}
219
Ted Kremeneka6ded352008-10-29 18:41:34 +0000220// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
221static unsigned getNumTypeParams(QualType T) {
222 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnere8733592008-04-06 23:09:52 +0000223 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000224 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000225 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000226}
227
Ted Kremeneka6ded352008-10-29 18:41:34 +0000228unsigned FunctionDecl::getNumParams() const {
229 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
230 if (!ParamInfo)
231 return 0;
232
233 return getNumTypeParams(getType());
234}
235
Ted Kremenek8494c962009-01-14 00:42:25 +0000236void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
237 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000238 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremeneka6ded352008-10-29 18:41:34 +0000239 assert(NumParams == getNumTypeParams(getType()) &&
240 "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000241
242 // Zero params -> null pointer.
243 if (NumParams) {
Ted Kremenek8494c962009-01-14 00:42:25 +0000244 void *Mem = C.getAllocator().Allocate<ParmVarDecl*>(NumParams);
245 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000246 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//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000273// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000274//===----------------------------------------------------------------------===//
275
Douglas Gregor98b27542009-01-17 00:42:38 +0000276void TagDecl::startDefinition() {
277 cast<TagType>(TypeForDecl)->decl.setPointer(this);
278 cast<TagType>(TypeForDecl)->decl.setInt(1);
279}
280
281void TagDecl::completeDefinition() {
282 assert((!TypeForDecl ||
283 cast<TagType>(TypeForDecl)->decl.getPointer() == this) &&
284 "Attempt to redefine a tag definition?");
285 IsDefinition = true;
286 cast<TagType>(TypeForDecl)->decl.setPointer(this);
287 cast<TagType>(TypeForDecl)->decl.setInt(0);
288}
289
Ted Kremenek46a837c2008-09-05 17:16:31 +0000290TagDecl* TagDecl::getDefinition(ASTContext& C) const {
291 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
292 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
293 return D->isDefinition() ? D : 0;
294}
295
296//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000297// RecordDecl Implementation
298//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000299
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000300RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000301 IdentifierInfo *Id)
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000302 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000303 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000304 AnonymousStructOrUnion = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000305 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000306}
307
308RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000309 SourceLocation L, IdentifierInfo *Id,
310 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000311
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000312 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000313 RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000314 C.getTypeDeclType(R, PrevDecl);
315 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000316}
317
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000318RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000319}
320
321void RecordDecl::Destroy(ASTContext& C) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000322 DeclContext::DestroyDecls(C);
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000323 TagDecl::Destroy(C);
324}
325
Douglas Gregor8acb7272008-12-11 16:49:14 +0000326/// completeDefinition - Notes that the definition of this type is now
327/// complete.
328void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000329 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000330 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000331}
332
Steve Naroff9ac456d2008-10-08 17:01:13 +0000333//===----------------------------------------------------------------------===//
334// BlockDecl Implementation
335//===----------------------------------------------------------------------===//
336
337BlockDecl::~BlockDecl() {
338}
339
340void BlockDecl::Destroy(ASTContext& C) {
341 if (Body)
342 Body->Destroy(C);
343
344 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
345 (*I)->Destroy(C);
346
347 Decl::Destroy(C);
348}