blob: 25317dbeb1094a969bbc37fe5f9962f77adf10c7 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000017#include "clang/AST/Expr.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/Basic/IdentifierTable.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000019
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Chris Lattnerd3b90652008-03-15 05:43:15 +000022//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000023// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000025
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000026TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Steve Naroff3e970492009-01-27 21:25:57 +000027 return new (C) TranslationUnitDecl();
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000028}
29
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000030NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
31 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000032 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000033}
34
Ted Kremenekd1ac17a2008-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 Kremenekebf27b12008-05-24 15:09:56 +000039 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000040 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000041}
42
43
Chris Lattner41110242008-06-17 18:05:57 +000044ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000045 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000046 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000047}
48
Chris Lattner9fdf9c62008-04-22 18:39:57 +000049ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000050 SourceLocation L, IdentifierInfo *Id,
51 QualType T, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000052 Expr *DefArg) {
Steve Naroff3e970492009-01-27 21:25:57 +000053 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahanian4306d3c2008-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 Lattner9e151e12008-03-15 21:10:16 +000061}
62
Fariborz Jahanian73da9e42008-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 Gregor4afa39d2009-01-20 01:17:11 +000067 Expr *DefArg) {
Steve Naroff3e970492009-01-27 21:25:57 +000068 return new (C) ParmVarWithOriginalTypeDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian73da9e42008-12-20 20:56:12 +000069}
70
Chris Lattner9fdf9c62008-04-22 18:39:57 +000071FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000072 SourceLocation L,
Douglas Gregor10bd3682008-11-17 22:58:34 +000073 DeclarationName N, QualType T,
Chris Lattnera98e58d2008-03-15 21:24:04 +000074 StorageClass S, bool isInline,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000075 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +000076 return new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000077 TypeSpecStartLoc);
Chris Lattnera98e58d2008-03-15 21:24:04 +000078}
79
Steve Naroff090276f2008-10-10 01:28:17 +000080BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +000081 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +000082}
83
Douglas Gregor44b43212008-12-11 16:49:14 +000084FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
85 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000086 bool Mutable) {
Steve Naroff3e970492009-01-27 21:25:57 +000087 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +000088}
89
Douglas Gregor6b3945f2009-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 Lattnera98e58d2008-03-15 21:24:04 +000099
Chris Lattner0ed844b2008-04-04 06:12:32 +0000100EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
101 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000102 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000103 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000104 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000105}
106
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000107void EnumConstantDecl::Destroy(ASTContext& C) {
108 if (Init) Init->Destroy(C);
109 Decl::Destroy(C);
110}
111
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000112TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000113 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000114 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000115 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000116}
117
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000118EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000119 IdentifierInfo *Id,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000120 EnumDecl *PrevDecl) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000121 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000122 C.getTypeDeclType(Enum, PrevDecl);
123 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000124}
125
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000126void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000127 Decl::Destroy(C);
128}
129
Douglas Gregor44b43212008-12-11 16:49:14 +0000130void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
131 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000132 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000133 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000134}
135
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000136FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000137 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000138 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000139 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000140}
141
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000142//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000143// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000144//===----------------------------------------------------------------------===//
145
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000146bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-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 Gregor4afa39d2009-01-20 01:17:11 +0000159
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000160//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000161// VarDecl Implementation
162//===----------------------------------------------------------------------===//
163
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000164VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
165 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000166 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +0000167 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000168}
169
170void VarDecl::Destroy(ASTContext& C) {
171 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000172 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000173}
174
175VarDecl::~VarDecl() {
176 delete getInit();
177}
178
179//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000180// FunctionDecl Implementation
181//===----------------------------------------------------------------------===//
182
Ted Kremenek27f8a282008-05-20 00:43:19 +0000183void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-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 Lopes460b0ac2009-01-18 19:57:27 +0000189
Steve Naroff3e970492009-01-27 21:25:57 +0000190 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000191
Ted Kremenek27f8a282008-05-20 00:43:19 +0000192 Decl::Destroy(C);
193}
194
195
Douglas Gregorf0097952008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000205}
206
Ted Kremenek4f03fd62008-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 Lattnerd8bdba52008-04-06 23:09:52 +0000210 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000211 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000212 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000213}
214
Ted Kremenek4f03fd62008-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 Kremenekfc767612009-01-14 00:42:25 +0000223void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
224 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000226 assert(NumParams == getNumTypeParams(getType()) &&
227 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000228
229 // Zero params -> null pointer.
230 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000231 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000232 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
234 }
235}
236
Chris Lattner8123a952008-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 Lattner9e979552008-04-12 23:52:44 +0000240/// arguments (in C++).
Chris Lattner8123a952008-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 Gregor1cd1b1e2008-11-06 22:13:31 +0000250/// getOverloadedOperator - Which C++ overloaded operator this
251/// function represents, if any.
252OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000253 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
254 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000255 else
256 return OO_None;
257}
258
Chris Lattner8a934232008-03-31 00:36:02 +0000259//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000260// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000261//===----------------------------------------------------------------------===//
262
Douglas Gregor0b7a1582009-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 Kremenek4b7c9832008-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 Lattner8a934232008-03-31 00:36:02 +0000284// RecordDecl Implementation
285//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000286
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000287RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000288 IdentifierInfo *Id)
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000289 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek63597922008-09-02 21:12:32 +0000290 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000291 AnonymousStructOrUnion = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000292 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000293}
294
295RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000296 SourceLocation L, IdentifierInfo *Id,
297 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000298
Steve Naroff3e970492009-01-27 21:25:57 +0000299 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000300 C.getTypeDeclType(R, PrevDecl);
301 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000302}
303
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000304RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000305}
306
307void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000308 TagDecl::Destroy(C);
309}
310
Douglas Gregor44b43212008-12-11 16:49:14 +0000311/// completeDefinition - Notes that the definition of this type is now
312/// complete.
313void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000315 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000316}
317
Steve Naroff56ee6892008-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}