blob: 25317dbeb1094a969bbc37fe5f9962f77adf10c7 [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) {
Steve Naroff5abb0282009-01-27 21:25:57 +000027 return new (C) TranslationUnitDecl();
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000028}
29
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000030NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
31 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +000032 return new (C) NamespaceDecl(DC, L, Id);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000033}
34
Ted Kremenek5be49242008-05-20 04:49:55 +000035void NamespaceDecl::Destroy(ASTContext& C) {
36 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
37 // together. They are all top-level Decls.
38
Ted Kremenek0f433842008-05-24 15:09:56 +000039 this->~NamespaceDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000040 C.Deallocate((void *)this);
Ted Kremenek5be49242008-05-20 04:49:55 +000041}
42
43
Chris Lattner8c7c6a12008-06-17 18:05:57 +000044ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000045 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +000046 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000047}
48
Chris Lattneref87a202008-04-22 18:39:57 +000049ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000050 SourceLocation L, IdentifierInfo *Id,
51 QualType T, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000052 Expr *DefArg) {
Steve Naroff5abb0282009-01-27 21:25:57 +000053 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000054}
55
56QualType ParmVarDecl::getOriginalType() const {
57 if (const ParmVarWithOriginalTypeDecl *PVD =
58 dyn_cast<ParmVarWithOriginalTypeDecl>(this))
59 return PVD->OriginalType;
60 return getType();
Chris Lattner48d225c2008-03-15 21:10:16 +000061}
62
Fariborz Jahanian160e8812008-12-20 20:56:12 +000063ParmVarWithOriginalTypeDecl *ParmVarWithOriginalTypeDecl::Create(
64 ASTContext &C, DeclContext *DC,
65 SourceLocation L, IdentifierInfo *Id,
66 QualType T, QualType OT, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000067 Expr *DefArg) {
Steve Naroff5abb0282009-01-27 21:25:57 +000068 return new (C) ParmVarWithOriginalTypeDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +000069}
70
Chris Lattneref87a202008-04-22 18:39:57 +000071FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000072 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +000073 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +000074 StorageClass S, bool isInline,
Steve Naroff71cd7762008-10-03 00:02:03 +000075 SourceLocation TypeSpecStartLoc) {
Steve Naroff5abb0282009-01-27 21:25:57 +000076 return new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
Steve Naroff71cd7762008-10-03 00:02:03 +000077 TypeSpecStartLoc);
Chris Lattner4c7802b2008-03-15 21:24:04 +000078}
79
Steve Naroff52059382008-10-10 01:28:17 +000080BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +000081 return new (C) 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,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000086 bool Mutable) {
Steve Naroff5abb0282009-01-27 21:25:57 +000087 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +000088}
89
Douglas Gregorc7f01612009-01-07 19:46:03 +000090bool FieldDecl::isAnonymousStructOrUnion() const {
91 if (!isImplicit() || getDeclName())
92 return false;
93
94 if (const RecordType *Record = getType()->getAsRecordType())
95 return Record->getDecl()->isAnonymousStructOrUnion();
96
97 return false;
98}
Chris Lattner4c7802b2008-03-15 21:24:04 +000099
Chris Lattnereee57c02008-04-04 06:12:32 +0000100EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
101 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000102 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000103 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000104 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000105}
106
Ted Kremenek5be49242008-05-20 04:49:55 +0000107void EnumConstantDecl::Destroy(ASTContext& C) {
108 if (Init) Init->Destroy(C);
109 Decl::Destroy(C);
110}
111
Chris Lattneref87a202008-04-22 18:39:57 +0000112TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000113 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000114 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000115 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000116}
117
Chris Lattneref87a202008-04-22 18:39:57 +0000118EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000119 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000120 EnumDecl *PrevDecl) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000121 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregorae644892008-12-15 16:32:14 +0000122 C.getTypeDeclType(Enum, PrevDecl);
123 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000124}
125
Ted Kremenek25d8be12008-09-02 20:13:32 +0000126void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000127 Decl::Destroy(C);
128}
129
Douglas Gregor8acb7272008-12-11 16:49:14 +0000130void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
131 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000132 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000133 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000134}
135
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000136FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000137 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000138 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000139 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000140}
141
Chris Lattnere4650482008-03-15 06:12:44 +0000142//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000143// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000144//===----------------------------------------------------------------------===//
145
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000146bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000147 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
148
149 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
150 // For function declarations, we keep track of redeclarations.
151 return FD->getPreviousDeclaration() == OldD;
152
153 // For non-function declarations, if the declarations are of the
154 // same kind then this must be a redeclaration, or semantic analysis
155 // would not have given us the new declaration.
156 return this->getKind() == OldD->getKind();
157}
158
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000159
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000160//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000161// VarDecl Implementation
162//===----------------------------------------------------------------------===//
163
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000164VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
165 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopesc98406e2008-12-17 23:39:55 +0000166 SourceLocation TypeSpecStartLoc) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000167 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000168}
169
170void VarDecl::Destroy(ASTContext& C) {
171 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000172 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000173}
174
175VarDecl::~VarDecl() {
176 delete getInit();
177}
178
179//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000180// FunctionDecl Implementation
181//===----------------------------------------------------------------------===//
182
Ted Kremenekafdf8112008-05-20 00:43:19 +0000183void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000184 if (Body)
185 Body->Destroy(C);
186
187 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
188 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000189
Steve Naroff5abb0282009-01-27 21:25:57 +0000190 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000191
Ted Kremenekafdf8112008-05-20 00:43:19 +0000192 Decl::Destroy(C);
193}
194
195
Douglas Gregor42214c52008-04-21 02:02:58 +0000196Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
197 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
198 if (FD->Body) {
199 Definition = FD;
200 return FD->Body;
201 }
202 }
203
204 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000205}
206
Ted Kremeneka6ded352008-10-29 18:41:34 +0000207// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
208static unsigned getNumTypeParams(QualType T) {
209 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnere8733592008-04-06 23:09:52 +0000210 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000211 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000212 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000213}
214
Ted Kremeneka6ded352008-10-29 18:41:34 +0000215unsigned FunctionDecl::getNumParams() const {
216 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
217 if (!ParamInfo)
218 return 0;
219
220 return getNumTypeParams(getType());
221}
222
Ted Kremenek8494c962009-01-14 00:42:25 +0000223void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
224 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000225 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremeneka6ded352008-10-29 18:41:34 +0000226 assert(NumParams == getNumTypeParams(getType()) &&
227 "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000228
229 // Zero params -> null pointer.
230 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000231 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000232 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000233 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
234 }
235}
236
Chris Lattner97316c02008-04-10 02:22:51 +0000237/// getMinRequiredArguments - Returns the minimum number of arguments
238/// needed to call this function. This may be fewer than the number of
239/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000240/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000241unsigned FunctionDecl::getMinRequiredArguments() const {
242 unsigned NumRequiredArgs = getNumParams();
243 while (NumRequiredArgs > 0
244 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
245 --NumRequiredArgs;
246
247 return NumRequiredArgs;
248}
249
Douglas Gregore60e5d32008-11-06 22:13:31 +0000250/// getOverloadedOperator - Which C++ overloaded operator this
251/// function represents, if any.
252OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000253 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
254 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000255 else
256 return OO_None;
257}
258
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000259//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000260// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000261//===----------------------------------------------------------------------===//
262
Douglas Gregor98b27542009-01-17 00:42:38 +0000263void TagDecl::startDefinition() {
264 cast<TagType>(TypeForDecl)->decl.setPointer(this);
265 cast<TagType>(TypeForDecl)->decl.setInt(1);
266}
267
268void TagDecl::completeDefinition() {
269 assert((!TypeForDecl ||
270 cast<TagType>(TypeForDecl)->decl.getPointer() == this) &&
271 "Attempt to redefine a tag definition?");
272 IsDefinition = true;
273 cast<TagType>(TypeForDecl)->decl.setPointer(this);
274 cast<TagType>(TypeForDecl)->decl.setInt(0);
275}
276
Ted Kremenek46a837c2008-09-05 17:16:31 +0000277TagDecl* TagDecl::getDefinition(ASTContext& C) const {
278 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
279 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
280 return D->isDefinition() ? D : 0;
281}
282
283//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000284// RecordDecl Implementation
285//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000286
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000287RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000288 IdentifierInfo *Id)
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000289 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000290 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000291 AnonymousStructOrUnion = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000292 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000293}
294
295RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000296 SourceLocation L, IdentifierInfo *Id,
297 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000298
Steve Naroff5abb0282009-01-27 21:25:57 +0000299 RecordDecl* R = new (C) 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) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000308 TagDecl::Destroy(C);
309}
310
Douglas Gregor8acb7272008-12-11 16:49:14 +0000311/// completeDefinition - Notes that the definition of this type is now
312/// complete.
313void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000314 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000315 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000316}
317
Steve Naroff9ac456d2008-10-08 17:01:13 +0000318//===----------------------------------------------------------------------===//
319// BlockDecl Implementation
320//===----------------------------------------------------------------------===//
321
322BlockDecl::~BlockDecl() {
323}
324
325void BlockDecl::Destroy(ASTContext& C) {
326 if (Body)
327 Body->Destroy(C);
328
329 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
330 (*I)->Destroy(C);
331
332 Decl::Destroy(C);
333}